This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
eslint: Refactor eslint-loader and eslintrc config generation #1182
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,7 +132,6 @@ error: Missing semicolon (semi) at src/index.js:35:51: | |
37 | | ||
38 | | ||
|
||
|
||
1 error found. | ||
1 error potentially fixable with the `--fix` option. | ||
``` | ||
|
@@ -153,12 +152,13 @@ module.exports = { | |
|
||
## Middleware options | ||
|
||
This preset uses the same middleware options as [@neutrinojs/eslint](https://neutrinojs.org/packages/eslint/). | ||
This preset uses the same middleware options as [@neutrinojs/eslint](https://neutrinojs.org/packages/eslint/#usage). | ||
If you wish to customize what is included, excluded, or any ESLint options, you can provide an options object with the | ||
middleware and this will be merged with our internal defaults for this preset. Use an array pair instead of a string | ||
to supply these options. | ||
|
||
_Example: Turn off semicolons from being required as defined by the Airbnb rules._ | ||
_Example: Extend from a custom configuration (it will be applied after Airbnb) | ||
and turn off semicolons from being required._ | ||
|
||
```js | ||
module.exports = { | ||
|
@@ -168,64 +168,26 @@ module.exports = { | |
use: [ | ||
['@neutrinojs/airbnb-base', { | ||
eslint: { | ||
rules: { | ||
'babel/semi': 'off' | ||
// For supported options, see: | ||
// https://github.com/webpack-contrib/eslint-loader#options | ||
// https://eslint.org/docs/developer-guide/nodejs-api#cliengine | ||
// The options under `baseConfig` correspond to those | ||
// that can be used in an `.eslintrc.*` file. | ||
baseConfig: { | ||
extends: [ | ||
'my-custom-config' | ||
], | ||
rules: { | ||
'babel/semi': 'off' | ||
} | ||
} | ||
} | ||
}] | ||
] | ||
}; | ||
``` | ||
|
||
## Customizing | ||
|
||
To override the build configuration, start with the documentation on [customization](https://neutrinojs.org/customization/). | ||
`@neutrinojs/airbnb-base` creates some conventions to make overriding the configuration easier once you are ready to | ||
make changes. | ||
|
||
### Rules | ||
|
||
The following is a list of rules and their identifiers which can be overridden: | ||
|
||
| Name | Description | NODE_ENV | | ||
| --- | --- | --- | | ||
| `lint` | Lints JS and JSX files from the `src` directory using ESLint. Contains a single loader named `eslint`. This is inherited from `@neutrinojs/eslint`. | all | | ||
|
||
### Information | ||
|
||
If you want your preset or middleware to also extend from another **ESLint configuration or preset** that you have made | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now covered by all of the defaults above being listed under |
||
a dependency, you must use `baseConfig.extends` rather than just `extends`. This is a limitation of ESLint, not this | ||
middleware. | ||
|
||
### Override configuration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section is unnecessary IMO (given most people should not be using this method of customisation), and given the pain points we see around lint configuration, I think we need to take a strict "less is more" approach to what we put in the docs for it. |
||
|
||
By following the [customization guide](https://neutrinojs.org/customization/) and knowing the rule and loader IDs above, | ||
you can also override or augment the build by providing a function to your `.neutrinorc.js` use array. You can also | ||
make this change from the Neutrino API when using the `use` method. | ||
|
||
_Example: Turn off semicolons from being required as defined by the Airbnb rules from `.neutrinorc.js` using a function and the API:_ | ||
|
||
```js | ||
module.exports = { | ||
options: { | ||
root: __dirname | ||
}, | ||
use: [ | ||
'@neutrinojs/airbnb-base', | ||
(neutrino) => neutrino.config.module | ||
.rule('lint') | ||
.use('eslint') | ||
.tap(options => ({ | ||
...options, | ||
rules: { | ||
'babel/semi': 'off' | ||
} | ||
})) | ||
] | ||
}; | ||
``` | ||
|
||
## eslintrc Config | ||
## Exposing generated lint configuration via `.eslintrc.js` | ||
|
||
`@neutrinojs/eslint`, from which this preset inherits, provides an `.eslintrc()` output handler for | ||
generating the ESLint configuration in a format suitable for use in an `.eslintrc.js` file. This | ||
|
@@ -259,6 +221,29 @@ specify included and excluded files from the `.eslintrc.js` configuration. Inste | |
[.eslintignore](https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories) file that controls | ||
which files should be excluded from linting. | ||
|
||
## Using your own `.eslintrc.*` | ||
|
||
If instead you would prefer to use your own non-generated `.eslintrc.*` file, set `useEslintrc` to `true`. | ||
This will cause `@neutrinojs/airbnb-base` to only set the loader-specific configuration defaults, and leave | ||
all other linting configuration to be managed by the standalone `.eslintrc.*` file. | ||
|
||
See the `@neutrinojs/eslint` [documentation](https://neutrinojs.org/packages/eslint/#using-your-own-eslintrc) | ||
for more details. | ||
|
||
## Customizing | ||
|
||
To override the lint configuration, start with the documentation on [customization](https://neutrinojs.org/customization/). | ||
`@neutrinojs/airbnb-base` creates some conventions to make overriding the configuration easier once you are ready to | ||
make changes. | ||
|
||
### Rules | ||
|
||
The following is a list of rules and their identifiers which can be overridden: | ||
|
||
| Name | Description | NODE_ENV | | ||
| --- | --- | --- | | ||
| `lint` | By default, lints JS and JSX files from the `src` and `test` directories using ESLint. Contains a single loader named `eslint`. This is inherited from `@neutrinojs/eslint`. | all | | ||
|
||
## Contributing | ||
|
||
This preset is part of the [neutrino](https://github.com/neutrinojs/neutrino) repository, a monorepo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
const lint = require('@neutrinojs/eslint'); | ||
const { merge: eslintMerge } = require('eslint/lib/config/config-ops'); | ||
const { rules: airbnbBaseStyle } = require('eslint-config-airbnb-base/rules/style'); | ||
const { rules: airbnbBaseBestPractices } = require('eslint-config-airbnb-base/rules/best-practices'); | ||
|
||
module.exports = (neutrino, opts = {}) => { | ||
neutrino.use(lint, lint.merge({ | ||
module.exports = (neutrino, { eslint = {}, ...opts } = {}) => { | ||
neutrino.use(lint, { | ||
...opts, | ||
eslint: { | ||
baseConfig: { | ||
extends: [require.resolve('eslint-config-airbnb-base')] | ||
}, | ||
rules: { | ||
// Disable rules for which there are eslint-plugin-babel replacements: | ||
// https://github.com/babel/eslint-plugin-babel#rules | ||
'new-cap': 'off', | ||
'no-invalid-this': 'off', | ||
'object-curly-spacing': 'off', | ||
'semi': 'off', | ||
'no-unused-expressions': 'off', | ||
// Ensure the replacement rules use the options set by airbnb-base rather than ESLint defaults. | ||
'babel/new-cap': airbnbBaseStyle['new-cap'], | ||
'babel/no-invalid-this': airbnbBaseBestPractices['no-invalid-this'], | ||
'babel/object-curly-spacing': airbnbBaseStyle['object-curly-spacing'], | ||
'babel/semi': airbnbBaseStyle.semi, | ||
'babel/no-unused-expressions': airbnbBaseBestPractices['no-unused-expressions'] | ||
} | ||
...eslint, | ||
baseConfig: eslintMerge( | ||
{ | ||
extends: [require.resolve('eslint-config-airbnb-base')], | ||
rules: { | ||
// Disable rules for which there are eslint-plugin-babel replacements: | ||
// https://github.com/babel/eslint-plugin-babel#rules | ||
'new-cap': 'off', | ||
'no-invalid-this': 'off', | ||
'object-curly-spacing': 'off', | ||
'semi': 'off', | ||
'no-unused-expressions': 'off', | ||
// Ensure the replacement rules use the options set by airbnb-base rather than ESLint defaults. | ||
'babel/new-cap': airbnbBaseStyle['new-cap'], | ||
'babel/no-invalid-this': airbnbBaseBestPractices['no-invalid-this'], | ||
'babel/object-curly-spacing': airbnbBaseStyle['object-curly-spacing'], | ||
'babel/semi': airbnbBaseStyle.semi, | ||
'babel/no-unused-expressions': airbnbBaseBestPractices['no-unused-expressions'] | ||
} | ||
}, | ||
eslint.baseConfig || {} | ||
) | ||
} | ||
}, | ||
opts)); | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth noting that this form still works, it's just not what we're using for the defaults / what we're recommending. Any preset that pass in the old form will still correctly override the values under
baseConfig
, since ESLint's CLIEngine (plus our eslintrc conversion function) merges them in such a way that the top-level options take precedence.