Skip to content
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

fix(eslint-plugin): [no-unused-vars] fix race condition between naming-convention and no-unused-vars #2848

Merged
merged 1 commit into from
Dec 4, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,17 @@ export default util.createRule<Options, MessageIds>({
}
}

if (!hasRestSpreadSibling(variable)) {
unusedVariablesReturn.push(variable);
if (hasRestSpreadSibling(variable)) {
continue;
}

// in case another rule has run and used the collectUnusedVariables,
// we want to ensure our selectors that marked variables as used are respected
if (variable.eslintUsed) {
continue;
}

unusedVariablesReturn.push(variable);
}

return unusedVariablesReturn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import rule from '../../../src/rules/no-unused-vars';
import { collectUnusedVariables } from '../../../src/util';
import { noFormat, RuleTester } from '../../RuleTester';

const ruleTester = new RuleTester({
Expand All @@ -10,6 +11,12 @@ const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

// this is used to ensure that the caching the utility does does not impact the results done by no-unused-vars
ruleTester.defineRule('collect-unused-vars', context => {
collectUnusedVariables(context);
return {};
});

ruleTester.run('no-unused-vars', rule, {
valid: [
`
Expand Down Expand Up @@ -960,8 +967,17 @@ interface _Foo {
// ignored by pattern, even though it's only self-referenced
options: [{ varsIgnorePattern: '^_' }],
},
// https://github.com/ocavue/typescript-eslint-issue-2831/blob/master/index.ts
'export const classes = [class C1 {}, class C2 {}, class C3 {}];',
// https://github.com/typescript-eslint/typescript-eslint/issues/2844
`
/* eslint collect-unused-vars: "error" */
declare module 'next-auth' {
interface User {
id: string;
givenName: string;
familyName: string;
}
}
`,
],

invalid: [
Expand Down