Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prefer-optional-chain] Breaking change in v5.16.0 - requires type information #4755

Closed
3 tasks done
avaly opened this issue Mar 28, 2022 · 5 comments
Closed
3 tasks done
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@avaly
Copy link
Contributor

avaly commented Mar 28, 2022

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/prefer-optional-chain": ["error"]
  }
}
// your repro code case

Expected Result

This rule used to work without type information before v5.16.0

Actual Result

Upgrading to v5.16.0, it now requires type information.

Error: Error while loading rule '@typescript-eslint/prefer-optional-chain': 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.

Additional Info

This is a breaking change and should be included in a major version bump.

Also, the documentation table on the rules do not mention this rule requires type information: https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/eslint-plugin

Regression introduced in #4430

Versions

package version
@typescript-eslint/eslint-plugin 5.16.0
@typescript-eslint/parser 5.16.0
TypeScript 4.6.2
ESLint 8.11.0
node 14.19.1
@avaly avaly added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 28, 2022
@bradzacher
Copy link
Member

The rule does not require type information. Our tests for the rule do not run with type information and are all passing.

Likely there is something else at play here.
Could you please provide an isolated reproduction for this?

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Mar 28, 2022
@avaly
Copy link
Contributor Author

avaly commented Mar 28, 2022

Sorry for my initial assumption. Indeed it does not require type information.

However when used together with the graphql-eslint plugin, it does trigger that error.

Reproducible with: https://gist.github.com/avaly/674f8bc818b2493ff54260090e9f73aa

$ eslint --ext graphql .

Oops! Something went wrong! :(

ESLint: 8.11.0

Error: Error while loading rule '@typescript-eslint/prefer-optional-chain': 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.
Occurred while linting /tmp/eslint-bugs/index.graphql
    at Object.getParserServices (/tmp/eslint-bugs/node_modules/@typescript-eslint/utils/dist/eslint-utils/getParserServices.js:16:15)
    at create (/tmp/eslint-bugs/node_modules/@typescript-eslint/eslint-plugin/dist/rules/prefer-optional-chain.js:65:37)
    at Object.create (/tmp/eslint-bugs/node_modules/@typescript-eslint/utils/dist/eslint-utils/RuleCreator.js:42:20)
    at createRuleListeners (/tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:922:21)
    at /tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:1104:31
    at Array.forEach (<anonymous>)
    at runRules (/tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:1041:34)
    at Linter._verifyWithoutProcessors (/tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:1389:31)
    at Linter._verifyWithConfigArray (/tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:1729:21)
    at Linter.verify (/tmp/eslint-bugs/node_modules/eslint/lib/linter/linter.js:1471:65)

If I disable the prefer-optional-chain rule, eslint runs without errors.

@rpastro
Copy link

rpastro commented Mar 28, 2022

I'm having the same error with version v5.16.0.

@bradzacher
Copy link
Member

bradzacher commented Mar 28, 2022

Using our rules on files that are not TypeScript files or with parsers that are not @typescript-eslint/parser is considered "undefined" behaviour.

The fact that it used to work and now that it doesn't is not a breaking change because we never defined that our rules would work for this usecase. This is the danger of relying upon undefined behaviour - it may break at any time and without warning.

You can use overrides to specify which rules should or should not be run on files.

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed awaiting response Issues waiting for a reply from the OP or another party labels Mar 28, 2022
@pleunv
Copy link

pleunv commented Mar 28, 2022

Fwiw, I ran into this issue as well, and took this as an opportunity to finally move my top-level plugins & rules to a JS/TS specific override so they do not by default also apply to other file types. It doesn't make a lot of sense to have @typescript-eslint and its rules run on GraphQL files, as @bradzacher says.

In essence, it means moving all your global config to a JS/TS override, i.e.

module.exports = {
  files: '**/*.{js,ts,jsx,tsx}',
  parser: '@typescript-eslint/parser',
  parserOptions: {
    sourceType: 'module',
    project: './tsconfig.json'
  },
  plugins: ['@typescript-eslint'],
  rules: {
    // ...
  },
  overrides: [
    {
      files: '**/*.gql',
      parserOptions: {
        operations: './src/**/*.gql'
      },
      extends: ['plugin:@graphql-eslint/operations-recommended']
    }
  ]
}

to

module.exports = {
  overrides: [
    {
      files: '**/*.{js,ts,jsx,tsx}',
      parser: '@typescript-eslint/parser',
      parserOptions: {
        sourceType: 'module',
        project: './tsconfig.json'
      },
      plugins: ['@typescript-eslint'],
      rules: {
        // ...
      }
    },
    {
      files: '**/*.gql',
      parserOptions: {
        operations: './src/**/*.gql'
      },
      extends: ['plugin:@graphql-eslint/operations-recommended']
    }
  ]
}

Everything should keep working as before, and probably a little faster as well.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

4 participants