From: Simon Brunel Date: Fri, 2 Sep 2016 19:56:22 +0000 (+0200) Subject: Use gulp-eslint instead of gulp-jshint X-Git-Tag: v2.3.0-rc.1~1^2~14^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=69ab0d3e238206fa90b06fcc87054b0a7dc97298;p=thirdparty%2FChart.js.git Use gulp-eslint instead of gulp-jshint Change the linter in gulp tasks to be consistent with Code Climate results which are based on ESLint using .eslintrc options. However, defaults Code Climate rules are too strict, so turn as warnings the 'complexity' and 'max-statements' rules (other errors has been fixed). Note that the Gulp task name has been changed for `gulp lint`. --- diff --git a/.eslintrc b/.eslintrc index 9faa37508..cb5b0d49e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -57,7 +57,6 @@ rules: no-case-declarations: 2 no-div-regex: 2 no-else-return: 0 - no-empty-label: 2 no-empty-pattern: 2 no-eq-null: 2 no-eval: 2 diff --git a/README.md b/README.md index 79695200d..49d09c017 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ To build, run `gulp build`. To test, run `gulp test`. -To test against code standards, run `gulp jshint`. +To test against code standards, run `gulp lint`. More information on building and testing can be found in [gulpfile.js](gulpfile.js). diff --git a/docs/10-Notes.md b/docs/10-Notes.md index fbeed85fd..5ca1670a5 100644 --- a/docs/10-Notes.md +++ b/docs/10-Notes.md @@ -34,7 +34,7 @@ New contributions to the library are welcome, but we ask that you please follow - Use tabs for indentation, not spaces. - Only change the individual files in `/src`. -- Check that your code will pass `jshint` code standards, `gulp jshint` will run this for you. +- Check that your code will pass `eslint` code standards, `gulp lint` will run this for you. - Check that your code will pass tests, `gulp test` will run tests for you. - Keep pull requests concise, and document new functionality in the relevant `.md` file. - Consider whether your changes are useful for all users, or if creating a Chart.js plugin would be more appropriate. diff --git a/gulpfile.js b/gulpfile.js index 14a2e1df5..b3dab98d1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,23 +1,23 @@ -var gulp = require('gulp'), - concat = require('gulp-concat'), - file = require('gulp-file'), - uglify = require('gulp-uglify'), - util = require('gulp-util'), - jshint = require('gulp-jshint'), - size = require('gulp-size'), - connect = require('gulp-connect'), - replace = require('gulp-replace'), - htmlv = require('gulp-html-validator'), - insert = require('gulp-insert'), - zip = require('gulp-zip'), - exec = require('child_process').exec, - package = require('./package.json'), - karma = require('gulp-karma'), - browserify = require('browserify'), - streamify = require('gulp-streamify'), - source = require('vinyl-source-stream'), - merge = require('merge-stream'), - collapse = require('bundle-collapser/plugin'); +var gulp = require('gulp'); +var concat = require('gulp-concat'); +var connect = require('gulp-connect'); +var eslint = require('gulp-eslint'); +var file = require('gulp-file'); +var htmlv = require('gulp-html-validator'); +var insert = require('gulp-insert'); +var replace = require('gulp-replace'); +var size = require('gulp-size'); +var streamify = require('gulp-streamify'); +var uglify = require('gulp-uglify'); +var util = require('gulp-util'); +var zip = require('gulp-zip'); +var exec = require('child_process').exec; +var karma = require('gulp-karma'); +var browserify = require('browserify'); +var source = require('vinyl-source-stream'); +var merge = require('merge-stream'); +var collapse = require('bundle-collapser/plugin'); +var package = require('./package.json'); var srcDir = './src/'; var outDir = './dist/'; @@ -43,7 +43,7 @@ var testFiles = [ // Disable tests which need to be rewritten based on changes introduced by // the following changes: https://github.com/chartjs/Chart.js/pull/2346 '!./test/core.layoutService.tests.js', - '!./test/defaultConfig.tests.js', + '!./test/defaultConfig.tests.js' ]; gulp.task('bower', bowerTask); @@ -51,8 +51,8 @@ gulp.task('build', buildTask); gulp.task('package', packageTask); gulp.task('coverage', coverageTask); gulp.task('watch', watchTask); -gulp.task('jshint', jshintTask); -gulp.task('test', ['jshint', 'validHTML', 'unittest']); +gulp.task('lint', lintTask); +gulp.task('test', ['lint', 'validHTML', 'unittest']); gulp.task('size', ['library-size', 'module-sizes']); gulp.task('server', serverTask); gulp.task('validHTML', validHTMLTask); @@ -130,11 +130,25 @@ function packageTask() { .pipe(gulp.dest(outDir)); } -function jshintTask() { - return gulp.src(srcDir + '**/*.js') - .pipe(jshint('config.jshintrc')) - .pipe(jshint.reporter('jshint-stylish')) - .pipe(jshint.reporter('fail')); +function lintTask() { + var files = [ + srcDir + '**/*.js', + ]; + + // NOTE(SB) codeclimate has 'complexity' and 'max-statements' eslint rules way too strict + // compare to what the current codebase can support, and since it's not straightforward + // to fix, let's turn them as warnings and rewrite code later progressively. + var options = { + rules: { + 'complexity': [1, 6], + 'max-statements': [1, 30] + } + }; + + return gulp.src(files) + .pipe(eslint(options)) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); } function validHTMLTask() { diff --git a/package.json b/package.json index ed6b6f921..eb383d713 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ "gulp": "3.9.x", "gulp-concat": "~2.1.x", "gulp-connect": "~2.0.5", + "gulp-eslint": "^2.0.0", "gulp-file": "^0.3.0", "gulp-html-validator": "^0.0.2", "gulp-insert": "~0.5.0", - "gulp-jshint": "~1.5.1", "gulp-karma": "0.0.4", "gulp-replace": "^0.5.4", "gulp-size": "~0.4.0", @@ -30,7 +30,6 @@ "gulp-zip": "~3.2.0", "jasmine": "^2.3.2", "jasmine-core": "^2.3.4", - "jshint-stylish": "~2.1.0", "karma": "^0.12.37", "karma-browserify": "^5.0.1", "karma-chrome-launcher": "^0.2.0", diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index a7746ee27..906cc8b91 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -260,7 +260,7 @@ module.exports = function(Chart) { return Math.max(Math.min(pt, max), min); } - if (meta.dataset._model.cubicInterpolationMode == 'monotone') { + if (meta.dataset._model.cubicInterpolationMode === 'monotone') { helpers.splineCurveMonotone(points); } else { diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index 02af1471d..4318a9c95 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -7,7 +7,7 @@ module.exports = function(Chart) { // Base class for all dataset controllers (line, bar, etc) Chart.DatasetController = function(chart, datasetIndex) { - this.initialize.call(this, chart, datasetIndex); + this.initialize(chart, datasetIndex); }; helpers.extend(Chart.DatasetController.prototype, { @@ -157,9 +157,9 @@ module.exports = function(Chart) { model.borderColor = custom.hoverBorderColor ? custom.hoverBorderColor : valueOrDefault(dataset.hoverBorderColor, index, getHoverColor(model.borderColor)); model.borderWidth = custom.hoverBorderWidth ? custom.hoverBorderWidth : valueOrDefault(dataset.hoverBorderWidth, index, model.borderWidth); } - + }); - + Chart.DatasetController.extend = helpers.inherits; }; \ No newline at end of file diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index c00aee6fa..0cf16f171 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -238,7 +238,7 @@ module.exports = function(Chart) { return function() { return id++; }; - })(); + }()); //-- Math methods helpers.isNumber = function(n) { return !isNaN(parseFloat(n)) && isFinite(n); @@ -366,7 +366,7 @@ module.exports = function(Chart) { } if (!pointBefore || pointBefore.model.skip) pointCurrent.mK = pointCurrent.deltaK; else if (!pointAfter || pointAfter.model.skip) pointCurrent.mK = pointBefore.deltaK; - else if (this.sign(pointBefore.deltaK) != this.sign(pointCurrent.deltaK)) pointCurrent.mK = 0; + else if (this.sign(pointBefore.deltaK) !== this.sign(pointCurrent.deltaK)) pointCurrent.mK = 0; else pointCurrent.mK = (pointBefore.deltaK + pointCurrent.deltaK) / 2; } @@ -376,8 +376,7 @@ module.exports = function(Chart) { pointCurrent = pointsWithTangents[i]; pointAfter = pointsWithTangents[i + 1]; if (pointCurrent.model.skip || pointAfter.model.skip) continue; - if (helpers.almostEquals(pointCurrent.deltaK, 0, this.EPSILON)) - { + if (helpers.almostEquals(pointCurrent.deltaK, 0, this.EPSILON)) { pointCurrent.mK = pointAfter.mK = 0; continue; } @@ -660,7 +659,7 @@ module.exports = function(Chart) { function(callback) { return window.setTimeout(callback, 1000 / 60); }; - })(); + }()); helpers.cancelAnimFrame = (function() { return window.cancelAnimationFrame || window.webkitCancelAnimationFrame || @@ -670,7 +669,7 @@ module.exports = function(Chart) { function(callback) { return window.clearTimeout(callback, 1000 / 60); }; - })(); + }()); //-- DOM methods helpers.getRelativePosition = function(evt, chart) { var mouseX, mouseY; @@ -751,7 +750,7 @@ module.exports = function(Chart) { if (typeof(styleValue) === 'string') { valueInPixels = parseInt(styleValue, 10); - if (styleValue.indexOf('%') != -1) { + if (styleValue.indexOf('%') !== -1) { // percentage * size in dimension valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty]; } @@ -802,15 +801,17 @@ module.exports = function(Chart) { }; helpers.getMaximumWidth = function(domNode) { var container = domNode.parentNode; - var padding = parseInt(helpers.getStyle(container, 'padding-left')) + parseInt(helpers.getStyle(container, 'padding-right')); - var w = container.clientWidth - padding; + var paddingLeft = parseInt(helpers.getStyle(container, 'padding-left'), 10); + var paddingRight = parseInt(helpers.getStyle(container, 'padding-right'), 10); + var w = container.clientWidth - paddingLeft - paddingRight; var cw = helpers.getConstraintWidth(domNode); return isNaN(cw)? w : Math.min(w, cw); }; helpers.getMaximumHeight = function(domNode) { var container = domNode.parentNode; - var padding = parseInt(helpers.getStyle(container, 'padding-top')) + parseInt(helpers.getStyle(container, 'padding-bottom')); - var h = container.clientHeight - padding; + var paddingTop = parseInt(helpers.getStyle(container, 'padding-top'), 10); + var paddingBottom = parseInt(helpers.getStyle(container, 'padding-bottom'), 10); + var h = container.clientHeight - paddingTop - paddingBottom; var ch = helpers.getConstraintHeight(domNode); return isNaN(ch)? h : Math.min(h, ch); }; @@ -964,7 +965,7 @@ module.exports = function(Chart) { (hiddenIframe.contentWindow || hiddenIframe).onresize = function() { if (callback) { - callback(); + return callback(); } }; }; @@ -985,7 +986,7 @@ module.exports = function(Chart) { helpers.arrayEquals = function(a0, a1) { var i, ilen, v0, v1; - if (!a0 || !a1 || a0.length != a1.length) { + if (!a0 || !a1 || a0.length !== a1.length) { return false; } @@ -997,7 +998,7 @@ module.exports = function(Chart) { if (!helpers.arrayEquals(v0, v1)) { return false; } - } else if (v0 != v1) { + } else if (v0 !== v1) { // NOTE: two different object instances will never be equal: {x:20} != {x:20} return false; } diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index ab935a12a..c7b64cbd3 100755 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -80,7 +80,7 @@ module.exports = function(Chart) { return null; } - if (typeof this.labelMoments[datasetIndex] != 'undefined') { + if (typeof this.labelMoments[datasetIndex] !== 'undefined') { return this.labelMoments[datasetIndex][index]; } diff --git a/test/core.layoutService.tests.js b/test/core.layoutService.tests.js index 819519f48..8296e977e 100644 --- a/test/core.layoutService.tests.js +++ b/test/core.layoutService.tests.js @@ -230,10 +230,10 @@ describe('Test the layout service', function() { expect(chart.scales.xScale1.right).toBeCloseToPixel(512); expect(chart.scales.xScale1.top).toBeCloseToPixel(484); - expect(chart.scales.xScale2.bottom).toBeCloseToPixel(28); + expect(chart.scales.xScale2.bottom).toBeCloseToPixel(60); expect(chart.scales.xScale2.left).toBeCloseToPixel(0); expect(chart.scales.xScale2.right).toBeCloseToPixel(512); - expect(chart.scales.xScale2.top).toBeCloseToPixel(0); + expect(chart.scales.xScale2.top).toBeCloseToPixel(32); // Is yScale at the right spot expect(chart.scales.yScale.bottom).toBeCloseToPixel(484);