Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ESLint configuration to enable better IDE integration #13914

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

const {
es5Paths,
esNextPaths,
} = require('./scripts/shared/pathsByLanguageVersion');

const OFF = 0;
const ERROR = 2;

Expand All @@ -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: {
Expand Down Expand Up @@ -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.
NMinhNguyen marked this conversation as resolved.
Show resolved Hide resolved
// 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'],
Expand Down Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to turn this rule off because strict: ERROR with sourceType: 'module' causes errors such as

/react/packages/create-subscription/index.js
  10:1  error  'use strict' is unnecessary inside of modules  strict

},
},
{
files: ['**/__tests__/*.js'],
rules: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'use strict';

(function(global, factory) {
// eslint-disable-next-line no-unused-expressions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding these to each UMD bundle, I could add another overrides section disabling this rule. Something like

// .eslintrc.js
{
  overrides: [
    // ...
    {
      files: ['packages/*/npm/umd/*.js'],
      rules: {
        'no-unused-expressions': OFF,
      }
    }
  ]
}

typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory(require('react')))
: typeof define === 'function' && define.amd // eslint-disable-line no-undef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/scheduler/npm/umd/scheduler.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/scheduler/npm/umd/scheduler.production.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/scheduler/npm/umd/scheduler.profiling.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions scripts/eslint/eslintrc.default.js

This file was deleted.

26 changes: 0 additions & 26 deletions scripts/eslint/eslintrc.es5.js

This file was deleted.

21 changes: 0 additions & 21 deletions scripts/eslint/eslintrc.esnext.js

This file was deleted.

24 changes: 4 additions & 20 deletions scripts/eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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;
}
Expand Down