Refactor ESLint configuration to enable better IDE integration (#13914)

* Refactor ESLint configuration to enable better IDE integration

* Minor tweaks
This commit is contained in:
Minh Nguyen 2018-11-08 20:56:35 +03:00 committed by Dan Abramov
parent 051272f201
commit 3d8bda70e5
11 changed files with 58 additions and 98 deletions

View File

@ -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: {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,
}),
});

View File

@ -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,
},
});

View File

@ -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,
}),
});

View File

@ -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;
}