diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index b5a0a7f858..a5194ccd75 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -1692,3 +1692,37 @@ export class WithPrivatePropertyInConstructor { return this.privateField === 'Success!'; } } + +/** + * Verifies that singleton enums are handled correctly + * + * https://github.com/awslabs/jsii/issues/231 + */ +export class SingletonString { + private constructor() { } + + public isSingletonString(value: string): boolean { + return value === SingletonStringEnum.SingletonString; + } +} +/** A singleton string */ +export enum SingletonStringEnum { + /** 1337 */ + SingletonString = '3L1T3!' +} +/** + * Verifies that singleton enums are handled correctly + * + * https://github.com/awslabs/jsii/issues/231 + */ +export class SingletonInt { + private constructor() { } + public isSingletonInt(value: number): boolean { + return value === SingletonIntEnum.SingletonInt; + } +} +/** A singleton integer. */ +export enum SingletonIntEnum { + /** Elite! */ + SingletonInt = 1337 +} diff --git a/packages/jsii-calc/test/assembly.jsii b/packages/jsii-calc/test/assembly.jsii index 5246a4694d..23f1aa9ffe 100644 --- a/packages/jsii-calc/test/assembly.jsii +++ b/packages/jsii-calc/test/assembly.jsii @@ -7121,6 +7121,132 @@ ], "name": "SingleInstanceTwoTypes" }, + "jsii-calc.SingletonInt": { + "assembly": "jsii-calc", + "docs": { + "remarks": "https://github.com/awslabs/jsii/issues/231", + "stability": "experimental", + "summary": "Verifies that singleton enums are handled correctly." + }, + "fqn": "jsii-calc.SingletonInt", + "kind": "class", + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1718 + }, + "methods": [ + { + "docs": { + "stability": "experimental" + }, + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1720 + }, + "name": "isSingletonInt", + "parameters": [ + { + "name": "value", + "type": { + "primitive": "number" + } + } + ], + "returns": { + "type": { + "primitive": "boolean" + } + } + } + ], + "name": "SingletonInt" + }, + "jsii-calc.SingletonIntEnum": { + "assembly": "jsii-calc", + "docs": { + "stability": "experimental", + "summary": "A singleton integer." + }, + "fqn": "jsii-calc.SingletonIntEnum", + "kind": "enum", + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1725 + }, + "members": [ + { + "docs": { + "stability": "experimental", + "summary": "Elite!" + }, + "name": "SingletonInt" + } + ], + "name": "SingletonIntEnum" + }, + "jsii-calc.SingletonString": { + "assembly": "jsii-calc", + "docs": { + "remarks": "https://github.com/awslabs/jsii/issues/231", + "stability": "experimental", + "summary": "Verifies that singleton enums are handled correctly." + }, + "fqn": "jsii-calc.SingletonString", + "kind": "class", + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1701 + }, + "methods": [ + { + "docs": { + "stability": "experimental" + }, + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1704 + }, + "name": "isSingletonString", + "parameters": [ + { + "name": "value", + "type": { + "primitive": "string" + } + } + ], + "returns": { + "type": { + "primitive": "boolean" + } + } + } + ], + "name": "SingletonString" + }, + "jsii-calc.SingletonStringEnum": { + "assembly": "jsii-calc", + "docs": { + "stability": "experimental", + "summary": "A singleton string." + }, + "fqn": "jsii-calc.SingletonStringEnum", + "kind": "enum", + "locationInModule": { + "filename": "lib/compliance.ts", + "line": 1709 + }, + "members": [ + { + "docs": { + "stability": "experimental", + "summary": "1337." + }, + "name": "SingletonString" + } + ], + "name": "SingletonStringEnum" + }, "jsii-calc.StableClass": { "assembly": "jsii-calc", "docs": { @@ -8648,5 +8774,5 @@ } }, "version": "0.11.2", - "fingerprint": "eQpFH3EHC2GlCSnThymTxnuO9HyZBFvsvddZqu1Fy+8=" + "fingerprint": "5TwMNffhxUueZQEAGZG6+JIfS6jcTwCJWc9vixH5aLc=" } diff --git a/packages/jsii/lib/assembler.ts b/packages/jsii/lib/assembler.ts index 5bf1b68990..860f752d3c 100644 --- a/packages/jsii/lib/assembler.ts +++ b/packages/jsii/lib/assembler.ts @@ -735,11 +735,17 @@ export class Assembler implements Emitter { LOG.trace(`Processing enum: ${colors.gray(ctx.namespace.join('.'))}.${colors.cyan(type.symbol.name)}`); } - if (_hasInternalJsDocTag(type.symbol)) { + // Forcefully resolving to the EnumDeclaration symbol for single-valued enums + const symbol: ts.Symbol = type.isLiteral() ? (type.symbol as any).parent : type.symbol; + if (!symbol) { + throw new Error(`Unable to resolve enum declaration for ${type.symbol.name}!`); + } + + if (_hasInternalJsDocTag(symbol)) { return undefined; } - const decl = type.symbol.valueDeclaration; + const decl = symbol.valueDeclaration; const flags = ts.getCombinedModifierFlags(decl); // tslint:disable-next-line:no-bitwise if (flags & ts.ModifierFlags.Const) { @@ -748,19 +754,20 @@ export class Assembler implements Emitter { `Exported enum cannot be declared 'const'`); } - const docs = this._visitDocumentation(type.symbol, ctx); + const docs = this._visitDocumentation(symbol, ctx); const typeContext = ctx.replaceStability(docs && docs.stability); + const members = type.isUnion() ? type.types : [type]; const jsiiType: spec.EnumType = { assembly: this.projectInfo.name, - fqn: `${[this.projectInfo.name, ...ctx.namespace].join('.')}.${type.symbol.name}`, + fqn: `${[this.projectInfo.name, ...ctx.namespace].join('.')}.${symbol.name}`, kind: spec.TypeKind.Enum, - members: ((type as ts.UnionType).types || []).map(m => ({ + members: members.map(m => ({ name: m.symbol.name, docs: this._visitDocumentation(m.symbol, typeContext), })), - name: type.symbol.name, + name: symbol.name, namespace: ctx.namespace.length > 0 ? ctx.namespace.join('.') : undefined, docs };