question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: no-invalid-this: false positive with this in class field

See original GitHub issue

Environment

Node version: v16.13.1 npm version: 8.3.0 Local ESLint version: 8.6.0 Global ESLint version: N/A Operating System: Ubuntu 20.04.2 LTS

What parser are you using?

Default (Espree)

What did you do?

Configuration
{
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module"
    },
    "rules": {
        "no-invalid-this": 2
    }
}
class Foo {
    bar = this;
}

What did you expect to happen?

No error reported.

What actually happened?

$ npx eslint index.js 

/home/regseb/testcase/index.js
  2:11  error  Unexpected 'this'  no-invalid-this

✖ 1 problem (1 error, 0 warnings)

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No error reported with this configuration (without "sourceType": "module"):

{
    "parserOptions": {
        "ecmaVersion": "latest"
    },
    "rules": {
        "no-invalid-this": 2
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Jan 1, 2022

To clarify, this rule only flags any use of this where the value of this is undefined (for example, at the top level of modules), which isn’t the case in the provided example. If this is an object, the rule doesn’t make assumptions about whether a particular usage makes sense or not.

This bug is reproducible in v8.0.0 as well. It happens only when PropertyDefinition#value is ThisExpression. Due to a higher specificity of "PropertyDefinition > *.value", ThisExpression gets called first and is therefore mistakenly treated as if it were in an outer context.

I’ll work on the fix.

0reactions
ljharbcommented, Jan 1, 2022

ah, i misunderstood the purpose of the rule then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

False positive "(ESLint) no-invalid-this" in class property method
Class fields only recently gained stage 4, so eslint doesn't support them yet (see #14343). (Separately, arrow functions in a class field are ......
Read more >
ESLint v8.7.0 released - ESLint - Pluggable JavaScript Linter
This release adds some new features and fixes several bugs found in the ... a8db9a5 fix: no-invalid-this false positive in class field ...
Read more >
Documentation bug for S4035 - Sonar Community
The code shown behaves correctly and is no example for the issue. To make it illustrate the problem, class B must inherit from...
Read more >
Code Issues - Embold Help Center
Similarly, any defect or bug found in the code leads to code issues. ... DisposeFieldsProperly, This class has a disposable field and is...
Read more >
node_modules/@typescript-eslint/eslint-plugin/CHANGELOG ...
eslint-plugin: [no-unnecessary-condition] false positive when array ... eslint-plugin: [no-invalid-this] allow "this" in class property ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found