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

Commit 159c325

Browse files
armano2JamesHenry
authored andcommitted
feat: fix issues in AST (#42)
changes: DeclareFunction to TSDeclareFunction changes: VariableDeclarator[kind='type'] to TSTypeAliasDeclaration makes abstract optional in interfaces BREAKING CHANGE: This changes the AST
1 parent f5dcd5d commit 159c325

17 files changed

+3450
-1950
lines changed

src/ast-node-types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
2626
ConditionalExpression: 'ConditionalExpression',
2727
ContinueStatement: 'ContinueStatement',
2828
DebuggerStatement: 'DebuggerStatement',
29-
DeclareFunction: 'DeclareFunction',
3029
Decorator: 'Decorator',
3130
DoWhileStatement: 'DoWhileStatement',
3231
EmptyStatement: 'EmptyStatement',
@@ -100,6 +99,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
10099
TSConstructorType: 'TSConstructorType',
101100
TSConstructSignature: 'TSConstructSignature',
102101
TSDeclareKeyword: 'TSDeclareKeyword',
102+
TSDeclareFunction: 'TSDeclareFunction',
103103
TSEnumDeclaration: 'TSEnumDeclaration',
104104
TSEnumMember: 'TSEnumMember',
105105
TSExportAssignment: 'TSExportAssignment',
@@ -132,6 +132,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
132132
TSStringKeyword: 'TSStringKeyword',
133133
TSSymbolKeyword: 'TSSymbolKeyword',
134134
TSTypeAnnotation: 'TSTypeAnnotation',
135+
TSTypeAliasDeclaration: 'TSTypeAliasDeclaration',
135136
TSTypeLiteral: 'TSTypeLiteral',
136137
TSTypeOperator: 'TSTypeOperator',
137138
TSTypeParameter: 'TSTypeParameter',

src/convert.ts

+29-37
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
694694

695695
case SyntaxKind.FunctionDeclaration: {
696696
let functionDeclarationType = AST_NODE_TYPES.FunctionDeclaration;
697-
698-
if (node.modifiers && node.modifiers.length) {
699-
const isDeclareFunction = nodeUtils.hasModifier(
700-
SyntaxKind.DeclareKeyword,
701-
node
702-
);
703-
if (isDeclareFunction) {
704-
functionDeclarationType = AST_NODE_TYPES.DeclareFunction;
705-
}
697+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
698+
functionDeclarationType = AST_NODE_TYPES.TSDeclareFunction;
706699
}
707700

708701
Object.assign(result, {
@@ -712,14 +705,18 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
712705
expression: false,
713706
async: nodeUtils.hasModifier(SyntaxKind.AsyncKeyword, node),
714707
params: convertParameters(node.parameters),
715-
body: convertChild(node.body)
708+
body: convertChild(node.body) || undefined
716709
});
717710

718711
// Process returnType
719712
if (node.type) {
720713
(result as any).returnType = convertTypeAnnotation(node.type);
721714
}
722715

716+
if (functionDeclarationType === AST_NODE_TYPES.TSDeclareFunction) {
717+
result.declare = true;
718+
}
719+
723720
// Process typeParameters
724721
if (node.typeParameters && node.typeParameters.length) {
725722
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
@@ -758,6 +755,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
758755
kind: nodeUtils.getDeclarationKind(node.declarationList)
759756
});
760757

758+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
759+
result.declare = true;
760+
}
761+
761762
// check for exports
762763
result = nodeUtils.fixExports(node, result as any, ast);
763764
break;
@@ -1618,6 +1619,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
16181619
);
16191620
}
16201621

1622+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
1623+
result.declare = true;
1624+
}
1625+
16211626
if (node.decorators) {
16221627
result.decorators = convertDecorators(node.decorators);
16231628
}
@@ -2245,40 +2250,26 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
22452250
additionalOptions
22462251
});
22472252

2248-
/**
2249-
* Convert TypeAliasDeclaration node into VariableDeclaration
2250-
* to allow core rules such as "semi" to work automatically
2251-
*/
22522253
case SyntaxKind.TypeAliasDeclaration: {
2253-
const typeAliasDeclarator = {
2254-
type: AST_NODE_TYPES.VariableDeclarator,
2254+
Object.assign(result, {
2255+
type: AST_NODE_TYPES.TSTypeAliasDeclaration,
22552256
id: convertChild(node.name),
2256-
init: convertChild(node.type),
2257-
range: [node.name.getStart(ast), node.end]
2258-
};
2257+
typeAnnotation: convertChild(node.type)
2258+
});
22592259

2260-
(typeAliasDeclarator as any).loc = nodeUtils.getLocFor(
2261-
typeAliasDeclarator.range[0],
2262-
typeAliasDeclarator.range[1],
2263-
ast
2264-
);
2260+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
2261+
result.declare = true;
2262+
}
22652263

22662264
// Process typeParameters
22672265
if (node.typeParameters && node.typeParameters.length) {
2268-
(typeAliasDeclarator as any).typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
2266+
(result as any).typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
22692267
node.typeParameters
22702268
);
22712269
}
22722270

2273-
Object.assign(result, {
2274-
type: AST_NODE_TYPES.VariableDeclaration,
2275-
kind: nodeUtils.getDeclarationKind(node),
2276-
declarations: [typeAliasDeclarator]
2277-
});
2278-
22792271
// check for exports
22802272
result = nodeUtils.fixExports(node, result as any, ast);
2281-
22822273
break;
22832274
}
22842275

@@ -2400,10 +2391,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24002391
}
24012392

24022393
const hasImplementsClause = interfaceHeritageClauses.length > 0;
2403-
const hasAbstractKeyword = nodeUtils.hasModifier(
2404-
SyntaxKind.AbstractKeyword,
2405-
node
2406-
);
24072394
const interfaceOpenBrace = nodeUtils.findNextToken(
24082395
interfaceLastClassToken,
24092396
ast,
@@ -2422,7 +2409,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24222409
};
24232410

24242411
Object.assign(result, {
2425-
abstract: hasAbstractKeyword,
24262412
type: AST_NODE_TYPES.TSInterfaceDeclaration,
24272413
body: interfaceBody,
24282414
id: convertChild(node.name),
@@ -2440,6 +2426,12 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24402426
if (node.decorators) {
24412427
result.decorators = convertDecorators(node.decorators);
24422428
}
2429+
if (nodeUtils.hasModifier(SyntaxKind.AbstractKeyword, node)) {
2430+
result.abstract = true;
2431+
}
2432+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
2433+
result.declare = true;
2434+
}
24432435
// check for exports
24442436
result = nodeUtils.fixExports(node, result as any, ast);
24452437

src/node-utils.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,8 @@ function isTypeKeyword(kind: number): boolean {
350350
* @param {ts.Node} node TypeScript AST node
351351
* @returns {string} declaration kind
352352
*/
353-
function getDeclarationKind(node: ts.Node): string {
353+
function getDeclarationKind(node: ts.Node): 'let' | 'const' | 'var' {
354354
switch (node.kind) {
355-
case SyntaxKind.TypeAliasDeclaration:
356-
return 'type';
357355
case SyntaxKind.VariableDeclarationList:
358356
if (node.flags & ts.NodeFlags.Let) {
359357
return 'let';

src/temp-types-based-on-js-source.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ESTreeNode {
4141
static?: boolean;
4242
export?: boolean;
4343
parameter?: any;
44+
abstract?: boolean;
4445
}
4546

4647
export interface ESTreeComment extends ESTreeNode {}

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

+21-17
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,6 @@ let fixturePatternConfigsToTest = [
395395
'class-with-implements-generic',
396396
'class-with-implements',
397397
'class-with-extends-and-implements',
398-
/**
399-
* Babylon: TSDeclareFunction + declare: true
400-
* tsep: DeclareFunction
401-
*/
402-
'declare-function',
403-
/**
404-
* Babylon: TSTypeReference + identifier
405-
* tsep: TSUnknownKeyword
406-
*/
407-
'unknown-type-annotation',
408398
/**
409399
* Other major AST differences (e.g. fundamentally different node types)
410400
*/
@@ -418,15 +408,9 @@ let fixturePatternConfigsToTest = [
418408
'interface-with-jsdoc',
419409
'interface-with-optional-properties',
420410
'interface-without-type-annotation',
421-
'type-alias-declaration-with-constrained-type-parameter',
422-
'type-alias-declaration',
423-
'type-alias-object-without-annotation',
424411
'typed-this',
425412
'export-type-function-declaration',
426-
'export-type-class-declaration',
427413
'abstract-interface',
428-
'export-type-alias-declaration',
429-
'unique-symbol',
430414
'keyof-operator',
431415
/**
432416
* tsep bug - Program.body[0].expression.left.properties[0].value.right is currently showing up
@@ -456,6 +440,8 @@ let fixturePatternConfigsToTest = [
456440
parseWithSourceTypeModule: [
457441
'export-named-enum',
458442
'export-assignment',
443+
'export-type-alias-declaration',
444+
'export-type-class-declaration',
459445
'export-default-class-with-generic',
460446
'export-default-class-with-multiple-generics',
461447
'export-named-class-with-generic',
@@ -483,7 +469,7 @@ let fixturePatternConfigsToTest = [
483469
fileType: 'ts',
484470
ignore: [
485471
/**
486-
* currently babylon not supported
472+
* there is difference in range between babel and tsep
487473
*/
488474
'tagged-template-expression-type-arguments'
489475
]
@@ -508,6 +494,24 @@ let fixturePatternConfigsToTest = [
508494
]
509495
}),
510496

497+
createFixturePatternConfigFor('typescript/declare', {
498+
fileType: 'ts',
499+
ignore: [
500+
/**
501+
* AST difference
502+
* tsep: TSAbstractClassDeclaration
503+
* babel: ClassDeclaration[abstract=true]
504+
*/
505+
'interface',
506+
/**
507+
* AST difference
508+
* tsep: heritage = []
509+
* babel: heritage = undefined
510+
*/
511+
'abstract-class'
512+
]
513+
}),
514+
511515
createFixturePatternConfigFor('typescript/namespaces-and-modules', {
512516
fileType: 'ts',
513517
ignore: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare abstract class Foo {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare class Foo {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare enum Foo {
2+
Bar,
3+
Baz
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare function foo(): void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare interface Foo {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module Foo {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare namespace Foo {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare type Foo = string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare var foo: any;

tests/lib/__snapshots__/javascript.ts.snap

-2
Original file line numberDiff line numberDiff line change
@@ -105760,7 +105760,6 @@ Object {
105760105760
"body": Array [
105761105761
Object {
105762105762
"async": false,
105763-
"body": null,
105764105763
"expression": false,
105765105764
"generator": false,
105766105765
"id": Object {
@@ -106077,7 +106076,6 @@ Object {
106077106076
"body": Array [
106078106077
Object {
106079106078
"async": false,
106080-
"body": null,
106081106079
"expression": false,
106082106080
"generator": false,
106083106081
"id": Object {

0 commit comments

Comments
 (0)