Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 1090947

Browse files
brainkimbradzacher
authored andcommitted
[FIX] [no-unused-vars] address more false positives (#153)
1 parent a68d424 commit 1090947

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

lib/rules/no-unused-vars.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ module.exports = {
215215
node.superTypeParameters.params.forEach(markTypeAnnotationAsUsed);
216216
}
217217

218+
/**
219+
* Checks the given expression and marks any type parameters as used.
220+
* @param {ASTNode} node the relevant AST node.
221+
* @returns {void}
222+
* @private
223+
*/
224+
function markExpressionAsUsed(node) {
225+
if (node.typeParameters && node.typeParameters.params) {
226+
node.typeParameters.params.forEach(markTypeAnnotationAsUsed);
227+
}
228+
}
229+
218230
/**
219231
* Checks the given interface and marks it as used.
220232
* Generic arguments are also included in the check.
@@ -236,19 +248,22 @@ module.exports = {
236248
}
237249

238250
/**
239-
* Checks the given function return type and marks it as used.
251+
* Checks the given function and marks return types and type parameter constraints as used.
240252
* @param {ASTNode} node the relevant AST node.
241253
* @returns {void}
242254
* @private
243255
*/
244-
function markFunctionReturnTypeAsUsed(node) {
256+
function markFunctionOptionsAsUsed(node) {
257+
if (node.typeParameters && node.typeParameters.params) {
258+
node.typeParameters.params.forEach(markTypeAnnotationAsUsed);
259+
}
245260
if (node.returnType) {
246261
markTypeAnnotationAsUsed(node.returnType);
247262
}
248263
}
249264

250265
/**
251-
* Checks the given class and marks super classes, interfaces and decoratores as used.
266+
* Checks the given class and marks super classes, interfaces, type parameter constraints and decorators as used.
252267
* @param {ASTNode} node the relevant AST node.
253268
* @returns {void}
254269
* @private
@@ -286,17 +301,12 @@ module.exports = {
286301
}
287302
},
288303

289-
FunctionDeclaration: markFunctionReturnTypeAsUsed,
290-
FunctionExpression: markFunctionReturnTypeAsUsed,
291-
ArrowFunctionExpression: markFunctionReturnTypeAsUsed,
304+
FunctionDeclaration: markFunctionOptionsAsUsed,
305+
FunctionExpression: markFunctionOptionsAsUsed,
306+
ArrowFunctionExpression: markFunctionOptionsAsUsed,
292307

293-
CallExpression(node) {
294-
if (node.typeParameters && node.typeParameters.params) {
295-
node.typeParameters.params.forEach(
296-
markTypeAnnotationAsUsed
297-
);
298-
}
299-
},
308+
CallExpression: markExpressionAsUsed,
309+
NewExpression: markExpressionAsUsed,
300310

301311
Decorator: markDecoratorAsUsed,
302312
TSInterfaceHeritage: markExtendedInterfaceAsUsed,

tests/lib/rules/no-unused-vars.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ console.log(a);
220220
import { Nullable } from 'nullable';
221221
import { Component } from 'react';
222222
class Foo implements Component<Nullable>{};
223+
223224
new Foo();
224225
`,
225226
`
@@ -368,6 +369,38 @@ export const map: { [name in Foo]: Bar } = {
368369
b: 2,
369370
c: 3
370371
}
372+
`,
373+
`
374+
import { Nullable } from 'nullable';
375+
class A<T> {
376+
bar: T
377+
}
378+
new A<Nullable>();
379+
`,
380+
`
381+
import { Nullable } from 'nullable';
382+
import { SomeOther } from 'other';
383+
function foo<T extends Nullable>() {
384+
}
385+
foo<SomeOther>();
386+
`,
387+
`
388+
import { Nullable } from 'nullable';
389+
import { SomeOther } from 'other';
390+
class A<T extends Nullable> {
391+
bar: T;
392+
}
393+
new A<SomeOther>();
394+
`,
395+
`
396+
import { Nullable } from 'nullable';
397+
import { SomeOther } from 'other';
398+
interface A<T extends Nullable> {
399+
bar: T;
400+
}
401+
export const a: A<SomeOther> = {
402+
foo: "bar"
403+
};
371404
`
372405
],
373406

0 commit comments

Comments
 (0)