Skip to content

Commit 3306198

Browse files
armano2JamesHenry
authored andcommitted
fix(ts-estree): align typeArguments and typeParameters across nodes (#223)
1 parent 690bff3 commit 3306198

13 files changed

+4605
-12
lines changed

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

+2,284
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new Foo<>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function f1<>() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class foo {
2+
constructor<>() {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const foo = function<>() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface foo {
2+
test<>();
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class foo {
2+
test<>() {}
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function f1<>() {}

packages/typescript-estree/src/convert.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ export class Converter {
690690
}
691691

692692
// Process typeParameters
693-
if (node.typeParameters && node.typeParameters.length) {
693+
if (node.typeParameters) {
694694
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
695695
node.typeParameters
696696
);
@@ -897,7 +897,7 @@ export class Converter {
897897
}
898898

899899
// Process typeParameters
900-
if (node.typeParameters && node.typeParameters.length) {
900+
if (node.typeParameters) {
901901
method.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
902902
node.typeParameters
903903
);
@@ -1003,7 +1003,7 @@ export class Converter {
10031003
});
10041004

10051005
// Process typeParameters
1006-
if (node.typeParameters && node.typeParameters.length) {
1006+
if (node.typeParameters) {
10071007
constructor.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
10081008
node.typeParameters
10091009
);
@@ -1060,7 +1060,7 @@ export class Converter {
10601060
}
10611061

10621062
// Process typeParameters
1063-
if (node.typeParameters && node.typeParameters.length) {
1063+
if (node.typeParameters) {
10641064
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
10651065
node.typeParameters
10661066
);
@@ -1159,7 +1159,7 @@ export class Converter {
11591159
}
11601160

11611161
// Process typeParameters
1162-
if (node.typeParameters && node.typeParameters.length) {
1162+
if (node.typeParameters) {
11631163
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
11641164
node.typeParameters
11651165
);
@@ -1368,7 +1368,7 @@ export class Converter {
13681368
}
13691369
}
13701370

1371-
if (node.typeParameters && node.typeParameters.length) {
1371+
if (node.typeParameters) {
13721372
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
13731373
node.typeParameters
13741374
);
@@ -1640,7 +1640,7 @@ export class Converter {
16401640
callee: this.convertChild(node.expression),
16411641
arguments: node.arguments.map(el => this.convertChild(el))
16421642
});
1643-
if (node.typeArguments && node.typeArguments.length) {
1643+
if (node.typeArguments) {
16441644
result.typeParameters = this.convertTypeArgumentsToTypeParameters(
16451645
node.typeArguments
16461646
);
@@ -1656,7 +1656,7 @@ export class Converter {
16561656
? node.arguments.map(el => this.convertChild(el))
16571657
: []
16581658
});
1659-
if (node.typeArguments && node.typeArguments.length) {
1659+
if (node.typeArguments) {
16601660
result.typeParameters = this.convertTypeArgumentsToTypeParameters(
16611661
node.typeArguments
16621662
);
@@ -2060,7 +2060,7 @@ export class Converter {
20602060
}
20612061

20622062
// Process typeParameters
2063-
if (node.typeParameters && node.typeParameters.length) {
2063+
if (node.typeParameters) {
20642064
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
20652065
node.typeParameters
20662066
);
@@ -2216,7 +2216,7 @@ export class Converter {
22162216
expression: this.convertChild(node.expression)
22172217
});
22182218

2219-
if (node.typeArguments && node.typeArguments.length) {
2219+
if (node.typeArguments) {
22202220
result.typeParameters = this.convertTypeArgumentsToTypeParameters(
22212221
node.typeArguments
22222222
);
@@ -2236,7 +2236,7 @@ export class Converter {
22362236
id: this.convertChild(node.name)
22372237
});
22382238

2239-
if (node.typeParameters && node.typeParameters.length) {
2239+
if (node.typeParameters) {
22402240
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
22412241
node.typeParameters
22422242
);

packages/typescript-estree/tests/ast-alignment/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ export function preprocessBabylonAST(ast: any): any {
262262
/**
263263
* babel issue: ranges of typeParameters are not included in FunctionExpression range
264264
*/
265-
if (node.typeParameters) {
265+
if (
266+
node.typeParameters &&
267+
node.typeParameters.range[0] < node.range[0]
268+
) {
266269
node.range[0] = node.typeParameters.range[0];
267270
node.loc.start = Object.assign({}, node.typeParameters.loc.start);
268271
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,22 @@ Object {
20752075

20762076
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-arguments.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
20772077

2078+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-arguments-in-call-expression.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2079+
2080+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-arguments-in-new-expression.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2081+
2082+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2083+
2084+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters-in-arrow-function.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2085+
2086+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters-in-constructor.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2087+
2088+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters-in-function-expression.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2089+
2090+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters-in-method.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2091+
2092+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/empty-type-parameters-in-method-signature.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2093+
20782094
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/enum-with-keywords.src 1`] = `
20792095
Object {
20802096
"column": 7,

0 commit comments

Comments
 (0)