Skip to content

Commit d532dea

Browse files
committed
Support type params in CallExpression, NewExpression (TS)
Already supported in Flow. Fixes #343
1 parent 53123a2 commit d532dea

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

def/typescript.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,16 @@ export default function (fork: Fork) {
484484
def("TSDeclareMethod"),
485485
TSTypeMember
486486
)]);
487+
488+
def("CallExpression")
489+
.field("typeParameters", or(
490+
null,
491+
def("TSTypeParameterInstantiation"),
492+
), defaults["null"]);
493+
494+
def("NewExpression")
495+
.field("typeParameters", or(
496+
null,
497+
def("TSTypeParameterInstantiation"),
498+
), defaults["null"]);
487499
};

gen/builders.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ export interface NewExpressionBuilder {
584584
callee: K.ExpressionKind,
585585
comments?: K.CommentKind[] | null,
586586
loc?: K.SourceLocationKind | null,
587-
typeArguments?: null | K.TypeParameterInstantiationKind
587+
typeArguments?: null | K.TypeParameterInstantiationKind,
588+
typeParameters?: null | K.TSTypeParameterInstantiationKind
588589
}
589590
): namedTypes.NewExpression;
590591
}
@@ -601,7 +602,8 @@ export interface CallExpressionBuilder {
601602
comments?: K.CommentKind[] | null,
602603
loc?: K.SourceLocationKind | null,
603604
optional?: boolean,
604-
typeArguments?: null | K.TypeParameterInstantiationKind
605+
typeArguments?: null | K.TypeParameterInstantiationKind,
606+
typeParameters?: null | K.TSTypeParameterInstantiationKind
605607
}
606608
): namedTypes.CallExpression;
607609
}
@@ -1192,7 +1194,8 @@ export interface OptionalCallExpressionBuilder {
11921194
comments?: K.CommentKind[] | null,
11931195
loc?: K.SourceLocationKind | null,
11941196
optional?: boolean,
1195-
typeArguments?: null | K.TypeParameterInstantiationKind
1197+
typeArguments?: null | K.TypeParameterInstantiationKind,
1198+
typeParameters?: null | K.TSTypeParameterInstantiationKind
11961199
}
11971200
): namedTypes.OptionalCallExpression;
11981201
}

gen/namedTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,15 @@ export namespace namedTypes {
303303
callee: K.ExpressionKind;
304304
arguments: (K.ExpressionKind | K.SpreadElementKind)[];
305305
typeArguments?: null | K.TypeParameterInstantiationKind;
306+
typeParameters?: null | K.TSTypeParameterInstantiationKind;
306307
}
307308

308309
export interface CallExpression extends Omit<Expression, "type">, Omit<ChainElement, "type"> {
309310
type: "CallExpression";
310311
callee: K.ExpressionKind;
311312
arguments: (K.ExpressionKind | K.SpreadElementKind)[];
312313
typeArguments?: null | K.TypeParameterInstantiationKind;
314+
typeParameters?: null | K.TSTypeParameterInstantiationKind;
313315
}
314316

315317
export interface RestElement extends Omit<Pattern, "type"> {

test/typescript.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ glob("**/*.ts", {
166166
});
167167
});
168168
});
169+
});
170+
171+
describe('typescript types', () => {
172+
it('Explicit type arguments', function () {
173+
const program = babelParse([
174+
'test<A>();',
175+
'test<B, C>();',
176+
'new test<D>();',
177+
'new test<E, F>();',
178+
].join("\n"), {
179+
plugins: ['typescript']
180+
});
181+
182+
const typeParamNames: any[] = []
183+
184+
visit(program, {
185+
visitTSTypeReference(path: any) {
186+
typeParamNames.push(path.node.typeName.name);
187+
this.traverse(path);
188+
}
189+
});
190+
191+
assert.deepEqual(typeParamNames, ["A", "B", "C", "D", "E", "F"]);
192+
});
169193

170194
describe('scope', () => {
171195
const scope = [

0 commit comments

Comments
 (0)