Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Class fields cause false positive no-undef #471

Closed
ackvf opened this issue Apr 24, 2018 · 9 comments · Fixed by #540
Closed

Class fields cause false positive no-undef #471

ackvf opened this issue Apr 24, 2018 · 9 comments · Fixed by #540

Comments

@ackvf
Copy link

ackvf commented Apr 24, 2018

What version of TypeScript are you using?
2.7.2, 2.8.1

What version of typescript-eslint-parser are you using?

15.0.0

What code were you trying to parse?

class X {
  field = {} // [eslint] 'field' is not defined. (no-undef)
}

What did you expect to happen?
I did not expect any error

What happened?
I get [eslint] 'field' is not defined. (no-undef)

@ackvf ackvf changed the title Class fields false positive no-undef Class fields cause false positive no-undef Apr 24, 2018
@todorone
Copy link

todorone commented Jul 3, 2018

@ackvf DId You manage to resolve this issue?

@ackvf
Copy link
Author

ackvf commented Jul 4, 2018

No, I had to turn the rule off. It still reports false errors with latest 16.0.1. (typescript 3.0.0-dev.20180626)

@lostfictions
Copy link

lostfictions commented Aug 1, 2018

I'm also experiencing this (typescript-eslint-parser 17.0.1). Unfortunately, we're currently in the process of converting our codebase to TypeScript, so we have a mix of JS and TS files and it's not acceptable for us to turn no-undef off.

Unfortunately, seems like we'll simply need to revert to babel-eslint for the JS files and use TSLint instead for TypeScript.

@ackvf
Copy link
Author

ackvf commented Aug 2, 2018

@lostfictions @todorone The undef property is still covered by tslint if you use both (like I do).

image

@todorone
Copy link

todorone commented Aug 2, 2018

@ackvf Thanks for the hint. I don't want to introduce tslint in my project so I just had to turn this rule off...

@timche
Copy link

timche commented Aug 2, 2018

Check no-undef with tsc instead until this issue has been resolved:

  1. Disable no-undef in ESLint
  2. tsc will now error at not defined variables

Edit: Fixed my comment based on @lostfictions answer.

@lostfictions
Copy link

@timche sounds like you might be thinking of no-unused-vars, which is a different rule (which also sees lots of false positives with this parser and can be turned off in favour of TS rules noUnusedLocals and noUnusedParams, it's true.) This is a separate issue.

@ackvf It's true that no-undef is not required for TypeScript files -- the compiler handles it, no linter necessary. What I was trying to articulate is that in a project with mixed TypeScript and vanilla JavaScript files, it's not acceptable to turn off no-undef for the entire project, because then undefined variables in the JS files would not get caught by the linter.

In the end we set up a second eslint config file that turns these rules off for only the TS files and keeps it on for JS files (with a different parser). But having multiple config files means we lose ESLint editor integration for TS files, which is a big blow to productivity. So we may still switch to TSLint, since at least TSLint and ESLint can run side-by-side in VS Code, whereas there doesn't seem to be a way to make vscode-eslint handle two different file extensions with different config files.

@timche
Copy link

timche commented Aug 2, 2018

@lostfictions You are right, I'm confusing it with no-unused-vars.

There are currently two rules I've disabled in ESLint:

  • no-undef
  • no-unused-vars

Both are causing false-positives in ESLint with typescript-eslint-parser.

By deactivating them, we now let tsc check them:

  • no-undef: No configuration required
  • no-unused-vars: "noUnusedLocals": true in tsconfig.json

@Gvozd
Copy link

Gvozd commented Sep 19, 2018

@timche no-unused-vars can be replaced with typescript/no-unused-vars from eslint-plugin-typescript

mysticatea added a commit that referenced this issue Nov 8, 2018
mysticatea added a commit that referenced this issue Nov 13, 2018
* Update: add proper scope analysis (fixes #535)

* add computed-properties-in-type fixture

* add computed-properties-in-interface fixture

* add function-overload fixture

* add method-overload fixture

* add class-properties fixture

* add decorators fixture

* update visitor-keys

* add declare-global fixture

* fix typo

* add test for typeof in array destructuring

* add namespace fixture

* add declare-module fixture

* fix crash

* add declare-function.ts fixture

* add abstract-class fixture

* add typeof-in-call-signature fixture

* add test for #416

* add test for #435

* add test for #437

* add test for #443

* add test for #459

* add test for #466

* add test for #471

* add test for #487

* add test for #535

* add test for #536

* add test for #476

* fix test to use `expect()`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants