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

Commit 482d2a1

Browse files
committed
Breaking: Explicitly handle TSEnumDeclaration (fixes #345)
1 parent f5bd145 commit 482d2a1

File tree

5 files changed

+403
-13
lines changed

5 files changed

+403
-13
lines changed

lib/ast-node-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module.exports = {
109109
TSConstructSignature: "TSConstructSignature",
110110
TSDeclareKeyword: "TSDeclareKeyword",
111111
TSEnumDeclaration: "TSEnumDeclaration",
112+
TSEnumMember: "TSEnumMember",
112113
TSExportAssignment: "TSExportAssigment",
113114
TSIndexSignature: "TSIndexSignature",
114115
TSInterfaceBody: "TSInterfaceBody",

lib/convert.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,36 @@ module.exports = function convert(config) {
20002000
});
20012001
break;
20022002

2003+
case SyntaxKind.EnumDeclaration: {
2004+
Object.assign(result, {
2005+
type: AST_NODE_TYPES.TSEnumDeclaration,
2006+
id: convertChild(node.name),
2007+
members: node.members.map(convertChild)
2008+
});
2009+
// check for exports
2010+
result = nodeUtils.fixExports(node, result, ast);
2011+
/**
2012+
* Semantically, decorators are not allowed on enum declarations,
2013+
* but the TypeScript compiler will parse them and produce a valid AST,
2014+
* so we handle them here too.
2015+
*/
2016+
if (node.decorators) {
2017+
result.decorators = convertDecorators(node.decorators);
2018+
}
2019+
break;
2020+
}
2021+
2022+
case SyntaxKind.EnumMember: {
2023+
Object.assign(result, {
2024+
type: AST_NODE_TYPES.TSEnumMember,
2025+
id: convertChild(node.name)
2026+
});
2027+
if (node.initializer) {
2028+
result.initializer = convertChild(node.initializer);
2029+
}
2030+
break;
2031+
}
2032+
20032033
default:
20042034
deeplyCopy();
20052035
}

tests/ast-alignment/spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ const fixturePatternsToTest = [
421421
"typescript/basics/async-function-expression.src.ts",
422422
"typescript/basics/async-function-with-var-declaration.src.ts",
423423
"typescript/basics/function-with-await.src.ts",
424-
"typescript/errorRecovery/class-extends-empty-implements.src.ts"
424+
"typescript/errorRecovery/class-extends-empty-implements.src.ts",
425+
426+
{
427+
pattern: "typescript/basics/export-named-enum.src.ts",
428+
config: { babylonParserOptions: { sourceType: "module" } }
429+
}
425430

426431
/**
427432
* TypeScript-specific tests taken from "errorRecovery". Babylon is not being as forgiving as the TypeScript compiler here.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum Foo {
2+
foo = 1,
3+
bar
4+
}

0 commit comments

Comments
 (0)