Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 7032f96

Browse files
armano2JamesHenry
authored andcommitted
feat: update TSFunctionType and TSConstructorType nodes (#91)
BREAKING CHANGE: This changes the AST for TSFunctionType and TSConstructorType nodes
1 parent 7682eda commit 7032f96

14 files changed

+4502
-26
lines changed

src/convert.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
122122
*/
123123
function convertTypeAnnotation(child: ts.TypeNode): ESTreeNode {
124124
const annotation = convertChildType(child);
125-
const annotationStartCol = child.getFullStart() - 1;
125+
// in FunctionType and ConstructorType typeAnnotation has 2 characters `=>` and in other places is just colon
126+
const offset =
127+
node.kind === SyntaxKind.FunctionType ||
128+
node.kind === SyntaxKind.ConstructorType
129+
? 2
130+
: 1;
131+
const annotationStartCol = child.getFullStart() - offset;
132+
126133
const loc = nodeUtils.getLocFor(annotationStartCol, child.end, ast);
127134
return {
128135
type: AST_NODE_TYPES.TSTypeAnnotation,
@@ -2439,14 +2446,28 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24392446

24402447
break;
24412448
}
2442-
2449+
case SyntaxKind.ConstructorType:
2450+
case SyntaxKind.FunctionType:
24432451
case SyntaxKind.ConstructSignature:
24442452
case SyntaxKind.CallSignature: {
2453+
let type: AST_NODE_TYPES;
2454+
switch (node.kind) {
2455+
case SyntaxKind.ConstructSignature:
2456+
type = AST_NODE_TYPES.TSConstructSignatureDeclaration;
2457+
break;
2458+
case SyntaxKind.CallSignature:
2459+
type = AST_NODE_TYPES.TSCallSignatureDeclaration;
2460+
break;
2461+
case SyntaxKind.FunctionType:
2462+
type = AST_NODE_TYPES.TSFunctionType;
2463+
break;
2464+
case SyntaxKind.ConstructorType:
2465+
default:
2466+
type = AST_NODE_TYPES.TSConstructorType;
2467+
break;
2468+
}
24452469
Object.assign(result, {
2446-
type:
2447-
node.kind === SyntaxKind.ConstructSignature
2448-
? AST_NODE_TYPES.TSConstructSignatureDeclaration
2449-
: AST_NODE_TYPES.TSCallSignatureDeclaration,
2470+
type: type,
24502471
params: convertParameters(node.parameters)
24512472
});
24522473

tests/ast-alignment/fixtures-to-test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ tester.addFixturePatternConfig('typescript/basics', {
384384
'interface-with-optional-properties',
385385
'interface-without-type-annotation',
386386
'typed-this',
387-
'export-type-function-declaration',
388387
'abstract-interface',
389388
/**
390389
* Babel bug for optional or abstract methods?
@@ -480,7 +479,14 @@ tester.addFixturePatternConfig('typescript/errorRecovery', {
480479
});
481480

482481
tester.addFixturePatternConfig('typescript/types', {
483-
fileType: 'ts'
482+
fileType: 'ts',
483+
ignore: [
484+
/**
485+
* AST difference
486+
*/
487+
'function-with-rest',
488+
'constructor-with-rest'
489+
]
484490
});
485491

486492
tester.addFixturePatternConfig('typescript/declare', {

tests/ast-alignment/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ export function preprocessBabylonAST(ast: any): any {
168168
node.params = node.parameters;
169169
delete node.parameters;
170170
}
171+
},
172+
/**
173+
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
174+
*/
175+
TSFunctionType(node: any) {
176+
if (node.typeAnnotation) {
177+
node.returnType = node.typeAnnotation;
178+
delete node.typeAnnotation;
179+
}
180+
if (node.parameters) {
181+
node.params = node.parameters;
182+
delete node.parameters;
183+
}
184+
},
185+
/**
186+
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
187+
*/
188+
TSConstructorType(node: any) {
189+
if (node.typeAnnotation) {
190+
node.returnType = node.typeAnnotation;
191+
delete node.typeAnnotation;
192+
}
193+
if (node.parameters) {
194+
node.params = node.parameters;
195+
delete node.parameters;
196+
}
171197
}
172198
}
173199
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f: new <T>(a: T) => T;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x: Array<new () => string>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f: new (...a: number[]) => void;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f: new (a: number, b?: number) => void;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f: <T>(a: T) => T;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let x: Array<() => void>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let f: (...a: number[]) => void;

0 commit comments

Comments
 (0)