Skip to content

Commit c646b64

Browse files
authored
Added prettier (#371)
* Ran "yarn add --dev prettier" * Copied .prettierrc.json from https://github.com/vega/ts-json-schema-generator/blob/c037035146bcc8b9a62d89d4e0d7c6273392333e/.prettierrc.json - Removed trailingComma option since "es5" is the default since Prettier v2.0.0 - Removed endOfLine option since "lf" is the default since Prettier v2.0.0 * Added a prettier-ignore comment so the formatting in the yargs configuration is not lost. * Added a "style" script to run prettier. * Added a prettier-ignore comment so the formatting in the validationKeywords const object is not lost. * Ran "yarn style" * Added test/*.ts to the files that Prettier formats via the "style" script. * Ran "yarn style"
1 parent f6461ff commit c646b64

File tree

8 files changed

+436
-223
lines changed

8 files changed

+436
-223
lines changed

.prettierrc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"overrides": [
5+
{
6+
"files": "*.json",
7+
"options": {
8+
"tabWidth": 2
9+
}
10+
},
11+
{
12+
"files": "*.yml",
13+
"options": {
14+
"tabWidth": 2
15+
}
16+
}
17+
]
18+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"ajv": "^6.12.4",
6161
"chai": "^4.2.0",
6262
"mocha": "^8.1.1",
63+
"prettier": "^2.1.1",
6364
"source-map-support": "^0.5.19",
6465
"ts-node": "^9.0.0",
6566
"tslint": "^6.1.3"
@@ -71,6 +72,7 @@
7172
"docs": "./update-docs.js",
7273
"run": "ts-node typescript-json-schema-cli.ts",
7374
"build": "tsc -p .",
74-
"lint": "tslint --project tsconfig.json -c tslint.json --exclude '**/*.d.ts'"
75+
"lint": "tslint --project tsconfig.json -c tslint.json --exclude '**/*.d.ts'",
76+
"style": "yarn prettier --write *.js *.ts test/*.ts"
7577
}
7678
}

test/error.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import { exec, getDefaultArgs } from "../typescript-json-schema";
33

44
describe("error", () => {
55
it("error-check", () => {
6-
assert.throws(() => {
7-
// This needs a valid path. "dates" chosen basically at random
8-
exec("test/programs/dates/", "MyObject", getDefaultArgs());
9-
}, Error, "No output definition. Probably caused by errors prior to this?");
6+
assert.throws(
7+
() => {
8+
// This needs a valid path. "dates" chosen basically at random
9+
exec("test/programs/dates/", "MyObject", getDefaultArgs());
10+
},
11+
Error,
12+
"No output definition. Probably caused by errors prior to this?"
13+
);
1014
});
1115
});

test/schema.test.ts

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ const ajv = new Ajv();
99

1010
const BASE = "test/programs/";
1111

12-
export function assertSchema(group: string, type: string, settings: TJS.PartialArgs = {}, compilerOptions?: TJS.CompilerOptions, only?: boolean) {
12+
export function assertSchema(
13+
group: string,
14+
type: string,
15+
settings: TJS.PartialArgs = {},
16+
compilerOptions?: TJS.CompilerOptions,
17+
only?: boolean
18+
) {
1319
const run = only ? it.only : it;
1420

1521
run(group + " should create correct schema", () => {
@@ -36,36 +42,49 @@ export function assertSchema(group: string, type: string, settings: TJS.PartialA
3642
});
3743
}
3844

39-
export function assertSchemas(group: string, type: string, settings: TJS.PartialArgs = {}, compilerOptions?: TJS.CompilerOptions) {
45+
export function assertSchemas(
46+
group: string,
47+
type: string,
48+
settings: TJS.PartialArgs = {},
49+
compilerOptions?: TJS.CompilerOptions
50+
) {
4051
it(group + " should create correct schema", () => {
4152
if (!("required" in settings)) {
4253
settings.required = true;
4354
}
4455

45-
const generator = TJS.buildGenerator(TJS.getProgramFromFiles([resolve(BASE + group + "/main.ts")], compilerOptions), settings);
56+
const generator = TJS.buildGenerator(
57+
TJS.getProgramFromFiles([resolve(BASE + group + "/main.ts")], compilerOptions),
58+
settings
59+
);
4660
const symbols = generator!.getSymbols(type);
4761

4862
for (let symbol of symbols) {
49-
const actual = generator!.getSchemaForSymbol(symbol.name);
63+
const actual = generator!.getSchemaForSymbol(symbol.name);
5064

51-
// writeFileSync(BASE + group + `/schema.${symbol.name}.json`, JSON.stringify(actual, null, 4) + "\n\n");
65+
// writeFileSync(BASE + group + `/schema.${symbol.name}.json`, JSON.stringify(actual, null, 4) + "\n\n");
5266

53-
const file = readFileSync(BASE + group + `/schema.${symbol.name}.json`, "utf8");
54-
const expected = JSON.parse(file);
67+
const file = readFileSync(BASE + group + `/schema.${symbol.name}.json`, "utf8");
68+
const expected = JSON.parse(file);
5569

56-
assert.isObject(actual);
57-
assert.deepEqual(actual, expected, "The schema is not as expected");
70+
assert.isObject(actual);
71+
assert.deepEqual(actual, expected, "The schema is not as expected");
5872

59-
// test against the meta schema
60-
if (actual !== null) {
61-
ajv.validateSchema(actual);
62-
assert.equal(ajv.errors, null, "The schema is not valid");
63-
}
73+
// test against the meta schema
74+
if (actual !== null) {
75+
ajv.validateSchema(actual);
76+
assert.equal(ajv.errors, null, "The schema is not valid");
77+
}
6478
}
6579
});
6680
}
6781

68-
export function assertRejection(group: string, type: string, settings: TJS.PartialArgs = {}, compilerOptions?: TJS.CompilerOptions) {
82+
export function assertRejection(
83+
group: string,
84+
type: string,
85+
settings: TJS.PartialArgs = {},
86+
compilerOptions?: TJS.CompilerOptions
87+
) {
6988
it(group + " should reject input", () => {
7089
let schema = null;
7190
assert.throws(() => {
@@ -122,27 +141,26 @@ describe("interfaces", () => {
122141
$schema: "http://json-schema.org/draft-07/schema#",
123142
definitions: {
124143
Some: {
125-
type: "string"
126-
}
144+
type: "string",
145+
},
127146
},
128147
properties: {
129148
some: {
130-
$ref: "#/definitions/Some"
131-
}
149+
$ref: "#/definitions/Some",
150+
},
132151
},
133-
type: "object"
152+
type: "object",
134153
});
135154
}
136155
});
137156
});
138157

139158
describe("schema", () => {
140-
141159
describe("type aliases", () => {
142160
assertSchema("type-alias-single", "MyString");
143161
assertSchema("type-alias-single-annotated", "MyString");
144162
assertSchema("type-aliases", "MyObject", {
145-
aliasRef: true
163+
aliasRef: true,
146164
});
147165
assertSchema("type-aliases-fixed-size-array", "MyFixedSizeArray");
148166
assertSchema("type-aliases-multitype-array", "MyArray");
@@ -151,12 +169,12 @@ describe("schema", () => {
151169
strictNullChecks: true,
152170
});
153171
assertSchema("type-aliases-partial", "MyObject", {
154-
aliasRef: true
172+
aliasRef: true,
155173
});
156174

157175
assertSchema("type-aliases-alias-ref", "MyAlias", {
158176
aliasRef: true,
159-
topRef: false
177+
topRef: false,
160178
});
161179
// disabled beacuse of #80
162180
// assertSchema("type-aliases-alias-ref-topref", "MyAlias", {
@@ -165,7 +183,7 @@ describe("schema", () => {
165183
// });
166184
assertSchema("type-aliases-recursive-object-topref", "MyObject", {
167185
aliasRef: true,
168-
topRef: true
186+
topRef: true,
169187
});
170188
// disabled beacuse of #80
171189
// assertSchema("type-aliases-recursive-alias-topref", "MyAlias", {
@@ -174,7 +192,7 @@ describe("schema", () => {
174192
// });
175193
assertSchema("type-no-aliases-recursive-topref", "MyAlias", {
176194
aliasRef: false,
177-
topRef: true
195+
topRef: true,
178196
});
179197

180198
assertSchema("type-mapped-types", "MyMappedType");
@@ -206,7 +224,7 @@ describe("schema", () => {
206224
describe("unions and intersections", () => {
207225
assertSchema("type-union", "MyObject");
208226
assertSchema("type-intersection", "MyObject", {
209-
noExtraProps: true
227+
noExtraProps: true,
210228
});
211229
assertSchema("type-union-tagged", "Shape");
212230
assertSchema("type-aliases-union-namespace", "MyModel");
@@ -217,15 +235,15 @@ describe("schema", () => {
217235
assertSchema("annotation-default", "MyObject");
218236
assertSchema("annotation-ref", "MyObject");
219237
assertSchema("annotation-tjs", "MyObject", {
220-
validationKeywords: [ "hide" ]
238+
validationKeywords: ["hide"],
221239
});
222240
assertSchema("annotation-id", "MyObject");
223241
assertSchema("annotation-items", "MyObject");
224242

225-
assertSchema("typeof-keyword", "MyObject", {typeOfKeyword: true});
243+
assertSchema("typeof-keyword", "MyObject", { typeOfKeyword: true });
226244

227245
assertSchema("user-validation-keywords", "MyObject", {
228-
validationKeywords: [ "chance", "important" ]
246+
validationKeywords: ["chance", "important"],
229247
});
230248
});
231249

@@ -236,9 +254,9 @@ describe("schema", () => {
236254
assertSchema("generic-multiargs", "MyObject");
237255
assertSchema("generic-anonymous", "MyObject");
238256
assertSchema("generic-recursive", "MyObject", {
239-
topRef: true
257+
topRef: true,
240258
});
241-
if(+typescriptVersionMajorMinor < 3.7) {
259+
if (+typescriptVersionMajorMinor < 3.7) {
242260
assertSchema("generic-hell", "MyObject");
243261
}
244262
});
@@ -247,7 +265,7 @@ describe("schema", () => {
247265
assertSchema("comments", "MyObject");
248266
assertSchema("comments-override", "MyObject");
249267
assertSchema("comments-imports", "MyObject", {
250-
aliasRef: true
268+
aliasRef: true,
251269
});
252270
});
253271

@@ -275,7 +293,7 @@ describe("schema", () => {
275293
assertSchema("interface-extends", "MyObject");
276294

277295
assertSchema("interface-recursion", "MyObject", {
278-
topRef: true
296+
topRef: true,
279297
});
280298

281299
assertSchema("module-interface-single", "MyObject");
@@ -310,7 +328,7 @@ describe("schema", () => {
310328
describe("dates", () => {
311329
assertSchema("dates", "MyObject");
312330
assertRejection("dates", "MyObject", {
313-
rejectDateType: true
331+
rejectDateType: true,
314332
});
315333
});
316334

@@ -322,16 +340,16 @@ describe("schema", () => {
322340

323341
describe("uniqueNames", () => {
324342
assertSchemas("unique-names", "MyObject", {
325-
uniqueNames: true
343+
uniqueNames: true,
326344
});
327345

328346
// It should throw an error if there are two definitions for the top-level ref
329347
assertRejection("unique-names", "MyObject", {
330-
uniqueNames: true
348+
uniqueNames: true,
331349
});
332350

333351
assertSchema("unique-names-multiple-subdefinitions", "MyObject", {
334-
uniqueNames: true
352+
uniqueNames: true,
335353
});
336354
});
337355

@@ -342,27 +360,27 @@ describe("schema", () => {
342360
assertSchema("optionals-derived", "MyDerived");
343361

344362
assertSchema("strict-null-checks", "MyObject", undefined, {
345-
strictNullChecks: true
363+
strictNullChecks: true,
346364
});
347365

348366
assertSchema("imports", "MyObject");
349367

350368
assertSchema("generate-all-types", "*");
351369

352370
assertSchema("private-members", "MyObject", {
353-
excludePrivate: true
371+
excludePrivate: true,
354372
});
355373

356374
assertSchema("builtin-names", "Ext.Foo");
357375

358376
assertSchema("user-symbols", "*");
359377

360378
assertSchemas("argument-id", "MyObject", {
361-
id: "someSchemaId"
379+
id: "someSchemaId",
362380
});
363381

364382
assertSchemas("type-default-number-as-integer", "*", {
365-
defaultNumberType: "integer"
383+
defaultNumberType: "integer",
366384
});
367385
});
368386

@@ -385,9 +403,9 @@ describe("tsconfig.json", () => {
385403
}
386404
});
387405
it("should support includeOnlyFiles with tsconfig.json", () => {
388-
const program = TJS.programFromConfig(
389-
resolve(BASE + "tsconfig/tsconfig.json"), [resolve(BASE + "tsconfig/includedAlways.ts")]
390-
);
406+
const program = TJS.programFromConfig(resolve(BASE + "tsconfig/tsconfig.json"), [
407+
resolve(BASE + "tsconfig/includedAlways.ts"),
408+
]);
391409
const generator = TJS.buildGenerator(program);
392410
assert(generator !== null);
393411
assert.instanceOf(generator, TJS.JsonSchemaGenerator);

typescript-json-schema-cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { exec, getDefaultArgs } from "./typescript-json-schema";
33
export function run() {
44
var helpText = "Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>";
55
const defaultArgs = getDefaultArgs();
6+
7+
// prettier-ignore
68
var args = require("yargs")
79
.usage(helpText)
810
.demand(2)

0 commit comments

Comments
 (0)