Skip to content

Use single quote literals in output for tslint #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "npm run build-sources && npm test && npm run lint",
"build-sources": "tsc -p ./tsconfig.json",
"build-tests": "tsc -p ./tsconfig.test.json",
"build-tests": "rm -rf ./dist_tests && tsc -p ./tsconfig.test.json",
"watch": "tsc -p ./tsconfig.json --watch && tsc -p ./tsconfig.test.json --watch",
"lint": "tslint -c tslint.json src/*.ts",
"test": "npm run build-tests && ava",
Expand Down
8 changes: 7 additions & 1 deletion src/TsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ export namespace TsType {
export class Literal extends TsTypeBase {
constructor(private value: any) { super() }
_type() {
return JSON.stringify(this.value)
// if Literal is a string, format result with single quoted string literals
var stringValue = JSON.stringify(this.value)
const lastPos = stringValue.length - 1
if (stringValue.charAt(0) === '"' && stringValue.charAt(lastPos) === '"') {
stringValue = `'${stringValue.substring(1, lastPos).replace(/'/g,'\\\'')}'`
}
return stringValue
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/cases/additionalProperties.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "AdditionalProperties",
"type": "object",
Expand All @@ -10,6 +11,7 @@ export var schema = {
"type": "number"
}
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface AdditionalProperties {
foo?: string;
Expand Down
2 changes: 2 additions & 0 deletions test/cases/allOf.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "AllOf",
"type": "object",
Expand Down Expand Up @@ -30,6 +31,7 @@ export var schema = {
"required": ["foo", "bar"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface Foo {
a: string;
Expand Down
5 changes: 3 additions & 2 deletions test/cases/anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "AnyOf",
"type": "object",
Expand Down Expand Up @@ -34,14 +35,14 @@ export var schema = {
"required": ["foo"],
"additionalProperties": false
}

/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface Foo {
a: string;
b?: number;
}
export interface Bar {
a?: "a" | "b" | "c";
a?: 'a' | 'b' | 'c';
[k: string]: any;
}
export interface Baz {
Expand Down
2 changes: 2 additions & 0 deletions test/cases/array-of-type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Array of type",
"type": "object",
Expand All @@ -22,6 +23,7 @@ export var schema = {
}
}
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface ArrayOfType {
foo?: string[];
Expand Down
2 changes: 2 additions & 0 deletions test/cases/basics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"type": "object",
Expand Down Expand Up @@ -25,6 +26,7 @@ export var schema = {
},
"required": ["firstName", "lastName"]
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var configurations = [
{
Expand Down
2 changes: 2 additions & 0 deletions test/cases/disjoint-type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"description": "My cool schema",
Expand All @@ -12,6 +13,7 @@ export var schema = {
},
"required": ["value"]
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `/**
* My cool schema
Expand Down
19 changes: 13 additions & 6 deletions test/cases/enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Enum",
"type": "object",
Expand All @@ -9,6 +10,10 @@ export var schema = {
"impliedStringEnum": {
"enum": ["a", "b", "c"]
},
"literalWithSingleQuote": {
"type": "string",
"enum": ["Some 'string' with inner single quote", "b", "c"]
},
"booleanEnum": {
"type" : "boolean",
"enum": [ true ]
Expand All @@ -30,19 +35,20 @@ export var schema = {
"namedIntegerEnum": {
"type": "integer",
"enum": [1, 2, 3],
"tsEnumNames": ["One","Two","Three"]
"tsEnumNames": ["One", "Two", "Three"]
},
"impliedNamedIntegerEnum": {
"enum": [4, 5, 6],
"tsEnumNames": ["Four","Five","Six"]
"tsEnumNames": ["Four", "Five", "Six"]
},
"impliedHeterogeneousEnum": {
"enum": [-20.1, null, "foo", false]
}
},
"required": ["stringEnum", "impliedStringEnum", "booleanEnum", "impliedBooleanEnum", "integerEnum", "impliedIntegerEnum", "impliedNamedIntegerEnum"],
"required": ["stringEnum", "impliedStringEnum", "literalWithSingleQuote", "booleanEnum", "impliedBooleanEnum", "integerEnum", "impliedIntegerEnum", "impliedNamedIntegerEnum"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var configurations = [false, true].map(useConstEnums => {
return {
Expand All @@ -61,16 +67,17 @@ export${useConstEnums ? ' const ' : ' '}enum ImpliedNamedIntegerEnum {
Six = 6
}
export interface Enum {
stringEnum: "a" | "b" | "c";
impliedStringEnum: "a" | "b" | "c";
stringEnum: 'a' | 'b' | 'c';
impliedStringEnum: 'a' | 'b' | 'c';
literalWithSingleQuote: 'Some \\\'string\\\' with inner single quote' | 'b' | 'c';
booleanEnum: true;
impliedBooleanEnum: true;
integerEnum: -1 | 0 | 1;
impliedIntegerEnum: -1 | 0 | 1;
numberEnum?: -1.1 | 0 | 1.2;
namedIntegerEnum?: NamedIntegerEnum;
impliedNamedIntegerEnum: ImpliedNamedIntegerEnum;
impliedHeterogeneousEnum?: -20.1 | null | "foo" | false;
impliedHeterogeneousEnum?: -20.1 | null | 'foo' | false;
}`
}
})
2 changes: 2 additions & 0 deletions test/cases/enumValidation.5.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Enum",
"type": "object",
Expand All @@ -11,6 +12,7 @@ export var schema = {
"required": ["bar"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var settings = {
useTypescriptEnums: true
Expand Down
2 changes: 2 additions & 0 deletions test/cases/enumValidation.6.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Enum",
"type": "object",
Expand All @@ -11,6 +12,7 @@ export var schema = {
"required": ["bar"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var settings = {
useTypescriptEnums: true
Expand Down
8 changes: 5 additions & 3 deletions test/cases/json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -147,7 +148,8 @@ export var schema = {
"exclusiveMinimum": [ "minimum" ]
},
"default": {}
};
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var configurations = [
{
Expand All @@ -158,7 +160,7 @@ export var configurations = [
export type PositiveIntegerDefault0 = PositiveInteger;
export type SchemaArray = HttpJsonSchemaOrgDraft04Schema[];
export type StringArray = string[];
export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";
export type SimpleTypes = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
/**
* Core schema meta-schema
*/
Expand Down Expand Up @@ -210,7 +212,7 @@ export interface HttpJsonSchemaOrgDraft04Schema {
settings: {
declareSimpleType: false
},
types:`export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";
types:`export type SimpleTypes = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
/**
* Core schema meta-schema
*/
Expand Down
2 changes: 2 additions & 0 deletions test/cases/named-property.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"description": "My cool schema",
Expand All @@ -13,6 +14,7 @@ export var schema = {
}
}
}
/* tslint:enable:quotemark object-literal-key-quotes */

// TODO: 2nd block comment should annotate UserIdArray, not users
export var types = `export type UserIdArray = string[];
Expand Down
2 changes: 2 additions & 0 deletions test/cases/not-extendable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"type": "object",
Expand All @@ -17,6 +18,7 @@ export var schema = {
"required": ["firstName", "lastName"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface ExampleSchema {
firstName: string;
Expand Down
5 changes: 3 additions & 2 deletions test/cases/ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export var schema =
{
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Referencing",
"type": "object",
"properties": {
Expand All @@ -10,6 +10,7 @@ export var schema =
"required": ["foo"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var configurations = [
{
Expand Down
4 changes: 3 additions & 1 deletion test/cases/root-anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "RootAnyOf",
"anyOf": [
Expand Down Expand Up @@ -26,13 +27,14 @@ export var schema = {
}
}
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface Foo {
a: string;
b?: number;
}
export interface Bar {
a?: "a" | "b" | "c";
a?: 'a' | 'b' | 'c';
[k: string]: any;
}
export interface Baz {
Expand Down
2 changes: 2 additions & 0 deletions test/cases/unnamedSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"type": "object",
"properties": {
Expand All @@ -8,6 +9,7 @@ export var schema = {
"required": ["foo"],
"additionalProperties": false
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `export interface UnnamedSchema {
foo: string;
Expand Down
2 changes: 2 additions & 0 deletions test/cases/with-description-newlines.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"description": "My cool schema",
Expand All @@ -18,6 +19,7 @@ export var schema = {
},
"required": ["firstName", "lastName"]
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `/**
* My cool schema
Expand Down
2 changes: 2 additions & 0 deletions test/cases/with-description.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:quotemark object-literal-key-quotes */
export var schema = {
"title": "Example Schema",
"description": "My cool schema",
Expand All @@ -17,6 +18,7 @@ export var schema = {
},
"required": ["firstName", "lastName"]
}
/* tslint:enable:quotemark object-literal-key-quotes */

export var types = `/**
* My cool schema
Expand Down