From 3d8bda70e52a4d90f656cf6bf3021af53bb7ca6f Mon Sep 17 00:00:00 2001 From: Minh Nguyen Date: Thu, 8 Nov 2018 20:56:35 +0300 Subject: [PATCH] Refactor ESLint configuration to enable better IDE integration (#13914) * Refactor ESLint configuration to enable better IDE integration * Minor tweaks --- .eslintrc.js | 48 +++++++++++++++++++ .../npm/umd/scheduler-tracing.development.js | 1 + .../umd/scheduler-tracing.production.min.js | 1 + .../umd/scheduler-tracing.profiling.min.js | 1 + .../npm/umd/scheduler.development.js | 1 + .../npm/umd/scheduler.production.min.js | 1 + .../npm/umd/scheduler.profiling.min.js | 1 + scripts/eslint/eslintrc.default.js | 31 ------------ scripts/eslint/eslintrc.es5.js | 26 ---------- scripts/eslint/eslintrc.esnext.js | 21 -------- scripts/eslint/index.js | 24 ++-------- 11 files changed, 58 insertions(+), 98 deletions(-) delete mode 100644 scripts/eslint/eslintrc.default.js delete mode 100644 scripts/eslint/eslintrc.es5.js delete mode 100644 scripts/eslint/eslintrc.esnext.js diff --git a/.eslintrc.js b/.eslintrc.js index 4444c643eab8..51c9395f73c5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,10 @@ 'use strict'; +const { + es5Paths, + esNextPaths, +} = require('./scripts/shared/pathsByLanguageVersion'); + const OFF = 0; const ERROR = 2; @@ -16,6 +21,15 @@ module.exports = { 'react-internal', ], + parser: 'espree', + parserOptions: { + ecmaVersion: 2017, + sourceType: 'script', + ecmaFeatures: { + experimentalObjectRestSpread: true, + }, + }, + // We're stricter than the default config, mostly. We'll override a few rules // and then enable some React specific ones. rules: { @@ -44,6 +58,13 @@ module.exports = { 'space-before-function-paren': OFF, 'valid-typeof': [ERROR, {requireStringLiterals: true}], + // We apply these settings to files that should run on Node. + // They can't use JSX or ES6 modules, and must be in strict mode. + // They can, however, use other ES6 features. + // (Note these rules are overridden later for source files.) + 'no-var': ERROR, + strict: ERROR, + // React & JSX // Our transforms set this automatically 'react/jsx-boolean-value': [ERROR, 'always'], @@ -71,6 +92,33 @@ module.exports = { }, overrides: [ + { + // We apply these settings to files that we ship through npm. + // They must be ES5. + files: es5Paths, + parser: 'espree', + parserOptions: { + ecmaVersion: 5, + sourceType: 'script', + }, + rules: { + 'no-var': OFF, + strict: ERROR, + }, + }, + { + // We apply these settings to the source files that get compiled. + // They can use all features including JSX (but shouldn't use `var`). + files: esNextPaths, + parser: 'babel-eslint', + parserOptions: { + sourceType: 'module', + }, + rules: { + 'no-var': ERROR, + strict: OFF, + }, + }, { files: ['**/__tests__/*.js'], rules: { diff --git a/packages/scheduler/npm/umd/scheduler-tracing.development.js b/packages/scheduler/npm/umd/scheduler-tracing.development.js index f793ead9cc5c..6c5b5ab78793 100644 --- a/packages/scheduler/npm/umd/scheduler-tracing.development.js +++ b/packages/scheduler/npm/umd/scheduler-tracing.development.js @@ -10,6 +10,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/packages/scheduler/npm/umd/scheduler-tracing.production.min.js b/packages/scheduler/npm/umd/scheduler-tracing.production.min.js index f793ead9cc5c..6c5b5ab78793 100644 --- a/packages/scheduler/npm/umd/scheduler-tracing.production.min.js +++ b/packages/scheduler/npm/umd/scheduler-tracing.production.min.js @@ -10,6 +10,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/packages/scheduler/npm/umd/scheduler-tracing.profiling.min.js b/packages/scheduler/npm/umd/scheduler-tracing.profiling.min.js index f793ead9cc5c..6c5b5ab78793 100644 --- a/packages/scheduler/npm/umd/scheduler-tracing.profiling.min.js +++ b/packages/scheduler/npm/umd/scheduler-tracing.profiling.min.js @@ -10,6 +10,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/packages/scheduler/npm/umd/scheduler.development.js b/packages/scheduler/npm/umd/scheduler.development.js index 72e909ebc225..52d08c5cba4f 100644 --- a/packages/scheduler/npm/umd/scheduler.development.js +++ b/packages/scheduler/npm/umd/scheduler.development.js @@ -12,6 +12,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/packages/scheduler/npm/umd/scheduler.production.min.js b/packages/scheduler/npm/umd/scheduler.production.min.js index 72e909ebc225..52d08c5cba4f 100644 --- a/packages/scheduler/npm/umd/scheduler.production.min.js +++ b/packages/scheduler/npm/umd/scheduler.production.min.js @@ -12,6 +12,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/packages/scheduler/npm/umd/scheduler.profiling.min.js b/packages/scheduler/npm/umd/scheduler.profiling.min.js index 72e909ebc225..52d08c5cba4f 100644 --- a/packages/scheduler/npm/umd/scheduler.profiling.min.js +++ b/packages/scheduler/npm/umd/scheduler.profiling.min.js @@ -12,6 +12,7 @@ 'use strict'; (function(global, factory) { + // eslint-disable-next-line no-unused-expressions typeof exports === 'object' && typeof module !== 'undefined' ? (module.exports = factory(require('react'))) : typeof define === 'function' && define.amd // eslint-disable-line no-undef diff --git a/scripts/eslint/eslintrc.default.js b/scripts/eslint/eslintrc.default.js deleted file mode 100644 index a63481ad1cc0..000000000000 --- a/scripts/eslint/eslintrc.default.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -'use strict'; - -const eslintrc = require('../../.eslintrc'); - -const ERROR = 2; - -// We apply these settings to files that should run on Node. -// They can't use JSX or ES6 modules, and must be in strict mode. -// They can, however, use other ES6 features. - -module.exports = Object.assign({}, eslintrc, { - parser: 'espree', - parserOptions: { - ecmaVersion: 2017, - sourceType: 'script', - ecmaFeatures: { - experimentalObjectRestSpread: true, - }, - }, - rules: Object.assign({}, eslintrc.rules, { - 'no-var': ERROR, - strict: ERROR, - }), -}); diff --git a/scripts/eslint/eslintrc.es5.js b/scripts/eslint/eslintrc.es5.js deleted file mode 100644 index 84f864444080..000000000000 --- a/scripts/eslint/eslintrc.es5.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -'use strict'; - -const eslintrc = require('../../.eslintrc'); - -const ERROR = 2; - -// We apply these settings to files that we ship through npm. -// They must be ES5. - -module.exports = Object.assign({}, eslintrc, { - parser: 'espree', - parserOptions: { - ecmaVersion: 5, - sourceType: 'script', - }, - rules: { - strict: ERROR, - }, -}); diff --git a/scripts/eslint/eslintrc.esnext.js b/scripts/eslint/eslintrc.esnext.js deleted file mode 100644 index 80bf150efbdf..000000000000 --- a/scripts/eslint/eslintrc.esnext.js +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -'use strict'; - -const eslintrc = require('../../.eslintrc'); - -const ERROR = 2; - -// We apply these settings to the source files that get compiled. -// They can use all features including JSX (but shouldn't use `var`). - -module.exports = Object.assign({}, eslintrc, { - rules: Object.assign({}, eslintrc.rules, { - 'no-var': ERROR, - }), -}); diff --git a/scripts/eslint/index.js b/scripts/eslint/index.js index 6cb8c475ec6e..a752a7cbfe51 100644 --- a/scripts/eslint/index.js +++ b/scripts/eslint/index.js @@ -10,7 +10,6 @@ const minimatch = require('minimatch'); const CLIEngine = require('eslint').CLIEngine; const listChangedFiles = require('../shared/listChangedFiles'); -const {es5Paths, esNextPaths} = require('../shared/pathsByLanguageVersion'); const allPaths = ['**/*.js']; @@ -65,25 +64,10 @@ function runESLint({onlyChanged}) { if (typeof onlyChanged !== 'boolean') { throw new Error('Pass options.onlyChanged as a boolean.'); } - let errorCount = 0; - let warningCount = 0; - let output = ''; - [ - runESLintOnFilesWithOptions(allPaths, onlyChanged, { - configFile: `${__dirname}/eslintrc.default.js`, - ignorePattern: [...es5Paths, ...esNextPaths], - }), - runESLintOnFilesWithOptions(esNextPaths, onlyChanged, { - configFile: `${__dirname}/eslintrc.esnext.js`, - }), - runESLintOnFilesWithOptions(es5Paths, onlyChanged, { - configFile: `${__dirname}/eslintrc.es5.js`, - }), - ].forEach(result => { - errorCount += result.errorCount; - warningCount += result.warningCount; - output += result.output; - }); + const {errorCount, warningCount, output} = runESLintOnFilesWithOptions( + allPaths, + onlyChanged + ); console.log(output); return errorCount === 0 && warningCount === 0; }