Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unused-variable error with object destructuring #2876

Closed
cartant opened this issue Jun 4, 2017 · 11 comments · Fixed by #3058
Closed

no-unused-variable error with object destructuring #2876

cartant opened this issue Jun 4, 2017 · 11 comments · Fixed by #3058

Comments

@cartant
Copy link

cartant commented Jun 4, 2017

Bug Report

  • TSLint version: 5.4.2
  • TypeScript version: 2.3.4
  • Running TSLint via: CLI

TypeScript code being linted

To reproduce the problem, two files are required:

// main.ts
import { SomeClass } from "./some";
const person = { name: "alice" };
const { name } = person;
// some.ts
export class SomeClass {};

with tslint.json configuration:

{
    "defaultSeverity": "error",
    "rules": { "no-unused-variable": { "severity": "error" } }
}

Actual behavior

An internal error within the rule occurs:

Warning: Cannot read property 'flags' of undefined

Expected behavior

TSLint should execute without internal errors and should report that SomeClass and name are both unused.

Additional information

The import is required to effect the internal error. If the import is omitted, no internal error occurs.

With the import, the rule follows a code path into the getImplicitType function and the implementations of utils.isPropertyDeclaration and utils.isVariableDeclaration allow for the { name } = person node to be passed to checker.getTypeAtLocation - from within which the error is thrown.

@victornoel
Copy link

@cartant do you think this is the same as #3001?

@cartant
Copy link
Author

cartant commented Jul 11, 2017

@victornoel Maybe. That issue doesn't seem to include enough information for the reported problem to be reproduced, so who knows?

@victornoel
Copy link

@adidahiya any plan to fix this issue? it is really problematic…

@adidahiya
Copy link
Contributor

Is this still an issue with TSLint 5.5.0?

@cartant
Copy link
Author

cartant commented Jul 18, 2017

@adidahiya I've revisted the harness I created and the error is still effected with:

  • TSLint version: 5.5.0
  • TypeScript version: 2.4.1
  • Running TSLint via: CLI

TypeError: Cannot read property 'flags' of undefined
    at Object.getCheckFlags (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:10137:22)
    at getTypeOfSymbol (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:30957:20)
    at getTypeOfNode (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:47329:24)
    at Object.getTypeAtLocation (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:26823:31)
    at getImplicitType (...\tslint-no-unused-variable\node_modules\tslint\lib\rules\noUnusedVariableRule.js:269:24)
    at cb (...\tslint-no-unused-variable\node_modules\tslint\lib\rules\noUnusedVariableRule.js:259:20)
    at visitEachNode (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:15956:30)
    at Object.forEachChild (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:16143:24)
    at cb (...\tslint-no-unused-variable\node_modules\tslint\lib\rules\noUnusedVariableRule.js:264:19)
    at visitNode (...\tslint-no-unused-variable\node_modules\typescript\lib\typescript.js:15944:20)

@victornoel
Copy link

I concur :)

@SteveALee
Copy link

SteveALee commented Jul 20, 2017

For information I had same hard to locate warning with "no-use-before-declare": true, in TSL 4.5.1

It would really help if all TSL errors were prefixed with [TSLint] or similar.

@victornoel
Copy link

@SteveALee open another issue for this please…

@SteveALee
Copy link

@victornoel your mean for the prefix?

@victornoel
Copy link

@SteveALee yes, I meant that if you have a need for prefixing tslint messages, open another issue for that

@SteveALee
Copy link

@victornoel please see #3060 :)

adidahiya pushed a commit that referenced this issue Aug 2, 2017
Avoid typescript crash when trying to get type of destructured variable declaration.
Also avoid walking the AST multiple times when `--declaration` is not enabled. That should result in better performance and fewer false negatives.

[bugfix] `no-unused-variable` fixed crash when using destructuring
Fixes: #2876
Fixes: #3001
@adidahiya adidahiya added this to the TSLint v5.6 milestone Aug 2, 2017
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this issue Apr 9, 2018
Avoid typescript crash when trying to get type of destructured variable declaration.
Also avoid walking the AST multiple times when `--declaration` is not enabled. That should result in better performance and fewer false negatives.

[bugfix] `no-unused-variable` fixed crash when using destructuring
Fixes: palantir#2876
Fixes: palantir#3001
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.

4 participants