Skip to content

Commit 3495286

Browse files
authored
Allow extending config used in eslint-loader (facebook#7036)
1 parent 3a9b87b commit 3495286

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

docusaurus/docs/advanced-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ You can adjust various development and production settings by setting environmen
2222
| NODE_PATH | ✅ Used | ✅ Used | Same as [`NODE_PATH` in Node.js](https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders), but only relative folders are allowed. Can be handy for emulating a monorepo setup by setting `NODE_PATH=src`. |
2323
| INLINE_RUNTIME_CHUNK | 🚫 Ignored | ✅ Used | By default, Create React App will embed the runtime script into `index.html` during the production build. When set to `false`, the script will not be embedded and will be imported as usual. This is normally required when dealing with CSP. |
2424
| IMAGE_INLINE_SIZE_LIMIT | 🚫 Ignored | ✅ Used | By default, images smaller than 10,000 bytes are encoded as a data URI in base64 and inlined in the CSS or JS build artifact. Set this to control the size limit in bytes. Setting it to 0 will disable the inlining of images. |
25+
| EXTEND_ESLINT | ✅ Used | ✅ Used | When set to `true`, ESLint configs that extend `eslint-config-react-app` will be used by `eslint-loader`. Any rules that are set to `"error"` will stop the application from building. |

docusaurus/docs/setting-up-your-editor.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ To configure the syntax highlighting in your favorite text editor, head to the [
1818
1919
Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint.
2020

21-
They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do.
22-
23-
You would need to install an ESLint plugin for your editor first. Then, add a file called `.eslintrc.json` to the project root:
24-
25-
```json
26-
{
27-
"extends": "react-app"
28-
}
29-
```
21+
They are not required for linting. You should see the linter output right in your terminal as well as the browser console. If you prefer the lint results to appear right in your editor, please make sure you install an ESLint plugin/extension.
3022

3123
If you're using TypeScript and Visual Studio Code, the [ESLint Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint#overview) currently [doesn't have TypeScript support enabled by default](https://github.com/Microsoft/vscode-eslint/issues/609). To enable TypeScript support in the ESLint extension, add the following to your project's Visual Studio Code settings file, located at `.vscode/settings.json` (you can create this file if it doesn't already exist):
3224

@@ -43,10 +35,46 @@ If you're using TypeScript and Visual Studio Code, the [ESLint Visual Studio Cod
4335

4436
Now your editor should report the linting warnings.
4537

46-
Note that even if you edit your `.eslintrc.json` file further, these changes will **only affect the editor integration**. They won’t affect the terminal and in-browser lint output. This is because Create React App intentionally provides a minimal set of rules that find common mistakes.
38+
Note that even if you customise your ESLint config, these changes will **only affect the editor integration**. They won’t affect the terminal and in-browser lint output. This is because Create React App intentionally provides a minimal set of rules that find common mistakes.
4739

4840
If you want to enforce a coding style for your project, consider using [Prettier](https://github.com/jlongster/prettier) instead of ESLint style rules.
4941

42+
### Experimental: Extending the ESLint config
43+
44+
We recognise that in some cases, further customisation is required. It is now possible to extend the base ESLint config by setting the `EXTEND_ESLINT` environment variable to `true`. See [advanced configuration](advanced-configuration.md) for more information on available environment variables.
45+
46+
Note that any rules set to `"error"` will stop the project from building.
47+
48+
There are a few things to remember:
49+
50+
1. You must extend the base config, as removing it could introduce hard-to-find issues.
51+
1. When working with TypeScript, you'll need to provide an `overrides` object for rules that should _only_ target TypeScript files.
52+
53+
In the below example:
54+
55+
- the base config has been extended by a shared ESLint config,
56+
- a new rule has been set that applies to all JavaScript and TypeScript files, and
57+
- a new rule has been set that only targets TypeScript files.
58+
59+
```json
60+
{
61+
"eslintConfig": {
62+
"extends": ["react-app", "shared-config"],
63+
"rules": {
64+
"additional-rule": "warn"
65+
},
66+
"overrides": [
67+
{
68+
"files": ["**/*.ts?(x)"],
69+
"rules": {
70+
"additional-typescript-only-rule": "warn"
71+
}
72+
}
73+
]
74+
}
75+
}
76+
```
77+
5078
## Debugging in the Editor
5179

5280
**This feature is currently only supported by [Visual Studio Code](https://code.visualstudio.com) and [WebStorm](https://www.jetbrains.com/webstorm/).**

packages/eslint-config-react-app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454

5555
overrides: [
5656
{
57-
files: ['**/*.ts', '**/*.tsx'],
57+
files: ['**/*.ts?(x)'],
5858
parser: '@typescript-eslint/parser',
5959
parserOptions: {
6060
ecmaVersion: 2018,

packages/react-scripts/config/webpack.config.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const getClientEnvironment = require('./env');
3333
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
3434
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');
3535
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
36+
const eslint = require('eslint');
3637
// @remove-on-eject-begin
3738
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
3839
// @remove-on-eject-end
@@ -323,9 +324,30 @@ module.exports = function(webpackEnv) {
323324
formatter: require.resolve('react-dev-utils/eslintFormatter'),
324325
eslintPath: require.resolve('eslint'),
325326
// @remove-on-eject-begin
326-
baseConfig: {
327-
extends: [require.resolve('eslint-config-react-app')],
328-
},
327+
baseConfig: (() => {
328+
const eslintCli = new eslint.CLIEngine();
329+
let eslintConfig;
330+
try {
331+
eslintConfig = eslintCli.getConfigForFile(paths.appIndexJs);
332+
} catch (e) {
333+
// A config couldn't be found.
334+
}
335+
336+
// We allow overriding the config, only if it extends our config
337+
// (`extends` can be a string or array of strings).
338+
if (
339+
process.env.EXTEND_ESLINT &&
340+
eslintConfig &&
341+
eslintConfig.extends &&
342+
eslintConfig.extends.includes('react-app')
343+
) {
344+
return eslintConfig;
345+
} else {
346+
return {
347+
extends: [require.resolve('eslint-config-react-app')],
348+
};
349+
}
350+
})(),
329351
ignore: false,
330352
useEslintrc: false,
331353
// @remove-on-eject-end

packages/react-scripts/scripts/eject.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,12 @@ inquirer
239239
};
240240

241241
// Add ESlint config
242-
console.log(` Adding ${cyan('ESLint')} configuration`);
243-
appPackage.eslintConfig = {
244-
extends: 'react-app',
245-
};
242+
if (!appPackage.eslintConfig) {
243+
console.log(` Adding ${cyan('ESLint')} configuration`);
244+
appPackage.eslintConfig = {
245+
extends: 'react-app',
246+
};
247+
}
246248

247249
fs.writeFileSync(
248250
path.join(appPath, 'package.json'),

0 commit comments

Comments
 (0)