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

[no-undef] Linter does not report error when using undefined variables. #662

Closed
gkjohnson opened this issue Jun 30, 2019 · 7 comments
Closed
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser

Comments

@gkjohnson
Copy link

gkjohnson commented Jun 30, 2019

Repro

package.json

{
  "name": "typescript-test",
  "scripts": {
    "lint": "eslint ./src/* --ext js --ext ts"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^1.11.0",
    "@typescript-eslint/parser": "^1.11.0",
    "eslint": "^6.0.1",
    "typescript": "^3.5.2"
  },
  "eslintConfig": {
    "parser": "@typescript-eslint/parser",
    "plugins": [
      "@typescript-eslint"
    ],
    "rules": {
      "no-undef": 1
    }
  }
}

src/test.d.ts OR src/test.ts

export class TestClass {
    field: UndefinedClass;
}

Expected Result

A no-undef rule error is reported because UndefinedClass is being used but has not been defined.

Actual Result

No errors are reported.

Additional Info

The error is correctly reported when doing the equivalent in a js file.

Versions

package version
@typescript-eslint/eslint-plugin 1.11.0
@typescript-eslint/parser 1.11.0
TypeScript 3.5.2
ESLint 6.0.1
node 12.4.0
npm 6.9.0
@gkjohnson gkjohnson added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jun 30, 2019
@bradzacher bradzacher added bug Something isn't working scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser and removed triage Waiting for maintainers to take a look labels Jul 1, 2019
@bradzacher
Copy link
Member

We suggest turning off no-undef for typescript projects.
It can only ever report errors that are handled automatically (and better) by the typescript compiler itself.

@gkjohnson
Copy link
Author

It can only ever report errors that are handled automatically (and better) by the typescript compiler itself.

I'm not sure I follow -- by "it" do you mean the typescript eslint plugin? To elaborate I expect that this feature would be implemented as a new rule (such as @typescript-eslint/no-undef) that reports the TS2304 error the way the compiler / VSCode does for files.

Thanks!

@bradzacher
Copy link
Member

Our parser is only a parser. It leverages the typescript compiler to parse and generate an AST, and provide type information. However we don't currently have have access to the diagnostics typescript provides, nor can we access things like its scope analyser.

As it stands right now the rule maintains its own logic for analysing scope, which has no idea about typescript type scope.

If you're looking for a way to have typescript compiler error TS2304 reported, the best way is via running the typescript compiler..

@kaicataldo
Copy link
Member

kaicataldo commented Jul 1, 2019

For what it’s worth, we turned no-undef off and just let tsc report this at my last job and it was never an issue. Most popular editors are able to report errors reported by tsc, and we would run tsc —noEmit and ESLint during the linting step of our CI process.

@bradzacher
Copy link
Member

That was the implication of the first comment.
Recommend just turning this rule off because the typescript compiler can report the problem better than a custom written rule ever could.

Considering this rule's functionality is completely covered by the typescript compiler, rewriting the rule to work properly is low on the list of priorities...

@gkjohnson
Copy link
Author

Got it -- thanks for the clear explanation! I'll look into using the typescript compiler to catch these types of issues.

@bradzacher
Copy link
Member

Merging into #1856

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser
Projects
None yet
Development

No branches or pull requests

3 participants