Skip to content

Commit

Permalink
Add TypeScript linting support (#6513)
Browse files Browse the repository at this point in the history
* Initial pass adding typescript-eslint

* Add warning to shared rule set

* Add documentation for setting up VSCode extension

* Provide tsconfig path to typescript-eslitn
  • Loading branch information
ianschmitz authored and iansu committed Mar 15, 2019
1 parent 7ae3146 commit eee8491
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docusaurus/docs/setting-up-your-editor.md
Expand Up @@ -28,6 +28,19 @@ You would need to install an ESLint plugin for your editor first. Then, add a fi
}
```

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):

```json
{
"eslint.validate": [
"javascript",
"javascriptreact",
{ "language": "typescript", "autoFix": true },
{ "language": "typescriptreact", "autoFix": true }
]
}
```

Now your editor should report the linting warnings.

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.
Expand Down
51 changes: 50 additions & 1 deletion packages/eslint-config-react-app/index.js
Expand Up @@ -21,7 +21,17 @@
// This is dangerous as it hides accidentally undefined variables.
// We blacklist the globals that we deem potentially confusing.
// To use them, explicitly reference them, e.g. `window.name` or `window.status`.
var restrictedGlobals = require('confusing-browser-globals');
const restrictedGlobals = require('confusing-browser-globals');

// The following is copied from `react-scripts/config/paths.js`.
const path = require('path');
const fs = require('fs');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const projectRootPath = resolveApp('.');
const tsConfigPath = resolveApp('tsconfig.json');

module.exports = {
root: true,
Expand Down Expand Up @@ -52,6 +62,45 @@ module.exports = {
},
},

overrides: {
files: ['**/*.ts', '**/*.tsx'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},

// typescript-eslint specific options
project: tsConfigPath,
tsconfigRootDir: projectRootPath,
warnOnUnsupportedTypeScriptVersion: true,
},
plugins: ['@typescript-eslint'],
rules: {
// These ESLint rules are known to cause issues with typescript-eslint
// See https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
camelcase: 'off',
indent: 'off',
'no-array-constructor': 'off',
'no-unused-vars': 'off',

'@typescript-eslint/no-angle-bracket-type-assertion': 'warn',
'@typescript-eslint/no-array-constructor': 'warn',
'@typescript-eslint/no-namespace': 'error',
'@typescript-eslint/no-unused-vars': [
'warn',
{
args: 'none',
ignoreRestSiblings: true,
},
],
},
},

// NOTE: When adding rules here, you need to make sure they are compatible with
// `typescript-eslint`, as some rules such as `no-array-constructor` aren't compatible.
rules: {
// http://eslint.org/docs/rules/
'array-callback-return': 'warn',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-config-react-app/package.json
Expand Up @@ -11,6 +11,8 @@
"index.js"
],
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "1.x",
"@typescript-eslint/parser": "1.x",
"babel-eslint": "9.x",
"eslint": "5.x",
"eslint-plugin-flowtype": "2.x",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/config/webpack.config.js
Expand Up @@ -304,7 +304,7 @@ module.exports = function(webpackEnv) {
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|mjs|jsx)$/,
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/react-scripts/package.json
Expand Up @@ -26,6 +26,8 @@
"dependencies": {
"@babel/core": "7.3.4",
"@svgr/webpack": "4.1.0",
"@typescript-eslint/eslint-plugin": "1.4.1",
"@typescript-eslint/parser": "1.4.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.1",
"babel-jest": "23.6.0",
Expand Down

0 comments on commit eee8491

Please sign in to comment.