From 1bdea1965e3e398e496ccdf268a0bbacc8efea70 Mon Sep 17 00:00:00 2001 From: Sai Pc Date: Wed, 10 Jun 2015 15:23:41 -0700 Subject: [PATCH] chore(lint): switch from JSHint to ESLint Fixes #2550 Switch from JSHint to ESLint Also have JSHint in parallel --- .eslintignore | 4 +++ .eslintrc | 52 +++++++++++++++++++++++++++ app/.eslintrc | 58 ++++++++++++++++++++++++++++++ app/scripts/head/start.js | 1 + app/scripts/lib/app-start.js | 3 +- app/scripts/lib/metrics.js | 3 +- app/scripts/lib/screen-info.js | 1 + app/scripts/lib/storage.js | 2 +- app/scripts/require_config.js | 1 + app/scripts/views/base.js | 2 +- app/tests/spec/lib/channels/web.js | 4 +-- app/tests/spec/lib/xhr.js | 3 +- app/tests/spec/lib/xss.js | 4 ++- app/tests/spec/views/sign_up.js | 3 +- grunttasks/eslint.js | 32 +++++++++++++++++ grunttasks/lint.js | 6 +++- package.json | 1 + scripts/run_locally.js | 9 ++--- server/bin/fxa-content-server.js | 1 - server/lib/404.js | 1 - server/lib/csp.js | 4 +-- server/lib/statsd-collector.js | 1 + tests/intern.js | 1 + 23 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 app/.eslintrc create mode 100644 grunttasks/eslint.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000000..745e3f3ed1 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +.tmp/** +app/bower_components/** +app/scripts/vendor/** +dist/** diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000000..5cc980860d --- /dev/null +++ b/.eslintrc @@ -0,0 +1,52 @@ +{ + "env": { + "amd": true, + "es6": true, + "jasmine": true, + "node": true, + "mocha": true, + "browser": true, + "builtin": true + }, + "rules": { + "camelcase": 0, + "comma-dangle": 0, + "complexity": [2, 6], + "consistent-return": 0, + "curly": [2, "all"], + "dot-notation": 0, + "eqeqeq": [2, "allow-null"], + "global-strict": [0, "never"], + "globals": { + "define": true, + "Promise": true + }, + "handle-callback-err": 0, + "key-spacing": 0, + "new-cap": 0, + "no-cond-assign": [2, "except-parens"], + "no-debugger": 2, + "no-empty": 0, + "no-eval": 2, + "no-irregular-whitespace": 2, + "no-loop-func": 0, + "no-multi-spaces": 0, + "no-new": 2, + "no-script-url": 2, + "no-sequences": 2, + "no-shadow": 0, + "no-spaced-func": 0, + "no-undef": 2, + "no-underscore-dangle": 0, + "no-unused-vars": [1, {vars: all, args: none}], + "no-use-before-define": true, + "no-with": 2, + "quotes": [2, "single"], + "semi": [2, "always"], + "space-unary-ops": 0, + "strict": 2, + "valid-typeof": 1, + "wrap-iife": 0, + "yoda": 0 + } +} diff --git a/app/.eslintrc b/app/.eslintrc new file mode 100644 index 0000000000..eecc0dd83f --- /dev/null +++ b/app/.eslintrc @@ -0,0 +1,58 @@ +{ + "env": { + "amd": true, + "es6": true, + "jasmine": true, + "jquery": true, + "node": true, + "mocha": true, + "browser": true, + "builtin": true + }, + "rules": { + "camelcase": 0, + "complexity": [2, 6], + "consistent-return": 0, + "curly": [2, "all"], + "dot-notation": 0, + "eqeqeq": [2, "allow-null"], + "globals": { + "console": false, + "define": true, + "require": false, + "router": false, + "afterEach": false, + "beforeEach": false, + "describe": false, + "it": false + }, + "handle-callback-err": 0, + "key-spacing": 0, + "new-cap": 0, + "no-bitwise": 2, + "no-caller": 2, + "no-cond-assign": [2, "except-parens"], + "no-debugger": 2, + "no-empty": 2, + "no-eval": 2, + "no-extend-native": 2, + "no-irregular-whitespace": 2, + "no-loop-func": 0, + "no-multi-str": 2, + "no-new": 2, + "no-proto": 2, + "no-script-url": 2, + "no-sequences": 2, + "no-shadow": 0, + "no-spaced-func": 0, + "no-undef": 2, + "no-unused-vars": [1, {vars: all, args: none}], + "no-use-before-define": true, + "no-with": 2, + "quotes": [2, "single"], + "semi": [2, "always"], + "strict": [2, "global"], + "valid-typeof": 2, + "wrap-iife": 0 + } +} diff --git a/app/scripts/head/start.js b/app/scripts/head/start.js index 18ae842632..58763aab99 100644 --- a/app/scripts/head/start.js +++ b/app/scripts/head/start.js @@ -1,6 +1,7 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/*globals FxaHead */ 'use strict'; (function () { diff --git a/app/scripts/lib/app-start.js b/app/scripts/lib/app-start.js index d5b9925dbd..b61042562f 100644 --- a/app/scripts/lib/app-start.js +++ b/app/scripts/lib/app-start.js @@ -353,7 +353,8 @@ function ( }, initializeAuthenticationBroker: function () { - //jshint maxcomplexity: 7 + /*jshint maxcomplexity: 7 */ + /*eslint complexity: [2, 7] */ if (! this._authenticationBroker) { if (this._isFxDesktopV2()) { this._authenticationBroker = new FxDesktopV2AuthenticationBroker({ diff --git a/app/scripts/lib/metrics.js b/app/scripts/lib/metrics.js index 36161be9de..75e7c75f4e 100644 --- a/app/scripts/lib/metrics.js +++ b/app/scripts/lib/metrics.js @@ -1,7 +1,6 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -'use strict'; /* * A metrics module! @@ -14,6 +13,7 @@ * Metrics are automatically sent to the server on window.unload * but can also be sent by calling metrics.flush(); */ +'use strict'; define([ 'underscore', @@ -62,6 +62,7 @@ define([ function Metrics (options) { /*jshint maxcomplexity:18 */ + /*eslint complexity: [2, 18] */ options = options || {}; // by default, send the metrics to the content server. diff --git a/app/scripts/lib/screen-info.js b/app/scripts/lib/screen-info.js index e0db4dcec6..5338f0e2cd 100644 --- a/app/scripts/lib/screen-info.js +++ b/app/scripts/lib/screen-info.js @@ -12,6 +12,7 @@ define([ function ScreenInfo(win) { /*jshint maxcomplexity:8 */ + /*eslint complexity: [2, 8] */ var documentElement = win.document.documentElement || {}; var screen = win.screen || {}; diff --git a/app/scripts/lib/storage.js b/app/scripts/lib/storage.js index 5858b988ba..635abce27e 100644 --- a/app/scripts/lib/storage.js +++ b/app/scripts/lib/storage.js @@ -24,7 +24,7 @@ define([ var item; try { item = JSON.parse(this._backend.getItem(fullKey(key))); - } catch (e) { + } catch (e) {//eslint-disable-line no-empty } return item; }; diff --git a/app/scripts/require_config.js b/app/scripts/require_config.js index 6fd5b3c040..96cf26ed26 100644 --- a/app/scripts/require_config.js +++ b/app/scripts/require_config.js @@ -1,6 +1,7 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + 'use strict'; require.config({ baseUrl: '/scripts', diff --git a/app/scripts/views/base.js b/app/scripts/views/base.js index e1036af511..f0322c2ed5 100644 --- a/app/scripts/views/base.js +++ b/app/scripts/views/base.js @@ -1,7 +1,6 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - 'use strict'; define([ @@ -88,6 +87,7 @@ function (Cocktail, _, Backbone, $, p, AuthErrors, var BaseView = Backbone.View.extend({ constructor: function (options) { /*jshint maxcomplexity:10 */ + /*eslint complexity: [2, 10] */ options = options || {}; this.subviews = []; diff --git a/app/tests/spec/lib/channels/web.js b/app/tests/spec/lib/channels/web.js index 365af542d7..0597b25bfa 100644 --- a/app/tests/spec/lib/channels/web.js +++ b/app/tests/spec/lib/channels/web.js @@ -1,10 +1,8 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - 'use strict'; - define([ 'chai', 'sinon', @@ -31,7 +29,7 @@ function (chai, sinon, WebChannel, WindowMock) { it('requires an id', function () { assert.throws(function () { - new WebChannel(); + new WebChannel();//eslint-disable-line no-new }, 'WebChannel must have an id'); }); diff --git a/app/tests/spec/lib/xhr.js b/app/tests/spec/lib/xhr.js index 221ca92082..9bbf37b9f2 100644 --- a/app/tests/spec/lib/xhr.js +++ b/app/tests/spec/lib/xhr.js @@ -1,7 +1,6 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - 'use strict'; define([ @@ -12,7 +11,7 @@ define([ 'lib/xhr', 'lib/promise' ], -function (chai, sinon, $, _, Xhr, p, undefined) { +function (chai, sinon, $, _, Xhr, p, undefined) { //eslint-disable-line no-shadow-restricted-names var assert = chai.assert; diff --git a/app/tests/spec/lib/xss.js b/app/tests/spec/lib/xss.js index d07fee44e3..7f4f2505c8 100644 --- a/app/tests/spec/lib/xss.js +++ b/app/tests/spec/lib/xss.js @@ -39,7 +39,9 @@ function (chai, _, XSS, Constants) { }); it('disallows javascript: href', function () { - expectEmpty('javascript:alert(1)'); // jshint ignore:line + /*eslint-disable */ + expectEmpty('javascript:alert(1)'); // jshint ignore:line + /*eslint-enable */ }); it('disallows href without a scheme', function () { diff --git a/app/tests/spec/views/sign_up.js b/app/tests/spec/views/sign_up.js index bebba6b558..0e40ea74c7 100644 --- a/app/tests/spec/views/sign_up.js +++ b/app/tests/spec/views/sign_up.js @@ -1,10 +1,9 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - + /*global translator */ 'use strict'; - define([ 'chai', 'jquery', diff --git a/grunttasks/eslint.js b/grunttasks/eslint.js new file mode 100644 index 0000000000..31f94a954c --- /dev/null +++ b/grunttasks/eslint.js @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +'use strict'; + +module.exports = function (grunt) { + grunt.config('eslint', { + options: { + eslintrc: '.eslintrc' + }, + grunt: [ + 'Gruntfile.js', + 'grunttasks/*.js' + ], + app: { + options: { + eslintrc: '<%= yeoman.app %>/.eslintrc' + }, + src: [ + '<%= yeoman.app %>/**/*.js', + '!<%= yeoman.app %>/bower_components/**', + '!<%= yeoman.app %>/scripts/vendor/**' + ] + }, + tests: [ + '<%= yeoman.tests %>/**/*.js' + ], + server: [ + '<%= yeoman.server %>/**/*.js', + ] + }); +}; diff --git a/grunttasks/lint.js b/grunttasks/lint.js index bda66fd371..8ed371b1b0 100644 --- a/grunttasks/lint.js +++ b/grunttasks/lint.js @@ -7,6 +7,10 @@ module.exports = function (grunt) { grunt.registerTask('lint', 'lint all the things', [ - 'concurrent:lint' + 'eslint', + 'jshint', + 'jsonlint:app', + 'jscs', + 'amdcheck' ]); }; diff --git a/package.json b/package.json index 36f0e457f7..298096a7d9 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "grunt-contrib-watch": "0.6.1", "grunt-conventional-changelog": "1.1.0", "grunt-copyright": "0.1.0", + "grunt-eslint": "14.0.0", "grunt-jscs": "1.8.0", "grunt-jsonlint": "1.0.4", "grunt-nsp-shrinkwrap": "0.0.3", diff --git a/scripts/run_locally.js b/scripts/run_locally.js index 936ec5ad28..e31c7dc7fa 100755 --- a/scripts/run_locally.js +++ b/scripts/run_locally.js @@ -2,12 +2,13 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +'use strict'; -const path = require('path'); -const spawn = require('child_process').spawn; +var path = require('path'); +var spawn = require('child_process').spawn; -const BIN_ROOT = path.join(__dirname, '..', 'server', 'bin'); +var BIN_ROOT = path.join(__dirname, '..', 'server', 'bin'); module.exports = function (done) { process.chdir(path.dirname(__dirname)); @@ -21,7 +22,7 @@ module.exports = function (done) { if (done) { done(code); } else { - process.exit(code); + process.exit(code); //eslint-disable-line no-process-exit } }); }; diff --git a/server/bin/fxa-content-server.js b/server/bin/fxa-content-server.js index 74bb636ef9..0874b841fb 100755 --- a/server/bin/fxa-content-server.js +++ b/server/bin/fxa-content-server.js @@ -20,7 +20,6 @@ if (isMain) { process.chdir(path.dirname(__dirname)); } -var config = require('../lib/configuration'); mozlog.config(config.get('logging')); var logger = require('mozlog')('server.main'); diff --git a/server/lib/404.js b/server/lib/404.js index 7f9a16c923..09992d895e 100644 --- a/server/lib/404.js +++ b/server/lib/404.js @@ -1,7 +1,6 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - 'use strict'; // It's a 404 not found response. diff --git a/server/lib/csp.js b/server/lib/csp.js index 6252b506d1..588ff53b93 100644 --- a/server/lib/csp.js +++ b/server/lib/csp.js @@ -6,14 +6,14 @@ // option 'csp.enabled' is set (default true in development), with a special // exception for the /tests/index.html path, which are the frontend unit // tests. - 'use strict'; var helmet = require('helmet'); var config = require('./configuration'); var url = require('url'); - +/*eslint-disable */ var SELF = "'self'"; //jshint ignore: line +/*eslint-enable */ var DATA = 'data:'; var GRAVATAR = 'https://secure.gravatar.com'; diff --git a/server/lib/statsd-collector.js b/server/lib/statsd-collector.js index 6ed0a3a81d..c1a0f8797c 100644 --- a/server/lib/statsd-collector.js +++ b/server/lib/statsd-collector.js @@ -12,6 +12,7 @@ var STATSD_PREFIX = 'fxa.content.'; function getGenericTags(body) { /*jshint maxcomplexity:7 */ + /*eslint complexity: [2, 7] */ // see more about tags here: http://docs.datadoghq.com/guides/metrics/ var tags = [ 'campaign:' + body.campaign, diff --git a/tests/intern.js b/tests/intern.js index aa082bdb91..933a580032 100644 --- a/tests/intern.js +++ b/tests/intern.js @@ -13,6 +13,7 @@ define([ ], function (args, topic, firefoxProfile) { /*jshint maxcomplexity:11 */ + /*eslint complexity: [2, 11] */ var fxaAuthRoot = args.fxaAuthRoot || 'http://127.0.0.1:9000/v1'; var fxaContentRoot = args.fxaContentRoot || 'http://127.0.0.1:3030/'; var fxaEmailRoot = args.fxaEmailRoot || 'http://127.0.0.1:9001';