From b9d6c281869bcb0b1bbbda95f8009637e78b6303 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 11:36:40 -0500 Subject: [PATCH 01/21] feat: add no-property-in-node rule --- README.md | 106 +++++++++++-------- configs/recommended-type-checked.js | 8 ++ docs/rules/consistent-output.md | 2 +- docs/rules/fixer-return.md | 2 +- docs/rules/no-deprecated-context-methods.md | 2 +- docs/rules/no-deprecated-report-api.md | 2 +- docs/rules/no-identical-tests.md | 2 +- docs/rules/no-missing-message-ids.md | 2 +- docs/rules/no-missing-placeholders.md | 2 +- docs/rules/no-only-tests.md | 2 +- docs/rules/no-property-in-node.md | 38 +++++++ docs/rules/no-unused-message-ids.md | 2 +- docs/rules/no-unused-placeholders.md | 2 +- docs/rules/no-useless-token-range.md | 2 +- docs/rules/prefer-message-ids.md | 2 +- docs/rules/prefer-object-rule.md | 2 +- docs/rules/prefer-output-null.md | 2 +- docs/rules/require-meta-fixable.md | 2 +- docs/rules/require-meta-has-suggestions.md | 2 +- docs/rules/require-meta-schema.md | 2 +- docs/rules/require-meta-type.md | 2 +- lib/index.js | 4 +- lib/rules/no-property-in-node.js | 58 ++++++++++ package.json | 9 +- tests/lib/rules/fixtures/file.ts | 0 tests/lib/rules/fixtures/tsconfig.json | 5 + tests/lib/rules/no-property-in-node.js | 111 ++++++++++++++++++++ 27 files changed, 311 insertions(+), 64 deletions(-) create mode 100644 configs/recommended-type-checked.js create mode 100644 docs/rules/no-property-in-node.md create mode 100644 lib/rules/no-property-in-node.js create mode 100644 tests/lib/rules/fixtures/file.ts create mode 100644 tests/lib/rules/fixtures/tsconfig.json create mode 100644 tests/lib/rules/no-property-in-node.js diff --git a/README.md b/README.md index 18728fed..08063497 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ An ESLint plugin for linting ESLint plugins. Rules written in CJS, ESM, and TypeScript are all supported. + * [Installation](#Installation) * [Usage](#Usage) * [Rules](#Rules) @@ -69,6 +70,24 @@ module.exports = [ ]; ``` +### Type Checked Rules + +Rules marked with _πŸ’­ Requires type information_ require enabling [linting with type information](https://typescript-eslint.io/linting/typed-linting). +These rules are omitted from the `recommended` config, and instead are enabled in the `recommended-type-checked` superset config. + +If your plugin is authored in TypeScript, we recommend replacing `recommended` with `recommended-type-checked` and enabling typed linting: + +```js +module.exports = { + parser: '@typescript-eslint/parser', + parserOptions: { + project: true, + tsconfigRootDir: __dirname, + }, + extends: ['plugin:eslint-plugin/recommended-type-checked'], +}; +``` + ## Rules @@ -76,56 +95,59 @@ module.exports = [ πŸ’Ό [Configurations](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets) enabled in.\ βœ… Set in the `recommended` [configuration](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets).\ πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\ -πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). +πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\ +πŸ’­ Requires type information. ### Rules -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | -| :--------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :- | :- | :- | -| [fixer-return](docs/rules/fixer-return.md) | require fixer functions to return a fix | βœ… | | | -| [meta-property-ordering](docs/rules/meta-property-ordering.md) | enforce the order of meta properties | | πŸ”§ | | -| [no-deprecated-context-methods](docs/rules/no-deprecated-context-methods.md) | disallow usage of deprecated methods on rule context objects | βœ… | πŸ”§ | | -| [no-deprecated-report-api](docs/rules/no-deprecated-report-api.md) | disallow the version of `context.report()` with multiple arguments | βœ… | πŸ”§ | | -| [no-missing-message-ids](docs/rules/no-missing-message-ids.md) | disallow `messageId`s that are missing from `meta.messages` | βœ… | | | -| [no-missing-placeholders](docs/rules/no-missing-placeholders.md) | disallow missing placeholders in rule report messages | βœ… | | | -| [no-unused-message-ids](docs/rules/no-unused-message-ids.md) | disallow unused `messageId`s in `meta.messages` | βœ… | | | -| [no-unused-placeholders](docs/rules/no-unused-placeholders.md) | disallow unused placeholders in rule report messages | βœ… | | | -| [no-useless-token-range](docs/rules/no-useless-token-range.md) | disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` | βœ… | πŸ”§ | | -| [prefer-message-ids](docs/rules/prefer-message-ids.md) | require using `messageId` instead of `message` or `desc` to report rule violations | βœ… | | | -| [prefer-object-rule](docs/rules/prefer-object-rule.md) | disallow function-style rules | βœ… | πŸ”§ | | -| [prefer-placeholders](docs/rules/prefer-placeholders.md) | require using placeholders for dynamic report messages | | | | -| [prefer-replace-text](docs/rules/prefer-replace-text.md) | require using `replaceText()` instead of `replaceTextRange()` | | | | -| [report-message-format](docs/rules/report-message-format.md) | enforce a consistent format for rule report messages | | | | -| [require-meta-docs-description](docs/rules/require-meta-docs-description.md) | require rules to implement a `meta.docs.description` property with the correct format | | | | -| [require-meta-docs-url](docs/rules/require-meta-docs-url.md) | require rules to implement a `meta.docs.url` property | | πŸ”§ | | -| [require-meta-fixable](docs/rules/require-meta-fixable.md) | require rules to implement a `meta.fixable` property | βœ… | | | -| [require-meta-has-suggestions](docs/rules/require-meta-has-suggestions.md) | require suggestable rules to implement a `meta.hasSuggestions` property | βœ… | πŸ”§ | | -| [require-meta-schema](docs/rules/require-meta-schema.md) | require rules to implement a `meta.schema` property | βœ… | | πŸ’‘ | -| [require-meta-type](docs/rules/require-meta-type.md) | require rules to implement a `meta.type` property | βœ… | | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | +| :--------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :------------------------------------ | :- | :- | :- | +| [fixer-return](docs/rules/fixer-return.md) | require fixer functions to return a fix | βœ… ![badge-recommended-type-checked][] | | | | +| [meta-property-ordering](docs/rules/meta-property-ordering.md) | enforce the order of meta properties | | πŸ”§ | | | +| [no-deprecated-context-methods](docs/rules/no-deprecated-context-methods.md) | disallow usage of deprecated methods on rule context objects | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [no-deprecated-report-api](docs/rules/no-deprecated-report-api.md) | disallow the version of `context.report()` with multiple arguments | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [no-missing-message-ids](docs/rules/no-missing-message-ids.md) | disallow `messageId`s that are missing from `meta.messages` | βœ… ![badge-recommended-type-checked][] | | | | +| [no-missing-placeholders](docs/rules/no-missing-placeholders.md) | disallow missing placeholders in rule report messages | βœ… ![badge-recommended-type-checked][] | | | | +| [no-property-in-node](docs/rules/no-property-in-node.md) | disallow using `in` to narrow node types instead of looking at properties | ![badge-recommended-type-checked][] | | | πŸ’­ | +| [no-unused-message-ids](docs/rules/no-unused-message-ids.md) | disallow unused `messageId`s in `meta.messages` | βœ… ![badge-recommended-type-checked][] | | | | +| [no-unused-placeholders](docs/rules/no-unused-placeholders.md) | disallow unused placeholders in rule report messages | βœ… ![badge-recommended-type-checked][] | | | | +| [no-useless-token-range](docs/rules/no-useless-token-range.md) | disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [prefer-message-ids](docs/rules/prefer-message-ids.md) | require using `messageId` instead of `message` or `desc` to report rule violations | βœ… ![badge-recommended-type-checked][] | | | | +| [prefer-object-rule](docs/rules/prefer-object-rule.md) | disallow function-style rules | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [prefer-placeholders](docs/rules/prefer-placeholders.md) | require using placeholders for dynamic report messages | | | | | +| [prefer-replace-text](docs/rules/prefer-replace-text.md) | require using `replaceText()` instead of `replaceTextRange()` | | | | | +| [report-message-format](docs/rules/report-message-format.md) | enforce a consistent format for rule report messages | | | | | +| [require-meta-docs-description](docs/rules/require-meta-docs-description.md) | require rules to implement a `meta.docs.description` property with the correct format | | | | | +| [require-meta-docs-url](docs/rules/require-meta-docs-url.md) | require rules to implement a `meta.docs.url` property | | πŸ”§ | | | +| [require-meta-fixable](docs/rules/require-meta-fixable.md) | require rules to implement a `meta.fixable` property | βœ… ![badge-recommended-type-checked][] | | | | +| [require-meta-has-suggestions](docs/rules/require-meta-has-suggestions.md) | require suggestable rules to implement a `meta.hasSuggestions` property | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [require-meta-schema](docs/rules/require-meta-schema.md) | require rules to implement a `meta.schema` property | βœ… ![badge-recommended-type-checked][] | | πŸ’‘ | | +| [require-meta-type](docs/rules/require-meta-type.md) | require rules to implement a `meta.type` property | βœ… ![badge-recommended-type-checked][] | | | | ### Tests -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | -| :----------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :- | :- | :- | -| [consistent-output](docs/rules/consistent-output.md) | enforce consistent use of `output` assertions in rule tests | βœ… | | | -| [no-identical-tests](docs/rules/no-identical-tests.md) | disallow identical tests | βœ… | πŸ”§ | | -| [no-only-tests](docs/rules/no-only-tests.md) | disallow the test case property `only` | βœ… | | πŸ’‘ | -| [prefer-output-null](docs/rules/prefer-output-null.md) | disallow invalid RuleTester test cases where the `output` matches the `code` | βœ… | πŸ”§ | | -| [test-case-property-ordering](docs/rules/test-case-property-ordering.md) | require the properties of a test case to be placed in a consistent order | | πŸ”§ | | -| [test-case-shorthand-strings](docs/rules/test-case-shorthand-strings.md) | enforce consistent usage of shorthand strings for test cases with no options | | πŸ”§ | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | +| :----------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :------------------------------------ | :- | :- | :- | +| [consistent-output](docs/rules/consistent-output.md) | enforce consistent use of `output` assertions in rule tests | βœ… ![badge-recommended-type-checked][] | | | | +| [no-identical-tests](docs/rules/no-identical-tests.md) | disallow identical tests | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [no-only-tests](docs/rules/no-only-tests.md) | disallow the test case property `only` | βœ… ![badge-recommended-type-checked][] | | πŸ’‘ | | +| [prefer-output-null](docs/rules/prefer-output-null.md) | disallow invalid RuleTester test cases where the `output` matches the `code` | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | +| [test-case-property-ordering](docs/rules/test-case-property-ordering.md) | require the properties of a test case to be placed in a consistent order | | πŸ”§ | | | +| [test-case-shorthand-strings](docs/rules/test-case-shorthand-strings.md) | enforce consistent usage of shorthand strings for test cases with no options | | πŸ”§ | | | ## Presets -| | Name | Description | -|:--|:--------------------|:--------------------------------------------------------------------------| -| βœ… | `recommended` | enables all recommended rules in this plugin | -| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | -| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | -| | `all` | enables all rules in this plugin | -| | `rules` | enables all rules that are aimed at linting ESLint rule files | -| | `tests` | enables all rules that are aimed at linting ESLint test files | +| | Name | Description | +| :-- | :------------------------- | :--------------------------------------------------------------------------------------- | +| βœ… | `recommended` | enables all recommended rules in this plugin, excluding those requiring type information | +| | `recommended-type-checked` | enables all recommended rules in this plugin, including those requiring type information | +| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | +| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | +| | `all` | enables all rules in this plugin | +| | `rules` | enables all rules that are aimed at linting ESLint rule files | +| | `tests` | enables all rules that are aimed at linting ESLint test files | ### Semantic versioning policy @@ -137,9 +159,7 @@ Presets are enabled by adding a line to the `extends` list in your config file. ```json { - "extends": [ - "plugin:eslint-plugin/recommended" - ] + "extends": ["plugin:eslint-plugin/recommended"] } ``` @@ -155,7 +175,7 @@ Or to apply linting only to the appropriate rule or test files: { "files": ["tests/lib/rules/*.{js,ts}"], "extends": ["plugin:eslint-plugin/tests-recommended"] - }, + } ] } ``` diff --git a/configs/recommended-type-checked.js b/configs/recommended-type-checked.js new file mode 100644 index 00000000..54daad0e --- /dev/null +++ b/configs/recommended-type-checked.js @@ -0,0 +1,8 @@ +'use strict'; + +const mod = require('../lib/index.js'); + +module.exports = { + plugins: { 'eslint-plugin': mod }, + rules: mod.configs['recommended-type-checked'].rules, +}; diff --git a/docs/rules/consistent-output.md b/docs/rules/consistent-output.md index aae5ca8d..a2564af8 100644 --- a/docs/rules/consistent-output.md +++ b/docs/rules/consistent-output.md @@ -1,6 +1,6 @@ # Enforce consistent use of `output` assertions in rule tests (`eslint-plugin/consistent-output`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/fixer-return.md b/docs/rules/fixer-return.md index 10978101..c46441e5 100644 --- a/docs/rules/fixer-return.md +++ b/docs/rules/fixer-return.md @@ -1,6 +1,6 @@ # Require fixer functions to return a fix (`eslint-plugin/fixer-return`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/no-deprecated-context-methods.md b/docs/rules/no-deprecated-context-methods.md index 16d999d6..3741c439 100644 --- a/docs/rules/no-deprecated-context-methods.md +++ b/docs/rules/no-deprecated-context-methods.md @@ -1,6 +1,6 @@ # Disallow usage of deprecated methods on rule context objects (`eslint-plugin/no-deprecated-context-methods`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-deprecated-report-api.md b/docs/rules/no-deprecated-report-api.md index 1bac7e94..2c280e29 100644 --- a/docs/rules/no-deprecated-report-api.md +++ b/docs/rules/no-deprecated-report-api.md @@ -1,6 +1,6 @@ # Disallow the version of `context.report()` with multiple arguments (`eslint-plugin/no-deprecated-report-api`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-identical-tests.md b/docs/rules/no-identical-tests.md index 62fe850a..d9a5ea64 100644 --- a/docs/rules/no-identical-tests.md +++ b/docs/rules/no-identical-tests.md @@ -1,6 +1,6 @@ # Disallow identical tests (`eslint-plugin/no-identical-tests`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-missing-message-ids.md b/docs/rules/no-missing-message-ids.md index 592d6d24..8d4f03fa 100644 --- a/docs/rules/no-missing-message-ids.md +++ b/docs/rules/no-missing-message-ids.md @@ -1,6 +1,6 @@ # Disallow `messageId`s that are missing from `meta.messages` (`eslint-plugin/no-missing-message-ids`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/no-missing-placeholders.md b/docs/rules/no-missing-placeholders.md index 6e2ac6ca..54e86d1b 100644 --- a/docs/rules/no-missing-placeholders.md +++ b/docs/rules/no-missing-placeholders.md @@ -1,6 +1,6 @@ # Disallow missing placeholders in rule report messages (`eslint-plugin/no-missing-placeholders`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/no-only-tests.md b/docs/rules/no-only-tests.md index d5db7dd0..80c8e0fc 100644 --- a/docs/rules/no-only-tests.md +++ b/docs/rules/no-only-tests.md @@ -1,6 +1,6 @@ # Disallow the test case property `only` (`eslint-plugin/no-only-tests`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/no-property-in-node.md b/docs/rules/no-property-in-node.md new file mode 100644 index 00000000..edd1b9cf --- /dev/null +++ b/docs/rules/no-property-in-node.md @@ -0,0 +1,38 @@ +# Disallow using `in` to narrow node types instead of looking at properties (`eslint-plugin/no-property-in-node`) + +πŸ’Ό This rule is enabled in the `recommended-type-checked` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). + +πŸ’­ This rule requires type information. + + + +When working with a node of type `ESTree.Node` or `TSESTree.Node`, it can be tempting to use the `'in'` operator to narrow the node's type. +`'in'` narrowing is susceptible to confusing behavior from quirks of ASTs, such as node properties sometimes being omitted from nodes and other times explicitly being set to `null` or `undefined`. + +Using direct property checks is generally considered preferable. + +## Rule Details + +Examples of **incorrect** code for this rule: + +```ts +/* eslint eslint-plugin/no-property-in-node: error */ + +declare const node: TSESTree.Parameter; + +if ('optional' in node) { + node.optional; +} +``` + +Examples of **correct** code for this rule: + +```ts +/* eslint eslint-plugin/no-property-in-node: error */ + +declare const node: TSESTree.Parameter; + +if (node.type !== TSESTree.AST_NODE_TYPES.TSParameterProperty) { + node.optional; +} +``` diff --git a/docs/rules/no-unused-message-ids.md b/docs/rules/no-unused-message-ids.md index c691d67c..e5efb3ef 100644 --- a/docs/rules/no-unused-message-ids.md +++ b/docs/rules/no-unused-message-ids.md @@ -1,6 +1,6 @@ # Disallow unused `messageId`s in `meta.messages` (`eslint-plugin/no-unused-message-ids`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/no-unused-placeholders.md b/docs/rules/no-unused-placeholders.md index fb2df80f..b5f21fd6 100644 --- a/docs/rules/no-unused-placeholders.md +++ b/docs/rules/no-unused-placeholders.md @@ -1,6 +1,6 @@ # Disallow unused placeholders in rule report messages (`eslint-plugin/no-unused-placeholders`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/no-useless-token-range.md b/docs/rules/no-useless-token-range.md index e1bfaade..2db5cd06 100644 --- a/docs/rules/no-useless-token-range.md +++ b/docs/rules/no-useless-token-range.md @@ -1,6 +1,6 @@ # Disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` (`eslint-plugin/no-useless-token-range`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-message-ids.md b/docs/rules/prefer-message-ids.md index 59a2d7ea..acbb6cdd 100644 --- a/docs/rules/prefer-message-ids.md +++ b/docs/rules/prefer-message-ids.md @@ -1,6 +1,6 @@ # Require using `messageId` instead of `message` or `desc` to report rule violations (`eslint-plugin/prefer-message-ids`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/prefer-object-rule.md b/docs/rules/prefer-object-rule.md index a3a1b66a..51434089 100644 --- a/docs/rules/prefer-object-rule.md +++ b/docs/rules/prefer-object-rule.md @@ -1,6 +1,6 @@ # Disallow function-style rules (`eslint-plugin/prefer-object-rule`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-output-null.md b/docs/rules/prefer-output-null.md index 61a33ae7..af89da91 100644 --- a/docs/rules/prefer-output-null.md +++ b/docs/rules/prefer-output-null.md @@ -1,6 +1,6 @@ # Disallow invalid RuleTester test cases where the `output` matches the `code` (`eslint-plugin/prefer-output-null`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-fixable.md b/docs/rules/require-meta-fixable.md index 81ccf77c..a2232b8f 100644 --- a/docs/rules/require-meta-fixable.md +++ b/docs/rules/require-meta-fixable.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.fixable` property (`eslint-plugin/require-meta-fixable`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/docs/rules/require-meta-has-suggestions.md b/docs/rules/require-meta-has-suggestions.md index c64846f8..77ad1dc2 100644 --- a/docs/rules/require-meta-has-suggestions.md +++ b/docs/rules/require-meta-has-suggestions.md @@ -1,6 +1,6 @@ # Require suggestable rules to implement a `meta.hasSuggestions` property (`eslint-plugin/require-meta-has-suggestions`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-schema.md b/docs/rules/require-meta-schema.md index 5b768087..bb093537 100644 --- a/docs/rules/require-meta-schema.md +++ b/docs/rules/require-meta-schema.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.schema` property (`eslint-plugin/require-meta-schema`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/require-meta-type.md b/docs/rules/require-meta-type.md index 672fcb0d..77d9958f 100644 --- a/docs/rules/require-meta-type.md +++ b/docs/rules/require-meta-type.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.type` property (`eslint-plugin/require-meta-type`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. diff --git a/lib/index.js b/lib/index.js index 4ff4ef76..fb246f0b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,7 +16,9 @@ const PLUGIN_NAME = packageMetadata.name.replace(/^eslint-plugin-/, ''); const configFilters = { all: () => true, - recommended: (rule) => rule.meta.docs.recommended, + recommended: (rule) => + rule.meta.docs.recommended && !rule.meta.docs.requiresTypeChecking, + 'recommended-type-checked': (rule) => rule.meta.docs.recommended, rules: (rule) => rule.meta.docs.category === 'Rules', tests: (rule) => rule.meta.docs.category === 'Tests', 'rules-recommended': (rule) => diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js new file mode 100644 index 00000000..572c7276 --- /dev/null +++ b/lib/rules/no-property-in-node.js @@ -0,0 +1,58 @@ +'use strict'; + +const { ESLintUtils } = require('@typescript-eslint/utils'); +const tsutils = require('ts-api-utils'); + +const typedNodeSourceFileTesters = [ + /@types[/\\]estree[/\\]index\.d\.ts/, + /@typescript-eslint[/\\]types[/\\]dist[/\\]generated[/\\]ast-spec\.d\.ts/, +]; + +/** + * @param {import('typescript').Type} type + * @returns Whether the type seems to include a known ESTree or TSESTree AST node. + */ +function isAstNodeType(type) { + return tsutils + .typeParts(type) + .flatMap((typePart) => typePart.symbol?.declarations ?? []) + .some((declaration) => { + const fileName = declaration.getSourceFile().fileName; + return ( + fileName && + typedNodeSourceFileTesters.some((tester) => tester.test(fileName)) + ); + }); +} + +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + meta: { + type: 'suggestion', + docs: { + description: + 'disallow using `in` to narrow node types instead of looking at properties', + category: 'Rules', + recommended: true, + requiresTypeChecking: true, + url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-property-in-node.md', + }, + schema: [], + messages: { + in: 'Prefer checking specific node properties instead of a broad `in`.', + }, + }, + + create(context) { + return { + 'BinaryExpression[operator=in]'(node) { + const services = ESLintUtils.getParserServices(context); + const type = services.getTypeAtLocation(node.right); + + if (isAstNodeType(type)) { + context.report({ messageId: 'in', node }); + } + }, + }; + }, +}; diff --git a/package.json b/package.json index 3b2f1b21..94d7747e 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,10 @@ }, "homepage": "https://github.com/eslint-community/eslint-plugin-eslint-plugin#readme", "dependencies": { + "@typescript-eslint/utils": "^6.20.0", "eslint-utils": "^3.0.0", - "estraverse": "^5.3.0" + "estraverse": "^5.3.0", + "ts-api-utils": "^1.2.0" }, "nyc": { "branches": 94, @@ -55,7 +57,10 @@ "@eslint/eslintrc": "^2.0.2", "@eslint/js": "^8.37.0", "@release-it/conventional-changelog": "^4.3.0", - "@typescript-eslint/parser": "^5.36.2", + "@types/eslint": "^8.56.2", + "@types/estree": "^1.0.5", + "@typescript-eslint/parser": "^6.20.0", + "@typescript-eslint/rule-tester": "^6.20.0", "chai": "^4.3.6", "dirty-chai": "^2.0.1", "eslint": "^8.23.0", diff --git a/tests/lib/rules/fixtures/file.ts b/tests/lib/rules/fixtures/file.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/lib/rules/fixtures/tsconfig.json b/tests/lib/rules/fixtures/tsconfig.json new file mode 100644 index 00000000..0d505be7 --- /dev/null +++ b/tests/lib/rules/fixtures/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "moduleResolution": "NodeNext" + } +} diff --git a/tests/lib/rules/no-property-in-node.js b/tests/lib/rules/no-property-in-node.js new file mode 100644 index 00000000..eec4a937 --- /dev/null +++ b/tests/lib/rules/no-property-in-node.js @@ -0,0 +1,111 @@ +'use strict'; + +const { RuleTester } = require('@typescript-eslint/rule-tester'); +const path = require('path'); +const rule = require('../../../lib/rules/no-property-in-node'); + +RuleTester.afterAll = after; + +const ruleTester = new RuleTester({ + parser: '@typescript-eslint/parser', + parserOptions: { + project: './tsconfig.json', + tsconfigRootDir: path.join(__dirname, 'fixtures'), + }, +}); + +ruleTester.run('no-property-in-node', rule, { + invalid: [ + { + code: ` + import { TSESTree } from '@typescript-eslint/utils'; + declare const node: TSESTree.Node; + 'a' in node; + `, + errors: [ + { + column: 9, + line: 4, + endColumn: 20, + endLine: 4, + messageId: 'in', + }, + ], + }, + { + code: ` + import { TSESTree } from '@typescript-eslint/utils'; + type Other = { key: true }; + declare const node: TSESTree.Node | Other; + 'a' in node; + `, + errors: [ + { + column: 9, + line: 5, + endColumn: 20, + endLine: 5, + messageId: 'in', + }, + ], + }, + { + code: ` + import * as ESTree from "estree"; + declare const node: ESTree.Node; + 'a' in node; + `, + errors: [ + { + column: 9, + line: 4, + endColumn: 20, + endLine: 4, + messageId: 'in', + }, + ], + }, + { + code: ` + import * as eslint from 'eslint'; + const listener: eslint.Rule.RuleListener = { + ArrayExpression(node) { + 'a' in node; + }, + }; + `, + errors: [ + { + column: 13, + line: 5, + endColumn: 24, + endLine: 5, + messageId: 'in', + }, + ], + }, + ], + valid: [ + `'a' in window;`, + ` + declare const node: Node; + 'a' in node; + `, + ` + type Node = { unrelated: true; }; + declare const node: Node; + 'a' in node; + `, + ` + interface Node { + unrelated: true; + }; + declare const node: Node; + 'a' in node; + `, + ` + declare const node: UnresolvedType; + 'a' in node; + `, + ], +}); From 8cfaf6d48f5aa3737e81d04d13c0101ab5273f1c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:19:29 -0500 Subject: [PATCH 02/21] =?UTF-8?q?Add=20=E2=98=91=EF=B8=8F=20emoji=20for=20?= =?UTF-8?q?recommended-type-checked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslint-doc-generatorrc.js | 1 + README.md | 63 +++++++++++---------- docs/rules/consistent-output.md | 2 +- docs/rules/fixer-return.md | 2 +- docs/rules/no-deprecated-context-methods.md | 2 +- docs/rules/no-deprecated-report-api.md | 2 +- docs/rules/no-identical-tests.md | 2 +- docs/rules/no-missing-message-ids.md | 2 +- docs/rules/no-missing-placeholders.md | 2 +- docs/rules/no-only-tests.md | 2 +- docs/rules/no-property-in-node.md | 2 +- docs/rules/no-unused-message-ids.md | 2 +- docs/rules/no-unused-placeholders.md | 2 +- docs/rules/no-useless-token-range.md | 2 +- docs/rules/prefer-message-ids.md | 2 +- docs/rules/prefer-object-rule.md | 2 +- docs/rules/prefer-output-null.md | 2 +- docs/rules/require-meta-fixable.md | 2 +- docs/rules/require-meta-has-suggestions.md | 2 +- docs/rules/require-meta-schema.md | 2 +- docs/rules/require-meta-type.md | 2 +- 21 files changed, 52 insertions(+), 50 deletions(-) diff --git a/.eslint-doc-generatorrc.js b/.eslint-doc-generatorrc.js index 5c4ef3b6..e4d736c5 100644 --- a/.eslint-doc-generatorrc.js +++ b/.eslint-doc-generatorrc.js @@ -2,6 +2,7 @@ /** @type {import('eslint-doc-generator').GenerateOptions} */ module.exports = { + configEmoji: [['recommended-type-checked', 'β˜‘οΈ']], ignoreConfig: [ 'all', 'rules', diff --git a/README.md b/README.md index 08063497..19307a80 100644 --- a/README.md +++ b/README.md @@ -94,46 +94,47 @@ module.exports = { πŸ’Ό [Configurations](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets) enabled in.\ βœ… Set in the `recommended` [configuration](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets).\ +β˜‘οΈ Set in the `recommended-type-checked` [configuration](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets).\ πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\ πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\ πŸ’­ Requires type information. ### Rules -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | -| :--------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :------------------------------------ | :- | :- | :- | -| [fixer-return](docs/rules/fixer-return.md) | require fixer functions to return a fix | βœ… ![badge-recommended-type-checked][] | | | | -| [meta-property-ordering](docs/rules/meta-property-ordering.md) | enforce the order of meta properties | | πŸ”§ | | | -| [no-deprecated-context-methods](docs/rules/no-deprecated-context-methods.md) | disallow usage of deprecated methods on rule context objects | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [no-deprecated-report-api](docs/rules/no-deprecated-report-api.md) | disallow the version of `context.report()` with multiple arguments | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [no-missing-message-ids](docs/rules/no-missing-message-ids.md) | disallow `messageId`s that are missing from `meta.messages` | βœ… ![badge-recommended-type-checked][] | | | | -| [no-missing-placeholders](docs/rules/no-missing-placeholders.md) | disallow missing placeholders in rule report messages | βœ… ![badge-recommended-type-checked][] | | | | -| [no-property-in-node](docs/rules/no-property-in-node.md) | disallow using `in` to narrow node types instead of looking at properties | ![badge-recommended-type-checked][] | | | πŸ’­ | -| [no-unused-message-ids](docs/rules/no-unused-message-ids.md) | disallow unused `messageId`s in `meta.messages` | βœ… ![badge-recommended-type-checked][] | | | | -| [no-unused-placeholders](docs/rules/no-unused-placeholders.md) | disallow unused placeholders in rule report messages | βœ… ![badge-recommended-type-checked][] | | | | -| [no-useless-token-range](docs/rules/no-useless-token-range.md) | disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [prefer-message-ids](docs/rules/prefer-message-ids.md) | require using `messageId` instead of `message` or `desc` to report rule violations | βœ… ![badge-recommended-type-checked][] | | | | -| [prefer-object-rule](docs/rules/prefer-object-rule.md) | disallow function-style rules | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [prefer-placeholders](docs/rules/prefer-placeholders.md) | require using placeholders for dynamic report messages | | | | | -| [prefer-replace-text](docs/rules/prefer-replace-text.md) | require using `replaceText()` instead of `replaceTextRange()` | | | | | -| [report-message-format](docs/rules/report-message-format.md) | enforce a consistent format for rule report messages | | | | | -| [require-meta-docs-description](docs/rules/require-meta-docs-description.md) | require rules to implement a `meta.docs.description` property with the correct format | | | | | -| [require-meta-docs-url](docs/rules/require-meta-docs-url.md) | require rules to implement a `meta.docs.url` property | | πŸ”§ | | | -| [require-meta-fixable](docs/rules/require-meta-fixable.md) | require rules to implement a `meta.fixable` property | βœ… ![badge-recommended-type-checked][] | | | | -| [require-meta-has-suggestions](docs/rules/require-meta-has-suggestions.md) | require suggestable rules to implement a `meta.hasSuggestions` property | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [require-meta-schema](docs/rules/require-meta-schema.md) | require rules to implement a `meta.schema` property | βœ… ![badge-recommended-type-checked][] | | πŸ’‘ | | -| [require-meta-type](docs/rules/require-meta-type.md) | require rules to implement a `meta.type` property | βœ… ![badge-recommended-type-checked][] | | | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | +| :--------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :--- | :- | :- | :- | +| [fixer-return](docs/rules/fixer-return.md) | require fixer functions to return a fix | βœ… β˜‘οΈ | | | | +| [meta-property-ordering](docs/rules/meta-property-ordering.md) | enforce the order of meta properties | | πŸ”§ | | | +| [no-deprecated-context-methods](docs/rules/no-deprecated-context-methods.md) | disallow usage of deprecated methods on rule context objects | βœ… β˜‘οΈ | πŸ”§ | | | +| [no-deprecated-report-api](docs/rules/no-deprecated-report-api.md) | disallow the version of `context.report()` with multiple arguments | βœ… β˜‘οΈ | πŸ”§ | | | +| [no-missing-message-ids](docs/rules/no-missing-message-ids.md) | disallow `messageId`s that are missing from `meta.messages` | βœ… β˜‘οΈ | | | | +| [no-missing-placeholders](docs/rules/no-missing-placeholders.md) | disallow missing placeholders in rule report messages | βœ… β˜‘οΈ | | | | +| [no-property-in-node](docs/rules/no-property-in-node.md) | disallow using `in` to narrow node types instead of looking at properties | β˜‘οΈ | | | πŸ’­ | +| [no-unused-message-ids](docs/rules/no-unused-message-ids.md) | disallow unused `messageId`s in `meta.messages` | βœ… β˜‘οΈ | | | | +| [no-unused-placeholders](docs/rules/no-unused-placeholders.md) | disallow unused placeholders in rule report messages | βœ… β˜‘οΈ | | | | +| [no-useless-token-range](docs/rules/no-useless-token-range.md) | disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` | βœ… β˜‘οΈ | πŸ”§ | | | +| [prefer-message-ids](docs/rules/prefer-message-ids.md) | require using `messageId` instead of `message` or `desc` to report rule violations | βœ… β˜‘οΈ | | | | +| [prefer-object-rule](docs/rules/prefer-object-rule.md) | disallow function-style rules | βœ… β˜‘οΈ | πŸ”§ | | | +| [prefer-placeholders](docs/rules/prefer-placeholders.md) | require using placeholders for dynamic report messages | | | | | +| [prefer-replace-text](docs/rules/prefer-replace-text.md) | require using `replaceText()` instead of `replaceTextRange()` | | | | | +| [report-message-format](docs/rules/report-message-format.md) | enforce a consistent format for rule report messages | | | | | +| [require-meta-docs-description](docs/rules/require-meta-docs-description.md) | require rules to implement a `meta.docs.description` property with the correct format | | | | | +| [require-meta-docs-url](docs/rules/require-meta-docs-url.md) | require rules to implement a `meta.docs.url` property | | πŸ”§ | | | +| [require-meta-fixable](docs/rules/require-meta-fixable.md) | require rules to implement a `meta.fixable` property | βœ… β˜‘οΈ | | | | +| [require-meta-has-suggestions](docs/rules/require-meta-has-suggestions.md) | require suggestable rules to implement a `meta.hasSuggestions` property | βœ… β˜‘οΈ | πŸ”§ | | | +| [require-meta-schema](docs/rules/require-meta-schema.md) | require rules to implement a `meta.schema` property | βœ… β˜‘οΈ | | πŸ’‘ | | +| [require-meta-type](docs/rules/require-meta-type.md) | require rules to implement a `meta.type` property | βœ… β˜‘οΈ | | | | ### Tests -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | -| :----------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :------------------------------------ | :- | :- | :- | -| [consistent-output](docs/rules/consistent-output.md) | enforce consistent use of `output` assertions in rule tests | βœ… ![badge-recommended-type-checked][] | | | | -| [no-identical-tests](docs/rules/no-identical-tests.md) | disallow identical tests | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [no-only-tests](docs/rules/no-only-tests.md) | disallow the test case property `only` | βœ… ![badge-recommended-type-checked][] | | πŸ’‘ | | -| [prefer-output-null](docs/rules/prefer-output-null.md) | disallow invalid RuleTester test cases where the `output` matches the `code` | βœ… ![badge-recommended-type-checked][] | πŸ”§ | | | -| [test-case-property-ordering](docs/rules/test-case-property-ordering.md) | require the properties of a test case to be placed in a consistent order | | πŸ”§ | | | -| [test-case-shorthand-strings](docs/rules/test-case-shorthand-strings.md) | enforce consistent usage of shorthand strings for test cases with no options | | πŸ”§ | | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | πŸ’­ | +| :----------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :--- | :- | :- | :- | +| [consistent-output](docs/rules/consistent-output.md) | enforce consistent use of `output` assertions in rule tests | βœ… β˜‘οΈ | | | | +| [no-identical-tests](docs/rules/no-identical-tests.md) | disallow identical tests | βœ… β˜‘οΈ | πŸ”§ | | | +| [no-only-tests](docs/rules/no-only-tests.md) | disallow the test case property `only` | βœ… β˜‘οΈ | | πŸ’‘ | | +| [prefer-output-null](docs/rules/prefer-output-null.md) | disallow invalid RuleTester test cases where the `output` matches the `code` | βœ… β˜‘οΈ | πŸ”§ | | | +| [test-case-property-ordering](docs/rules/test-case-property-ordering.md) | require the properties of a test case to be placed in a consistent order | | πŸ”§ | | | +| [test-case-shorthand-strings](docs/rules/test-case-shorthand-strings.md) | enforce consistent usage of shorthand strings for test cases with no options | | πŸ”§ | | | diff --git a/docs/rules/consistent-output.md b/docs/rules/consistent-output.md index a2564af8..7022219e 100644 --- a/docs/rules/consistent-output.md +++ b/docs/rules/consistent-output.md @@ -1,6 +1,6 @@ # Enforce consistent use of `output` assertions in rule tests (`eslint-plugin/consistent-output`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/fixer-return.md b/docs/rules/fixer-return.md index c46441e5..9aa165a1 100644 --- a/docs/rules/fixer-return.md +++ b/docs/rules/fixer-return.md @@ -1,6 +1,6 @@ # Require fixer functions to return a fix (`eslint-plugin/fixer-return`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/no-deprecated-context-methods.md b/docs/rules/no-deprecated-context-methods.md index 3741c439..0572f276 100644 --- a/docs/rules/no-deprecated-context-methods.md +++ b/docs/rules/no-deprecated-context-methods.md @@ -1,6 +1,6 @@ # Disallow usage of deprecated methods on rule context objects (`eslint-plugin/no-deprecated-context-methods`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-deprecated-report-api.md b/docs/rules/no-deprecated-report-api.md index 2c280e29..9475926f 100644 --- a/docs/rules/no-deprecated-report-api.md +++ b/docs/rules/no-deprecated-report-api.md @@ -1,6 +1,6 @@ # Disallow the version of `context.report()` with multiple arguments (`eslint-plugin/no-deprecated-report-api`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-identical-tests.md b/docs/rules/no-identical-tests.md index d9a5ea64..dcf8bc49 100644 --- a/docs/rules/no-identical-tests.md +++ b/docs/rules/no-identical-tests.md @@ -1,6 +1,6 @@ # Disallow identical tests (`eslint-plugin/no-identical-tests`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-missing-message-ids.md b/docs/rules/no-missing-message-ids.md index 8d4f03fa..a1434757 100644 --- a/docs/rules/no-missing-message-ids.md +++ b/docs/rules/no-missing-message-ids.md @@ -1,6 +1,6 @@ # Disallow `messageId`s that are missing from `meta.messages` (`eslint-plugin/no-missing-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/no-missing-placeholders.md b/docs/rules/no-missing-placeholders.md index 54e86d1b..6a52dd90 100644 --- a/docs/rules/no-missing-placeholders.md +++ b/docs/rules/no-missing-placeholders.md @@ -1,6 +1,6 @@ # Disallow missing placeholders in rule report messages (`eslint-plugin/no-missing-placeholders`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/no-only-tests.md b/docs/rules/no-only-tests.md index 80c8e0fc..704dd5c3 100644 --- a/docs/rules/no-only-tests.md +++ b/docs/rules/no-only-tests.md @@ -1,6 +1,6 @@ # Disallow the test case property `only` (`eslint-plugin/no-only-tests`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/no-property-in-node.md b/docs/rules/no-property-in-node.md index edd1b9cf..bcc2d512 100644 --- a/docs/rules/no-property-in-node.md +++ b/docs/rules/no-property-in-node.md @@ -1,6 +1,6 @@ # Disallow using `in` to narrow node types instead of looking at properties (`eslint-plugin/no-property-in-node`) -πŸ’Ό This rule is enabled in the `recommended-type-checked` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). +πŸ’Ό This rule is enabled in the β˜‘οΈ `recommended-type-checked` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ’­ This rule requires type information. diff --git a/docs/rules/no-unused-message-ids.md b/docs/rules/no-unused-message-ids.md index e5efb3ef..ebbcb43f 100644 --- a/docs/rules/no-unused-message-ids.md +++ b/docs/rules/no-unused-message-ids.md @@ -1,6 +1,6 @@ # Disallow unused `messageId`s in `meta.messages` (`eslint-plugin/no-unused-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/no-unused-placeholders.md b/docs/rules/no-unused-placeholders.md index b5f21fd6..d8cd0ae7 100644 --- a/docs/rules/no-unused-placeholders.md +++ b/docs/rules/no-unused-placeholders.md @@ -1,6 +1,6 @@ # Disallow unused placeholders in rule report messages (`eslint-plugin/no-unused-placeholders`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/no-useless-token-range.md b/docs/rules/no-useless-token-range.md index 2db5cd06..dcb70f7f 100644 --- a/docs/rules/no-useless-token-range.md +++ b/docs/rules/no-useless-token-range.md @@ -1,6 +1,6 @@ # Disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` (`eslint-plugin/no-useless-token-range`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-message-ids.md b/docs/rules/prefer-message-ids.md index acbb6cdd..87453748 100644 --- a/docs/rules/prefer-message-ids.md +++ b/docs/rules/prefer-message-ids.md @@ -1,6 +1,6 @@ # Require using `messageId` instead of `message` or `desc` to report rule violations (`eslint-plugin/prefer-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/prefer-object-rule.md b/docs/rules/prefer-object-rule.md index 51434089..313bdbec 100644 --- a/docs/rules/prefer-object-rule.md +++ b/docs/rules/prefer-object-rule.md @@ -1,6 +1,6 @@ # Disallow function-style rules (`eslint-plugin/prefer-object-rule`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-output-null.md b/docs/rules/prefer-output-null.md index af89da91..d55086ac 100644 --- a/docs/rules/prefer-output-null.md +++ b/docs/rules/prefer-output-null.md @@ -1,6 +1,6 @@ # Disallow invalid RuleTester test cases where the `output` matches the `code` (`eslint-plugin/prefer-output-null`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-fixable.md b/docs/rules/require-meta-fixable.md index a2232b8f..ed049cef 100644 --- a/docs/rules/require-meta-fixable.md +++ b/docs/rules/require-meta-fixable.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.fixable` property (`eslint-plugin/require-meta-fixable`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. diff --git a/docs/rules/require-meta-has-suggestions.md b/docs/rules/require-meta-has-suggestions.md index 77ad1dc2..86480e35 100644 --- a/docs/rules/require-meta-has-suggestions.md +++ b/docs/rules/require-meta-has-suggestions.md @@ -1,6 +1,6 @@ # Require suggestable rules to implement a `meta.hasSuggestions` property (`eslint-plugin/require-meta-has-suggestions`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-schema.md b/docs/rules/require-meta-schema.md index bb093537..9185ffd3 100644 --- a/docs/rules/require-meta-schema.md +++ b/docs/rules/require-meta-schema.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.schema` property (`eslint-plugin/require-meta-schema`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/require-meta-type.md b/docs/rules/require-meta-type.md index 77d9958f..41aac645 100644 --- a/docs/rules/require-meta-type.md +++ b/docs/rules/require-meta-type.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.type` property (`eslint-plugin/require-meta-type`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, `recommended-type-checked`. +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. From 4d163e1bc560a19d410eef08b6e7655513074814 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:19:54 -0500 Subject: [PATCH 03/21] tests: valid before invalid --- tests/lib/rules/no-property-in-node.js | 46 +++++++++++++------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/lib/rules/no-property-in-node.js b/tests/lib/rules/no-property-in-node.js index eec4a937..57cf2fc3 100644 --- a/tests/lib/rules/no-property-in-node.js +++ b/tests/lib/rules/no-property-in-node.js @@ -15,6 +15,29 @@ const ruleTester = new RuleTester({ }); ruleTester.run('no-property-in-node', rule, { + valid: [ + `'a' in window;`, + ` + declare const node: Node; + 'a' in node; + `, + ` + type Node = { unrelated: true; }; + declare const node: Node; + 'a' in node; + `, + ` + interface Node { + unrelated: true; + }; + declare const node: Node; + 'a' in node; + `, + ` + declare const node: UnresolvedType; + 'a' in node; + `, + ], invalid: [ { code: ` @@ -85,27 +108,4 @@ ruleTester.run('no-property-in-node', rule, { ], }, ], - valid: [ - `'a' in window;`, - ` - declare const node: Node; - 'a' in node; - `, - ` - type Node = { unrelated: true; }; - declare const node: Node; - 'a' in node; - `, - ` - interface Node { - unrelated: true; - }; - declare const node: Node; - 'a' in node; - `, - ` - declare const node: UnresolvedType; - 'a' in node; - `, - ], }); From d62539dd19806f69220a88fcc47e69278132da10 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:28:20 -0500 Subject: [PATCH 04/21] Also check for whether the node has a 'type' --- lib/rules/no-property-in-node.js | 1 + tests/lib/rules/no-property-in-node.js | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js index 572c7276..933b89c4 100644 --- a/lib/rules/no-property-in-node.js +++ b/lib/rules/no-property-in-node.js @@ -15,6 +15,7 @@ const typedNodeSourceFileTesters = [ function isAstNodeType(type) { return tsutils .typeParts(type) + .filter((type) => type.getProperty('type')) .flatMap((typePart) => typePart.symbol?.declarations ?? []) .some((declaration) => { const fileName = declaration.getSourceFile().fileName; diff --git a/tests/lib/rules/no-property-in-node.js b/tests/lib/rules/no-property-in-node.js index 57cf2fc3..40f5f9ec 100644 --- a/tests/lib/rules/no-property-in-node.js +++ b/tests/lib/rules/no-property-in-node.js @@ -37,6 +37,11 @@ ruleTester.run('no-property-in-node', rule, { declare const node: UnresolvedType; 'a' in node; `, + ` + import * as ESTree from "estree"; + declare const loc: ESTree.SourceLocation; + 'a' in loc; + `, ], invalid: [ { From 357564489ab96de3721e9fd96ca8b67b06b65c44 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:28:50 -0500 Subject: [PATCH 05/21] Added docs and example to isAstNodeType --- lib/rules/no-property-in-node.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js index 933b89c4..7d3010ee 100644 --- a/lib/rules/no-property-in-node.js +++ b/lib/rules/no-property-in-node.js @@ -9,6 +9,25 @@ const typedNodeSourceFileTesters = [ ]; /** + * Given a TypeScript type, determines whether the type appears to be for a known + * AST type from the typings of @typescript-eslint/types or estree. + * We check based on two rough conditions: + * - The type has a 'kind' property (as AST node types all do) + * - The type is declared in one of those package's .d.ts types + * + * @example + * ``` + * module.exports = { + * create(context) { + * BinaryExpression(node) { + * const type = services.getTypeAtLocation(node.right); + * // ^^^^ + * // This variable's type will be TSESTree.BinaryExpression + * } + * } + * } + * ``` + * * @param {import('typescript').Type} type * @returns Whether the type seems to include a known ESTree or TSESTree AST node. */ From e42c07531dbf659accf790a7e907601b2de5c924 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:47:47 -0500 Subject: [PATCH 06/21] Expanded rule details --- docs/rules/no-property-in-node.md | 38 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/docs/rules/no-property-in-node.md b/docs/rules/no-property-in-node.md index bcc2d512..f3740bf7 100644 --- a/docs/rules/no-property-in-node.md +++ b/docs/rules/no-property-in-node.md @@ -9,7 +9,7 @@ When working with a node of type `ESTree.Node` or `TSESTree.Node`, it can be tempting to use the `'in'` operator to narrow the node's type. `'in'` narrowing is susceptible to confusing behavior from quirks of ASTs, such as node properties sometimes being omitted from nodes and other times explicitly being set to `null` or `undefined`. -Using direct property checks is generally considered preferable. +Instead, checking a node's `type` property is generally considered preferable. ## Rule Details @@ -18,11 +18,19 @@ Examples of **incorrect** code for this rule: ```ts /* eslint eslint-plugin/no-property-in-node: error */ -declare const node: TSESTree.Parameter; - -if ('optional' in node) { - node.optional; -} +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + meta: { /* ... */ }, + create(context) { + return { + 'ClassDeclaration, FunctionDeclaration'(node) { + if ('superClass' in node) { + console.log("This is a class declaration:", node); + } + }, + }; + }, +}; ``` Examples of **correct** code for this rule: @@ -30,9 +38,17 @@ Examples of **correct** code for this rule: ```ts /* eslint eslint-plugin/no-property-in-node: error */ -declare const node: TSESTree.Parameter; - -if (node.type !== TSESTree.AST_NODE_TYPES.TSParameterProperty) { - node.optional; -} +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + meta: { /* ... */ }, + create(context) { + return { + 'ClassDeclaration, FunctionDeclaration'(node) { + if (node.type === 'ClassDeclaration') { + console.log("This is a class declaration:", node); + } + }, + }; + }, +}; ``` From 2a94dcb78877513c2e736c54fb8d140790b3e703 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:51:34 -0500 Subject: [PATCH 07/21] Add more valid test cases --- tests/lib/rules/no-property-in-node.js | 57 ++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/tests/lib/rules/no-property-in-node.js b/tests/lib/rules/no-property-in-node.js index 40f5f9ec..e48e8702 100644 --- a/tests/lib/rules/no-property-in-node.js +++ b/tests/lib/rules/no-property-in-node.js @@ -38,10 +38,61 @@ ruleTester.run('no-property-in-node', rule, { 'a' in node; `, ` - import * as ESTree from "estree"; + import * as ESTree from 'estree'; declare const loc: ESTree.SourceLocation; 'a' in loc; `, + ` + import * as ESTree from 'estree'; + declare const node: ESTree.Node; + a.superClass; + `, + ` + import * as ESTree from 'estree'; + declare const node: ESTree.Node; + a.type; + `, + ` + import * as ESTree from 'estree'; + declare const node: ESTree.Node; + a.type === 'ClassDeclaration'; + `, + ` + import * as ESTree from 'estree'; + declare const node: ESTree.ClassDeclaration | ESTree.FunctionDeclaration; + a.type === 'ClassDeclaration'; + `, + ` + import { TSESTree } from '@typescript-eslint/utils'; + declare const node: TSESTree.Node; + node.superClass; + `, + ` + import { TSESTree } from '@typescript-eslint/utils'; + declare const node: TSESTree.Node; + node.type; + `, + ` + import { TSESTree } from '@typescript-eslint/utils'; + declare const node: TSESTree.ClassDeclaration | TSESTree.FunctionDeclaration; + node.type === 'ClassDeclaration'; + `, + ` + import * as eslint from 'eslint'; + const listener: eslint.Rule.RuleListener = { + ClassDeclaration(node) { + node.type; + }, + }; + `, + ` + import * as eslint from 'eslint'; + const listener: eslint.Rule.RuleListener = { + 'ClassDeclaration, FunctionDeclaration'(node) { + node.type === 'ClassDeclaration'; + }, + }; + `, ], invalid: [ { @@ -79,7 +130,7 @@ ruleTester.run('no-property-in-node', rule, { }, { code: ` - import * as ESTree from "estree"; + import * as ESTree from 'estree'; declare const node: ESTree.Node; 'a' in node; `, @@ -97,7 +148,7 @@ ruleTester.run('no-property-in-node', rule, { code: ` import * as eslint from 'eslint'; const listener: eslint.Rule.RuleListener = { - ArrayExpression(node) { + ClassDeclaration(node) { 'a' in node; }, }; From 755cc08947d50b064ea475ef97b90f8b4958db5e Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:55:27 -0500 Subject: [PATCH 08/21] Fixed test path to fixtures --- tests/lib/{rules => }/fixtures/file.ts | 0 tests/lib/{rules => }/fixtures/tsconfig.json | 0 tests/lib/rules/no-property-in-node.js | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename tests/lib/{rules => }/fixtures/file.ts (100%) rename tests/lib/{rules => }/fixtures/tsconfig.json (100%) diff --git a/tests/lib/rules/fixtures/file.ts b/tests/lib/fixtures/file.ts similarity index 100% rename from tests/lib/rules/fixtures/file.ts rename to tests/lib/fixtures/file.ts diff --git a/tests/lib/rules/fixtures/tsconfig.json b/tests/lib/fixtures/tsconfig.json similarity index 100% rename from tests/lib/rules/fixtures/tsconfig.json rename to tests/lib/fixtures/tsconfig.json diff --git a/tests/lib/rules/no-property-in-node.js b/tests/lib/rules/no-property-in-node.js index e48e8702..02458078 100644 --- a/tests/lib/rules/no-property-in-node.js +++ b/tests/lib/rules/no-property-in-node.js @@ -10,7 +10,7 @@ const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', parserOptions: { project: './tsconfig.json', - tsconfigRootDir: path.join(__dirname, 'fixtures'), + tsconfigRootDir: path.join(__dirname, '../fixtures'), }, }); From d76b08aedddecafa7e0bf91e8b77e268441b28bb Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 13:59:05 -0500 Subject: [PATCH 09/21] Use parserOptions.project: true for eslint-remote-tester on TS files --- eslint-remote-tester.config.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eslint-remote-tester.config.js b/eslint-remote-tester.config.js index f9d44f69..06a14ca9 100644 --- a/eslint-remote-tester.config.js +++ b/eslint-remote-tester.config.js @@ -49,6 +49,9 @@ module.exports = { { files: ['*.ts', '*.mts', '*.cts'], parser: '@typescript-eslint/parser', + parserOptions: { + project: true, + }, }, ], }, From 5913127153abdf827af93d894cb143e5ead08b64 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 14:03:09 -0500 Subject: [PATCH 10/21] nit: avoid shadowing name for typePart --- lib/rules/no-property-in-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js index 7d3010ee..ce9a2da1 100644 --- a/lib/rules/no-property-in-node.js +++ b/lib/rules/no-property-in-node.js @@ -34,7 +34,7 @@ const typedNodeSourceFileTesters = [ function isAstNodeType(type) { return tsutils .typeParts(type) - .filter((type) => type.getProperty('type')) + .filter((typePart) => typePart.getProperty('type')) .flatMap((typePart) => typePart.symbol?.declarations ?? []) .some((declaration) => { const fileName = declaration.getSourceFile().fileName; From d45695d3d3dcaf01540c0049fb65eeb4cef8939f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2024 14:09:44 -0500 Subject: [PATCH 11/21] --- README.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 92ba1368..a9949a38 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,20 @@ -# eslint-plugin-eslint-plugin ![CI](https://github.com/eslint-community/eslint-plugin-eslint-plugin/workflows/CI/badge.svg) [![NPM version](https://img.shields.io/npm/v/eslint-plugin-eslint-plugin.svg?style=flat)](https://npmjs.org/package/eslint-plugin-eslint-plugin) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) +# eslint-plugin-eslint-plugin ![CI](https://github.com/eslint-community/eslint-plugin-eslint-plugin/workflows/CI/badge.svg) [![NPM version](https://img.shields.io/npm/v/eslint-plugin-eslint-plugin.svg?style=flat)](https://npmjs.org/package/eslint-plugin-eslint-plugin) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) An ESLint plugin for linting ESLint plugins. Rules written in CJS, ESM, and TypeScript are all supported. -- [eslint-plugin-eslint-plugin ](#eslint-plugin-eslint-plugin---) - - [Installation](#installation) - - [Usage](#usage) - - [**.eslintrc.json**](#eslintrcjson) - - [`eslint.config.js` (requires eslint\>=v8.23.0)](#eslintconfigjs-requires-eslintv8230) - - [Type Checked Rules](#type-checked-rules) - - [Rules](#rules) - - [Rules](#rules-1) - - [Tests](#tests) - - [Presets](#presets) - - [Semantic versioning policy](#semantic-versioning-policy) - - [Preset usage](#preset-usage) +- [Installation](#installation) +- [Usage](#usage) + - [**.eslintrc.json**](#eslintrcjson) + - [`eslint.config.js` (requires eslint\>=v8.23.0)](#eslintconfigjs-requires-eslintv8230) + - [Type Checked Rules](#type-checked-rules) +- [Rules](#rules) + - [Rules](#rules-1) + - [Tests](#tests) +- [Presets](#presets) + - [Semantic versioning policy](#semantic-versioning-policy) + - [Preset usage](#preset-usage) diff --git a/configs/recommended-type-checked.js b/configs/recommended-type-checked.js deleted file mode 100644 index 54daad0e..00000000 --- a/configs/recommended-type-checked.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -const mod = require('../lib/index.js'); - -module.exports = { - plugins: { 'eslint-plugin': mod }, - rules: mod.configs['recommended-type-checked'].rules, -}; diff --git a/docs/rules/consistent-output.md b/docs/rules/consistent-output.md index 8cdd8cba..c5d69f32 100644 --- a/docs/rules/consistent-output.md +++ b/docs/rules/consistent-output.md @@ -1,6 +1,6 @@ # Enforce consistent use of `output` assertions in rule tests (`eslint-plugin/consistent-output`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/fixer-return.md b/docs/rules/fixer-return.md index 9aa165a1..10978101 100644 --- a/docs/rules/fixer-return.md +++ b/docs/rules/fixer-return.md @@ -1,6 +1,6 @@ # Require fixer functions to return a fix (`eslint-plugin/fixer-return`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/no-deprecated-context-methods.md b/docs/rules/no-deprecated-context-methods.md index b011523c..50795425 100644 --- a/docs/rules/no-deprecated-context-methods.md +++ b/docs/rules/no-deprecated-context-methods.md @@ -1,6 +1,6 @@ # Disallow usage of deprecated methods on rule context objects (`eslint-plugin/no-deprecated-context-methods`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-deprecated-report-api.md b/docs/rules/no-deprecated-report-api.md index cb1c9989..407faf61 100644 --- a/docs/rules/no-deprecated-report-api.md +++ b/docs/rules/no-deprecated-report-api.md @@ -1,6 +1,6 @@ # Disallow the version of `context.report()` with multiple arguments (`eslint-plugin/no-deprecated-report-api`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-identical-tests.md b/docs/rules/no-identical-tests.md index dcf8bc49..62fe850a 100644 --- a/docs/rules/no-identical-tests.md +++ b/docs/rules/no-identical-tests.md @@ -1,6 +1,6 @@ # Disallow identical tests (`eslint-plugin/no-identical-tests`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/no-missing-message-ids.md b/docs/rules/no-missing-message-ids.md index e4f4cfba..383ecf1b 100644 --- a/docs/rules/no-missing-message-ids.md +++ b/docs/rules/no-missing-message-ids.md @@ -1,6 +1,6 @@ # Disallow `messageId`s that are missing from `meta.messages` (`eslint-plugin/no-missing-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/no-missing-placeholders.md b/docs/rules/no-missing-placeholders.md index c584908d..acead6a3 100644 --- a/docs/rules/no-missing-placeholders.md +++ b/docs/rules/no-missing-placeholders.md @@ -1,6 +1,6 @@ # Disallow missing placeholders in rule report messages (`eslint-plugin/no-missing-placeholders`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/no-only-tests.md b/docs/rules/no-only-tests.md index 704dd5c3..d5db7dd0 100644 --- a/docs/rules/no-only-tests.md +++ b/docs/rules/no-only-tests.md @@ -1,6 +1,6 @@ # Disallow the test case property `only` (`eslint-plugin/no-only-tests`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/no-property-in-node.md b/docs/rules/no-property-in-node.md index 7fa12b55..22da26dd 100644 --- a/docs/rules/no-property-in-node.md +++ b/docs/rules/no-property-in-node.md @@ -1,7 +1,5 @@ # Disallow using `in` to narrow node types instead of looking at properties (`eslint-plugin/no-property-in-node`) -πŸ’Ό This rule is enabled in the β˜‘οΈ `recommended-type-checked` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). - πŸ’­ This rule requires type information. diff --git a/docs/rules/no-unused-message-ids.md b/docs/rules/no-unused-message-ids.md index 3f68b73f..99660b00 100644 --- a/docs/rules/no-unused-message-ids.md +++ b/docs/rules/no-unused-message-ids.md @@ -1,6 +1,6 @@ # Disallow unused `messageId`s in `meta.messages` (`eslint-plugin/no-unused-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/no-unused-placeholders.md b/docs/rules/no-unused-placeholders.md index 487fc784..0f5454e1 100644 --- a/docs/rules/no-unused-placeholders.md +++ b/docs/rules/no-unused-placeholders.md @@ -1,6 +1,6 @@ # Disallow unused placeholders in rule report messages (`eslint-plugin/no-unused-placeholders`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/no-useless-token-range.md b/docs/rules/no-useless-token-range.md index aa079236..cb935d72 100644 --- a/docs/rules/no-useless-token-range.md +++ b/docs/rules/no-useless-token-range.md @@ -1,6 +1,6 @@ # Disallow unnecessary calls to `sourceCode.getFirstToken()` and `sourceCode.getLastToken()` (`eslint-plugin/no-useless-token-range`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-message-ids.md b/docs/rules/prefer-message-ids.md index 1ab40f29..45ea9d86 100644 --- a/docs/rules/prefer-message-ids.md +++ b/docs/rules/prefer-message-ids.md @@ -1,6 +1,6 @@ # Require using `messageId` instead of `message` or `desc` to report rule violations (`eslint-plugin/prefer-message-ids`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/prefer-object-rule.md b/docs/rules/prefer-object-rule.md index 5dbb6a99..fa3df403 100644 --- a/docs/rules/prefer-object-rule.md +++ b/docs/rules/prefer-object-rule.md @@ -1,6 +1,6 @@ # Disallow function-style rules (`eslint-plugin/prefer-object-rule`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/prefer-output-null.md b/docs/rules/prefer-output-null.md index d55086ac..61a33ae7 100644 --- a/docs/rules/prefer-output-null.md +++ b/docs/rules/prefer-output-null.md @@ -1,6 +1,6 @@ # Disallow invalid RuleTester test cases where the `output` matches the `code` (`eslint-plugin/prefer-output-null`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-fixable.md b/docs/rules/require-meta-fixable.md index 129a2fde..e5f52ee9 100644 --- a/docs/rules/require-meta-fixable.md +++ b/docs/rules/require-meta-fixable.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.fixable` property (`eslint-plugin/require-meta-fixable`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/docs/rules/require-meta-has-suggestions.md b/docs/rules/require-meta-has-suggestions.md index 3016446a..89c59e5a 100644 --- a/docs/rules/require-meta-has-suggestions.md +++ b/docs/rules/require-meta-has-suggestions.md @@ -1,6 +1,6 @@ # Require suggestable rules to implement a `meta.hasSuggestions` property (`eslint-plugin/require-meta-has-suggestions`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/docs/rules/require-meta-schema.md b/docs/rules/require-meta-schema.md index 7d2a2413..de1b6825 100644 --- a/docs/rules/require-meta-schema.md +++ b/docs/rules/require-meta-schema.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.schema` property (`eslint-plugin/require-meta-schema`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). πŸ’‘ This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). diff --git a/docs/rules/require-meta-type.md b/docs/rules/require-meta-type.md index 6726a49d..92f18265 100644 --- a/docs/rules/require-meta-type.md +++ b/docs/rules/require-meta-type.md @@ -1,6 +1,6 @@ # Require rules to implement a `meta.type` property (`eslint-plugin/require-meta-type`) -πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets): βœ… `recommended`, β˜‘οΈ `recommended-type-checked`. +πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-eslint-plugin#presets). diff --git a/lib/index.js b/lib/index.js index fb246f0b..4ff4ef76 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,9 +16,7 @@ const PLUGIN_NAME = packageMetadata.name.replace(/^eslint-plugin-/, ''); const configFilters = { all: () => true, - recommended: (rule) => - rule.meta.docs.recommended && !rule.meta.docs.requiresTypeChecking, - 'recommended-type-checked': (rule) => rule.meta.docs.recommended, + recommended: (rule) => rule.meta.docs.recommended, rules: (rule) => rule.meta.docs.category === 'Rules', tests: (rule) => rule.meta.docs.category === 'Tests', 'rules-recommended': (rule) => diff --git a/lib/rules/no-property-in-node.js b/lib/rules/no-property-in-node.js index e6393701..f3e2e0c8 100644 --- a/lib/rules/no-property-in-node.js +++ b/lib/rules/no-property-in-node.js @@ -51,7 +51,7 @@ module.exports = { description: 'disallow using `in` to narrow node types instead of looking at properties', category: 'Rules', - recommended: true, + recommended: false, requiresTypeChecking: true, url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-property-in-node.md', }, From de370b0a14a661917588e4135922d60b9b2d3f53 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Feb 2024 08:46:13 -0500 Subject: [PATCH 17/21] Removed README.md section too --- README.md | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/README.md b/README.md index 5ceb49ee..81cfbb56 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ An ESLint plugin for linting ESLint plugins. Rules written in CJS, ESM, and Type - [Usage](#usage) - [**.eslintrc.json**](#eslintrcjson) - [`eslint.config.js` (requires eslint\>=v8.23.0)](#eslintconfigjs-requires-eslintv8230) - - [Type Checked Rules](#type-checked-rules) - [Rules](#rules) - [Rules](#rules-1) - [Tests](#tests) @@ -73,24 +72,6 @@ module.exports = [ ]; ``` -### Type Checked Rules - -Rules marked with _πŸ’­ Requires type information_ require enabling [linting with type information](https://typescript-eslint.io/linting/typed-linting). -These rules are omitted from the `recommended` config, and instead are enabled in the `recommended-type-checked` superset config. - -If your plugin is authored in TypeScript, we recommend replacing `recommended` with `recommended-type-checked` and enabling typed linting: - -```js -module.exports = { - parser: '@typescript-eslint/parser', - parserOptions: { - project: true, - tsconfigRootDir: __dirname, - }, - extends: ['plugin:eslint-plugin/recommended-type-checked'], -}; -``` - ## Rules From 18b205a76796fb6869c0e160ea26988b5267dad7 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Feb 2024 08:46:54 -0500 Subject: [PATCH 18/21] Removed eslint-remote-tester.config.js parserOptions.project too --- eslint-remote-tester.config.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/eslint-remote-tester.config.js b/eslint-remote-tester.config.js index 06a14ca9..f9d44f69 100644 --- a/eslint-remote-tester.config.js +++ b/eslint-remote-tester.config.js @@ -49,9 +49,6 @@ module.exports = { { files: ['*.ts', '*.mts', '*.cts'], parser: '@typescript-eslint/parser', - parserOptions: { - project: true, - }, }, ], }, From 8d68dc9c9d822a8acfd92073f43f150a2053230b Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Feb 2024 08:47:43 -0500 Subject: [PATCH 19/21] Redid README.md presets table --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 81cfbb56..c7c9ccbe 100644 --- a/README.md +++ b/README.md @@ -123,15 +123,14 @@ module.exports = [ ## Presets -| | Name | Description | -| :-- | :------------------------- | :--------------------------------------------------------------------------------------- | -| βœ… | `recommended` | enables all recommended rules in this plugin, excluding those requiring type information | -| | `recommended-type-checked` | enables all recommended rules in this plugin, including those requiring type information | -| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | -| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | -| | `all` | enables all rules in this plugin | -| | `rules` | enables all rules that are aimed at linting ESLint rule files | -| | `tests` | enables all rules that are aimed at linting ESLint test files | +| | Name | Description | +| :-- | :------------------ | :------------------------------------------------------------------------ | +| βœ… | `recommended` | enables all recommended rules in this plugin | +| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | +| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | +| | `all` | enables all rules in this plugin | +| | `rules` | enables all rules that are aimed at linting ESLint rule files | +| | `tests` | enables all rules that are aimed at linting ESLint test files | ### Semantic versioning policy From d8dc1a0f899274561d5385830617a79d69769e08 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Feb 2024 08:54:00 -0500 Subject: [PATCH 20/21] Added all-type-checked --- .eslint-doc-generatorrc.js | 1 + README.md | 17 +++++++++-------- configs/all-type-checked.js | 13 +++++++++++++ lib/index.js | 3 ++- 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 configs/all-type-checked.js diff --git a/.eslint-doc-generatorrc.js b/.eslint-doc-generatorrc.js index 3279eca8..47100791 100644 --- a/.eslint-doc-generatorrc.js +++ b/.eslint-doc-generatorrc.js @@ -6,6 +6,7 @@ const prettier = require('prettier'); module.exports = { ignoreConfig: [ 'all', + 'all-type-checked', 'rules', 'rules-recommended', 'tests', diff --git a/README.md b/README.md index c7c9ccbe..d3c7169a 100644 --- a/README.md +++ b/README.md @@ -123,14 +123,15 @@ module.exports = [ ## Presets -| | Name | Description | -| :-- | :------------------ | :------------------------------------------------------------------------ | -| βœ… | `recommended` | enables all recommended rules in this plugin | -| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | -| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | -| | `all` | enables all rules in this plugin | -| | `rules` | enables all rules that are aimed at linting ESLint rule files | -| | `tests` | enables all rules that are aimed at linting ESLint test files | +| | Name | Description | +| :-- | :------------------ | :--------------------------------------------------------------------------- | +| βœ… | `recommended` | enables all recommended rules in this plugin | +| | `rules-recommended` | enables all recommended rules that are aimed at linting ESLint rule files | +| | `tests-recommended` | enables all recommended rules that are aimed at linting ESLint test files | +| | `all` | enables all rules in this plugin, including those requiring type information | +| | `all-type-checked` | enables all rules in this plugin, including those requiring type information | +| | `rules` | enables all rules that are aimed at linting ESLint rule files | +| | `tests` | enables all rules that are aimed at linting ESLint test files | ### Semantic versioning policy diff --git a/configs/all-type-checked.js b/configs/all-type-checked.js new file mode 100644 index 00000000..3d320fd7 --- /dev/null +++ b/configs/all-type-checked.js @@ -0,0 +1,13 @@ +/** + * @fileoverview the `all` config for `eslint.config.js` + * @author ε”―η„Ά + */ + +'use strict'; + +const mod = require('../lib/index.js'); + +module.exports = { + plugins: { 'eslint-plugin': mod }, + rules: mod.configs['all-type-checked'].rules, +}; diff --git a/lib/index.js b/lib/index.js index 4ff4ef76..b09550a2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -15,7 +15,8 @@ const packageMetadata = require('../package'); const PLUGIN_NAME = packageMetadata.name.replace(/^eslint-plugin-/, ''); const configFilters = { - all: () => true, + all: (rule) => !rule.meta.docs.requiresTypeChecking, + 'all-type-checked': () => true, recommended: (rule) => rule.meta.docs.recommended, rules: (rule) => rule.meta.docs.category === 'Rules', tests: (rule) => rule.meta.docs.category === 'Tests', From d8cc468a592dd8bf981f1b2f33fd686d2037e3fb Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 6 Feb 2024 08:54:55 -0500 Subject: [PATCH 21/21] Removed file notice --- configs/all-type-checked.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/configs/all-type-checked.js b/configs/all-type-checked.js index 3d320fd7..e32d57d2 100644 --- a/configs/all-type-checked.js +++ b/configs/all-type-checked.js @@ -1,8 +1,3 @@ -/** - * @fileoverview the `all` config for `eslint.config.js` - * @author ε”―η„Ά - */ - 'use strict'; const mod = require('../lib/index.js');