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

Commit

Permalink
Quote interface field names when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
xkr47 committed Oct 25, 2017
1 parent b82b00b commit 185e768
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
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.0"
},
"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
9 changes: 9 additions & 0 deletions src/writers/unquoted-property-validator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "unquoted-property-validator" {
function unquotedValidator(propertyName: string): {
needsQuotes: boolean,
needsBrackets: boolean,
es3Warning: boolean,
quotedValue: string,
};
export = unquotedValidator;
}
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.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unquoted-property-validator/-/unquoted-property-validator-1.0.0.tgz#e63618c9cadd25ce82f0e33db951ffdfa694fc3a"

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

0 comments on commit 185e768

Please sign in to comment.