Skip to content

Commit 418ed38

Browse files
committed
Support type params in CallExpression, NewExpression (TS)
Already supported in Flow. Fixes #343
1 parent c208a17 commit 418ed38

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
@@ -472,4 +472,16 @@ export default function (fork: Fork) {
472472
def("TSDeclareMethod"),
473473
TSTypeMember
474474
)]);
475+
476+
def("CallExpression")
477+
.field("typeParameters", or(
478+
null,
479+
def("TSTypeParameterInstantiation"),
480+
), defaults["null"]);
481+
482+
def("NewExpression")
483+
.field("typeParameters", or(
484+
null,
485+
def("TSTypeParameterInstantiation"),
486+
), defaults["null"]);
475487
};

gen/builders.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ export interface NewExpressionBuilder {
581581
callee: K.ExpressionKind,
582582
comments?: K.CommentKind[] | null,
583583
loc?: K.SourceLocationKind | null,
584-
typeArguments?: null | K.TypeParameterInstantiationKind
584+
typeArguments?: null | K.TypeParameterInstantiationKind,
585+
typeParameters?: null | K.TSTypeParameterInstantiationKind
585586
}
586587
): namedTypes.NewExpression;
587588
}
@@ -597,7 +598,8 @@ export interface CallExpressionBuilder {
597598
callee: K.ExpressionKind,
598599
comments?: K.CommentKind[] | null,
599600
loc?: K.SourceLocationKind | null,
600-
typeArguments?: null | K.TypeParameterInstantiationKind
601+
typeArguments?: null | K.TypeParameterInstantiationKind,
602+
typeParameters?: null | K.TSTypeParameterInstantiationKind
601603
}
602604
): namedTypes.CallExpression;
603605
}
@@ -3365,7 +3367,8 @@ export interface OptionalCallExpressionBuilder {
33653367
comments?: K.CommentKind[] | null,
33663368
loc?: K.SourceLocationKind | null,
33673369
optional?: boolean,
3368-
typeArguments?: null | K.TypeParameterInstantiationKind
3370+
typeArguments?: null | K.TypeParameterInstantiationKind,
3371+
typeParameters?: null | K.TSTypeParameterInstantiationKind
33693372
}
33703373
): namedTypes.OptionalCallExpression;
33713374
}

gen/namedTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,15 @@ export namespace namedTypes {
298298
callee: K.ExpressionKind;
299299
arguments: (K.ExpressionKind | K.SpreadElementKind)[];
300300
typeArguments?: null | K.TypeParameterInstantiationKind;
301+
typeParameters?: null | K.TSTypeParameterInstantiationKind;
301302
}
302303

303304
export interface CallExpression extends Omit<Expression, "type"> {
304305
type: "CallExpression";
305306
callee: K.ExpressionKind;
306307
arguments: (K.ExpressionKind | K.SpreadElementKind)[];
307308
typeArguments?: null | K.TypeParameterInstantiationKind;
309+
typeParameters?: null | K.TSTypeParameterInstantiationKind;
308310
}
309311

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

test/typescript.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@ glob("**/*.ts", {
149149
});
150150
});
151151
});
152+
});
153+
154+
describe('typescript types', () => {
155+
it('Explicit type arguments', function () {
156+
const program = babelParse([
157+
'test<A>();',
158+
'test<B, C>();',
159+
'new test<D>();',
160+
'new test<E, F>();',
161+
].join("\n"), {
162+
plugins: ['typescript']
163+
});
164+
165+
const typeParamNames: any[] = []
166+
167+
visit(program, {
168+
visitTSTypeReference(path: any) {
169+
typeParamNames.push(path.node.typeName.name);
170+
this.traverse(path);
171+
}
172+
});
173+
174+
assert.deepEqual(typeParamNames, ["A", "B", "C", "D", "E", "F"]);
175+
});
152176

153177
describe('scope', () => {
154178
const scope = [

0 commit comments

Comments
 (0)