Skip to content

Commit

Permalink
fix(eslint-plugin): [no-shadow] report correctly on parameters of fun…
Browse files Browse the repository at this point in the history
…ctions declared with the `declare` keyword (typescript-eslint#10543)

* initial implementation

* filter out only parameters

* remove redundant test

* simplify test

* fix the implementation to respect the 'ignoreFunctionTypeParameterNameValueShadow' flag

* add TSConstructSignatureDeclaration

* also TSConstructorType
  • Loading branch information
ronami authored Dec 31, 2024
1 parent c27399b commit cde2f97
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const allowedFunctionVariableDefTypes = new Set([
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSMethodSignature,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSDeclareFunction,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSConstructorType,
]);

export default createRule<Options, MessageIds>({
Expand Down
168 changes: 168 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 @@ -203,6 +203,120 @@ interface Test {
},
{
code: `
const arg = 0;
declare function test(arg: string): typeof arg;
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
const arg = 0;
declare const test: (arg: string) => typeof arg;
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
const arg = 0;
declare class Test {
p1(arg: string): typeof arg;
}
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
const arg = 0;
declare const Test: {
new (arg: string): typeof arg;
};
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
const arg = 0;
type Bar = new (arg: number) => typeof arg;
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
const arg = 0;
declare namespace Lib {
function test(arg: string): typeof arg;
}
`,
errors: [
{
data: {
name: 'arg',
shadowedColumn: 7,
shadowedLine: 2,
},
messageId: 'noShadow',
},
],
options: [{ ignoreFunctionTypeParameterNameValueShadow: false }],
},
{
code: `
import type { foo } from './foo';
function doThing(foo: number) {}
`,
Expand Down Expand Up @@ -617,6 +731,60 @@ const arg = 0;
interface Test {
p1(arg: string): typeof arg;
}
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
declare function test(arg: string): typeof arg;
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
declare const test: (arg: string) => typeof arg;
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
declare class Test {
p1(arg: string): typeof arg;
}
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
declare const Test: {
new (arg: string): typeof arg;
};
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
type Bar = new (arg: number) => typeof arg;
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
},
{
code: `
const arg = 0;
declare namespace Lib {
function test(arg: string): typeof arg;
}
`,
options: [{ ignoreFunctionTypeParameterNameValueShadow: true }],
Expand Down

0 comments on commit cde2f97

Please sign in to comment.