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

no-unused-variable false positive when variable is declared outside of a module #570

Closed
adidahiya opened this issue Aug 13, 2015 · 12 comments

Comments

@adidahiya
Copy link
Contributor

simple repro code:

const fs = require("fs");

module Foo {
    const path = require("path");

    console.log(fs);
    console.log(path);
}

tslint reports 'fs' on line 1 is unused.

@jkillian
Copy link
Contributor

Additional failure example:

class HelloWorld {
    constructor(public name: string) { }
    sayHello() {
        return `Hello, ${this.name}!`;
    }
};

let hello = new HelloWorld("TSLint");
hello.sayHello();

Output:

>> test/files/sample.ts[8, 5]: unused variable: 'hello'

This is a bad bug, going to try to get it fixed ASAP.

@myitcv
Copy link
Contributor

myitcv commented Sep 7, 2015

@jkillian @adidahiya - can I ask, is this the following an instance of the same issue? Or a problem specific to .tsx files?

/// <reference path="tsd.d.ts" />

import * as React from "react";
interface IMProps {
    Name: string;
}

export class HelloMsg extends React.Component<{}, {}> {
    constructor() {
        super();
    }
    render(): JSX.Element {
        let props: IMProps = { Name: "test" };

        return <div>
                <Msg {...props} />
            </div>;
    }
}

export class Msg extends React.Component<IMProps, {}> {
    constructor() {
        super();
    }
    render(): JSX.Element {
        return <div></div>;
    }
}
$ tslint -c app/tslint.json app/**/*.{ts,tsx}
app/example.tsx[13, 7]: unused variable: 'props'

Configuration as follows:

  • TypeScript v1.6.0-dev.20150907
  • tslint v2.5.0-dev.5
  • tsconfig.json:
{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "declaration": false,
    "noImplicitAny": true,
    "removeComments": false,
    "noLib": false,
    "sourceMap": true,
    "jsx": "react",
    "rootDir": ".",
    "outDir": "../dist/"
  }
}

@jkillian
Copy link
Contributor

jkillian commented Sep 8, 2015

@myitcv I think what you're seeing is a similar, but different problem. Definitely an issue, but my guess is it has a different cause than the other bugs above

@praveennet
Copy link

I'm getting false positive when using import variables in another import statement. (due to rule 'no-unused-variable'). Below is sample code.

import ExternalDefinition = require("ExternalDefinition");
import Def = ExternalDefinition.MyNamespace.MyClass1;

export class MyImplementation
    extends MyBaseClasses.BaseClass1
    implements Def.Contract {

}

I get below error even though 'ExternalDefinition' is used in another import statement:

TsLint: unused variable: 'ExternalDefinition'

@myitcv
Copy link
Contributor

myitcv commented Sep 11, 2015

@jkillian - just in case you haven't seen the link, I think I tracked it down to this:

microsoft/TypeScript#4688

@jkillian
Copy link
Contributor

Thanks both. @myitcv nice find, looks like it is a TypeScript bug. Hopefully they'll get that fixed pretty soon and then we'll be sure to get that into TSLint.

@praveennet Yours is because of a TSLint bug which I discuss here. I'm working on getting that fixed, (hopefully) should be released by next Friday.

@myitcv
Copy link
Contributor

myitcv commented Sep 11, 2015

@jkillian - no problem. I've found that using these tests as a template for finding issues like this really useful.

@myitcv
Copy link
Contributor

myitcv commented Sep 14, 2015

@jkillian @adidahiya - just so that I'm clear, are we thinking this issue is a separate bug and needs fixed from TypeScript core?

If so I'll open a new issue to track that.

@jkillian
Copy link
Contributor

@myitcv If the fourslash test is failing, definitely a TypeScript issue. Feel free to go ahead and open up a separate issue, I tried out your sample and am still getting 'unused variable: "props"'

@hashark
Copy link

hashark commented Aug 21, 2016

@jkillian hi,

I am using tslint version 3.14.0 and I just got a false positive similar to the one @praveennet got.
Is there a way to prevent it?

thanks!

@jkillian
Copy link
Contributor

@hashark I recommend using TS 2.0, these bugs should be fixed in it.

@hashark
Copy link

hashark commented Aug 23, 2016

thanks! I don't see them.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants