8000 fix(eslint-plugin): [no-shadow] handle false positives on generics and parameters by yeonjuan · Pull Request #5902 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [no-shadow] handle false positives on generics and parameters #5902

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 3 commits into from
Nov 13, 2022
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.
8000 Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ export default util.createRule<Options, MessageIds>({
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(
Expand Down
75 changes: 75 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ const ruleTester = new RuleTester({

ruleTester.run('no-shadow TS tests', rule, {
valid: [
'function foo<T = (arg: any) => any>(arg: T) {}',
'function foo<T = ([arg]: [any]) => any>(arg: T) {}',
'function foo<T = ({ args }: { args: any }) => any>(arg: T) {}',
'function foo<T = (...args: any[]) => any>(fn: T, args: any[]) {}',
'function foo<T extends (...args: any[]) => any>(fn: T, args: any[]) {}',
'function foo<T extends (...args: any[]) => any>(fn: T, ...args: any[]) {}',
'function foo<T extends ([args]: any[]) => any>(fn: T, args: any[]) {}',
'function foo<T extends ([...args]: any[]) => any>(fn: T, args: any[]) {}',
'function foo<T extends ({ args }: { args: any }) => any>(fn: T, args: any) {}',
`
function foo<T extends (id: string, ...args: any[]) => any>(
fn: T,
...args: any[]
) {}
`,
`
type Args = 1;
function foo<T extends (Args: any) => void>(arg: T) {}
`,
// nested conditional types
`
export type ArrayInput<Func> = Func extends (arg0: Array<infer T>) => any
Expand Down Expand Up @@ -375,6 +394,22 @@ type T = 1;
},
{
code: `
type T = 1;
function foo<T>(arg: T) {}
`,
errors: [
{
messageId: 'noShadow',
data: {
name: 'T',
shadowedLine: 2,
shadowedColumn: 6,
},
},
],
},
{
code: `
function foo<T>() {
return function <T>() {};
}
Expand All @@ -392,6 +427,22 @@ function foo<T>() {
},
{
code: `
type T = string;
function foo<T extends (arg: any) => void>(arg: T) {}
`,
errors: [
{
messageId: 'noShadow',
data: {
name: 'T',
shadowedLine: 2,
shadowedColumn: 6,
},
},
],
},
{
code: `
const x = 1;
{
type x = string;
Expand Down Expand Up @@ -703,5 +754,29 @@ let y;
},
],
},
{
code: `
function foo<T extends (...args: any[]) => any>(fn: T, args: any[]) {}
`,
options: [
{
ignoreTypeValueShadow: false,
builtinGlobals: true,
},
],
globals: {
args: 'writable',
},
errors: [
{
messageId: 'noShadowGlobal',
data: {
name: 'args',
shadowedLine: 2,
shadowedColumn: 5,
},
},
],
},
],
});
0