Skip to content

Commit

Permalink
refactor(ts-estree): improve definition of parameters (#209)
Browse files Browse the repository at this point in the history
- add missing tests for decorators
  • Loading branch information
armano2 committed Feb 5, 2019
1 parent d4dfa3b commit c1abae9
Show file tree
Hide file tree
Showing 10 changed files with 7,118 additions and 2,695 deletions.
4,876 changes: 3,533 additions & 1,343 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
bar(@special(true) [ bar ]: any) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
bar(@special(true) { bar }: any) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
bar(@special(true) ...foo: any) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@dec
function b(){}
15 changes: 8 additions & 7 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,19 @@ export class Converter {
*/
private convertParameters(
parameters: ts.NodeArray<ts.ParameterDeclaration>
): (es.TSParameterProperty | es.RestElement | es.AssignmentPattern)[] {
): es.Parameter[] {
if (!parameters || !parameters.length) {
return [];
}
return parameters.map(param => {
const convertedParam = this.convertChild(param);
if (!param.decorators || !param.decorators.length) {
return convertedParam;
const convertedParam = this.convertChild(param) as es.Parameter;

if (param.decorators && param.decorators.length) {
convertedParam.decorators = param.decorators.map(el =>
this.convertChild(el)
);
}
return Object.assign(convertedParam, {
decorators: param.decorators.map(el => this.convertChild(el))
});
return convertedParam;
});
}

Expand Down
14 changes: 13 additions & 1 deletion packages/typescript-estree/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,13 @@ export type ObjectLiteralElementLike =
| RestElement
| SpreadElement
| TSAbstractMethodDefinition;
export type Parameter = AssignmentPattern | RestElement | TSParameterProperty;
export type Parameter =
| AssignmentPattern
| RestElement
| ArrayPattern
| ObjectPattern
| Identifier
| TSParameterProperty;
export type PrimaryExpression =
| ArrayExpression
| ArrayPattern
Expand Down Expand Up @@ -541,6 +547,7 @@ export interface ArrayPattern extends BaseNode {
elements: Expression[];
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
}

export interface ArrowFunctionExpression extends BaseNode {
Expand All @@ -565,6 +572,7 @@ export interface AssignmentPattern extends BaseNode {
right?: Expression;
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
}

export interface AwaitExpression extends BaseNode {
Expand Down Expand Up @@ -715,6 +723,7 @@ export interface Identifier extends BaseNode {
name: string;
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
}

export interface IfStatement extends BaseNode {
Expand Down Expand Up @@ -875,6 +884,7 @@ export interface ObjectPattern extends BaseNode {
properties: ObjectLiteralElementLike[];
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
decorators?: Decorator[];
}

export interface Program extends BaseNode {
Expand All @@ -901,6 +911,7 @@ export interface RestElement extends BaseNode {
typeAnnotation?: TSTypeAnnotation;
optional?: boolean;
value?: AssignmentPattern;
decorators?: Decorator[];
}

export interface ReturnStatement extends BaseNode {
Expand Down Expand Up @@ -1218,6 +1229,7 @@ export interface TSParameterProperty extends BaseNode {
static?: boolean;
export?: boolean;
parameter: AssignmentPattern | BindingName | RestElement;
decorators?: Decorator[];
}

export interface TSParenthesizedType extends BaseNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,15 @@ tester.addFixturePatternConfig('typescript/decorators/method-decorators', {
fileType: 'ts'
});
tester.addFixturePatternConfig('typescript/decorators/parameter-decorators', {
fileType: 'ts'
fileType: 'ts',
ignore: [
/**
* babel does not support decorators on array and rest parameters
* TODO: report this to babel
*/
'parameter-array-pattern-decorator',
'parameter-rest-element-decorator'
]
});
tester.addFixturePatternConfig('typescript/decorators/property-decorators', {
fileType: 'ts'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/method-decorators/method-decorator-static-member.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-array-pattern-decorator.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand All @@ -2246,6 +2248,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-object-pattern-decorator.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/parameter-decorators/parameter-rest-element-decorator.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/property-decorators/property-decorator-factory-instance-member.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/decorators/property-decorators/property-decorator-factory-static-member.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down Expand Up @@ -2299,6 +2305,15 @@ Object {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/decorator-on-function.src 1`] = `
Object {
"column": 0,
"index": 0,
"lineNumber": 1,
"message": "Decorators are not valid here.",
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/decorator-on-interface-declaration.src 1`] = `
Object {
"column": 0,
Expand Down

0 comments on commit c1abae9

Please sign in to comment.