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

Commit

Permalink
feat: fix issues in AST (#42)
Browse files Browse the repository at this point in the history
changes: DeclareFunction to TSDeclareFunction
changes: VariableDeclarator[kind='type'] to TSTypeAliasDeclaration
makes abstract optional in interfaces

BREAKING CHANGE: This changes the AST
  • Loading branch information
armano2 authored and JamesHenry committed Dec 19, 2018
1 parent f5dcd5d commit 159c325
Show file tree
Hide file tree
Showing 17 changed files with 3,450 additions and 1,950 deletions.
3 changes: 2 additions & 1 deletion src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
ConditionalExpression: 'ConditionalExpression',
ContinueStatement: 'ContinueStatement',
DebuggerStatement: 'DebuggerStatement',
DeclareFunction: 'DeclareFunction',
Decorator: 'Decorator',
DoWhileStatement: 'DoWhileStatement',
EmptyStatement: 'EmptyStatement',
Expand Down Expand Up @@ -100,6 +99,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
TSConstructorType: 'TSConstructorType',
TSConstructSignature: 'TSConstructSignature',
TSDeclareKeyword: 'TSDeclareKeyword',
TSDeclareFunction: 'TSDeclareFunction',
TSEnumDeclaration: 'TSEnumDeclaration',
TSEnumMember: 'TSEnumMember',
TSExportAssignment: 'TSExportAssignment',
Expand Down Expand Up @@ -132,6 +132,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
TSStringKeyword: 'TSStringKeyword',
TSSymbolKeyword: 'TSSymbolKeyword',
TSTypeAnnotation: 'TSTypeAnnotation',
TSTypeAliasDeclaration: 'TSTypeAliasDeclaration',
TSTypeLiteral: 'TSTypeLiteral',
TSTypeOperator: 'TSTypeOperator',
TSTypeParameter: 'TSTypeParameter',
Expand Down
66 changes: 29 additions & 37 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,15 +694,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {

case SyntaxKind.FunctionDeclaration: {
let functionDeclarationType = AST_NODE_TYPES.FunctionDeclaration;

if (node.modifiers && node.modifiers.length) {
const isDeclareFunction = nodeUtils.hasModifier(
SyntaxKind.DeclareKeyword,
node
);
if (isDeclareFunction) {
functionDeclarationType = AST_NODE_TYPES.DeclareFunction;
}
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
functionDeclarationType = AST_NODE_TYPES.TSDeclareFunction;
}

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

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

if (functionDeclarationType === AST_NODE_TYPES.TSDeclareFunction) {
result.declare = true;
}

// Process typeParameters
if (node.typeParameters && node.typeParameters.length) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
Expand Down Expand Up @@ -758,6 +755,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
kind: nodeUtils.getDeclarationKind(node.declarationList)
});

if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
result.declare = true;
}

// check for exports
result = nodeUtils.fixExports(node, result as any, ast);
break;
Expand Down Expand Up @@ -1618,6 +1619,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
);
}

if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
result.declare = true;
}

if (node.decorators) {
result.decorators = convertDecorators(node.decorators);
}
Expand Down Expand Up @@ -2245,40 +2250,26 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
additionalOptions
});

/**
* Convert TypeAliasDeclaration node into VariableDeclaration
* to allow core rules such as "semi" to work automatically
*/
case SyntaxKind.TypeAliasDeclaration: {
const typeAliasDeclarator = {
type: AST_NODE_TYPES.VariableDeclarator,
Object.assign(result, {
type: AST_NODE_TYPES.TSTypeAliasDeclaration,
id: convertChild(node.name),
init: convertChild(node.type),
range: [node.name.getStart(ast), node.end]
};
typeAnnotation: convertChild(node.type)
});

(typeAliasDeclarator as any).loc = nodeUtils.getLocFor(
typeAliasDeclarator.range[0],
typeAliasDeclarator.range[1],
ast
);
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
result.declare = true;
}

// Process typeParameters
if (node.typeParameters && node.typeParameters.length) {
(typeAliasDeclarator as any).typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
(result as any).typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters
);
}

Object.assign(result, {
type: AST_NODE_TYPES.VariableDeclaration,
kind: nodeUtils.getDeclarationKind(node),
declarations: [typeAliasDeclarator]
});

// check for exports
result = nodeUtils.fixExports(node, result as any, ast);

break;
}

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

const hasImplementsClause = interfaceHeritageClauses.length > 0;
const hasAbstractKeyword = nodeUtils.hasModifier(
SyntaxKind.AbstractKeyword,
node
);
const interfaceOpenBrace = nodeUtils.findNextToken(
interfaceLastClassToken,
ast,
Expand All @@ -2422,7 +2409,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
};

Object.assign(result, {
abstract: hasAbstractKeyword,
type: AST_NODE_TYPES.TSInterfaceDeclaration,
body: interfaceBody,
id: convertChild(node.name),
Expand All @@ -2440,6 +2426,12 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
if (node.decorators) {
result.decorators = convertDecorators(node.decorators);
}
if (nodeUtils.hasModifier(SyntaxKind.AbstractKeyword, node)) {
result.abstract = true;
}
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
result.declare = true;
}
// check for exports
result = nodeUtils.fixExports(node, result as any, ast);

Expand Down
4 changes: 1 addition & 3 deletions src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,8 @@ function isTypeKeyword(kind: number): boolean {
* @param {ts.Node} node TypeScript AST node
* @returns {string} declaration kind
*/
function getDeclarationKind(node: ts.Node): string {
function getDeclarationKind(node: ts.Node): 'let' | 'const' | 'var' {
switch (node.kind) {
case SyntaxKind.TypeAliasDeclaration:
return 'type';
case SyntaxKind.VariableDeclarationList:
if (node.flags & ts.NodeFlags.Let) {
return 'let';
Expand Down
1 change: 1 addition & 0 deletions src/temp-types-based-on-js-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ESTreeNode {
static?: boolean;
export?: boolean;
parameter?: any;
abstract?: boolean;
}

export interface ESTreeComment extends ESTreeNode {}
Expand Down
38 changes: 21 additions & 17 deletions tests/ast-alignment/fixtures-to-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,16 +395,6 @@ let fixturePatternConfigsToTest = [
'class-with-implements-generic',
'class-with-implements',
'class-with-extends-and-implements',
/**
* Babylon: TSDeclareFunction + declare: true
* tsep: DeclareFunction
*/
'declare-function',
/**
* Babylon: TSTypeReference + identifier
* tsep: TSUnknownKeyword
*/
'unknown-type-annotation',
/**
* Other major AST differences (e.g. fundamentally different node types)
*/
Expand All @@ -418,15 +408,9 @@ let fixturePatternConfigsToTest = [
'interface-with-jsdoc',
'interface-with-optional-properties',
'interface-without-type-annotation',
'type-alias-declaration-with-constrained-type-parameter',
'type-alias-declaration',
'type-alias-object-without-annotation',
'typed-this',
'export-type-function-declaration',
'export-type-class-declaration',
'abstract-interface',
'export-type-alias-declaration',
'unique-symbol',
'keyof-operator',
/**
* tsep bug - Program.body[0].expression.left.properties[0].value.right is currently showing up
Expand Down Expand Up @@ -456,6 +440,8 @@ let fixturePatternConfigsToTest = [
parseWithSourceTypeModule: [
'export-named-enum',
'export-assignment',
'export-type-alias-declaration',
'export-type-class-declaration',
'export-default-class-with-generic',
'export-default-class-with-multiple-generics',
'export-named-class-with-generic',
Expand Down Expand Up @@ -483,7 +469,7 @@ let fixturePatternConfigsToTest = [
fileType: 'ts',
ignore: [
/**
* currently babylon not supported
* there is difference in range between babel and tsep
*/
'tagged-template-expression-type-arguments'
]
Expand All @@ -508,6 +494,24 @@ let fixturePatternConfigsToTest = [
]
}),

createFixturePatternConfigFor('typescript/declare', {
fileType: 'ts',
ignore: [
/**
* AST difference
* tsep: TSAbstractClassDeclaration
* babel: ClassDeclaration[abstract=true]
*/
'interface',
/**
* AST difference
* tsep: heritage = []
* babel: heritage = undefined
*/
'abstract-class'
]
}),

createFixturePatternConfigFor('typescript/namespaces-and-modules', {
fileType: 'ts',
ignore: [
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/typescript/declare/abstract-class.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare abstract class Foo {

}
3 changes: 3 additions & 0 deletions tests/fixtures/typescript/declare/class.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare class Foo {

}
4 changes: 4 additions & 0 deletions tests/fixtures/typescript/declare/enum.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare enum Foo {
Bar,
Baz
}
1 change: 1 addition & 0 deletions tests/fixtures/typescript/declare/function.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare function foo(): void
3 changes: 3 additions & 0 deletions tests/fixtures/typescript/declare/interface.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare interface Foo {

}
3 changes: 3 additions & 0 deletions tests/fixtures/typescript/declare/module.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module Foo {

}
3 changes: 3 additions & 0 deletions tests/fixtures/typescript/declare/namespace.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare namespace Foo {

}
1 change: 1 addition & 0 deletions tests/fixtures/typescript/declare/type-alias.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare type Foo = string
1 change: 1 addition & 0 deletions tests/fixtures/typescript/declare/variable.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare var foo: any;
2 changes: 0 additions & 2 deletions tests/lib/__snapshots__/javascript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105760,7 +105760,6 @@ Object {
"body": Array [
Object {
"async": false,
"body": null,
"expression": false,
"generator": false,
"id": Object {
Expand Down Expand Up @@ -106077,7 +106076,6 @@ Object {
"body": Array [
Object {
"async": false,
"body": null,
"expression": false,
"generator": false,
"id": Object {
Expand Down
Loading

0 comments on commit 159c325

Please sign in to comment.