Skip to content

Commit 225fc26

Browse files
authored
fix(parser): fix crash when visiting decorators in parameters (typescript-eslint#237)
- fix visiting decorators in parameters - fix issue with this in no-shadow
1 parent cc8f906 commit 225fc26

13 files changed

+5326
-2790
lines changed

Diff for: packages/eslint-plugin/src/rules/no-unused-vars.ts

-18
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,6 @@ export default util.createRule({
2424
create(context) {
2525
const rules = baseRule.create(context);
2626

27-
/**
28-
* Mark this function parameter as used
29-
* @param node The node currently being traversed
30-
*/
31-
function markThisParameterAsUsed(node: TSESTree.Identifier): void {
32-
if (node.name) {
33-
const variable = context
34-
.getScope()
35-
.variables.find(scopeVar => scopeVar.name === node.name);
36-
37-
if (variable) {
38-
variable.eslintUsed = true;
39-
}
40-
}
41-
}
42-
4327
/**
4428
* Mark heritage clause as used
4529
* @param node The node currently being traversed
@@ -59,8 +43,6 @@ export default util.createRule({
5943
}
6044

6145
return Object.assign({}, rules, {
62-
"FunctionDeclaration Identifier[name='this']": markThisParameterAsUsed,
63-
"FunctionExpression Identifier[name='this']": markThisParameterAsUsed,
6446
'TSTypeReference Identifier'(node: TSESTree.Identifier) {
6547
context.markVariableAsUsed(node.name);
6648
},

Diff for: packages/eslint-plugin/tests/eslint-rules/no-shadow.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function bar(foo: any) {}
2121
`
2222
export abstract class Foo {}
2323
export class FooBar extends Foo {}
24+
`,
25+
// https://github.com/typescript-eslint/typescript-eslint/issues/207
26+
`
27+
function test(this: Foo) {
28+
function test2(this: Bar) {}
29+
}
2430
`
2531
],
2632
invalid: []

Diff for: packages/eslint-plugin/tests/eslint-rules/no-unused-vars.test.ts

-28
This file was deleted.

Diff for: packages/eslint-plugin/tests/rules/no-unused-vars.test.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,36 @@ export function Foo() {
577577
);
578578
}
579579
`
580-
}
580+
},
581+
// https://github.com/eslint/typescript-eslint-parser/issues/535
582+
`
583+
import { observable } from 'mobx';
584+
export default class ListModalStore {
585+
@observable
586+
orderList: IObservableArray<BizPurchaseOrderTO> = observable([]);
587+
}
588+
`,
589+
// https://github.com/typescript-eslint/typescript-eslint/issues/122#issuecomment-462008078
590+
`
591+
import { Dec, TypeA, Class } from 'test';
592+
export default class Foo {
593+
constructor(
594+
@Dec(Class)
595+
private readonly prop: TypeA<Class>,
596+
) {}
597+
}
598+
`,
599+
`
600+
import { Dec, TypeA, Class } from 'test';
601+
export default class Foo {
602+
constructor(
603+
@Dec(Class)
604+
...prop: TypeA<Class>,
605+
) {
606+
prop()
607+
}
608+
}
609+
`
581610
],
582611

583612
invalid: [

Diff for: packages/parser/src/analyze-scope.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
PatternVisitorCallback,
1212
PatternVisitorOptions
1313
} from 'eslint-scope/lib/options';
14-
import { TSESTree } from '@typescript-eslint/typescript-estree';
14+
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
1515

1616
/**
1717
* Define the override function of `Scope#__define` for global augmentation.
@@ -90,6 +90,13 @@ class PatternVisitor extends OriginalPatternVisitor {
9090
this.rightHandNodes.push(node.typeAnnotation);
9191
}
9292
}
93+
94+
TSParameterProperty(node: TSESTree.TSParameterProperty): void {
95+
this.visit(node.parameter);
96+
if (node.decorators) {
97+
this.rightHandNodes.push(...node.decorators);
98+
}
99+
}
93100
}
94101

95102
class Referencer extends OriginalReferencer {
@@ -182,11 +189,16 @@ class Referencer extends OriginalReferencer {
182189
params[i],
183190
{ processRightHandNodes: true },
184191
(pattern, info) => {
185-
innerScope.__define(
186-
pattern,
187-
new ParameterDefinition(pattern, node, i, info.rest)
188-
);
189-
this.referencingDefaultValue(pattern, info.assignments, null, true);
192+
if (
193+
pattern.type !== AST_NODE_TYPES.Identifier ||
194+
pattern.name !== 'this'
195+
) {
196+
innerScope.__define(
197+
pattern,
198+
new ParameterDefinition(pattern, node, i, info.rest)
199+
);
200+
this.referencingDefaultValue(pattern, info.assignments, null, true);
201+
}
190202
}
191203
);
192204
}

Diff for: packages/parser/src/visitor-keys.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
44
// Additional estree nodes.
55
Import: [],
66
// Additional Properties.
7-
ArrayPattern: ['elements', 'typeAnnotation'],
7+
ArrayPattern: ['decorators', 'elements', 'typeAnnotation'],
88
ArrowFunctionExpression: ['typeParameters', 'params', 'returnType', 'body'],
99
ClassDeclaration: [
1010
'decorators',
@@ -24,12 +24,13 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
2424
'implements',
2525
'body'
2626
],
27+
TaggedTemplateExpression: ['tag', 'typeParameters', 'quasi'],
2728
FunctionDeclaration: ['id', 'typeParameters', 'params', 'returnType', 'body'],
2829
FunctionExpression: ['id', 'typeParameters', 'params', 'returnType', 'body'],
2930
Identifier: ['decorators', 'typeAnnotation'],
3031
MethodDefinition: ['decorators', 'key', 'value'],
31-
ObjectPattern: ['properties', 'typeAnnotation'],
32-
RestElement: ['argument', 'typeAnnotation'],
32+
ObjectPattern: ['decorators', 'properties', 'typeAnnotation'],
33+
RestElement: ['decorators', 'argument', 'typeAnnotation'],
3334
NewExpression: ['callee', 'typeParameters', 'arguments'],
3435
CallExpression: ['callee', 'typeParameters', 'arguments'],
3536
// JSX
@@ -56,7 +57,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
5657
TSConditionalType: ['checkType', 'extendsType', 'trueType', 'falseType'],
5758
TSConstructSignatureDeclaration: ['typeParameters', 'params', 'returnType'],
5859
TSConstructorType: ['typeParameters', 'params', 'returnType'],
59-
TSDeclareFunction: ['id', 'typeParameters', 'params', 'returnType'],
60+
TSDeclareFunction: ['id', 'typeParameters', 'params', 'returnType', 'body'],
6061
TSDeclareKeyword: [],
6162
TSEmptyBodyFunctionExpression: [
6263
'id',
@@ -91,7 +92,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
9192
TSNumberKeyword: [],
9293
TSObjectKeyword: [],
9394
TSOptionalType: ['typeAnnotation'],
94-
TSParameterProperty: ['parameter'],
95+
TSParameterProperty: ['decorators', 'parameter'],
9596
TSParenthesizedType: ['typeAnnotation'],
9697
TSPrivateKeyword: [],
9798
TSPropertySignature: ['typeAnnotation', 'key', 'initializer'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Foo {
2+
constructor(@Dec []: string[]) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Foo {
2+
constructor(@Dec test: string) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Foo {
2+
constructor(@Dec {}: any) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Foo {
2+
constructor(@Dec private readonly test: string) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Foo {
2+
constructor(@Dec ...test: string[]) {}
3+
}

0 commit comments

Comments
 (0)