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

Commit

Permalink
Fix: Ensure modifiers are applied to enums (fixes #365) (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry authored Aug 19, 2017
1 parent 9909aa1 commit 7bcc0d6
Show file tree
Hide file tree
Showing 6 changed files with 812 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ module.exports = {
* TS-prefixed nodes
*/
TSAbstractClassProperty: "TSAbstractClassProperty",
TSAbstractKeyword: "TSAbstractKeyword",
TSAbstractMethodDefinition: "TSAbstractMethodDefinition",
TSAnyKeyword: "TSAnyKeyword",
TSArrayType: "TSArrayType",
TSAsyncKeyword: "TSAsyncKeyword",
TSBooleanKeyword: "TSBooleanKeyword",
TSConstructorType: "TSConstructorType",
TSConstructSignature: "TSConstructSignature",
Expand All @@ -125,9 +127,14 @@ module.exports = {
TSNumberKeyword: "TSNumberKeyword",
TSObjectKeyword: "TSObjectKeyword",
TSParameterProperty: "TSParameterProperty",
TSPrivateKeyword: "TSPrivateKeyword",
TSPropertySignature: "TSPropertySignature",
TSProtectedKeyword: "TSProtectedKeyword",
TSPublicKeyword: "TSPublicKeyword",
TSQualifiedName: "TSQualifiedName",
TSQuestionToken: "TSQuestionToken",
TSReadonlyKeyword: "TSReadonlyKeyword",
TSStaticKeyword: "TSStaticKeyword",
TSStringKeyword: "TSStringKeyword",
TSTypeLiteral: "TSTypeLiteral",
TSTypePredicate: "TSTypePredicate",
Expand Down
50 changes: 49 additions & 1 deletion lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,52 @@ module.exports = function convert(config) {
return tagNameToken;
}

/**
* Applies the given TS modifiers to the given result object.
* @param {TSNode[]} modifiers original TSNodes from the node.modifiers array
* @returns {void} (the current result object will be mutated)
*/
function applyModifiersToResult(modifiers) {
if (!modifiers || !modifiers.length) {
return;
}
/**
* Some modifiers are explicitly handled by applying them as
* boolean values on the result node. As well as adding them
* to the result, we remove them from the array, so that they
* are not handled twice.
*/
const handledModifierIndices = {};
for (let i = 0; i < modifiers.length; i++) {
const modifier = modifiers[i];
switch (modifier.kind) {
/**
* Ignore ExportKeyword and DefaultKeyword, they are handled
* via the fixExports utility function
*/
case SyntaxKind.ExportKeyword:
case SyntaxKind.DefaultKeyword:
handledModifierIndices[i] = true;
break;
case SyntaxKind.ConstKeyword:
result.const = true;
handledModifierIndices[i] = true;
break;
default:
}
}
/**
* If there are still valid modifiers available which have
* not been explicitly handled above, we just convert and
* add the modifiers array to the result node.
*/
const remainingModifiers = modifiers.filter((_, i) => !handledModifierIndices[i]);
if (!remainingModifiers || !remainingModifiers.length) {
return;
}
result.modifiers = remainingModifiers.map(convertChild);
}

/**
* The core of the conversion logic:
* Identify and convert each relevant TypeScript SyntaxKind
Expand Down Expand Up @@ -2000,7 +2046,9 @@ module.exports = function convert(config) {
id: convertChild(node.name),
members: node.members.map(convertChild)
});
// check for exports
// apply modifiers first...
applyModifiersToResult(node.modifiers);
// ...then check for exports
result = nodeUtils.fixExports(node, result, ast);
/**
* Semantically, decorators are not allowed on enum declarations,
Expand Down
2 changes: 2 additions & 0 deletions tests/ast-alignment/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ const fixturePatternsToTest = [
"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/basics/const-enum.src.ts",

{
pattern: "typescript/basics/export-named-enum.src.ts",
Expand All @@ -435,6 +436,7 @@ const fixturePatternsToTest = [
// "typescript/errorRecovery/class-empty-extends.src.ts", // babylon parse errors
// "typescript/errorRecovery/decorator-on-enum-declaration.src.ts", // babylon parse errors
// "typescript/errorRecovery/interface-property-modifiers.src.ts", // babylon parse errors
// "typescript/errorRecovery/enum-with-keywords.src.ts" // babylon parse errors

/**
* Other babylon parse errors. TODO: Need to coordinate with TS Team.
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/typescript/basics/const-enum.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const enum Foo {
foo = 1,
bar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export private public protected static readonly abstract async enum X {}
Loading

0 comments on commit 7bcc0d6

Please sign in to comment.