Skip to content
This repository has been archived by the owner on Nov 27, 2019. It is now read-only.

Quote interface field names when needed #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"fp-ts": "^0.4.5",
"lodash": "^4.17.4",
"read-pkg-up": "^2.0.0",
"rosie": "^1.6.0"
"rosie": "^1.6.0",
"unquoted-property-validator": "^1.0.2"
},
"jest": {
"moduleFileExtensions": [
Expand Down
10 changes: 7 additions & 3 deletions src/writers/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { groupBy, keys, toPairs } from "lodash";
import * as path from "path";
import unquotedValidator = require("unquoted-property-validator");
import { Config } from "../config";
import {
hasOptionalField,
Expand Down Expand Up @@ -57,13 +58,16 @@ const fieldToString = (field: Field): string => {
return field.type.name;
};

const encodeFieldKey = (key: string): string => unquotedValidator(key).quotedValue;

const interfaceToString = (config: Config, type: VisitedType): string => {
const iface = type.class as InterfaceType;
const fields = iface.fields
.map(field => {
const encodedKey = encodeFieldKey(field.key);
const key = config.nullableMode === "option" || field.required
? field.key
: `${field.key}?`;
? encodedKey
: `${encodedKey}?`;
const fieldType = fieldToString(field);
const maybeFieldType = config.nullableMode === "option" && !field.required
? `Option<${fieldType}>`
Expand Down Expand Up @@ -97,7 +101,7 @@ ${alternatives};
`;
};

const typeToString = (config: Config) => (type: VisitedType): string => {
export const typeToString = (config: Config) => (type: VisitedType): string => {
if (isArray(type.class)) {
return arrayToString(type);
}
Expand Down
56 changes: 56 additions & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Config } from "../src/config";
import { VisitedType } from "../src/schemaVisitor/types";
import { typeToString } from "../src/writers/types";

const config: Config = {
nullableMode: "nullable",
paths: {
input: "dummy",
library: "dummy",
project: "dummy",
types: "dummy",
utils: "dummy",
},
typeImports: {},
};

describe("an interface member with special characters", () => {
test("it is quoted correctly", () => {
const inputType: VisitedType = {
class: {
fields: [
{
key: "foo",
required: true,
type: {
class: {
kind: "basic",
type: "string",
},
name: "string",
},
},
{
key: "foo-dashed",
required: true,
type: {
class: {
kind: "basic",
type: "string",
},
name: "string",
},
},
],
kind: "interface",
},
name: "Test",
};
const outputDecl = typeToString(config)(inputType);
expect(outputDecl).toBe("" +
"export interface Test {\n" +
" foo: string;\n" +
" 'foo-dashed': string;\n" +
"}\n");
});
});
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,10 @@ universalify@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.0.tgz#9eb1c4651debcc670cc94f1a75762332bb967778"

unquoted-property-validator@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/unquoted-property-validator/-/unquoted-property-validator-1.0.2.tgz#edd75c6a4481fbbc094b3b4c35035863df6bbcef"

user-home@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/user-home/-/user-home-1.1.1.tgz#2b5be23a32b63a7c9deb8d0f28d485724a3df190"
Expand Down