Skip to content

Bug: [prefer-readonly] should report all statically analyzable properties and property access #10655

Open
@ronami

Description

@ronami

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=5.7.3&fileType=.ts&code=MYGwhgzhAEAqCmEAuBGaBvAUNaB6X0EAFgPYCuIAJtAHYlLQBO8ADiY0ttC4wJYBuYJPG6MSLaAF5oAdgDcmLgDMSJABQBKDFxxIivCAG0A5D3HGAulOgoADApwBfTM8yhIMBMgBM2nPkJSCmpmNg4uHgEhEUMAZQBPAFsAIxIQADpeYUYhditpeRdFdyg4RCQAZj88AmJyKlp6JlZ2ThxIwWFoUzEWY2tC5VVNat19CHSzCWk7B2hnVxLPcoAWaoC64OawpAB%2BCL5OmJR82QVnIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0Wls4CGAEwD2TeIXRRe0EdEjgwAXxBKgA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

class Test1 {
  // should not report
  private prop = 7;

  foo() {
    this['prop'] = 10;
  }
}

class Test2 {
  // should report
  private [Symbol.iterator] = 7;
}

class Test3 {
  // should not report
  private 'prop' = 7;

  foo() {
    this.prop = 10;
  }
}

class Test4 {
  // should report?
  private [1] = 7;
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expected the rule not to report the first example and third examples and to report on the second and forth examples.

Actual Result

The rule reports on the first example, with the auto-fixer creating a type error: Cannot assign to 'prop' because it is a read-only property..

The second example should be reported but is missed.

The third example should not be reported, and also creates a type error once auto-fixed.

The forth example (I think) should be reported but it's missed.

Additional Info

I think this is very similar to #10211, and I noticed this while working on #10552. I think at least some of these can be tackled with getStaticMemberAccessValue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions