Skip to content

Commit

Permalink
fix(jsii): prohibit exported const enums (#372)
Browse files Browse the repository at this point in the history
'const enums' are inlined at the usage site by TypeScript and so the
generated type will not be in the JavaScript source code in the
assembly, even though the declaration will be there.

This leads to "symbol not found" errors upon trying to load it.

Ref: https://www.typescriptlang.org/docs/handbook/enums.html#const-enums
Related: aws/aws-cdk#1969
  • Loading branch information
rix0rrr authored and RomainMuller committed Mar 7, 2019
1 parent e429c41 commit 5a94825
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ export class Assembler implements Emitter {
LOG.trace(`Processing enum: ${colors.gray(namespace.join('.'))}.${colors.cyan(type.symbol.name)}`);
}

const decl = type.symbol.valueDeclaration;
const flags = ts.getCombinedModifierFlags(decl);
// tslint:disable-next-line:no-bitwise
if (flags & ts.ModifierFlags.Const) {
this._diagnostic(decl,
ts.DiagnosticCategory.Error,
`Exported enum cannot be declared 'const'`);
}

const jsiiType: spec.EnumType = {
assembly: this.projectInfo.name,
fqn: `${[this.projectInfo.name, ...namespace].join('.')}.${type.symbol.name}`,
Expand Down
8 changes: 8 additions & 0 deletions packages/jsii/test/negatives/neg.const-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
///!MATCH_ERROR: Exported enum cannot be declared 'const'

export const enum NotAllowed {
ThisEnum,
GetsInlined,
AndSoItGetsLost,
ForJsii
}

0 comments on commit 5a94825

Please sign in to comment.