Skip to content

Commit 8181b0c

Browse files
committed
Use single quote literals in output for tslint
1 parent ec7b27e commit 8181b0c

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

src/TsTypes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ export namespace TsType {
107107
export class Literal extends TsTypeBase {
108108
constructor(private value: any) { super() }
109109
_type() {
110-
return JSON.stringify(this.value)
110+
// if Literal is a string, format result with single quoted string literals
111+
var stringValue = JSON.stringify(this.value)
112+
const lastPos = stringValue.length - 1
113+
if (stringValue.charAt(0) === '"' && stringValue.charAt(lastPos) === '"') {
114+
stringValue = `'${stringValue.substring(1, lastPos).replace(/'/g,'\\\'')}'`
115+
}
116+
return stringValue
111117
}
112118
}
113119

test/cases/anyOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export var types = `export interface Foo {
4141
b?: number;
4242
}
4343
export interface Bar {
44-
a?: "a" | "b" | "c";
44+
a?: 'a' | 'b' | 'c';
4545
[k: string]: any;
4646
}
4747
export interface Baz {

test/cases/enum.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export var schema = {
99
"impliedStringEnum": {
1010
"enum": ["a", "b", "c"]
1111
},
12+
"literalWithSingleQuote": {
13+
"type": "string",
14+
"enum": ["Some 'string' with inner single quote", "b", "c"]
15+
},
1216
"booleanEnum": {
1317
"type" : "boolean",
1418
"enum": [ true ]
@@ -40,7 +44,7 @@ export var schema = {
4044
"enum": [-20.1, null, "foo", false]
4145
}
4246
},
43-
"required": ["stringEnum", "impliedStringEnum", "booleanEnum", "impliedBooleanEnum", "integerEnum", "impliedIntegerEnum", "impliedNamedIntegerEnum"],
47+
"required": ["stringEnum", "impliedStringEnum", "literalWithSingleQuote", "booleanEnum", "impliedBooleanEnum", "integerEnum", "impliedIntegerEnum", "impliedNamedIntegerEnum"],
4448
"additionalProperties": false
4549
}
4650

@@ -61,16 +65,17 @@ export${useConstEnums ? ' const ' : ' '}enum ImpliedNamedIntegerEnum {
6165
Six = 6
6266
}
6367
export interface Enum {
64-
stringEnum: "a" | "b" | "c";
65-
impliedStringEnum: "a" | "b" | "c";
68+
stringEnum: 'a' | 'b' | 'c';
69+
impliedStringEnum: 'a' | 'b' | 'c';
70+
literalWithSingleQuote: 'Some \\\'string\\\' with inner single quote' | 'b' | 'c';
6671
booleanEnum: true;
6772
impliedBooleanEnum: true;
6873
integerEnum: -1 | 0 | 1;
6974
impliedIntegerEnum: -1 | 0 | 1;
7075
numberEnum?: -1.1 | 0 | 1.2;
7176
namedIntegerEnum?: NamedIntegerEnum;
7277
impliedNamedIntegerEnum: ImpliedNamedIntegerEnum;
73-
impliedHeterogeneousEnum?: -20.1 | null | "foo" | false;
78+
impliedHeterogeneousEnum?: -20.1 | null | 'foo' | false;
7479
}`
7580
}
7681
})

test/cases/json-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export var configurations = [
158158
export type PositiveIntegerDefault0 = PositiveInteger;
159159
export type SchemaArray = HttpJsonSchemaOrgDraft04Schema[];
160160
export type StringArray = string[];
161-
export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";
161+
export type SimpleTypes = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
162162
/** Core schema meta-schema */
163163
export interface HttpJsonSchemaOrgDraft04Schema {
164164
id?: string;
@@ -208,7 +208,7 @@ export interface HttpJsonSchemaOrgDraft04Schema {
208208
settings: {
209209
declareSimpleType: false
210210
},
211-
types:`export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";
211+
types:`export type SimpleTypes = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
212212
/** Core schema meta-schema */
213213
export interface HttpJsonSchemaOrgDraft04Schema {
214214
id?: string;

test/cases/root-anyOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export var types = `export interface Foo {
3232
b?: number;
3333
}
3434
export interface Bar {
35-
a?: "a" | "b" | "c";
35+
a?: 'a' | 'b' | 'c';
3636
[k: string]: any;
3737
}
3838
export interface Baz {

test/test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
import { compile } from '../src/index'
2+
import { TsType } from '../src/TsTypes'
23
import test from 'ava'
34
import * as fs from 'fs'
45
import * as path from 'path'
56

7+
test('literal strings use single quotes for tslint', t => {
8+
const someLiteralString: string = 'a'
9+
t.is((new TsType.Literal(someLiteralString))._type(), `'${someLiteralString}'`)
10+
})
11+
test('literal number is not wrapped in quotes', t => {
12+
const aNumber: number = -1.2
13+
t.is((new TsType.Literal(aNumber))._type(), aNumber.toString())
14+
})
15+
test('literal boolean is not wrapped in quotes', t => {
16+
t.is((new TsType.Literal(true))._type(), 'true')
17+
})
18+
test('literal null is not wrapped in quotes', t => {
19+
t.is((new TsType.Literal(null))._type(), 'null')
20+
})
21+
test('literal object is not wrapped in quotes', t => {
22+
t.is((new TsType.Literal({}))._type(), '{}')
23+
})
24+
625
var modules = new Map<string, any>()
726
var dir = __dirname + '/cases'
827
fs.readdirSync(dir).forEach(function(moduleName) {

0 commit comments

Comments
 (0)