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

Rule: "@typescript-eslint/prefer-optional-chain" fails with "Error: Unexpected member object type: TSNonNullExpression" #5553

Closed
4 tasks done
B4nan opened this issue Aug 26, 2022 · 2 comments · Fixed by #5564
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@B4nan
Copy link

B4nan commented Aug 26, 2022

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Issue Description

Latest version fails to process the MikroORM codebase. You can see the failed build in CI here:

https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true

There are more dependency updates, but the error was there even before, when it was only about eslint - I did verify it locally too, by only updating eslint.

$ eslint packages/**/*.ts

Oops! Something went wrong! :(

ESLint: 8.22.0

Error: Unexpected member object type: TSNonNullExpression
Occurred while linting /home/runner/work/mikro-orm/mikro-orm/packages/core/src/EntityManager.ts:1104
Rule: "@typescript-eslint/prefer-optional-chain"
    at getMemberExpressionText (/home/runner/work/mikro-orm/mikro-orm/node_modules/@typescript-eslint/eslint-plugin/dist/rules/prefer-optional-chain.js:284:27)
    at getText (/home/runner/work/mikro-orm/mikro-orm/node_modules/@typescript-eslint/eslint-plugin/dist/rules/prefer-optional-chain.js:2[6](https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true#step:8:7)3:20)
    at LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > Identifier,LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > MemberExpression,LogicalExpression[operator="||"] > UnaryExpression[operator="!"] > ChainExpression > MemberExpression (/home/runner/work/mikro-orm/mikro-orm/node_modules/@typescript-eslint/eslint-plugin/dist/rules/prefer-optional-chain.js:12[7](https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true#step:8:8):40)
    at ruleErrorHandler (/home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/linter.js:1114:2[8](https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true#step:8:9))
    at /home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/node-event-generator.js:2[9](https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true#step:8:10)7:26)
    at NodeEventGenerator.applySelectors (/home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/node-event-generator.js:326:22)
    at NodeEventGenerator.enterNode (/home/runner/work/mikro-orm/mikro-orm/node_modules/eslint/lib/linter/node-event-generator.js:340:[14](https://github.com/mikro-orm/mikro-orm/runs/8041169217?check_suite_focus=true#step:8:15))
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The line where it fails is here:

https://github.com/mikro-orm/mikro-orm/blob/40f725e851d0217fb1a3022492f72bcdad331e26/packages/core/src/EntityManager.ts#L1104

Reproduction Repository Link

https://github.com/mikro-orm/mikro-orm

Repro Steps

  1. clone the repo
  2. update eslint to latest version in the root package.json
  3. yarn install
  4. yarn lint

Versions

package version
@typescript-eslint/eslint-plugin 5.35.1
@typescript-eslint/parser 5.35.1
@typescript-eslint/scope-manager 5.35.1
@typescript-eslint/typescript-estree 5.35.1
@typescript-eslint/type-utils 5.35.1
@typescript-eslint/utils 5.35.1
TypeScript 4.7 & 4.8
ESLint 8.22.0
node 16.16 & 18.7
@B4nan B4nan added bug Something isn't working triage Waiting for maintainers to take a look labels Aug 26, 2022
@revelt
Copy link

revelt commented Aug 27, 2022

I've just raised similar #5556 Unexpected member property type: Identifier (pattern !foo[i - 1].length || i;). It seems the late improvements to the rule prefer-optional-chain didn't take into account all the possible language element combinations. This implies the rule's algorithm is checking too explicitly and it's not defensive-enough picking out patterns from the sources.

@bradzacher bradzacher added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Aug 29, 2022
@bradzacher
Copy link
Member

The issue is this line right here:

let previousLeftText = getText(initialIdentifierOrNotEqualsExpr);

The getText function assumes that the node has already been validated by the isValidChainTarget function (hence it has hard errors in it), but the changes that were introduced in #5266 do not respect that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants