gund / eslint-plugin-deprecation

ESLint rule that reports usage of deprecated code
GNU Lesser General Public License v3.0
313 stars 36 forks source link

ESLint v9 compatibility #78

Open karlhorky opened 6 months ago

karlhorky commented 6 months ago

Currently, using eslint@9.0.0-alpha.0, eslint-plugin-deprecation fails with the following error:

An unexpected error occurred:
TypeError: context.getAncestors is not a function
Occurred while linting /Users/k/p/project/eslint.config.js:1
Rule: "deprecation/deprecation"
    at getParent (/Users/k/p/project/node_modules/eslint-plugin-deprecation/dist/rules/deprecation.js:86:31)
    at isDeclaration (/Users/k/p/project/node_modules/eslint-plugin-deprecation/dist/rules/deprecation.js:94:20)
    at identifierRule (/Users/k/p/project/node_modules/eslint-plugin-deprecation/dist/rules/deprecation.js:60:13)
    at ruleErrorHandler (/Users/k/p/project/node_modules/eslint/lib/linter/linter.js:1059:28)
    at /Users/k/p/project/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/k/p/project/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/Users/k/p/project/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
    at NodeEventGenerator.applySelectors (/Users/k/p/project/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
    at NodeEventGenerator.enterNode (/Users/k/p/project/node_modules/eslint/lib/linter/node-event-generator.js:340:14)

It looks like there's a blog post about preparing rules for ESLint v9 here:

From a quick look, it appears that context.sourceCode.getAncestors(node) should be used instead.

gund commented 6 months ago

@karlhorky thanks for testing it with eslint v9!

Indeed there are some BC that have to be addressed in order to support v9.

If you like you are welcome to create a PR to fix this piece of code but it should be done in backwards compatible way just like [they show in example](https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/#context.getancestors()), so we can keep supporting all previous eslint versions.

Also if you decide to make a PR it would be necessary to update CI workflows to test this rule against both eslint v8 and v9, just like we already do with previous versions. But if you are not comfortable doing it no worries, I can adjust it on your PR as well 😊

gund commented 6 months ago

Did some initial work on the v9 support but in general it's not possible to use it due to upstream dependencies not supporting it yet.

stianjensen commented 4 months ago

typescript-eslint are at least preparing for it now, but this lib may need to also upgrade @typescript-eslint/utils to 7.x first.

https://typescript-eslint.io/blog/announcing-typescript-eslint-v7/

They are dropping support for older typescript, node and eslint versions in that major (but otherwise it is unchanged). I look into adding support here for 6.x and 7.x at the same time, but I'm not sure that can work nicely without being a breaking change, due to how npm resolution works. As long as this library itself wants to support older eslint and node versions than typescript-eslint 7.x I'm not sure we can just add 7.x to the list of supported versions next to 6.x?

maranomynet commented 3 months ago

Another data point: I'm upgrading my eslint@8.57 config to use "flat config" and the @ts-check flags this plugin being incorrectly typed.

Shinigami92 commented 3 months ago

How is work going forward? This is blocking us from upgrading to eslint v9 and we cant already upgrade eslint-plugin-vitest >=0.5 due to this

ej612 commented 2 months ago

I'm seeing the following problem after upgrading to eslint 9:

ESLint: 9.2.0

Error: Error while loading rule 'deprecation/deprecation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser. Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.

My config looks like this:

const eslintJs = require('@eslint/js');
const eslintTs = require('typescript-eslint');
const pluginDeprecation = require('eslint-plugin-deprecation');

const config = {
  languageOptions: {
    parser: eslintTs.parser,
    parserOptions: {
      project: 'tsconfig.lint.json'
    }
  },
  plugins: {
    deprecation: pluginDeprecation,
    '@typescript-eslint': eslintTs.plugin
  },
  rules: {
    'deprecation/deprecation': 'error'
  }
}

module.exports = eslintTs.config(
    eslintJs.configs.recommended,

    ...eslintTs.configs.strictTypeChecked,
    ...eslintTs.configs.stylisticTypeChecked,

    config
);

Am I doing it wrong or is this a compatibility issue?

matchai commented 1 month ago

@ej612 In my case, it was due to having the rules active for JS files. Disabling type-aware rules in my JS files solved the problem.

To then get it working with ESLint 9, I had to use @eslint/compat's fixupPluginRules function.

This is what the final config looked like:

import { fixupPluginRules } from "@eslint/compat";
import deprecationPlugin from "eslint-plugin-deprecation";
import tseslint from "typescript-eslint";

export default [
  {
    plugins: {
      ["@typescript-eslint"]: tseslint.plugin,
      ["deprecation"]: fixupPluginRules(deprecationPlugin),
    },
    languageOptions: {
      parserOptions: {
        project: true,
      },
    },
  },
  {
    files: ["**/*.js"],
    // Don't typecheck JS files
    extends: [tseslint.configs.disableTypeChecked],
    // Disable type-aware rules
    rules: {
      "deprecation/deprecation": "off",
    },
  },
];
SirPhemmiey commented 2 weeks ago

@ej612 In my case, it was due to having the rules active for JS files. Disabling type-aware rules in my JS files solved the problem.

To then get it working with ESLint 9, I had to use @eslint/compat's fixupPluginRules function.

This is what the final config looked like:

import { fixupPluginRules } from "@eslint/compat";
import deprecationPlugin from "eslint-plugin-deprecation";
import tseslint from "typescript-eslint";

export default [
  {
    plugins: {
      ["@typescript-eslint"]: tseslint.plugin,
      ["deprecation"]: fixupPluginRules(deprecationPlugin),
    },
    languageOptions: {
      parserOptions: {
        project: true,
      },
    },
  },
  {
    files: ["**/*.js"],
    // Don't typecheck JS files
    extends: [tseslint.configs.disableTypeChecked],
    // Disable type-aware rules
    rules: {
      "deprecation/deprecation": "off",
    },
  },
];

this worked for me, thanks!