Skip to content

Commit 5a9788c

Browse files
authored
fix(scope-manager): visit params decorator before nest scope (#10475)
1 parent 75837fc commit 5a9788c

5 files changed

Lines changed: 124 additions & 14 deletions

File tree

‎packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,58 @@ export const x = _Foo;
11161116
},
11171117
],
11181118
},
1119+
{
1120+
code: `
1121+
const command = (): ParameterDecorator => {
1122+
return () => {};
1123+
};
1124+
1125+
export class Foo {
1126+
bar(@command() command: string) {}
1127+
}
1128+
`,
1129+
errors: [
1130+
{
1131+
data: {
1132+
action: 'defined',
1133+
additional: '',
1134+
varName: 'command',
1135+
},
1136+
line: 7,
1137+
messageId: 'unusedVar',
1138+
},
1139+
],
1140+
},
1141+
{
1142+
code: `
1143+
declare const deco: () => ParameterDecorator;
1144+
1145+
export class Foo {
1146+
bar(@deco() deco, @deco() param) {}
1147+
}
1148+
`,
1149+
errors: [
1150+
{
1151+
column: 15,
1152+
data: {
1153+
action: 'defined',
1154+
additional: '',
1155+
varName: 'deco',
1156+
},
1157+
line: 5,
1158+
messageId: 'unusedVar',
1159+
},
1160+
{
1161+
data: {
1162+
action: 'defined',
1163+
additional: '',
1164+
varName: 'param',
1165+
},
1166+
line: 5,
1167+
messageId: 'unusedVar',
1168+
},
1169+
],
1170+
},
11191171
],
11201172

11211173
valid: [
@@ -2274,5 +2326,16 @@ export enum Foo {
22742326
`,
22752327
options: [{ reportUsedIgnorePattern: true, varsIgnorePattern: '_' }],
22762328
},
2329+
`
2330+
const command = (): ParameterDecorator => {
2331+
return () => {};
2332+
};
2333+
2334+
export class Foo {
2335+
bar(@command() command: string) {
2336+
console.log(command);
2337+
}
2338+
}
2339+
`,
22772340
],
22782341
});

‎packages/scope-manager/src/referencer/ClassVisitor.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class ClassVisitor extends Visitor {
115115
this.#referencer.scopeManager.nestFunctionExpressionNameScope(node);
116116
}
117117

118+
node.params.forEach(param => {
119+
param.decorators.forEach(d => this.visit(d));
120+
});
121+
118122
// Consider this function is in the MethodDefinition.
119123
this.#referencer.scopeManager.nestFunctionScope(node, true);
120124

@@ -205,7 +209,6 @@ class ClassVisitor extends Visitor {
205209
{ processRightHandNodes: true },
206210
);
207211
this.visitFunctionParameterTypeAnnotation(param);
208-
param.decorators.forEach(d => this.visit(d));
209212
}
210213

211214
this.visitType(node.returnType);

‎packages/scope-manager/tests/fixtures/decorators/parameter-property.ts.shot‎

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
function decorator() {}
22
class A {
3-
foo(@decorator a, @decorator [b], @decorator { c }, @decorator d = 1) {}
3+
foo(
4+
@decorator a,
5+
@decorator [b],
6+
@decorator { c },
7+
@decorator d = 1,
8+
@decorator decorator,
9+
@d decorator,
10+
) {}
411
}

‎packages/scope-manager/tests/fixtures/decorators/parameter.ts.shot‎

Lines changed: 43 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)