Skip to content

Commit eee8491

Browse files
ianschmitziansu
authored andcommitted
Add TypeScript linting support (#6513)
* Initial pass adding typescript-eslint * Add warning to shared rule set * Add documentation for setting up VSCode extension * Provide tsconfig path to typescript-eslitn
1 parent 7ae3146 commit eee8491

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ You would need to install an ESLint plugin for your editor first. Then, add a fi
2828
}
2929
```
3030

31+
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):
32+
33+
```json
34+
{
35+
"eslint.validate": [
36+
"javascript",
37+
"javascriptreact",
38+
{ "language": "typescript", "autoFix": true },
39+
{ "language": "typescriptreact", "autoFix": true }
40+
]
41+
}
42+
```
43+
3144
Now your editor should report the linting warnings.
3245

3346
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.

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

+50-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
// This is dangerous as it hides accidentally undefined variables.
2222
// We blacklist the globals that we deem potentially confusing.
2323
// To use them, explicitly reference them, e.g. `window.name` or `window.status`.
24-
var restrictedGlobals = require('confusing-browser-globals');
24+
const restrictedGlobals = require('confusing-browser-globals');
25+
26+
// The following is copied from `react-scripts/config/paths.js`.
27+
const path = require('path');
28+
const fs = require('fs');
29+
// Make sure any symlinks in the project folder are resolved:
30+
// https://github.com/facebook/create-react-app/issues/637
31+
const appDirectory = fs.realpathSync(process.cwd());
32+
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
33+
const projectRootPath = resolveApp('.');
34+
const tsConfigPath = resolveApp('tsconfig.json');
2535

2636
module.exports = {
2737
root: true,
@@ -52,6 +62,45 @@ module.exports = {
5262
},
5363
},
5464

65+
overrides: {
66+
files: ['**/*.ts', '**/*.tsx'],
67+
parser: '@typescript-eslint/parser',
68+
parserOptions: {
69+
ecmaVersion: 2018,
70+
sourceType: 'module',
71+
ecmaFeatures: {
72+
jsx: true,
73+
},
74+
75+
// typescript-eslint specific options
76+
project: tsConfigPath,
77+
tsconfigRootDir: projectRootPath,
78+
warnOnUnsupportedTypeScriptVersion: true,
79+
},
80+
plugins: ['@typescript-eslint'],
81+
rules: {
82+
// These ESLint rules are known to cause issues with typescript-eslint
83+
// See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
84+
camelcase: 'off',
85+
indent: 'off',
86+
'no-array-constructor': 'off',
87+
'no-unused-vars': 'off',
88+
89+
'@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
90+
'@typescript-eslint/no-array-constructor': 'warn',
91+
'@typescript-eslint/no-namespace': 'error',
92+
'@typescript-eslint/no-unused-vars': [
93+
'warn',
94+
{
95+
args: 'none',
96+
ignoreRestSiblings: true,
97+
},
98+
],
99+
},
100+
},
101+
102+
// NOTE: When adding rules here, you need to make sure they are compatible with
103+
// `typescript-eslint`, as some rules such as `no-array-constructor` aren't compatible.
55104
rules: {
56105
// http://eslint.org/docs/rules/
57106
'array-callback-return': 'warn',

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

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"index.js"
1212
],
1313
"peerDependencies": {
14+
"@typescript-eslint/eslint-plugin": "1.x",
15+
"@typescript-eslint/parser": "1.x",
1416
"babel-eslint": "9.x",
1517
"eslint": "5.x",
1618
"eslint-plugin-flowtype": "2.x",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ module.exports = function(webpackEnv) {
304304
// First, run the linter.
305305
// It's important to do this before Babel processes the JS.
306306
{
307-
test: /\.(js|mjs|jsx)$/,
307+
test: /\.(js|mjs|jsx|ts|tsx)$/,
308308
enforce: 'pre',
309309
use: [
310310
{

packages/react-scripts/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"dependencies": {
2727
"@babel/core": "7.3.4",
2828
"@svgr/webpack": "4.1.0",
29+
"@typescript-eslint/eslint-plugin": "1.4.1",
30+
"@typescript-eslint/parser": "1.4.1",
2931
"babel-core": "7.0.0-bridge.0",
3032
"babel-eslint": "10.0.1",
3133
"babel-jest": "23.6.0",

0 commit comments

Comments
 (0)