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

Bug: prefer-optional-chain throws error unexpected Identifier #5556

Closed
4 tasks done
revelt opened this issue Aug 27, 2022 · 1 comment
Closed
4 tasks done

Bug: prefer-optional-chain throws error unexpected Identifier #5556

revelt opened this issue Aug 27, 2022 · 1 comment
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@revelt
Copy link

revelt commented Aug 27, 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.

Playground Link

https://typescript-eslint.io/play/#ts=4.7.4&sourceType=module&code=MYewdgzgLgBAZiEMC8MBEBDARsNBuAKFElgEsUYAmQgQgRAG1yBaGARgF0A6AGwFMwAcygALGAB9xMUniA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WloHsalfkwCG8WmQAWo5uii9o-aJHBgAviHVA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

const foo = "abc";
const i = 2;
!foo[i - 1].length || i;

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/prefer-optional-chain": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

I expect the rule not to cause ESLint to throw; it is a valid code and lint rule "prefer-optional-chain" does not apply there.

Actual Result

Rule causes an ESLint throw error.

Additional Info

The minimal pattern !foo[i - 1].length || i; is a bit obscure, so here's the actual source code I distilled it from:

const padLeftIfTheresOnTheLeft = [':'];
// ...
if (
  !source[y - 3].trim().length ||
  padLeftIfTheresOnTheLeft.some((val) =>
    source
      .slice(startPoint, y - 2)
      .trim()
      .endsWith(val)
  )
) {
  // ...
}

it's an npm package generate-atomic-css, line 742 in the source: https://github.com/codsen/codsen/blob/main/packages/generate-atomic-css/src/util.ts#L742

Also, I can confirm the linting rule was not breaking on @typescript-eslint/eslint-plugin previous version 5.33.1, I was trying to bump it to 5.35.1 and only now it throws.

Versions

package version
@typescript-eslint/eslint-plugin 5.35.1
@typescript-eslint/parser 5.35.1
TypeScript 4.7.4
ESLint 8.22.0
node 16.17.0
@revelt revelt added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 27, 2022
@bradzacher
Copy link
Member

thanks for the report - closing as a duplicate of #5553

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2022
@bradzacher bradzacher added duplicate This issue or pull request already exists and removed bug Something isn't working triage Waiting for team members to take a look labels Aug 29, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants