Skip to content

Commit b3c215a

Browse files
bradzacherJamesHenry
authored andcommitted
[FIX][no-unused-vars] assume constructor parameter args are used (typescript-eslint#173)
Fixes typescript-eslint#150 Whilst `TSParameterProperty`s are arguments, they are also technically class properties. `no-unused-vars` has no handling for class properties, so just mark them as used.
1 parent f38fe84 commit b3c215a

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

packages/eslint-plugin-typescript/lib/rules/no-unused-vars.js

+5
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ module.exports = {
301301
}
302302
},
303303

304+
TSParameterProperty(node) {
305+
// just assume parameter properties are used
306+
markVariableAsUsed(context, node.parameter.name);
307+
},
308+
304309
FunctionDeclaration: markFunctionOptionsAsUsed,
305310
FunctionExpression: markFunctionOptionsAsUsed,
306311
ArrowFunctionExpression: markFunctionOptionsAsUsed,

packages/eslint-plugin-typescript/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"docs:check": "eslint-docs check",
1818
"format": "prettier --write --tab-width 4 lib/**/*.js tests/**/*.js",
1919
"mocha": "mocha tests --recursive --reporter=dot",
20-
"test": "npm run lint && npm run mocha && npm run docs:check",
20+
"pretest": "npm run lint",
21+
"test": "mocha tests --recursive --reporter=dot",
22+
"posttest": "npm run docs:check",
2123
"precommit": "npm test && lint-staged"
2224
},
2325
"dependencies": {

packages/eslint-plugin-typescript/tests/lib/rules/no-unused-vars.js

+40
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,46 @@ interface A<T extends Nullable> {
401401
export const a: A<SomeOther> = {
402402
foo: "bar"
403403
};
404+
`,
405+
// https://github.com/nzakas/eslint-plugin-typescript/issues/150
406+
`
407+
export class App {
408+
constructor(private logger: Logger) {
409+
console.log(this.logger);
410+
}
411+
}
412+
`,
413+
`
414+
export class App {
415+
constructor(bar: string);
416+
constructor(private logger: Logger) {
417+
console.log(this.logger);
418+
}
419+
}
420+
`,
421+
`
422+
export class App {
423+
constructor(baz: string, private logger: Logger) {
424+
console.log(baz);
425+
console.log(this.logger);
426+
}
427+
}
428+
`,
429+
`
430+
export class App {
431+
constructor(baz: string, private logger: Logger, private bar: () => void) {
432+
console.log(this.logger);
433+
this.bar();
434+
}
435+
}
436+
`,
437+
`
438+
export class App {
439+
constructor(private logger: Logger) {}
440+
meth() {
441+
console.log(this.logger);
442+
}
443+
}
404444
`
405445
],
406446

0 commit comments

Comments
 (0)