SonarSource / eslint-plugin-sonarjs

SonarJS rules for ESLint
GNU Lesser General Public License v3.0
1.2k stars 73 forks source link

Add support for eslint flat config #454

Closed TheElegantCoding closed 6 months ago

TheElegantCoding commented 7 months ago
crystalfp commented 6 months ago

The rules I had to disable to have ESlint 9.0.0 (using flat config) succeed are:

        "sonarjs/no-gratuitous-expressions": "off",
        "sonarjs/no-empty-collection": "off",
        "sonarjs/no-unused-collection": "off",
        "security/detect-child-process": "off",
        "sonarjs/no-use-of-empty-return-value": "off",
        "sonarjs/no-extra-arguments": "off",
        "sonarjs/no-redundant-jump": "off",
        "sonarjs/no-one-iteration-loop": "off",
caseybaggz commented 6 months ago

I tried using the FlatCompat and this still doesn't work. So sad.

gquittet commented 6 months ago

@caseybaggz

I tried using the FlatCompat and this still doesn't work. So sad.

You need to install @eslint/eslintrc

I made it work by adding this to my flat eslint config file:

eslint.config.mjs

import eslint from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
import path from "path";
import { fileURLToPath } from "url";
import tsEslint from "typescript-eslint";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({ baseDirectory: __dirname });

And then:

export default tsEslint.config(
  eslint.configs.recommended,
  ...tsEslint.configs.recommended,
  ...compat.extends("plugin:sonarjs/recommended"),
// ... and so on

Have a nice day! 🌞

caseybaggz commented 6 months ago

@gquittet How did you do that and not get the TypeError: context.getScope is not a function error? I was already doing what you mentioned.

gquittet commented 6 months ago

@caseybaggz Which version of eslint you are using?

I'm using v8.57.0 with typescript-eslint v7.7.1

caseybaggz commented 6 months ago

@gquittet that makes sense. I'm using v9.1.1.

gquittet commented 6 months ago

@caseybaggz If you don't need the latest version of eslint, I suggest that you stay with the v8.x.x. because it's working and wait to upgrade until this issue is closed 😉

ericmorand-sonarsource commented 6 months ago

@TheElegantCoding, thanks for your report.

Can you please clarify what you mean by "Add support for eslint flat config"? ESLint plugins don't deal with the ESLint configuration and are not impacted by how the ESLint engine resolves it, and eslint-plugin-sonarjs is no different.

crystalfp commented 6 months ago

I disagree @ericmorand-sonarsource . ESlint 9.0 changed API for plugins, so some eslint-plugin-sonarjs rules do not work anymore. For example context.getScope() does not exist anymore. So it is true that the plugin does not care if flat or not flat config is used, but it cares about ESlint API.

ericmorand-sonarsource commented 6 months ago

Thanks for the clarification, @crystalfp. You are right; the plugin is not yet compatible with ESLint 9, but we are working on it. You can track the progress from the dedicated issue: https://github.com/SonarSource/eslint-plugin-sonarjs/issues/438

@TheElegantCoding , I close this issue. Feel free to ask for a reopen if I missed something related to the flat configuration files.

TheElegantCoding commented 6 months ago

@ericmorand-sonarsource thanks, i will see the track from #438

crystalfp commented 2 months ago

The problems continues in 2.0.2:

Oops! Something went wrong! :(

ESLint: 9.9.1

TypeError: context.getSource is not a function
Occurred while linting D:\Projects\STMexperimental\new\components\EnableCapture.vue
Rule: "sonarjs/pluginRules-of-hooks"
    at onCodePathEnd (D:\Projects\STMexperimental\node_modules\eslint-plugin-react-hooks\cjs\eslint-plugin-react-hooks.development.js:605:61)
    at D:\Projects\STMexperimental\node_modules\eslint-plugin-sonarjs\lib\helpers\decorators\merger.js:30:17
    at ruleErrorHandler (D:\Projects\STMexperimental\node_modules\eslint\lib\linter\linter.js:1088:48)
    at D:\Projects\STMexperimental\node_modules\eslint\lib\linter\safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (D:\Projects\STMexperimental\node_modules\eslint\lib\linter\safe-emitter.js:45:38)
    at runRules (D:\Projects\STMexperimental\node_modules\eslint\lib\linter\linter.js:1144:25)
    at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (D:\Projects\STMexperimental\node_modules\eslint\lib\linter\linter.js:1914:31)
    at D:\Projects\STMexperimental\node_modules\eslint\lib\linter\linter.js:1585:29
    at Array.map (<anonymous>)
bmulholland commented 2 months ago

@crystalfp https://github.com/SonarSource/eslint-plugin-sonarjs/issues/438#issuecomment-2324062419