From 7f189041eb4415f1f2fe647cf194f2c7eb0d1472 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Apr 2023 14:43:45 -0400 Subject: [PATCH 1/4] docs: flesh out tips for typed linting and .eslintrc.cjs --- docs/linting/Troubleshooting.mdx | 54 +++++++++++++++++++++++++++++--- docs/linting/Typed_Linting.mdx | 9 ++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/docs/linting/Troubleshooting.mdx b/docs/linting/Troubleshooting.mdx index b8611b5bf4eb..bbb58bdfae9e 100644 --- a/docs/linting/Troubleshooting.mdx +++ b/docs/linting/Troubleshooting.mdx @@ -30,21 +30,67 @@ If you don't find an existing extension rule, or the extension rule doesn't work ## I get errors telling me "ESLint was configured to run ... However, that TSConfig does not / none of those TSConfigs include this file" +These errors come are caused by an ESLint config requesting type information be generated for a file that isn't included in the TypeScript configuration. + ### Fixing the Error + + - If you **do not** want to lint the file: - Use [one of the options ESLint offers](https://eslint.org/docs/latest/user-guide/configuring/ignoring-code) to ignore files, namely a `.eslintignore` file, or `ignorePatterns` config. - If you **do** want to lint the file: - If you **do not** want to lint the file with [type-aware linting](./Typed_Linting.mdx): - - Use [ESLint's `overrides` configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns) to configure the file to not be parsed with type information. - - A popular setup is to omit the above additions from top-level configuration and only apply them to TypeScript files via an override. - - Alternatively, you can add `parserOptions: { project: null }` to an override for the files you wish to exclude. Note that `{ project: undefined }` will not work. + - Use [ESLint's `overrides` configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns) to configure the file to not be parsed with type information: +
+ + A popular setup is remove all typed rules from top-level configuration + and only apply them to TypeScript files via an override. + + + ```js title=".eslintrc.cjs" + module.exports = { + // ... the rest of your config ... + overrides: [ + { + extends: [ + 'plugin:@typescript-eslint/recommended-requiring-type-checking', + ], + files: ['./**/*.{ts,tsx}'], + }, + ], + }; + ``` + +
+
+ + Alternatively, in our version v6, you can add use our{' '} + disable-type-checked config to disable type checking for just that type of file. + + + ```js title=".eslintrc.cjs" + module.exports = { + // ... the rest of your config ... + overrides: [ + { + extends: ['plugin:@typescript-eslint/disable-type-checked'], + files: ['./**/*.js'], + }, + ], + }; + ``` + To disable type checking for files manually, set `parserOptions: { project: null }` to an override for the files you wish to exclude. Note that `{ project: undefined }` will not work, and you'll also need to disable any rules or rule options that require type checking. +
+ - If you **do** want to lint the file with [type-aware linting](./Typed_Linting.mdx): - - Check the `include` option of each of the tsconfigs that you provide to `parserOptions.project` - you must ensure that all files match an `include` glob, or else our tooling will not be able to find it. + - Check the `include` option of each of the TSConfigs that you provide to `parserOptions.project` - you must ensure that all files match an `include` glob, or else our tooling will not be able to find it. + - If the file is a `.cjs`, `.js`, or `.mjs` file, make sure [`allowJs`](https://www.typescriptlang.org/tsconfig#allowJs) is enabled. - If your file shouldn't be a part of one of your existing tsconfigs (for example, it is a script/tool local to the repo), then consider creating a new tsconfig (we advise calling it `tsconfig.eslint.json`) in your project root which lists this file in its `include`. For an example of this, you can check out the configuration we use in this repo: - [`tsconfig.eslint.json`](https://github.com/typescript-eslint/typescript-eslint/blob/main/tsconfig.eslint.json) - [`.eslintrc.js`](https://github.com/typescript-eslint/typescript-eslint/blob/main/.eslintrc.js) + + ### More Details This error may appear from the combination of two things: diff --git a/docs/linting/Typed_Linting.mdx b/docs/linting/Typed_Linting.mdx index a51afdad02b5..d250f5c97fce 100644 --- a/docs/linting/Typed_Linting.mdx +++ b/docs/linting/Typed_Linting.mdx @@ -6,7 +6,7 @@ title: Linting with Type Information Some typescript-eslint rules utilize the awesome power of TypeScript's type checking APIs to provide much deeper insights into your code. To tap into TypeScript's additional powers, there are two small changes you need to make to your config file: -```js title=".eslintrc.js" +```js title=".eslintrc.cjs" module.exports = { extends: [ 'eslint:recommended', @@ -26,10 +26,15 @@ module.exports = { }; ``` +:::caution +Your `.eslintrc.cjs` file may start receiving a parsing error about type information. +See [our TSConfig inclusion FAQ](./Troubleshooting.mdx#i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file). +::: + In more detail: - `plugin:@typescript-eslint/recommended-requiring-type-checking` is another [recommended configuration](./CONFIGURATIONS.mdx) we provide. This one contains recommended rules that additionally require type information. -- `parserOptions.project` tells our parser the relative path where your project's `tsconfig.json` is. +- `parserOptions.project` tells our parser that each source file's type information settings are described by the closest `tsconfig.json` - If your project is a multi-package monorepo, see [our docs on configuring a monorepo](./typed-linting/Monorepos.mdx). - `parserOptions.tsconfigRootDir` tells our parser the absolute path of your project's root directory (see [Parser#tsconfigRootDir](../architecture/Parser.mdx#tsconfigRootDir)). From 0e23a30a9f68998f8f5519674718648fb28eeb8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 18 Apr 2023 10:08:33 -0400 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Joshua Chen --- docs/linting/Troubleshooting.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/linting/Troubleshooting.mdx b/docs/linting/Troubleshooting.mdx index bbb58bdfae9e..562884752980 100644 --- a/docs/linting/Troubleshooting.mdx +++ b/docs/linting/Troubleshooting.mdx @@ -30,7 +30,7 @@ If you don't find an existing extension rule, or the extension rule doesn't work ## I get errors telling me "ESLint was configured to run ... However, that TSConfig does not / none of those TSConfigs include this file" -These errors come are caused by an ESLint config requesting type information be generated for a file that isn't included in the TypeScript configuration. +These errors are caused by an ESLint config requesting type information be generated for a file that isn't included in the TypeScript configuration. ### Fixing the Error @@ -43,7 +43,7 @@ These errors come are caused by an ESLint config requesting type information be - Use [ESLint's `overrides` configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns) to configure the file to not be parsed with type information:
- A popular setup is remove all typed rules from top-level configuration + A popular setup is to remove all rules requiring type information from the top-level configuration and only apply them to TypeScript files via an override. @@ -64,7 +64,7 @@ These errors come are caused by an ESLint config requesting type information be
- Alternatively, in our version v6, you can add use our{' '} + Alternatively, in our version v6, you can use our{' '} disable-type-checked config to disable type checking for just that type of file. From 87117c751bd0329a40e4e3188633bc99734d6dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 18 Apr 2023 10:13:59 -0400 Subject: [PATCH 3/4] Update docs/linting/Typed_Linting.mdx Co-authored-by: Joshua Chen --- docs/linting/Typed_Linting.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/linting/Typed_Linting.mdx b/docs/linting/Typed_Linting.mdx index d250f5c97fce..94ca8ed6e408 100644 --- a/docs/linting/Typed_Linting.mdx +++ b/docs/linting/Typed_Linting.mdx @@ -34,7 +34,7 @@ See [our TSConfig inclusion FAQ](./Troubleshooting.mdx#i-get-errors-telling-me-e In more detail: - `plugin:@typescript-eslint/recommended-requiring-type-checking` is another [recommended configuration](./CONFIGURATIONS.mdx) we provide. This one contains recommended rules that additionally require type information. -- `parserOptions.project` tells our parser that each source file's type information settings are described by the closest `tsconfig.json` +- `parserOptions.project` tells our parser that each source file's type information settings are described by the closest `tsconfig.json`. - If your project is a multi-package monorepo, see [our docs on configuring a monorepo](./typed-linting/Monorepos.mdx). - `parserOptions.tsconfigRootDir` tells our parser the absolute path of your project's root directory (see [Parser#tsconfigRootDir](../architecture/Parser.mdx#tsconfigRootDir)). From 6c35975af68108d4fc37dac6a1e2ec7b991787e0 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 25 Apr 2023 18:18:24 -0400 Subject: [PATCH 4/4] Correct parserOptions.project description --- docs/linting/Typed_Linting.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/linting/Typed_Linting.mdx b/docs/linting/Typed_Linting.mdx index 94ca8ed6e408..8b6d39977093 100644 --- a/docs/linting/Typed_Linting.mdx +++ b/docs/linting/Typed_Linting.mdx @@ -34,7 +34,7 @@ See [our TSConfig inclusion FAQ](./Troubleshooting.mdx#i-get-errors-telling-me-e In more detail: - `plugin:@typescript-eslint/recommended-requiring-type-checking` is another [recommended configuration](./CONFIGURATIONS.mdx) we provide. This one contains recommended rules that additionally require type information. -- `parserOptions.project` tells our parser that each source file's type information settings are described by the closest `tsconfig.json`. +- `parserOptions.project` tells our parser how to find the TSConfig for each source file (`true` indicates to find the closest `tsconfig.json` for each source file) - If your project is a multi-package monorepo, see [our docs on configuring a monorepo](./typed-linting/Monorepos.mdx). - `parserOptions.tsconfigRootDir` tells our parser the absolute path of your project's root directory (see [Parser#tsconfigRootDir](../architecture/Parser.mdx#tsconfigRootDir)).