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

feat: update TSFunctionType and TSConstructorType nodes #91

Merged
merged 6 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
*/
function convertTypeAnnotation(child: ts.TypeNode): ESTreeNode {
const annotation = convertChildType(child);
const annotationStartCol = child.getFullStart() - 1;
// in FunctionType and ConstructorType typeAnnotation has 2 characters `=>` and in other places is just colon
const offset =
node.kind === SyntaxKind.FunctionType ||
node.kind === SyntaxKind.ConstructorType
? 2
: 1;
const annotationStartCol = child.getFullStart() - offset;

const loc = nodeUtils.getLocFor(annotationStartCol, child.end, ast);
return {
type: AST_NODE_TYPES.TSTypeAnnotation,
Expand Down Expand Up @@ -2439,14 +2446,28 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {

break;
}

case SyntaxKind.ConstructorType:
case SyntaxKind.FunctionType:
case SyntaxKind.ConstructSignature:
case SyntaxKind.CallSignature: {
let type: AST_NODE_TYPES;
switch (node.kind) {
case SyntaxKind.ConstructSignature:
type = AST_NODE_TYPES.TSConstructSignatureDeclaration;
break;
case SyntaxKind.CallSignature:
type = AST_NODE_TYPES.TSCallSignatureDeclaration;
break;
case SyntaxKind.FunctionType:
type = AST_NODE_TYPES.TSFunctionType;
break;
case SyntaxKind.ConstructorType:
default:
type = AST_NODE_TYPES.TSConstructorType;
break;
}
Object.assign(result, {
type:
node.kind === SyntaxKind.ConstructSignature
? AST_NODE_TYPES.TSConstructSignatureDeclaration
: AST_NODE_TYPES.TSCallSignatureDeclaration,
type: type,
params: convertParameters(node.parameters)
});

Expand Down
10 changes: 8 additions & 2 deletions tests/ast-alignment/fixtures-to-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ tester.addFixturePatternConfig('typescript/basics', {
'interface-with-optional-properties',
'interface-without-type-annotation',
'typed-this',
'export-type-function-declaration',
'abstract-interface',
/**
* Babel bug for optional or abstract methods?
Expand Down Expand Up @@ -480,7 +479,14 @@ tester.addFixturePatternConfig('typescript/errorRecovery', {
});

tester.addFixturePatternConfig('typescript/types', {
fileType: 'ts'
fileType: 'ts',
ignore: [
/**
* AST difference
*/
'function-with-rest',
'constructor-with-rest'
]
});

tester.addFixturePatternConfig('typescript/declare', {
Expand Down
26 changes: 26 additions & 0 deletions tests/ast-alignment/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,32 @@ export function preprocessBabylonAST(ast: any): any {
node.params = node.parameters;
delete node.parameters;
}
},
/**
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
*/
TSFunctionType(node: any) {
if (node.typeAnnotation) {
node.returnType = node.typeAnnotation;
delete node.typeAnnotation;
}
if (node.parameters) {
node.params = node.parameters;
delete node.parameters;
}
},
/**
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
*/
TSConstructorType(node: any) {
if (node.typeAnnotation) {
node.returnType = node.typeAnnotation;
delete node.typeAnnotation;
}
if (node.parameters) {
node.params = node.parameters;
delete node.parameters;
}
}
}
);
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/constructor-generic.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: new <T>(a: T) => T;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: Array<new () => string>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: new (...a: number[]) => void;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/constructor.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: new (a: number, b?: number) => void;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/function-generic.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: <T>(a: T) => T;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/function-in-generic.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x: Array<() => void>;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/function-with-rest.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: (...a: number[]) => void;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/function-with-this.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: (this: number) => void;
1 change: 1 addition & 0 deletions tests/fixtures/typescript/types/function.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let f: (a: number, b?: number) => void;
18 changes: 18 additions & 0 deletions tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,24 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/conditional-with-null.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/constructor.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/constructor-generic.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/constructor-in-generic.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/constructor-with-rest.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/function.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/function-generic.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/function-in-generic.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/function-with-rest.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/function-with-this.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/indexed.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/intersection-type.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Loading