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

Commit

Permalink
feat: align call and construct signature declarations (#74)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This changes the AST
  • Loading branch information
armano2 authored and JamesHenry committed Jan 4, 2019
1 parent af73af2 commit 3ec7fce
Show file tree
Hide file tree
Showing 7 changed files with 1,536 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
TSBigIntKeyword: 'TSBigIntKeyword',
TSConditionalType: 'TSConditionalType',
TSConstructorType: 'TSConstructorType',
TSConstructSignature: 'TSConstructSignature',
TSCallSignatureDeclaration: 'TSCallSignatureDeclaration',
TSConstructSignatureDeclaration: 'TSConstructSignatureDeclaration',
TSDeclareKeyword: 'TSDeclareKeyword',
TSDeclareFunction: 'TSDeclareFunction',
TSEnumDeclaration: 'TSEnumDeclaration',
Expand Down
15 changes: 11 additions & 4 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2450,13 +2450,20 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
break;
}

case SyntaxKind.ConstructSignature: {
case SyntaxKind.ConstructSignature:
case SyntaxKind.CallSignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSConstructSignature,
params: convertParameters(node.parameters),
typeAnnotation: node.type ? convertTypeAnnotation(node.type) : null
type:
node.kind === SyntaxKind.ConstructSignature
? AST_NODE_TYPES.TSConstructSignatureDeclaration
: AST_NODE_TYPES.TSCallSignatureDeclaration,
params: convertParameters(node.parameters)
});

if (node.type) {
(result as any).returnType = convertTypeAnnotation(node.type);
}

if (node.typeParameters) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters
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 @@ -142,6 +142,32 @@ export function preprocessBabylonAST(ast: any): any {
BooleanLiteral(node: any) {
node.type = 'Literal';
node.raw = String(node.value);
},
/**
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
*/
TSCallSignatureDeclaration(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
*/
TSConstructSignatureDeclaration(node: any) {
if (node.typeAnnotation) {
node.returnType = node.typeAnnotation;
delete node.typeAnnotation;
}
if (node.parameters) {
node.params = node.parameters;
delete node.parameters;
}
}
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type foo = {
<T>(a: string): string
new<T>(a: string): string
}
4 changes: 4 additions & 0 deletions tests/fixtures/typescript/basics/call-signatures.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type foo = {
(a: string): string
new(a: string): string
}
4 changes: 4 additions & 0 deletions tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" en

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/async-function-with-var-declaration.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/call-signatures.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/call-signatures-with-generics.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/cast-as-expression.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/cast-as-multi.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Loading

0 comments on commit 3ec7fce

Please sign in to comment.