Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] fix race condition between namin…
Browse files Browse the repository at this point in the history
…g-convention and no-unused-vars (#2848)

Fixes #2844
  • Loading branch information
bradzacher committed Dec 4, 2020
1 parent fed89f2 commit ccb6b94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
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
@@ -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

0 comments on commit ccb6b94

Please sign in to comment.