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

Commit 3cb4850

Browse files
committed
feat: align call and construct signature declarations
BREAKING CHANGE
1 parent af73af2 commit 3cb4850

File tree

7 files changed

+1536
-11
lines changed

7 files changed

+1536
-11
lines changed

src/ast-node-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
108108
TSBigIntKeyword: 'TSBigIntKeyword',
109109
TSConditionalType: 'TSConditionalType',
110110
TSConstructorType: 'TSConstructorType',
111-
TSConstructSignature: 'TSConstructSignature',
111+
TSCallSignatureDeclaration: 'TSCallSignatureDeclaration',
112+
TSConstructSignatureDeclaration: 'TSConstructSignatureDeclaration',
112113
TSDeclareKeyword: 'TSDeclareKeyword',
113114
TSDeclareFunction: 'TSDeclareFunction',
114115
TSEnumDeclaration: 'TSEnumDeclaration',

src/convert.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,13 +2450,20 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24502450
break;
24512451
}
24522452

2453-
case SyntaxKind.ConstructSignature: {
2453+
case SyntaxKind.ConstructSignature:
2454+
case SyntaxKind.CallSignature: {
24542455
Object.assign(result, {
2455-
type: AST_NODE_TYPES.TSConstructSignature,
2456-
params: convertParameters(node.parameters),
2457-
typeAnnotation: node.type ? convertTypeAnnotation(node.type) : null
2456+
type:
2457+
node.kind === SyntaxKind.ConstructSignature
2458+
? AST_NODE_TYPES.TSConstructSignatureDeclaration
2459+
: AST_NODE_TYPES.TSCallSignatureDeclaration,
2460+
params: convertParameters(node.parameters)
24582461
});
24592462

2463+
if (node.type) {
2464+
(result as any).returnType = convertTypeAnnotation(node.type);
2465+
}
2466+
24602467
if (node.typeParameters) {
24612468
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
24622469
node.typeParameters

tests/ast-alignment/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ export function preprocessBabylonAST(ast: any): any {
142142
BooleanLiteral(node: any) {
143143
node.type = 'Literal';
144144
node.raw = String(node.value);
145+
},
146+
/**
147+
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
148+
*/
149+
TSCallSignatureDeclaration(node: any) {
150+
if (node.typeAnnotation) {
151+
node.returnType = node.typeAnnotation;
152+
delete node.typeAnnotation;
153+
}
154+
if (node.parameters) {
155+
node.params = node.parameters;
156+
delete node.parameters;
157+
}
158+
},
159+
/**
160+
* Awaiting feedback on Babel issue https://github.com/babel/babel/issues/9231
161+
*/
162+
TSConstructSignatureDeclaration(node: any) {
163+
if (node.typeAnnotation) {
164+
node.returnType = node.typeAnnotation;
165+
delete node.typeAnnotation;
166+
}
167+
if (node.parameters) {
168+
node.params = node.parameters;
169+
delete node.parameters;
170+
}
145171
}
146172
}
147173
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type foo = {
2+
<T>(a: string): string
3+
new<T>(a: string): string
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type foo = {
2+
(a: string): string
3+
new(a: string): string
4+
}

tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" en
16741674

16751675
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"`;
16761676

1677+
exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/call-signatures.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1678+
1679+
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"`;
1680+
16771681
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"`;
16781682

16791683
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"`;

0 commit comments

Comments
 (0)