-
Notifications
You must be signed in to change notification settings - Fork 889
no-unused-variable configuration request #191
Comments
i see. can you please give a couple of examples of these false positives? |
The big problem I think is “private class members”. Imports, variables, and functions can all never be accessed dynamically without using |
I don't buy that. private members are used to prevent access; sure javascript has no semantics for this, but typescript does. if you're accessing a private member only using the array operator, then i'd suggest making it public and use it directly. |
What does the access control model for class properties in TypeScript have to do with preventing the accidental creation of unused variables? They’re two different things. Unused private class methods can be discovered easily during unit testing of the public APIs using code coverage analysis, by identifying methods that are never called, so it’s not critical to lint them; this is not the case for unused variables whose declarations will be covered in testing even when the variables are never actually accessed. Additionally, in the design of the language itself, variables are guaranteed to be unused if they are never accessed within the scope where they are defined; as we have already discussed, the same is not true about object properties, which both JavaScript and TypeScript allow to be accessed using array operators. This leads to false positives: class A {
private _handles:Handle[];
constructor(emitter:EventEmitter) {
this._handles = [
emitter.on('response', lang.hitch(this, '_handleResponse'));
];
}
destroy():void {
var handle:Handle;
while ((handle = this._handles.pop()) {
handle.remove();
}
}
// this method is used, but tslint says it isn’t
private _handleResponse(event:Event) {}
}
As mentioned above, TypeScript does not prescribe that you must access a property of a class using dot operators.
Thanks for your consideration, |
Sorry if bumping this is against the rules - I've come across the same problem. class Test {
private done: boolean;
constructor() {
(function (): void {
this.doPrivateThing();
(<Test>this).doPrivateThing();
}).bind(this)();
}
private doPrivateThing(): void {
this.done = false;
}
} ...gives the following error:
It looks like tslint isn't checking inside the bound function. It's understandable that function.bind erases some information, but should |
I have a similar problem maybe? import Foo = this.that.Foo;
module some.module.blah {
export class bar {
private foo: Foo;
}
} returns the error |
The use of an import alias is also not detected when it's used with extends import DateTimeOpts = Intl.DateTimeFormatOptions;
interface MyDateTimeOpts extends DateTimeOpts {
timezoneOffset: number;
}
let opts: MyDateTimeOpts;
console.log(opts.timezoneOffset - 1); |
@SenHeng, @nikklassen: those look like related issues; I've filed a new ticket for those false positives: #613. as for the other issues in this thread, I think we could address them by adding a new option that makes the |
closing this for now. I can reopen if someone wants to discuss further... or feel free to file a new, more specific issue |
Forgive me if I'm misunderstanding, but does that not solve a different problem? Caring whether private variables are unused is different from correctly reporting whether they're unused, yes? For further clarification, in the below example, class Test {
private waste: boolean;
private done: boolean;
constructor() {
(function (): void {
this.doPrivateThing();
(<Test>this).doPrivateThing();
}).bind(this)();
}
private doPrivateThing(): void {
this.done = false;
}
} |
ok, I think I see what you mean with the difference between basically the option I've suggested in #614 is a less strict version of the |
Unfortunately I'm not familiar with the tslint codebase, so that brings up a question. Should the following line above mark (<Test>this).doPrivateThing(); ...which brings up another question: does tslint know the anonymous function is being executed? constructor() {
(function (): void {
(<Test>this).doPrivateThing();
}).bind(this)();
} |
No, we don't do much control flow analysis to know things like "is this anonymous function being executed". I'm hesitant to do that unless we have really good reasons to do so (it complicates the codebase).
|
@JoshuaKGoldberg However, I think |
@JoshuaKGoldberg If I run TSLint on your example, I get an unused variable warning for |
The incorrect mark was the behavior in July, but I just ran it locally and seems to be working. Thanks for taking the time to look into it, @adidahiya and @jkillian ! Everything seems to be fixed :) |
No problem! Always nice when bugs disappear haha |
As the no-unused-variable is implemented now, I find it has too many false positives to really be useful. However, if I could just enable a portion of it that would check only local variables, that would be really useful.
The text was updated successfully, but these errors were encountered: