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): constants can't mix letters and digits #3209

Merged
merged 1 commit into from
Nov 26, 2021
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
16 changes: 14 additions & 2 deletions packages/jsii/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function _defaultValidations(): ValidationFunction[] {
}

for (const member of type.members) {
if (member.name && member.name !== Case.constant(member.name)) {
if (member.name && !isConstantCase(member.name)) {
diagnostic(
JsiiDiagnostic.JSII_8001_ALL_CAPS_ENUM_MEMBERS.createDetached(
member.name,
Expand Down Expand Up @@ -130,7 +130,7 @@ function _defaultValidations(): ValidationFunction[] {
}
if (
member.name &&
member.name !== Case.constant(member.name) &&
!isConstantCase(member.name) &&
member.name !== Case.pascal(member.name) &&
member.name !== Case.camel(member.name)
) {
Expand Down Expand Up @@ -713,3 +713,15 @@ function _dereference(
function _isEmpty(array: undefined | any[]): array is undefined {
return array == null || array.length === 0;
}

/**
* Return whether an identifier only consists of upperchase characters, digits and underscores
*
* We have our own check here (isConstantCase) which is more lenient than what
* `case.constant()` prescribes. We also want to allow combinations of letters
* and digits without underscores: `C5A`, which `case` would force to `C5_A`.
* The hint we print will still use `case.constant()` but that is fine.
*/
function isConstantCase(x: string) {
return !/[^A-Z0-9_]/.exec(x);
}
18 changes: 18 additions & 0 deletions packages/jsii/test/enums.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EnumType } from '@jsii/spec';

import { sourceToAssemblyHelper } from '../lib';

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -39,3 +41,19 @@ test('test parsing enum with two members and assigned values', async () => {
symbolId: 'index:Foo',
});
});

// ----------------------------------------------------------------------

test('enums can have a mix of letters and number', async () => {
const assembly = await sourceToAssemblyHelper(`
export enum Foo {
Q5X,
IB3M,
}
`);

expect((assembly.types!['testpkg.Foo'] as EnumType).members).toEqual([
{ name: 'Q5X' },
{ name: 'IB3M' },
]);
});