Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsii): fail compilation when two or more enum members have same val #3412

Merged
merged 4 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,9 @@ export class Assembler implements Emitter {
return undefined;
}

// check the enum to see if there are duplicate enum values
this.assertNoDuplicateEnumValues(decl);

this._warnAboutReservedWords(symbol);

const flags = ts.getCombinedModifierFlags(decl);
Expand Down Expand Up @@ -1791,6 +1794,57 @@ export class Assembler implements Emitter {
return jsiiType;
}

private assertNoDuplicateEnumValues(decl: ts.EnumDeclaration): void {
type EnumValue = {
name: string;
value: string;
decl: ts.DeclarationName | undefined;
};

const enumValues = decl.members
.filter((m) => m.initializer)
.map((member): EnumValue => {
return {
value: member.initializer!.getText(),
name: member.name.getText(),
decl: ts.getNameOfDeclaration(member),
};
});

const hasDuplicateEnumValues = enumValues.some(
(val, _, arr) => arr.filter((e) => val.value === e.value).length > 1,
);

if (hasDuplicateEnumValues) {
const enumValueMap = enumValues.reduce<Record<string, EnumValue[]>>(
(acc, val) => {
if (!acc[val.value]) {
acc[val.value] = [];
}
acc[val.value].push(val);
return acc;
},
{},
);
for (const duplicateValue of Object.keys(enumValueMap)) {
if (enumValueMap[duplicateValue].length > 1) {
const err = JsiiDiagnostic.JSII_1004_DUPLICATE_ENUM_VALUE.create(
enumValueMap[duplicateValue][0].decl!,
duplicateValue,
enumValueMap[duplicateValue].map((v) => v.name),
);
for (let i = 1; i < enumValueMap[duplicateValue].length; i++) {
err.addRelatedInformation(
enumValueMap[duplicateValue][i].decl!,
'The conflicting declaration is here',
);
}
this._diagnostics.push(err);
}
}
}
}

/**
* Return docs for a symbol
*/
Expand Down
11 changes: 10 additions & 1 deletion packages/jsii/lib/jsii-diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Code<
*
* @param code the numeric code for the diagnostic
* @param name the symbolic name for the diagnostic
* @param defaultCategory the default category this diagnostic ransk in
* @param defaultCategory the default category this diagnostic ranks in
* @param formatter a message formatter for easy creation of diagnostics
*/
private constructor(
Expand Down Expand Up @@ -292,6 +292,15 @@ export class JsiiDiagnostic implements ts.Diagnostic {
name: 'typescript-restrictions/unsupported-type',
});

public static readonly JSII_1004_DUPLICATE_ENUM_VALUE = Code.error({
code: 1004,
formatter: (enumValue: string, enumMemberNames: string[]) =>
`Value ${enumValue} is used for multiple enum values: ${enumMemberNames.join(
', ',
)}`,
name: 'typescript-restrictions/duplicate-enum-value',
});

//////////////////////////////////////////////////////////////////////////////
// 2000 => 2999 -- RESERVED

Expand Down
22 changes: 22 additions & 0 deletions packages/jsii/test/__snapshots__/negatives.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions packages/jsii/test/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ test('enums can have a mix of letters and number', () => {
{ name: 'IB3M' },
]);
});

test('enums with the same assigned value should fail', () => {
expect(() =>
sourceToAssemblyHelper(`
export enum Foo {
BAR = 'Bar',
BAR_DUPE = 'Bar',
BAZ = 'Baz',
BAZ_DUPE = 'Baz',
}
`),
).toThrow(/There were compiler errors/);
});
6 changes: 6 additions & 0 deletions packages/jsii/test/negatives/neg.enum-duplicate-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum Foo {
FOO = 'foo',
FOO_DUPLICATE = 'foo',
BAR = 'bar',
BAR_COPY = 'bar',
}