From 769e8c8b9a51cd3448e47d13c7b0dab0468ee23c Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Mon, 14 Nov 2022 08:41:32 +0900 Subject: [PATCH] fix(eslint-plugin): [no-shadow] handle false positives on generics and parameters (#5902) * fix(eslint-plugin): [no-shadow] handle false positives on generics and parameters * Update packages/eslint-plugin/src/rules/no-shadow.ts Co-authored-by: Josh Goldberg --- packages/eslint-plugin/src/rules/no-shadow.ts | 5 +- .../tests/rules/no-shadow/no-shadow.test.ts | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 1f746a3201c..7d8eff0b9e2 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -147,8 +147,9 @@ export default util.createRule({ return false; } - const id = variable.identifiers[0]; - return util.isFunctionType(id.parent); + return variable.defs.every( + def => def.node.type === AST_NODE_TYPES.TSFunctionType, + ); } function isGenericOfStaticMethod( diff --git a/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts index 1097f466a18..1616051f0e3 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts @@ -12,6 +12,25 @@ const ruleTester = new RuleTester({ ruleTester.run('no-shadow TS tests', rule, { valid: [ + 'function foo any>(arg: T) {}', + 'function foo any>(arg: T) {}', + 'function foo any>(arg: T) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, ...args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any) {}', + ` +function foo any>( + fn: T, + ...args: any[] +) {} + `, + ` +type Args = 1; +function foo void>(arg: T) {} + `, // nested conditional types ` export type ArrayInput = Func extends (arg0: Array) => any @@ -375,6 +394,22 @@ type T = 1; }, { code: ` +type T = 1; +function foo(arg: T) {} + `, + errors: [ + { + messageId: 'noShadow', + data: { + name: 'T', + shadowedLine: 2, + shadowedColumn: 6, + }, + }, + ], + }, + { + code: ` function foo() { return function () {}; } @@ -392,6 +427,22 @@ function foo() { }, { code: ` +type T = string; +function foo void>(arg: T) {} + `, + errors: [ + { + messageId: 'noShadow', + data: { + name: 'T', + shadowedLine: 2, + shadowedColumn: 6, + }, + }, + ], + }, + { + code: ` const x = 1; { type x = string; @@ -703,5 +754,29 @@ let y; }, ], }, + { + code: ` +function foo any>(fn: T, args: any[]) {} + `, + options: [ + { + ignoreTypeValueShadow: false, + builtinGlobals: true, + }, + ], + globals: { + args: 'writable', + }, + errors: [ + { + messageId: 'noShadowGlobal', + data: { + name: 'args', + shadowedLine: 2, + shadowedColumn: 5, + }, + }, + ], + }, ], });