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

feat(jsii): enforce enum names to be UPPER_CASE #541

Merged
merged 18 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ like any other class.

> NOTE: Due to performance of the hosted JavaScript engine and marshaling costs,
__jsii__ modules are likely to be used for development and build tools, as
oppose to performance-sensitive runtime behavior.
opposed to performance-sensitive runtime behavior.

From Java:

Expand Down
4 changes: 2 additions & 2 deletions packages/jsii-calc-lib/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export interface StructWithOnlyOptionals {
* See awslabs/jsii#138
*/
export enum EnumFromScopedModule {
Value1,
Value2
VALUE1,
VALUE2
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/jsii/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type ValidationFunction = (validator: Validator,
function _defaultValidations(): ValidationFunction[] {
return [
_typeNamesMustUsePascalCase,
_enumMembersMustUserUpperSnakeCase,
_memberNamesMustUseCamelCase,
_staticConstantNamesMustUseUpperSnakeCase,
_memberNamesMustNotLookLikeJavaGettersOrSetters,
Expand All @@ -70,6 +71,19 @@ function _defaultValidations(): ValidationFunction[] {
}
}

function _enumMembersMustUserUpperSnakeCase(_: Validator, assembly: spec.Assembly, diagnostic: DiagnosticEmitter) {
for (const type of _allTypes(assembly)) {
if (!spec.isEnumType(type)) { continue; }

for (const member of type.members) {
if (member.name && member.name !== Case.constant(member.name)) {
diagnostic(ts.DiagnosticCategory.Error,
`Enum members must use TRUMP_CASE: ${member.name}`);
shivlaks marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

function _memberNamesMustUseCamelCase(_: Validator, assembly: spec.Assembly, diagnostic: DiagnosticEmitter) {
for (const member of _allMembers(assembly)) {
if (member.static && (member as spec.Property).const) { continue; }
Expand Down
4 changes: 2 additions & 2 deletions packages/jsii/test/negatives/neg.enum-name.1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///!MATCH_ERROR: Type names must use PascalCase: myEnum

export enum myEnum {
Foo,
Goo
FOO,
GOO
}
4 changes: 2 additions & 2 deletions packages/jsii/test/negatives/neg.enum-name.2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///!MATCH_ERROR: Type names must use PascalCase: My_Enum

export enum My_Enum {
Foo,
Goo
FOO,
GOO
}
12 changes: 6 additions & 6 deletions packages/jsii/test/test.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ export = {
async 'test parsing enum with two members and no values'(test: Test) {
const assembly = await sourceToAssemblyHelper(`
export enum Foo {
Bar,
Baz
BAR,
BAZ
}
`);

test.deepEqual(assembly.types!['testpkg.Foo'] , {
assembly: 'testpkg',
fqn: 'testpkg.Foo',
kind: 'enum',
members: [{ name: 'Bar' }, { name: 'Baz' }],
members: [{ name: 'BAR' }, { name: 'BAZ' }],
locationInModule: { filename: 'index.ts', line: 2 },
name: 'Foo'
});
Expand All @@ -29,16 +29,16 @@ export = {
async 'test parsing enum with two members and assigned values'(test: Test) {
const assembly = await sourceToAssemblyHelper(`
export enum Foo {
Bar = 'Bar',
Baz = 'Baz'
BAR = 'Bar',
BAZ = 'Baz'
}
`);

test.deepEqual(assembly.types!['testpkg.Foo'] , {
assembly: 'testpkg',
fqn: 'testpkg.Foo',
kind: 'enum',
members: [{ name: 'Bar' }, { name: 'Baz' }],
members: [{ name: 'BAR' }, { name: 'BAZ' }],
locationInModule: { filename: 'index.ts', line: 2 },
name: 'Foo'
});
Expand Down