From 6f440b50770cb16050e703db298fb9a9ea7b1c07 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 7 Mar 2019 09:22:36 +0100 Subject: [PATCH] fix(jsii): prohibit exported const enums '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. https://www.typescriptlang.org/docs/handbook/enums.html#const-enums Fixes 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