Skip to content

Commit 5a94825

Browse files
rix0rrrRomainMuller
authored andcommitted
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: aws/aws-cdk#1969
1 parent e429c41 commit 5a94825

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

packages/jsii/lib/assembler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,15 @@ export class Assembler implements Emitter {
482482
LOG.trace(`Processing enum: ${colors.gray(namespace.join('.'))}.${colors.cyan(type.symbol.name)}`);
483483
}
484484

485+
const decl = type.symbol.valueDeclaration;
486+
const flags = ts.getCombinedModifierFlags(decl);
487+
// tslint:disable-next-line:no-bitwise
488+
if (flags & ts.ModifierFlags.Const) {
489+
this._diagnostic(decl,
490+
ts.DiagnosticCategory.Error,
491+
`Exported enum cannot be declared 'const'`);
492+
}
493+
485494
const jsiiType: spec.EnumType = {
486495
assembly: this.projectInfo.name,
487496
fqn: `${[this.projectInfo.name, ...namespace].join('.')}.${type.symbol.name}`,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
///!MATCH_ERROR: Exported enum cannot be declared 'const'
2+
3+
export const enum NotAllowed {
4+
ThisEnum,
5+
GetsInlined,
6+
AndSoItGetsLost,
7+
ForJsii
8+
}

0 commit comments

Comments
 (0)