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

Commit 598f881

Browse files
committed
feat: fix issues in AST
1 parent c7af360 commit 598f881

16 files changed

+2189
-391
lines changed

src/ast-node-types.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
688688

689689
case SyntaxKind.FunctionDeclaration: {
690690
let functionDeclarationType = AST_NODE_TYPES.FunctionDeclaration;
691-
692-
if (node.modifiers && node.modifiers.length) {
693-
const isDeclareFunction = nodeUtils.hasModifier(
694-
SyntaxKind.DeclareKeyword,
695-
node
696-
);
697-
if (isDeclareFunction) {
698-
functionDeclarationType = AST_NODE_TYPES.DeclareFunction;
699-
}
691+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
692+
functionDeclarationType = AST_NODE_TYPES.TSDeclareFunction;
700693
}
701694

702695
Object.assign(result, {
@@ -706,14 +699,18 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
706699
expression: false,
707700
async: nodeUtils.hasModifier(SyntaxKind.AsyncKeyword, node),
708701
params: convertParameters(node.parameters),
709-
body: convertChild(node.body)
702+
body: convertChild(node.body) || undefined
710703
});
711704

712705
// Process returnType
713706
if (node.type) {
714707
(result as any).returnType = convertTypeAnnotation(node.type);
715708
}
716709

710+
if (functionDeclarationType === AST_NODE_TYPES.TSDeclareFunction) {
711+
result.declare = true;
712+
}
713+
717714
// Process typeParameters
718715
if (node.typeParameters && node.typeParameters.length) {
719716
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
@@ -752,6 +749,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
752749
kind: nodeUtils.getDeclarationKind(node.declarationList)
753750
});
754751

752+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
753+
result.declare = true;
754+
}
755+
755756
// check for exports
756757
result = nodeUtils.fixExports(node, result as any, ast);
757758
break;
@@ -1612,6 +1613,10 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
16121613
);
16131614
}
16141615

1616+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
1617+
result.declare = true;
1618+
}
1619+
16151620
if (node.decorators) {
16161621
result.decorators = convertDecorators(node.decorators);
16171622
}
@@ -2394,10 +2399,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
23942399
}
23952400

23962401
const hasImplementsClause = interfaceHeritageClauses.length > 0;
2397-
const hasAbstractKeyword = nodeUtils.hasModifier(
2398-
SyntaxKind.AbstractKeyword,
2399-
node
2400-
);
24012402
const interfaceOpenBrace = nodeUtils.findNextToken(
24022403
interfaceLastClassToken,
24032404
ast,
@@ -2416,7 +2417,6 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24162417
};
24172418

24182419
Object.assign(result, {
2419-
abstract: hasAbstractKeyword,
24202420
type: AST_NODE_TYPES.TSInterfaceDeclaration,
24212421
body: interfaceBody,
24222422
id: convertChild(node.name),
@@ -2434,6 +2434,12 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
24342434
if (node.decorators) {
24352435
result.decorators = convertDecorators(node.decorators);
24362436
}
2437+
if (nodeUtils.hasModifier(SyntaxKind.AbstractKeyword, node)) {
2438+
result.abstract = true;
2439+
}
2440+
if (nodeUtils.hasModifier(SyntaxKind.DeclareKeyword, node)) {
2441+
result.declare = true;
2442+
}
24372443
// check for exports
24382444
result = nodeUtils.fixExports(node, result as any, ast);
24392445

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 16 additions & 6 deletions
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

Lines changed: 6 additions & 8 deletions
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
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare abstract class Foo {
2+
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare class Foo {
2+
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare enum Foo {
2+
Bar,
3+
Baz
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare function foo(): void
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare interface Foo {
2+
3+
}

0 commit comments

Comments
 (0)