Skip to content

Commit

Permalink
refactor(typescript): quote all keys (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 23, 2023
1 parent 6aafc7a commit 42d2344
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/__tests__/languages/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ describe('arrays', () => {

it('duplicate maps should be removed from arrays', () => {
expect(convertTokenToTypeScript(tokenize([{ key: 'mads' }, { key: 'was' }, { key: 'here' }]))).toEqual(
'Array<{ key: string }>'
'Array<{ "key": string }>'
);
});

it('maps should be able to be mixed in arrays', () => {
expect(convertTokenToTypeScript(tokenize([{ key: 1.23 }, { key: 'mads' }, { key: 1 }]))).toEqual(
'Array<{ key: number } | { key: string }>'
'Array<{ "key": number } | { "key": string }>'
);
});
});
Expand All @@ -85,7 +85,7 @@ describe('maps', () => {
});

it('maps should support primitive value children', () => {
expect(convertTokenToTypeScript(tokenize({ key: 'value' }))).toEqual('{ key: string }');
expect(convertTokenToTypeScript(tokenize({ key: 'value' }))).toEqual('{ "key": string }');

expect(
convertTokenToTypeScript(
Expand All @@ -97,7 +97,9 @@ describe('maps', () => {
falseKey: false,
})
)
).toEqual('{ falseKey: boolean; nullKey: null; numberKey: number; stringKey: string; trueKey: boolean }');
).toEqual(
'{ "falseKey": boolean; "nullKey": null; "numberKey": number; "stringKey": string; "trueKey": boolean }'
);
});

it('maps should be able to be nested', () => {
Expand All @@ -115,11 +117,11 @@ describe('maps', () => {
},
})
)
).toEqual('{ a: { b: { c: { d: { key: string } } } } }');
).toEqual('{ "a": { "b": { "c": { "d": { "key": string } } } } }');
});

it('it should be possible to mix map with arrays', () => {
expect(convertTokenToTypeScript(tokenize({ arr: [1.23] }))).toEqual('{ arr: Array<number> }');
expect(convertTokenToTypeScript(tokenize({ arr: [1.23] }))).toEqual('{ "arr": Array<number> }');
});

it('maps should be sorted automatically', () => {
Expand Down Expand Up @@ -182,7 +184,7 @@ describe('json2ts', async () => {
}`;

const expectedResult =
'type GeneratedStruct = { printWidth: number; semi: boolean; singleQuote: boolean; tabWidth: number; useTabs: boolean }';
'type GeneratedStruct = { "printWidth": number; "semi": boolean; "singleQuote": boolean; "tabWidth": number; "useTabs": boolean }';

expect(generateTypeScriptType(tokenize(JSON.parse(jsonStr)))).toEqual(expectedResult);
});
Expand All @@ -200,7 +202,7 @@ describe('json2ts', async () => {
}`;

const expectedResult =
'type GeneratedStruct = { data: Array<{ length: number; message: string; retry_after: number }> }';
'type GeneratedStruct = { "data": Array<{ "length": number; "message": string; "retry_after": number }> }';

expect(generateTypeScriptType(tokenize(JSON.parse(jsonStr)))).toEqual(expectedResult);
});
Expand Down
2 changes: 1 addition & 1 deletion src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function convertMap(token: MapToken): string {
const children = new Set<string>();

for (let i = 0; i < token.children.length; i += 1) {
children.add(`${token.children[i].key}: ${convertTokenToTypeScript(token.children[i])}`);
children.add(`"${token.children[i].key}": ${convertTokenToTypeScript(token.children[i])}`);
}

const childTypesArr = Array.from(children);
Expand Down

0 comments on commit 42d2344

Please sign in to comment.