eslint / typescript-eslint-parser

An ESLint custom parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code.
Other
915 stars 75 forks source link

Class fields cause false positive no-undef #471

Closed ackvf closed 5 years ago

ackvf commented 6 years ago

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)

todorone commented 6 years ago

@ackvf DId You manage to resolve this issue?

ackvf commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

image

todorone commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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

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

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

By deactivating them, we now let tsc check them:

Gvozd commented 6 years ago

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