From 5a9482556a5d317358117704ed3b296cf0f9cba4 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 7 Mar 2019 09:37:46 +0100 Subject: [PATCH] fix(jsii): prohibit exported const enums (#372) '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: awslabs/aws-cdk#1969 --- packages/jsii/lib/assembler.ts | 9 +++++++++ packages/jsii/test/negatives/neg.const-enum.ts | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/jsii/test/negatives/neg.const-enum.ts diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index d997ab95eb..58fdb81c03 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -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}`, diff --git a/packages/jsii/test/negatives/neg.const-enum.ts b/packages/jsii/test/negatives/neg.const-enum.ts new file mode 100644 index 0000000000..bc3a792ba4 --- /dev/null +++ b/packages/jsii/test/negatives/neg.const-enum.ts @@ -0,0 +1,8 @@ +///!MATCH_ERROR: Exported enum cannot be declared 'const' + +export const enum NotAllowed { + ThisEnum, + GetsInlined, + AndSoItGetsLost, + ForJsii +} \ No newline at end of file