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

Commit 7bcc0d6

Browse files
authored
Fix: Ensure modifiers are applied to enums (fixes #365) (#366)
1 parent 9909aa1 commit 7bcc0d6

File tree

6 files changed

+812
-1
lines changed

6 files changed

+812
-1
lines changed

lib/ast-node-types.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ module.exports = {
101101
* TS-prefixed nodes
102102
*/
103103
TSAbstractClassProperty: "TSAbstractClassProperty",
104+
TSAbstractKeyword: "TSAbstractKeyword",
104105
TSAbstractMethodDefinition: "TSAbstractMethodDefinition",
105106
TSAnyKeyword: "TSAnyKeyword",
106107
TSArrayType: "TSArrayType",
108+
TSAsyncKeyword: "TSAsyncKeyword",
107109
TSBooleanKeyword: "TSBooleanKeyword",
108110
TSConstructorType: "TSConstructorType",
109111
TSConstructSignature: "TSConstructSignature",
@@ -125,9 +127,14 @@ module.exports = {
125127
TSNumberKeyword: "TSNumberKeyword",
126128
TSObjectKeyword: "TSObjectKeyword",
127129
TSParameterProperty: "TSParameterProperty",
130+
TSPrivateKeyword: "TSPrivateKeyword",
128131
TSPropertySignature: "TSPropertySignature",
132+
TSProtectedKeyword: "TSProtectedKeyword",
133+
TSPublicKeyword: "TSPublicKeyword",
129134
TSQualifiedName: "TSQualifiedName",
130135
TSQuestionToken: "TSQuestionToken",
136+
TSReadonlyKeyword: "TSReadonlyKeyword",
137+
TSStaticKeyword: "TSStaticKeyword",
131138
TSStringKeyword: "TSStringKeyword",
132139
TSTypeLiteral: "TSTypeLiteral",
133140
TSTypePredicate: "TSTypePredicate",

lib/convert.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,52 @@ module.exports = function convert(config) {
330330
return tagNameToken;
331331
}
332332

333+
/**
334+
* Applies the given TS modifiers to the given result object.
335+
* @param {TSNode[]} modifiers original TSNodes from the node.modifiers array
336+
* @returns {void} (the current result object will be mutated)
337+
*/
338+
function applyModifiersToResult(modifiers) {
339+
if (!modifiers || !modifiers.length) {
340+
return;
341+
}
342+
/**
343+
* Some modifiers are explicitly handled by applying them as
344+
* boolean values on the result node. As well as adding them
345+
* to the result, we remove them from the array, so that they
346+
* are not handled twice.
347+
*/
348+
const handledModifierIndices = {};
349+
for (let i = 0; i < modifiers.length; i++) {
350+
const modifier = modifiers[i];
351+
switch (modifier.kind) {
352+
/**
353+
* Ignore ExportKeyword and DefaultKeyword, they are handled
354+
* via the fixExports utility function
355+
*/
356+
case SyntaxKind.ExportKeyword:
357+
case SyntaxKind.DefaultKeyword:
358+
handledModifierIndices[i] = true;
359+
break;
360+
case SyntaxKind.ConstKeyword:
361+
result.const = true;
362+
handledModifierIndices[i] = true;
363+
break;
364+
default:
365+
}
366+
}
367+
/**
368+
* If there are still valid modifiers available which have
369+
* not been explicitly handled above, we just convert and
370+
* add the modifiers array to the result node.
371+
*/
372+
const remainingModifiers = modifiers.filter((_, i) => !handledModifierIndices[i]);
373+
if (!remainingModifiers || !remainingModifiers.length) {
374+
return;
375+
}
376+
result.modifiers = remainingModifiers.map(convertChild);
377+
}
378+
333379
/**
334380
* The core of the conversion logic:
335381
* Identify and convert each relevant TypeScript SyntaxKind
@@ -2000,7 +2046,9 @@ module.exports = function convert(config) {
20002046
id: convertChild(node.name),
20012047
members: node.members.map(convertChild)
20022048
});
2003-
// check for exports
2049+
// apply modifiers first...
2050+
applyModifiersToResult(node.modifiers);
2051+
// ...then check for exports
20042052
result = nodeUtils.fixExports(node, result, ast);
20052053
/**
20062054
* Semantically, decorators are not allowed on enum declarations,

tests/ast-alignment/spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ const fixturePatternsToTest = [
422422
"typescript/basics/async-function-with-var-declaration.src.ts",
423423
"typescript/basics/function-with-await.src.ts",
424424
"typescript/errorRecovery/class-extends-empty-implements.src.ts",
425+
"typescript/basics/const-enum.src.ts",
425426

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

439441
/**
440442
* Other babylon parse errors. TODO: Need to coordinate with TS Team.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const enum Foo {
2+
foo = 1,
3+
bar
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export private public protected static readonly abstract async enum X {}

0 commit comments

Comments
 (0)