Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit e258532

Browse files
authored
Remove ESLint extends and add Jest rules (facebook#9587)
1 parent 97695bc commit e258532

File tree

11 files changed

+65
-18
lines changed

11 files changed

+65
-18
lines changed

docusaurus/docs/advanced-configuration.md

-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ You can adjust various development and production settings by setting environmen
2424
| GENERATE_SOURCEMAP | 🚫 Ignored | ✅ Used | When set to `false`, source maps are not generated for a production build. This solves out of memory (OOM) issues on some smaller machines. |
2525
| 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. |
2626
| 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. |
27-
| EXTEND_ESLINT | ✅ Used | ✅ Used | When set to `true`, user provided ESLint configs will be used by `eslint-loader`. Note that any rules set to `"error"` will stop the application from building. |
2827
| FAST_REFRESH | ✅ Used | 🚫 Ignored | When set to `false`, disables experimental support for Fast Refresh to allow you to tweak your components in real time without reloading the page. |
2928
| TSC_COMPILE_ON_ERROR | ✅ Used | ✅ Used | When set to `true`, you can run and properly build TypeScript projects even if there are TypeScript type check errors. These errors are printed as warnings in the terminal and/or browser console. |

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ Note that even if you customise your ESLint config, these changes will **only af
2626

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

29-
### Experimental: Extending the ESLint config
29+
### Extending or replacing the default ESLint config
3030

31-
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.
32-
33-
Note that any rules set to `"error"` will stop the project from building.
31+
You can extend our base ESLint config, or replace it completely if you need.
3432

3533
There are a few things to remember:
3634

3735
1. We highly recommend extending the base config, as removing it could introduce hard-to-find issues.
3836
1. When working with TypeScript, you'll need to provide an `overrides` object for rules that should _only_ target TypeScript files.
37+
1. It's important to note that any rules that are set to `"error"` will stop the project from building.
3938

4039
In the below example:
4140

packages/cra-template-typescript/template.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"@types/jest": "^26.0.0",
1111
"typescript": "^3.9.5",
1212
"web-vitals": "^0.2.2"
13+
},
14+
"eslintConfig": {
15+
"extends": ["react-app", "react-app/jest"]
1316
}
1417
}
1518
}

packages/cra-template/template.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"@testing-library/react": "^10.4.3",
66
"@testing-library/user-event": "^12.0.11",
77
"web-vitals": "^0.2.2"
8+
},
9+
"eslintConfig": {
10+
"extends": ["react-app", "react-app/jest"]
811
}
912
}
1013
}

packages/eslint-config-react-app/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ Then create a file named `.eslintrc.json` with following contents in the root fo
3232

3333
That's it! You can override the settings from `eslint-config-react-app` by editing the `.eslintrc.json` file. Learn more about [configuring ESLint](http://eslint.org/docs/user-guide/configuring) on the ESLint website.
3434

35+
## Jest rules
36+
37+
This config also ships with optional Jest rules for ESLint (based on [`eslint-plugin-jest`](https://github.com/jest-community/eslint-plugin-jest)).
38+
39+
You'll first need to add the plugin.
40+
41+
```sh
42+
npm install --save-dev [email protected]
43+
```
44+
45+
You can then enable these rules by adding the Jest config to the `extends` array in your ESLint config.
46+
47+
```json
48+
{
49+
"extends": ["react-app", "react-app/jest"]
50+
}
51+
```
52+
3553
## Accessibility Checks
3654

3755
The following rules from the [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y) plugin are activated:

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
// Inspired by https://github.com/airbnb/javascript but less opinionated.
1111

1212
// We use eslint-loader so even warnings are very visible.
13-
// This is why we only use "WARNING" level for potential errors,
14-
// and we don't use "ERROR" level at all.
13+
// This is why we prefer to use "WARNING" level for potential errors,
14+
// and we try not to use "ERROR" level at all.
1515

1616
// In the future, we might create a separate list of rules for production.
1717
// It would probably be more strict.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
// We use eslint-loader so even warnings are very visible.
11+
// This is why we prefer to use "WARNING" level for potential errors,
12+
// and we try not to use "ERROR" level at all.
13+
14+
module.exports = {
15+
plugins: ['jest'],
16+
overrides: [
17+
{
18+
files: ['**/__tests__/**/*', '**/*.{spec,test}.*'],
19+
env: {
20+
'jest/globals': true,
21+
},
22+
// A subset of the recommended rules:
23+
// https://github.com/jest-community/eslint-plugin-jest#rules
24+
rules: {
25+
'jest/expect-expect': 'warn',
26+
'jest/no-identical-title': 'warn',
27+
'jest/valid-describe': 'warn',
28+
'jest/valid-expect': 'warn',
29+
'jest/valid-expect-in-promise': 'warn',
30+
},
31+
},
32+
],
33+
};

packages/eslint-config-react-app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"eslint": "^7.0.0",
2222
"eslint-plugin-flowtype": "^5.2.0",
2323
"eslint-plugin-import": "^2.22.0",
24+
"eslint-plugin-jest": "^24.0.0",
2425
"eslint-plugin-jsx-a11y": "^6.3.1",
2526
"eslint-plugin-react": "^7.20.3",
2627
"eslint-plugin-react-hooks": "^4.0.8"

packages/react-error-overlay/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"eslint-config-react-app": "^5.2.1",
4949
"eslint-plugin-flowtype": "^5.2.0",
5050
"eslint-plugin-import": "^2.22.0",
51+
"eslint-plugin-jest": "^24.0.0",
5152
"eslint-plugin-jsx-a11y": "^6.3.1",
5253
"eslint-plugin-react": "^7.20.3",
5354
"eslint-plugin-react-hooks": "^4.0.8",

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

-11
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ const webpackDevClientEntry = require.resolve(
5151
// makes for a smoother build process.
5252
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
5353

54-
const isExtendingEslintConfig = process.env.EXTEND_ESLINT === 'true';
55-
5654
const imageInlineSizeLimit = parseInt(
5755
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
5856
);
@@ -364,15 +362,6 @@ module.exports = function (webpackEnv) {
364362
formatter: require.resolve('react-dev-utils/eslintFormatter'),
365363
eslintPath: require.resolve('eslint'),
366364
resolvePluginsRelativeTo: __dirname,
367-
// @remove-on-eject-begin
368-
ignore: isExtendingEslintConfig,
369-
baseConfig: isExtendingEslintConfig
370-
? undefined
371-
: {
372-
extends: [require.resolve('eslint-config-react-app')],
373-
},
374-
useEslintrc: isExtendingEslintConfig,
375-
// @remove-on-eject-end
376365
},
377366
loader: require.resolve('eslint-loader'),
378367
},

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"eslint-loader": "^4.0.2",
5050
"eslint-plugin-flowtype": "^5.2.0",
5151
"eslint-plugin-import": "^2.22.0",
52+
"eslint-plugin-jest": "^24.0.0",
5253
"eslint-plugin-jsx-a11y": "^6.3.1",
5354
"eslint-plugin-react": "^7.20.3",
5455
"eslint-plugin-react-hooks": "^4.0.8",

0 commit comments

Comments
 (0)