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

Commit

Permalink
Breaking: Explicitly handle TSEnumDeclaration (fixes #345)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Aug 19, 2017
1 parent 5f741a9 commit 634fc52
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module.exports = {
TSConstructSignature: "TSConstructSignature",
TSDeclareKeyword: "TSDeclareKeyword",
TSEnumDeclaration: "TSEnumDeclaration",
TSEnumMember: "TSEnumMember",
TSExportAssignment: "TSExportAssigment",
TSIndexSignature: "TSIndexSignature",
TSInterfaceBody: "TSInterfaceBody",
Expand Down
30 changes: 30 additions & 0 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,36 @@ module.exports = function convert(config) {
});
break;

case SyntaxKind.EnumDeclaration: {
Object.assign(result, {
type: AST_NODE_TYPES.TSEnumDeclaration,
id: convertChild(node.name),
members: node.members.map(convertChild)
});
// check for exports
result = nodeUtils.fixExports(node, result, ast);
/**
* Semantically, decorators are not allowed on enum declarations,
* but the TypeScript compiler will parse them and produce a valid AST,
* so we handle them here too.
*/
if (node.decorators) {
result.decorators = convertDecorators(node.decorators);
}
break;
}

case SyntaxKind.EnumMember: {
Object.assign(result, {
type: AST_NODE_TYPES.TSEnumMember,
id: convertChild(node.name)
});
if (node.initializer) {
result.initializer = convertChild(node.initializer);
}
break;
}

default:
deeplyCopy();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"license": "BSD-2-Clause",
"devDependencies": {
"babel-code-frame": "^6.22.0",
"babylon": "^7.0.0-beta.19",
"babylon": "^7.0.0-beta.20",
"eslint": "3.19.0",
"eslint-config-eslint": "4.0.0",
"eslint-plugin-node": "4.2.2",
Expand Down
7 changes: 6 additions & 1 deletion tests/ast-alignment/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,12 @@ const fixturePatternsToTest = [
"typescript/basics/async-function-expression.src.ts",
"typescript/basics/async-function-with-var-declaration.src.ts",
"typescript/basics/function-with-await.src.ts",
"typescript/errorRecovery/class-extends-empty-implements.src.ts"
"typescript/errorRecovery/class-extends-empty-implements.src.ts",

{
pattern: "typescript/basics/export-named-enum.src.ts",
config: { babylonParserOptions: { sourceType: "module" } }
}

/**
* TypeScript-specific tests taken from "errorRecovery". Babylon is not being as forgiving as the TypeScript compiler here.
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/typescript/basics/export-named-enum.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Foo {
foo = 1,
bar
}
Loading

0 comments on commit 634fc52

Please sign in to comment.