Skip to content

Commit

Permalink
fix: Don't throw on names with leading number (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeh committed Nov 27, 2022
1 parent baea1e2 commit 7bcd1fb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,33 @@ type Person=rt.Static<typeof person>;"
"
`);
});

it('handles names that are not valid JS indetifiers', () => {
const source = generateRuntypes([
{
name: '007',
type: { kind: 'string' },
},

{
name: 'agent',
type: { kind: 'named', name: '007' },
},
]);

expect(source).toMatchInlineSnapshot(`
"import * as rt from \\"runtypes\\";
const _007 = rt.String;
type _007 = rt.Static<typeof _007>;
const agent = _007;
type Agent = rt.Static<typeof agent>;
"
`);
});
});

it('generates runtype with `union` kind', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getNamedTypes,
getUnknownNamedTypes,
groupFieldKinds,
makeValidIdentifier,
rootToType,
topoSortRoots,
} from '../util';
Expand Down Expand Up @@ -606,3 +607,9 @@ describe('anyTypeToTsType', () => {
]);
});
});

test('makeValidIdentifier', () => {
expect(makeValidIdentifier('asdf')).toEqual('asdf');
expect(makeValidIdentifier('1234')).toEqual('_1234');
expect(makeValidIdentifier('a1234')).toEqual('a1234');
});
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
anyTypeToTsType,
getCyclicDependencies,
groupFieldKinds,
makeValidIdentifier,
topoSortRoots,
} from './util';

Expand Down Expand Up @@ -128,8 +129,9 @@ const defaultOptions: GenerateOptions = {
format: true,
includeImport: true,
includeTypes: true,
formatRuntypeName: (e) => e[0].toLowerCase() + e.slice(1),
formatTypeName: (e) => e[0].toUpperCase() + e.slice(1),
formatRuntypeName: (e) =>
makeValidIdentifier(e[0].toLowerCase() + e.slice(1)),
formatTypeName: (e) => makeValidIdentifier(e[0].toUpperCase() + e.slice(1)),
rejectCyclicDependencies: false,
rejectUnknownNamedTypes: false,
};
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,14 @@ export function topoSortRoots(roots: readonly RootType[]): RootType[] {
roots.forEach((e) => visitor(e));
return ret.reverse();
}

const startsWithNumberRegex = /^\d/;

/**
* If the name starts with a number, which is not allowed for identifiers in
* JavaScript, add a leading underscore to the name and return the new name.
* Otherwise, return the input unchanged.
*/
export function makeValidIdentifier(name: string): string {
return startsWithNumberRegex.test(name) ? `_${name}` : name;
}

0 comments on commit 7bcd1fb

Please sign in to comment.