Skip to content

Commit c1abae9

Browse files
authored
refactor(ts-estree): improve definition of parameters (typescript-eslint#209)
- add missing tests for decorators
1 parent d4dfa3b commit c1abae9

File tree

10 files changed

+7118
-2695
lines changed

10 files changed

+7118
-2695
lines changed

packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Lines changed: 3533 additions & 1343 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
bar(@special(true) [ bar ]: any) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
bar(@special(true) { bar }: any) {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
bar(@special(true) ...foo: any) {}
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@dec
2+
function b(){}

packages/typescript-estree/src/convert.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,19 @@ export class Converter {
282282
*/
283283
private convertParameters(
284284
parameters: ts.NodeArray<ts.ParameterDeclaration>
285-
): (es.TSParameterProperty | es.RestElement | es.AssignmentPattern)[] {
285+
): es.Parameter[] {
286286
if (!parameters || !parameters.length) {
287287
return [];
288288
}
289289
return parameters.map(param => {
290-
const convertedParam = this.convertChild(param);
291-
if (!param.decorators || !param.decorators.length) {
292-
return convertedParam;
290+
const convertedParam = this.convertChild(param) as es.Parameter;
291+
292+
if (param.decorators && param.decorators.length) {
293+
convertedParam.decorators = param.decorators.map(el =>
294+
this.convertChild(el)
295+
);
293296
}
294-
return Object.assign(convertedParam, {
295-
decorators: param.decorators.map(el => this.convertChild(el))
296-
});
297+
return convertedParam;
297298
});
298299
}
299300

packages/typescript-estree/src/typedefs.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,13 @@ export type ObjectLiteralElementLike =
351351
| RestElement
352352
| SpreadElement
353353
| TSAbstractMethodDefinition;
354-
export type Parameter = AssignmentPattern | RestElement | TSParameterProperty;
354+
export type Parameter =
355+
| AssignmentPattern
356+
| RestElement
357+
| ArrayPattern
358+
| ObjectPattern
359+
| Identifier
360+
| TSParameterProperty;
355361
export type PrimaryExpression =
356362
| ArrayExpression
357363
| ArrayPattern
@@ -541,6 +547,7 @@ export interface ArrayPattern extends BaseNode {
541547
elements: Expression[];
542548
typeAnnotation?: TSTypeAnnotation;
543549
optional?: boolean;
550+
decorators?: Decorator[];
544551
}
545552

546553
export interface ArrowFunctionExpression extends BaseNode {
@@ -565,6 +572,7 @@ export interface AssignmentPattern extends BaseNode {
565572
right?: Expression;
566573
typeAnnotation?: TSTypeAnnotation;
567574
optional?: boolean;
575+
decorators?: Decorator[];
568576
}
569577

570578
export interface AwaitExpression extends BaseNode {
@@ -715,6 +723,7 @@ export interface Identifier extends BaseNode {
715723
name: string;
716724
typeAnnotation?: TSTypeAnnotation;
717725
optional?: boolean;
726+
decorators?: Decorator[];
718727
}
719728

720729
export interface IfStatement extends BaseNode {
@@ -875,6 +884,7 @@ export interface ObjectPattern extends BaseNode {
875884
properties: ObjectLiteralElementLike[];
876885
typeAnnotation?: TSTypeAnnotation;
877886
optional?: boolean;
887+
decorators?: Decorator[];
878888
}
879889

880890
export interface Program extends BaseNode {
@@ -901,6 +911,7 @@ export interface RestElement extends BaseNode {
901911
typeAnnotation?: TSTypeAnnotation;
902912
optional?: boolean;
903913
value?: AssignmentPattern;
914+
decorators?: Decorator[];
904915
}
905916

906917
export interface ReturnStatement extends BaseNode {
@@ -1218,6 +1229,7 @@ export interface TSParameterProperty extends BaseNode {
12181229
static?: boolean;
12191230
export?: boolean;
12201231
parameter: AssignmentPattern | BindingName | RestElement;
1232+
decorators?: Decorator[];
12211233
}
12221234

12231235
export interface TSParenthesizedType extends BaseNode {

packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,15 @@ tester.addFixturePatternConfig('typescript/decorators/method-decorators', {
396396
fileType: 'ts'
397397
});
398398
tester.addFixturePatternConfig('typescript/decorators/parameter-decorators', {
399-
fileType: 'ts'
399+
fileType: 'ts',
400+
ignore: [
401+
/**
402+
* babel does not support decorators on array and rest parameters
403+
* TODO: report this to babel
404+
*/
405+
'parameter-array-pattern-decorator',
406+
'parameter-rest-element-decorator'
407+
]
400408
});
401409
tester.addFixturePatternConfig('typescript/decorators/property-decorators', {
402410
fileType: 'ts'

packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
22362236

22372237
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"`;
22382238

2239+
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"`;
2240+
22392241
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"`;
22402242

22412243
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"`;
@@ -2246,6 +2248,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
22462248

22472249
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"`;
22482250

2251+
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"`;
2252+
2253+
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"`;
2254+
22492255
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"`;
22502256

22512257
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"`;
@@ -2299,6 +2305,15 @@ Object {
22992305
}
23002306
`;
23012307

2308+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/decorator-on-function.src 1`] = `
2309+
Object {
2310+
"column": 0,
2311+
"index": 0,
2312+
"lineNumber": 1,
2313+
"message": "Decorators are not valid here.",
2314+
}
2315+
`;
2316+
23022317
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/decorator-on-interface-declaration.src 1`] = `
23032318
Object {
23042319
"column": 0,

0 commit comments

Comments
 (0)