10000 fix(parser): fix crash when visiting decorators in parameters by armano2 · Pull Request #237 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(parser): fix crash when visiting decorators in parameters #237

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 5 commits into from
Feb 12, 2019
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.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ export default util.createRule({
create(context) {
const rules = baseRule.create(context);

/**
* Mark this function parameter as used
* @param node The node currently being traversed
*/
function markThisParameterAsUsed(node: TSESTree.Identifier): void {
if (node.name) {
const variable = context
.getScope()
.variables.find(scopeVar => scopeVar.name === node.name);

if (variable) {
variable.eslintUsed = true;
}
}
}

/**
* Mark heritage clause as used
* @param node The node currently being traversed
Expand All @@ -59,8 +43,6 @@ export default util.createRule({
}

return Object.assign({}, rules, {
"FunctionDeclaration Identifier[name='this']": markThisParameterAsUsed,
"FunctionExpression Identifier[name='this']": markThisParameterAsUsed,
'TSTypeReference Identifier'(node: TSESTree.Identifier) {
context.markVariableAsUsed(node.name);
},
Expand Down
6 changes: 6 additions & 0 deletions packages/eslint-plugin/tests/eslint-rules/no-shadow.test.ts
< 8000 tr data-hunk="b5e03e3eb4aa916d0851de3c9b63f0715ea3f648f5843ac26383c4683cbbf735" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function bar(foo: any) {}
`
export abstract class Foo {}
export class FooBar extends Foo {}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/207
`
function test(this: Foo) {
function test2(this: Bar) {}
}
`
],
invalid: []
Expand Down
28 changes: 0 additions & 28 deletions packages/eslint-plugin/tests/eslint-rules/no-unused-vars.test.ts

This file was deleted.

31 changes: 30 additions & 1 deletion packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,36 @@ export function Foo() {
);
}
`
}
},
// https://github.com/eslint/typescript-eslint-parser/issues/535
`
import { observable } from 'mobx';
export default class ListModalStore {
@observable
orderList: IObservableArray<BizPurchaseOrderTO> = observable([]);
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/122#issuecomment-462008078
`
import { Dec, TypeA, Class } from 'test';
export default class Foo {
constructor(
@Dec(Class)
private readonly prop: TypeA<Class>,
) {}
}
`,
`
import { Dec, TypeA, Class } from 'test';
export default class Foo {
constructor(
@Dec(Class)
...prop: TypeA<Class>,
) {
prop()
}
}
`
],

invalid: [
Expand Down
24 changes: 18 additions & 6 deletions packages/parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PatternVisitorCallback,
PatternVisitorOptions
} from 'eslint-scope/lib/options';
import { TSESTree } from '@typescript-eslint/typescript-estree';
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';

/**
* Define the override function of `Scope#__define` for global augmentation.
Expand Down Expand Up @@ -90,6 +90,13 @@ class PatternVisitor extends OriginalPatternVisitor {
this.rightHandNodes.push(node.typeAnnotation);
}
}

TSParameterProperty(node: TSESTree.TSParameterProperty): void {
this.visit(node.parameter);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
}
}

class Referencer extends OriginalReferencer {
Expand Down Expand Up @@ -182,11 +189,16 @@ class Referencer extends OriginalReferencer {
params[i],
{ processRightHandNodes: true },
(pattern, info) => {
innerScope.__define(
pattern,
new ParameterDefinition(pattern, node, i, info.rest)
);
this.referencingDefaultValue(pattern, info.assignments, null, true);
if (
pattern.type !== AST_NODE_TYPES.Identifier ||
pattern.name !== 'this'
) {
innerScope.__define(
pattern,
new ParameterDefinition(pattern, node, i, info.rest)
);
this.referencingDefaultValue(pattern, info.assignments, null, true);
}
}
);
}
Expand Down
11 changes: 6 additions & 5 deletions packages/parser/src/visitor-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
// Additional estree nodes.
Import: [],
// Additional Properties.
ArrayPattern: ['elements', 'typeAnnotation'],
ArrayPattern: ['decorators', 'elements', 'typeAnnotati B422 on'],
ArrowFunctionExpression: ['typeParameters', 'params', 'returnType', 'body'],
ClassDeclaration: [
'decorators',
Expand All @@ -24,12 +24,13 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
'implements',
'body'
],
TaggedTemplateExpression: ['tag', 'typeParameters', 'quasi'],
FunctionDeclaration: ['id', 'typeParameters', 'params', 'returnType', 'body'],
FunctionExpression: ['id', 'typeParameters', 'params', 'returnType', 'body'],
Identifier: ['decorators', 'typeAnnotation'],
MethodDefinition: ['decorators', 'key', 'value'],
ObjectPattern: ['properties', 'typeAnnotation'],
RestElement: ['argument', 'typeAnnotation'],
ObjectPattern: ['decorators', 'properties', 'typeAnnotation'],
RestElement: ['decorators', 'argument', 'typeAnnotation'],
NewExpression: ['callee', 'typeParameters', 'arguments'],
CallExpression: ['callee', 'typeParameters', 'arguments'],
// JSX
Expand All @@ -56,7 +57,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
TSConditionalType: ['checkType', 'extendsType', 'trueType', 'falseType'],
TSConstructSignatureDeclaration: ['typeParameters', 'params', 'returnType'],
TSConstructorType: ['typeParameters', 'params', 'returnType'],
TSDeclareFunction: ['id', 'typeParameters', 'params', 'returnType'],
TSDeclareFunction: ['id', 'typeParameters', 'params', 'returnType', 'body'],
TSDeclareKeyword: [],
TSEmptyBodyFunctionExpression: [
'id',
Expand Down Expand Up @@ -91,7 +92,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
TSNumberKeyword: [],
TSObjectKeyword: [],
TSOptionalType: ['typeAnnotation'],
TSParameterProperty: ['parameter'],
TSParameterProperty: ['decorators', 'parameter'],
TSParenthesizedType: ['typeAnnotation'],
TSPrivateKeyword: [],
TSPropertySignature: ['typeAnnotation', 'key', 'initializer'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
constructor(@Dec []: string[]) {}
}
F438
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
constructor(@Dec test: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
constructor(@Dec {}: any) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
constructor(@Dec private readonly test: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class Foo {
constructor(@Dec ...test: string[]) {}
}
Loading
0