Skip to content

fix(eslint-plugin): improve detection of used vars in heritage #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/eslint-plugin/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ module.exports = Object.assign({}, baseRule, {
}
}

/**
* Mark heritage clause as used
* @param node The node currently being traversed
* @returns {void}
*/
function markHeritageAsUsed(node) {
switch (node.type) {
case 'Identifier':
context.markVariableAsUsed(node.name);
break;
case 'MemberExpression':
markHeritageAsUsed(node.object);
break;
case 'CallExpression':
markHeritageAsUsed(node.callee);
break;
}
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand All @@ -54,8 +73,15 @@ module.exports = Object.assign({}, baseRule, {
'TSTypeReference Identifier'(node) {
context.markVariableAsUsed(node.name);
},
'TSClassImplements Identifier'(node) {
context.markVariableAsUsed(node.name);
TSInterfaceHeritage(node) {
if (node.expression) {
markHeritageAsUsed(node.expression);
}
},
TSClassImplements(node) {
if (node.expression) {
markHeritageAsUsed(node.expression);
}
},
'TSParameterProperty Identifier'(node) {
// just assume parameter properties are used
Expand Down
65 changes: 65 additions & 0 deletions packages/eslint-plugin/tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@ declare var Foo: {
new (value?: any): Object,
foo(): string
}
`,
`
import foo from 'foo';
export interface Bar extends foo.i18n {}
`,
`
import foo from 'foo';
import bar from 'foo';
export interface Bar extends foo.i18n<bar> {}
`
],

Expand Down Expand Up @@ -763,6 +772,62 @@ enum FormFieldIds {
column: 6
}
]
},
{
code: `
import test from 'test';
import baz from 'baz';
export interface Bar extends baz.test {}
`,
errors: [
{
message: "'test' is defined but never used.",
line: 2,
column: 8
}
]
},
{
code: `
import test from 'test';
import baz from 'baz';
export interface Bar extends baz().test {}
`,
errors: [
{
message: "'test' is defined but never used.",
line: 2,
column: 8
}
]
},
{
code: `
import test from 'test';
import baz from 'baz';
export class Bar implements baz.test {}
`,
errors: [
{
message: "'test' is defined but never used.",
line: 2,
column: 8
}
]
},
{
code: `
import test from 'test';
import baz from 'baz';
export class Bar implements baz().test {}
`,
errors: [
{
message: "'test' is defined but never used.",
line: 2,
column: 8
}
]
}
]
});