Skip to content

Commit

Permalink
Export enums as const enums but leave original strings intact.
Browse files Browse the repository at this point in the history
This avoids a breaking change for existing consumers whilst new
consumers can still pass in enum values and typecheck. E.g. both of
these work:

```
const message: ConsoleMessage = {
  source: 'xml',
  level: 'log',
  text: 'foo'
}
```

```
const message2: ConsoleMessage = {
  source: ConsoleMessageSource.XML,
  level: ConsoleMessageLevel.Log,
  text: 'foo'
}
```
  • Loading branch information
jackfranklin committed Jun 4, 2020
1 parent 2bb3bb0 commit 08875fa
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 176 deletions.
23 changes: 12 additions & 11 deletions scripts/protocol-dts-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const fixCamelCase = (name: string): string => {


const emitEnum = (enumName: string, enumValues: string[]) => {
emitOpenBlock(`export enum ${enumName}`);
emitOpenBlock(`export const enum ${enumName}`);
enumValues.forEach(value => {
emitLine(`${fixCamelCase(value)} = '${value}',`);
});
Expand Down Expand Up @@ -114,13 +114,8 @@ const isPropertyInlineEnum = (prop: P.ProtocolType): boolean => {
const getPropertyDef = (interfaceName: string, prop: P.PropertyType): string => {
// Quote key if it has a . in it.
const propName = prop.name.includes('.') ? `'${prop.name}'` : prop.name
let type: string;
if (isPropertyInlineEnum(prop)) {
type = interfaceName + toTitleCase(prop.name);
} else {
type = getPropertyType(interfaceName, prop);
}
return `${propName}${prop.optional ? '?' : ''}: ${type}`;
const type = getPropertyType(interfaceName, prop);
return `${propName}${prop.optional ? '?' : ''}: ${type}`;
};

const getPropertyType = (interfaceName: string, prop: P.ProtocolType): string => {
Expand Down Expand Up @@ -149,10 +144,16 @@ const getPropertyType = (interfaceName: string, prop: P.ProtocolType): string =
}

const emitProperty = (interfaceName: string, prop: P.PropertyType) => {
emitDescription(prop.description)
emitLine(`${getPropertyDef(interfaceName, prop)};`)
let description = prop.description;
if (isPropertyInlineEnum(prop)) {
const enumName = interfaceName + toTitleCase(prop.name);
description = `${description || ''} (${enumName} enum)`;
}

emitDescription(description)
emitLine(`${getPropertyDef(interfaceName, prop)};`)
}


const emitInlineEnumForDomainType = (type: P.DomainType) => {
if (type.type === 'object') {
Expand Down
Loading

0 comments on commit 08875fa

Please sign in to comment.