Skip to content

Commit 42d2344

Browse files
authored
refactor(typescript): quote all keys (#15)
1 parent 6aafc7a commit 42d2344

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/__tests__/languages/typescript.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ describe('arrays', () => {
6868

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

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

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

9090
expect(
9191
convertTokenToTypeScript(
@@ -97,7 +97,9 @@ describe('maps', () => {
9797
falseKey: false,
9898
})
9999
)
100-
).toEqual('{ falseKey: boolean; nullKey: null; numberKey: number; stringKey: string; trueKey: boolean }');
100+
).toEqual(
101+
'{ "falseKey": boolean; "nullKey": null; "numberKey": number; "stringKey": string; "trueKey": boolean }'
102+
);
101103
});
102104

103105
it('maps should be able to be nested', () => {
@@ -115,11 +117,11 @@ describe('maps', () => {
115117
},
116118
})
117119
)
118-
).toEqual('{ a: { b: { c: { d: { key: string } } } } }');
120+
).toEqual('{ "a": { "b": { "c": { "d": { "key": string } } } } }');
119121
});
120122

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

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

184186
const expectedResult =
185-
'type GeneratedStruct = { printWidth: number; semi: boolean; singleQuote: boolean; tabWidth: number; useTabs: boolean }';
187+
'type GeneratedStruct = { "printWidth": number; "semi": boolean; "singleQuote": boolean; "tabWidth": number; "useTabs": boolean }';
186188

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

202204
const expectedResult =
203-
'type GeneratedStruct = { data: Array<{ length: number; message: string; retry_after: number }> }';
205+
'type GeneratedStruct = { "data": Array<{ "length": number; "message": string; "retry_after": number }> }';
204206

205207
expect(generateTypeScriptType(tokenize(JSON.parse(jsonStr)))).toEqual(expectedResult);
206208
});

src/languages/typescript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function convertMap(token: MapToken): string {
2424
const children = new Set<string>();
2525

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

3030
const childTypesArr = Array.from(children);

0 commit comments

Comments
 (0)