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

Commit 7507043

Browse files
committed
feat: fix issues in AST
1 parent f083824 commit 7507043

16 files changed

+2189
-391
lines changed

src/ast-node-types.ts

+1-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',
@@ -99,6 +98,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
9998
TSBooleanKeyword: 'TSBooleanKeyword',
10099
TSConstructorType: 'TSConstructorType',
101100
TSConstructSignature: 'TSConstructSignature',
101+
TSDeclareFunction: 'TSDeclareFunction',
102102
TSDeclareKeyword: 'TSDeclareKeyword',
103103
TSEnumDeclaration: 'TSEnumDeclaration',
104104
TSEnumMember: 'TSEnumMember',

src/convert.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
676676

677677
case SyntaxKind.FunctionDeclaration: {
678678
let functionDeclarationType = AST_NODE_TYPES.FunctionDeclaration;
679-
680-
if (node.modifiers && node.modifiers.length) {
681-
const isDeclareFunction = nodeUtils.hasModifier(
682-
SyntaxKind.DeclareKeyword,
683-
node
684-
);
685-
if (isDeclareFunction) {
686-
functionDeclarationType = AST_NODE_TYPES.DeclareFunction;
687-
}
679+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
680+
functionDeclarationType = AST_NODE_TYPES.TSDeclareFunction;
688681
}
689682

690683
Object.assign(result, {
@@ -694,14 +687,18 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
694687
expression: false,
695688
async: nodeUtils.hasModifier(SyntaxKind.AsyncKeyword, node),
696689
params: convertParameters((node as any).parameters),
697-
body: convertChild((node as any).body)
690+
body: convertChild((node as any).body) || undefined
698691
});
699692

700693
// Process returnType
701694
if ((node as any).type) {
702695
(result as any).returnType = convertTypeAnnotation((node as any).type);
703696
}
704697

698+
if (functionDeclarationType === AST_NODE_TYPES.TSDeclareFunction) {
699+
result.declare = true;
700+
}
701+
705702
// Process typeParameters
706703
if ((node as any).typeParameters && (node as any).typeParameters.length) {
707704
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
@@ -744,6 +741,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
744741
kind: nodeUtils.getDeclarationKind((node as any).declarationList)
745742
});
746743

744+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
745+
result.declare = true;
746+
}
747+
747748
// check for exports
748749
result = nodeUtils.fixExports(node, result as any, ast);
749750
break;
@@ -1611,6 +1612,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
16111612
);
16121613
}
16131614

1615+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
1616+
result.declare = true;
1617+
}
1618+
16141619
if (node.decorators) {
16151620
result.decorators = convertDecorators(node.decorators);
16161621
}
@@ -2421,10 +2426,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24212426
}
24222427

24232428
const hasImplementsClause = interfaceHeritageClauses.length > 0;
2424-
const hasAbstractKeyword = nodeUtils.hasModifier(
2425-
SyntaxKind.AbstractKeyword,
2426-
node
2427-
);
24282429
const interfaceOpenBrace = nodeUtils.findNextToken(
24292430
interfaceLastClassToken,
24302431
ast,
@@ -2443,7 +2444,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24432444
};
24442445

24452446
Object.assign(result, {
2446-
abstract: hasAbstractKeyword,
24472447
type: AST_NODE_TYPES.TSInterfaceDeclaration,
24482448
body: interfaceBody,
24492449
id: convertChild((node as any).name),
@@ -2461,6 +2461,12 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24612461
if (node.decorators) {
24622462
result.decorators = convertDecorators(node.decorators);
24632463
}
2464+
if (nodeUtils.hasModifier(SyntaxKind.AbstractKeyword, node)) {
2465+
result.abstract = true;
2466+
}
2467+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
2468+
result.declare = true;
2469+
}
24642470
// check for exports
24652471
result = nodeUtils.fixExports(node, result as any, ast);
24662472

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { ParserOptions } from '../../src/temp-types-based-on-js-source';
66

77
interface Fixture {
88
filename: string;
9-
config?: any;
9+
config?: {
10+
babelParserOptions?: BabelParserOptions;
11+
typeScriptESTreeOptions?: ParserOptions;
12+
};
1013
}
1114

1215
interface FixturePatternConfig {
@@ -395,11 +398,6 @@ let fixturePatternConfigsToTest = [
395398
'class-with-implements-generic',
396399
'class-with-implements',
397400
'class-with-extends-and-implements',
398-
/**
399-
* Babylon: TSDeclareFunction + declare: true
400-
* tsep: DeclareFunction
401-
*/
402-
'declare-function',
403401
/**
404402
* Babylon: TSTypeReference + identifier
405403
* tsep: TSUnknownKeyword
@@ -508,6 +506,18 @@ let fixturePatternConfigsToTest = [
508506
]
509507
}),
510508

509+
createFixturePatternConfigFor('typescript/declare', {
510+
fileType: 'ts',
511+
ignore: [
512+
/**
513+
* AST difference
514+
*/
515+
'type-alias',
516+
'interface',
517+
'abstract-class'
518+
]
519+
}),
520+
511521
createFixturePatternConfigFor('typescript/namespaces-and-modules', {
512522
fileType: 'ts',
513523
ignore: [

tests/ast-alignment/spec.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,24 @@ fixturesToTest.forEach(fixture => {
77
const filename = fixture.filename;
88
const source = fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n');
99

10+
const config = fixture.config || {};
11+
config.typeScriptESTreeOptions = config.typeScriptESTreeOptions || {};
12+
config.babelParserOptions = config.babelParserOptions || {};
13+
1014
/**
1115
* Parse with typescript-estree
1216
*/
1317
const typeScriptESTreeResult = parse(source, {
1418
parser: 'typescript-estree',
15-
typeScriptESTreeOptions:
16-
fixture.config && fixture.config.typeScriptESTreeOptions
17-
? fixture.config.typeScriptESTreeOptions
18-
: null
19+
typeScriptESTreeOptions: config.typeScriptESTreeOptions
1920
});
2021

2122
/**
2223
* Parse the source with babylon typescript-plugin
2324
*/
2425
const babelParserResult = parse(source, {
2526
parser: '@babel/parser',
26-
babelParserOptions:
27-
fixture.config && fixture.config.babelParserOptions
28-
? fixture.config.babelParserOptions
29-
: null
27+
babelParserOptions: config.babelParserOptions
3028
});
3129

3230
/**
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)