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

False positive "(ESLint) no-invalid-this" in class property method #14534

Closed
LukeOwlclaw opened this issue Apr 28, 2021 · 2 comments
Closed

False positive "(ESLint) no-invalid-this" in class property method #14534

LukeOwlclaw opened this issue Apr 28, 2021 · 2 comments
Labels
3rd party plugin This is an issue related to a 3rd party plugin, config, or parser archived due to age This issue has been archived; please open a new issue for any further discussion
Projects

Comments

@LukeOwlclaw
Copy link

LukeOwlclaw commented Apr 28, 2021

Tell us about your environment

  • ESLint Version: v7.25.0
  • Node Version: v12.20.1
  • npm Version: v6.14.11
  • Operating System: win32 10.0.19042

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

"@typescript-eslint/parser": "4.22.0",
"@typescript-eslint/eslint-plugin": "4.22.0",

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

file.ts:

class TestClass {
    private mText: string = "test";

    public Baz = (): void => {
        console.log(this.mText); // (ESLint) no-invalid-this	Unexpected 'this'.
    }

    public Foo(): void {
        console.log(this.mText);
    }
}

const t = new TestClass();
t.Foo();
t.Baz();

ESLint rule:
"no-invalid-this": "error"

Run:

eslint file.ts

What did you expect to happen?

No errors.

What actually happened? Please copy-paste the actual, raw output from ESLint.

file.ts
  5:21  error  Unexpected 'this'  no-invalid-this

✖ 1 problem (1 error, 0 warnings)

Steps to reproduce this issue:

  1. Create file.ts as shown above
  2. Run eslint file.ts applying rule "no-invalid-this": "error"
@LukeOwlclaw LukeOwlclaw added bug ESLint is working incorrectly repro:needed labels Apr 28, 2021
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Apr 28, 2021
@ljharb
Copy link
Sponsor Contributor

ljharb commented Apr 28, 2021

Class fields only recently gained stage 4, so eslint doesn't support them yet (see #14343).

(Separately, arrow functions in a class field are almost always a bad idea, and there's nothing in this example that suggests Baz should be an arrow function)

@mdjermanovic mdjermanovic moved this from Needs Triage to Triaging in Triage Apr 28, 2021
@mdjermanovic mdjermanovic added 3rd party plugin This is an issue related to a 3rd party plugin, config, or parser and removed bug ESLint is working incorrectly repro:needed labels Apr 28, 2021
@mdjermanovic
Copy link
Member

Hi @LukeOwlclaw, thanks for the issue!

This rule will be updated as a part of #14343, so I'm closing this issue as a duplicate.

In the meantime, you can use a replacement rule @typescript-eslint/no-invalid-this from @typescript-eslint/eslint-plugin:

{  
  "no-invalid-this": "off",
  "@typescript-eslint/no-invalid-this": ["error"]
}

Triage automation moved this from Triaging to Complete Apr 28, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Oct 26, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Oct 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3rd party plugin This is an issue related to a 3rd party plugin, config, or parser archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
Archived in project
Triage
Complete
Development

No branches or pull requests

3 participants