Skip to content

Commit

Permalink
fix: escape characters in field names (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeh authored Jun 13, 2021
1 parent a0bc815 commit 3d9cf65
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@types/faker": "^5.5.6",
"@types/jest": "~26.0.22",
"@types/jsesc": "^3.0.0",
"@types/node": "~14.17.3",
"@typescript-eslint/eslint-plugin": "~4.26.1",
"@typescript-eslint/parser": "~4.26.1",
Expand All @@ -46,6 +47,7 @@
"typedoc-plugin-markdown": "^3.7.1"
},
"dependencies": {
"jsesc": "^3.0.2",
"prettier": "^2.3.1",
"runtypes": "^6.2.1",
"typescript": "~4.3.2"
Expand Down
44 changes: 40 additions & 4 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,42 @@ describe('runtype generation', () => {
"
`);
});

it('handles field names that must be quoted', async () => {
const raw = generateRuntypes({
name: 'test',
type: {
kind: 'record',
fields: [{ name: 'name with space', type: { kind: 'string' } }],
},
});
expect(raw).toMatchInlineSnapshot(`
"import * as rt from \\"runtypes\\";
const test = rt.Record({ \\"name with space\\": rt.String });
type Test = rt.Static<typeof test>;
"
`);
});

it('handles field names with quotes in', async () => {
const raw = generateRuntypes({
name: 'test',
type: {
kind: 'record',
fields: [{ name: 'name with "quote"', type: { kind: 'string' } }],
},
});
expect(raw).toMatchInlineSnapshot(`
"import * as rt from \\"runtypes\\";
const test = rt.Record({ 'name with \\"quote\\"': rt.String });
type Test = rt.Static<typeof test>;
"
`);
});
});

it('formatting', () => {
Expand Down Expand Up @@ -333,12 +369,12 @@ describe('runtype generation', () => {

const sourceUnformatted = generateRuntypes(root, { format: false });
expect(sourceUnformatted).toMatchInlineSnapshot(`
"import * as rt from \\"runtypes\\";
"import * as rt from \\"runtypes\\";
const person=rt.Record({id:rt.String,name:rt.String,age:rt.String,}).asReadonly();
const person=rt.Record({\\"id\\":rt.String,\\"name\\":rt.String,\\"age\\":rt.String,}).asReadonly();
type Person=rt.Static<typeof person>;"
`);
type Person=rt.Static<typeof person>;"
`);
});

it('omit imports', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as jsEsc from 'jsesc';
import { Options as PrettierOptions, format } from 'prettier';
import {
AnyType,
Expand Down Expand Up @@ -365,7 +366,7 @@ function writeRecordType(
if (field.comment) {
writeComment(w, field.comment);
}
w.write(field.name);
w.write(`"${jsEsc(field.name, { quotes: 'double' })}"`);
w.write(':');
writeAnyType(options, w, field.type);
w.write(',');
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,11 @@
jest-diff "^26.0.0"
pretty-format "^26.0.0"

"@types/jsesc@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/jsesc/-/jsesc-3.0.0.tgz#56983a9bf1bf3d19c6bc736b3fb5eb841eec607a"
integrity sha512-zcwBQyIczoTfJW6WGHmyTEe3Yg2DY3w1Y41MhaLSRrc5tOvbBJfVfJxY5EB3Ud9lQ1eIbdCsOtPbyJ4qjdXCcg==

"@types/json-schema@^7.0.7":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
Expand Down Expand Up @@ -2766,6 +2771,11 @@ jsesc@^2.5.1:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==

jsesc@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-3.0.2.tgz#bb8b09a6597ba426425f2e4a07245c3d00b9343e"
integrity sha512-xKqzzWXDttJuOcawBt4KnKHHIf5oQ/Cxax+0PWFG+DFDgHNAdi+TXECADI+RYiFUMmx8792xsMbbgXj4CwnP4g==

json-parse-better-errors@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
Expand Down

0 comments on commit 3d9cf65

Please sign in to comment.