Skip to content

Commit 9b34f66

Browse files
committed
Merge remote-tracking branch 'upstream/master' into multi-defs
2 parents 9ccb230 + 6c6f9c9 commit 9b34f66

File tree

13 files changed

+400
-324
lines changed

13 files changed

+400
-324
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module.exports = {
3535
"@typescript-eslint/no-namespace": "error",
3636
"@typescript-eslint/no-non-null-assertion": "off",
3737
"max-len": ["error", { code: 120, tabWidth: 4 }],
38-
"linebreak-style": ["error", "unix"],
3938
"no-alert": "error",
4039
"prefer-const": "error",
4140
"no-return-assign": "error",

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.ts text eol=lf
2+
*.js text eol=lf

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ jobs:
2727
- name: Install Node dependencies
2828
run: yarn --frozen-lockfile
2929

30-
- name: lint (only on ubuntu)
31-
if: ${{ matrix.os !== 'windows-latest' }}
30+
- name: Lint
3231
run: yarn lint
3332

34-
- run: yarn build
33+
- name: Build
34+
run: yarn build
3535

3636
- name: Test coverage
3737
run: yarn jest test/ --collectCoverage=true

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-json-schema-generator",
3-
"version": "0.84.0",
3+
"version": "0.85.0",
44
"description": "Generate JSON schema from your Typescript sources",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -44,35 +44,35 @@
4444
"node": ">=10.0.0"
4545
},
4646
"dependencies": {
47-
"@types/json-schema": "^7.0.6",
48-
"commander": "^7.0.0",
47+
"@types/json-schema": "^7.0.7",
48+
"commander": "^7.1.0",
4949
"fast-json-stable-stringify": "^2.1.0",
5050
"glob": "^7.1.6",
5151
"json-stable-stringify": "^1.0.1",
52-
"typescript": "~4.1.3"
52+
"typescript": "~4.1.5"
5353
},
5454
"devDependencies": {
55-
"@babel/core": "^7.12.10",
56-
"@babel/preset-env": "^7.12.11",
57-
"@babel/preset-typescript": "^7.12.7",
55+
"@babel/core": "^7.12.16",
56+
"@babel/preset-env": "^7.12.16",
57+
"@babel/preset-typescript": "^7.12.16",
5858
"@semantic-release/git": "^9.0.0",
5959
"@types/fast-json-stable-stringify": "^2.0.0",
6060
"@types/glob": "^7.1.3",
6161
"@types/jest": "^26.0.20",
6262
"@types/json-stable-stringify": "^1.0.32",
63-
"@types/node": "^14.14.21",
64-
"@typescript-eslint/eslint-plugin": "^4.13.0",
65-
"@typescript-eslint/parser": "^4.13.0",
66-
"ajv": "^7.0.3",
63+
"@types/node": "^14.14.28",
64+
"@typescript-eslint/eslint-plugin": "^4.15.1",
65+
"@typescript-eslint/parser": "^4.15.1",
66+
"ajv": "^7.1.1",
6767
"ajv-formats": "^1.5.1",
68-
"chai": "^4.2.0",
69-
"eslint": "^7.18.0",
70-
"eslint-config-prettier": "^7.1.0",
68+
"chai": "^4.3.0",
69+
"eslint": "^7.20.0",
70+
"eslint-config-prettier": "^7.2.0",
7171
"eslint-plugin-prettier": "^3.3.1",
7272
"jest": "^26.6.3",
7373
"jest-junit": "^12.0.0",
7474
"prettier": "^2.2.1",
75-
"semantic-release": "^17.3.3",
75+
"semantic-release": "^17.3.9",
7676
"ts-node": "^9.1.1"
7777
},
7878
"scripts": {

src/NodeParser/TypeOperatorNodeParser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export class TypeOperatorNodeParser implements SubNodeParser {
2020
public createType(node: ts.TypeOperatorNode, context: Context): BaseType {
2121
const type = this.childNodeParser.createType(node.type, context);
2222
const derefed = derefType(type);
23+
// Remove readonly modifier from type
24+
if (node.operator === ts.SyntaxKind.ReadonlyKeyword && derefed) {
25+
return derefed;
26+
}
2327
if (derefed instanceof ArrayType) {
2428
return new NumberType();
2529
}

src/SchemaGenerator.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import { JSONSchema7, JSONSchema7Definition } from "json-schema";
1616

1717
export class SchemaGenerator {
1818
public constructor(
19-
private readonly program: ts.Program,
20-
private readonly nodeParser: NodeParser,
21-
private readonly typeFormatter: TypeFormatter,
22-
private readonly config?: Config
19+
protected readonly program: ts.Program,
20+
protected readonly nodeParser: NodeParser,
21+
protected readonly typeFormatter: TypeFormatter,
22+
protected readonly config?: Config
2323
) {}
2424

2525
public createSchema(fullName?: string): Schema {
@@ -62,7 +62,7 @@ export class SchemaGenerator {
6262
return resolveIdRefs(schema, idNameMap, this.config?.encodeRefs ?? true) as JSONSchema7;
6363
}
6464

65-
private getRootNodes(fullName: string | undefined) {
65+
protected getRootNodes(fullName: string | undefined) {
6666
if (fullName && fullName !== "*") {
6767
return [this.findNamedNode(fullName)];
6868
} else {
@@ -75,7 +75,7 @@ export class SchemaGenerator {
7575
return [...rootNodes.values()];
7676
}
7777
}
78-
private findNamedNode(fullName: string): ts.Node {
78+
protected findNamedNode(fullName: string): ts.Node {
7979
const typeChecker = this.program.getTypeChecker();
8080
const allTypes = new Map<string, ts.Node>();
8181
const { projectFiles, externalFiles } = this.partitionFiles();
@@ -94,14 +94,10 @@ export class SchemaGenerator {
9494

9595
throw new NoRootTypeError(fullName);
9696
}
97-
private getRootTypeDefinition(rootType: BaseType): Definition {
97+
protected getRootTypeDefinition(rootType: BaseType): Definition {
9898
return this.typeFormatter.getDefinition(rootType);
9999
}
100-
private appendRootChildDefinitions(
101-
rootType: BaseType,
102-
childDefinitions: StringMap<Definition>,
103-
idNameMap: Map<string, string>
104-
): void {
100+
protected appendRootChildDefinitions(rootType: BaseType, childDefinitions: StringMap<Definition>, idNameMap: Map<string, string>): void {
105101
const seen = new Set<string>();
106102
const children = this.typeFormatter
107103
.getChildren(rootType)
@@ -134,7 +130,7 @@ export class SchemaGenerator {
134130
return definitions;
135131
}, childDefinitions);
136132
}
137-
private partitionFiles() {
133+
protected partitionFiles() {
138134
const projectFiles = new Array<ts.SourceFile>();
139135
const externalFiles = new Array<ts.SourceFile>();
140136

@@ -145,7 +141,7 @@ export class SchemaGenerator {
145141

146142
return { projectFiles, externalFiles };
147143
}
148-
private appendTypes(
144+
protected appendTypes(
149145
sourceFiles: readonly ts.SourceFile[],
150146
typeChecker: ts.TypeChecker,
151147
types: Map<string, ts.Node>
@@ -154,7 +150,7 @@ export class SchemaGenerator {
154150
this.inspectNode(sourceFile, typeChecker, types);
155151
}
156152
}
157-
private inspectNode(node: ts.Node, typeChecker: ts.TypeChecker, allTypes: Map<string, ts.Node>): void {
153+
protected inspectNode(node: ts.Node, typeChecker: ts.TypeChecker, allTypes: Map<string, ts.Node>): void {
158154
switch (node.kind) {
159155
case ts.SyntaxKind.InterfaceDeclaration:
160156
case ts.SyntaxKind.ClassDeclaration:
@@ -173,17 +169,17 @@ export class SchemaGenerator {
173169
return;
174170
}
175171
}
176-
private isExportType(node: ts.Node): boolean {
172+
protected isExportType(node: ts.Node): boolean {
177173
if (this.config?.jsDoc !== "none" && hasJsDocTag(node, "internal")) {
178174
return false;
179175
}
180176
const localSymbol = localSymbolAtNode(node);
181177
return localSymbol ? "exportSymbol" in localSymbol : false;
182178
}
183-
private isGenericType(node: ts.TypeAliasDeclaration): boolean {
179+
protected isGenericType(node: ts.TypeAliasDeclaration): boolean {
184180
return !!(node.typeParameters && node.typeParameters.length > 0);
185181
}
186-
private getFullName(node: ts.Node, typeChecker: ts.TypeChecker): string {
182+
protected getFullName(node: ts.Node, typeChecker: ts.TypeChecker): string {
187183
const symbol = symbolAtNode(node)!;
188184
return typeChecker.getFullyQualifiedName(symbol).replace(/".*"\./, "");
189185
}

test/valid-data-other.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ describe("valid-data-other", () => {
6767
it("duplicates", assertValidSchema("duplicates", "MyType"));
6868
it("duplicates-composition", assertValidSchema("duplicates-composition", "MyObject"));
6969
it("duplicates-inheritance", assertValidSchema("duplicates-inheritance", "MyObject"));
70+
it("shorthand-array", assertValidSchema("shorthand-array", "MyType"));
7071
});

test/valid-data/duplicates/import1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type A = number;
1+
export type A = number;

test/valid-data/duplicates/import2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type A = string;
1+
export type A = string;

test/valid-data/duplicates/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {A as A1} from "./import1";
2-
import {A as A2} from "./import2";
3-
4-
export type MyType = A1 | A2;
1+
import {A as A1} from "./import1";
2+
import {A as A2} from "./import2";
3+
4+
export type MyType = A1 | A2;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface MyType {
2+
numberArray: number[];
3+
stringArray: readonly string[];
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"MyType": {
5+
"type": "object",
6+
"properties": {
7+
"numberArray": {
8+
"type": "array",
9+
"items": {
10+
"type": "number"
11+
}
12+
},
13+
"stringArray": {
14+
"type": "array",
15+
"items": {
16+
"type": "string"
17+
}
18+
}
19+
},
20+
"required": [
21+
"numberArray",
22+
"stringArray"
23+
],
24+
"additionalProperties": false
25+
}
26+
},
27+
"$ref": "#/definitions/MyType"
28+
}

0 commit comments

Comments
 (0)