ts-json-schema-generator 2.4.1-next.0 → 2.5.0--canary.76e1a68.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -21
- package/dist/factory/generator.js +1 -1
- package/dist/factory/generator.js.map +1 -1
- package/dist/factory/parser.js +11 -2
- package/dist/factory/parser.js.map +1 -1
- package/dist/package.json +23 -22
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.d.ts +2 -1
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js +22 -10
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js.map +1 -1
- package/dist/src/Config.d.ts +5 -2
- package/dist/src/Config.js +1 -0
- package/dist/src/Config.js.map +1 -1
- package/dist/src/NodeParser/IdentifierNodeParser.d.ts +11 -0
- package/dist/src/NodeParser/IdentifierNodeParser.js +33 -0
- package/dist/src/NodeParser/IdentifierNodeParser.js.map +1 -0
- package/dist/src/NodeParser/MappedTypeNodeParser.js +4 -1
- package/dist/src/NodeParser/MappedTypeNodeParser.js.map +1 -1
- package/dist/src/NodeParser/NewExpressionParser.d.ts +13 -0
- package/dist/src/NodeParser/NewExpressionParser.js +41 -0
- package/dist/src/NodeParser/NewExpressionParser.js.map +1 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.d.ts +10 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.js +21 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.js.map +1 -0
- package/dist/src/NodeParser/TypeOperatorNodeParser.d.ts +1 -1
- package/dist/src/NodeParser/TypeOperatorNodeParser.js +2 -1
- package/dist/src/NodeParser/TypeOperatorNodeParser.js.map +1 -1
- package/dist/src/NodeParser/TypeReferenceNodeParser.js +1 -1
- package/dist/src/NodeParser/TypeReferenceNodeParser.js.map +1 -1
- package/dist/src/SchemaGenerator.d.ts +2 -2
- package/dist/src/SchemaGenerator.js +17 -7
- package/dist/src/SchemaGenerator.js.map +1 -1
- package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js +10 -5
- package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js.map +1 -1
- package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.js +6 -2
- package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.js.map +1 -1
- package/dist/src/Utils/castArray.d.ts +1 -0
- package/dist/src/Utils/castArray.js +10 -0
- package/dist/src/Utils/castArray.js.map +1 -0
- package/dist/src/Utils/derefType.d.ts +1 -0
- package/dist/src/Utils/derefType.js +13 -0
- package/dist/src/Utils/derefType.js.map +1 -1
- package/dist/src/Utils/getFullDescription.d.ts +2 -0
- package/dist/src/Utils/getFullDescription.js +33 -0
- package/dist/src/Utils/getFullDescription.js.map +1 -0
- package/dist/ts-json-schema-generator.js +7 -3
- package/dist/ts-json-schema-generator.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/factory/generator.ts +1 -1
- package/factory/parser.ts +17 -2
- package/package.json +23 -22
- package/src/AnnotationsReader/ExtendedAnnotationsReader.ts +25 -10
- package/src/Config.ts +124 -2
- package/src/NodeParser/IdentifierNodeParser.ts +38 -0
- package/src/NodeParser/MappedTypeNodeParser.ts +6 -2
- package/src/NodeParser/NewExpressionParser.ts +46 -0
- package/src/NodeParser/SpreadElementNodeParser.ts +25 -0
- package/src/NodeParser/TypeOperatorNodeParser.ts +2 -2
- package/src/NodeParser/TypeReferenceNodeParser.ts +1 -1
- package/src/SchemaGenerator.ts +26 -9
- package/src/TypeFormatter/LiteralUnionTypeFormatter.ts +12 -6
- package/src/TypeFormatter/PrimitiveUnionTypeFormatter.ts +9 -2
- package/src/Utils/castArray.ts +7 -0
- package/src/Utils/derefType.ts +19 -0
- package/src/Utils/getFullDescription.ts +39 -0
- package/ts-json-schema-generator.ts +12 -3
package/src/Utils/derefType.ts
CHANGED
|
@@ -3,8 +3,10 @@ import { AnnotatedType } from "../Type/AnnotatedType.js";
|
|
|
3
3
|
import type { BaseType } from "../Type/BaseType.js";
|
|
4
4
|
import { DefinitionType } from "../Type/DefinitionType.js";
|
|
5
5
|
import { HiddenType } from "../Type/HiddenType.js";
|
|
6
|
+
import { LiteralType } from "../Type/LiteralType.js";
|
|
6
7
|
import { NeverType } from "../Type/NeverType.js";
|
|
7
8
|
import { ReferenceType } from "../Type/ReferenceType.js";
|
|
9
|
+
import { UnionType } from "../Type/UnionType.js";
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* Dereference the type as far as possible.
|
|
@@ -38,6 +40,23 @@ export function isHiddenType(type: BaseType): boolean {
|
|
|
38
40
|
return false;
|
|
39
41
|
}
|
|
40
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Recursively checks whether the given type is a union composed entirely of literal types.
|
|
45
|
+
*/
|
|
46
|
+
export function isDeepLiteralUnion(type: BaseType): boolean {
|
|
47
|
+
const resolved = derefType(type);
|
|
48
|
+
|
|
49
|
+
if (resolved instanceof LiteralType) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (resolved instanceof UnionType) {
|
|
54
|
+
return resolved.getTypes().every((t) => isDeepLiteralUnion(t));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
41
60
|
export function derefAliasedType(type: BaseType): BaseType {
|
|
42
61
|
if (type instanceof AliasType) {
|
|
43
62
|
return derefAliasedType(type.getType());
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export function getFullDescription(node: ts.Node): string | undefined {
|
|
4
|
+
const sourceFile = node.getSourceFile();
|
|
5
|
+
const jsDocNodes = ts.getJSDocCommentsAndTags(node);
|
|
6
|
+
|
|
7
|
+
if (!jsDocNodes || jsDocNodes.length === 0) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let rawText = "";
|
|
12
|
+
|
|
13
|
+
for (const jsDoc of jsDocNodes) {
|
|
14
|
+
rawText += jsDoc.getFullText(sourceFile) + "\n";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
rawText = rawText.trim();
|
|
18
|
+
|
|
19
|
+
return getTextWithoutStars(rawText).trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getTextWithoutStars(inputText: string) {
|
|
23
|
+
const innerTextWithStars = inputText.replace(/^\/\*\*[^\S\n]*\n?/, "").replace(/(\r?\n)?[^\S\n]*\*\/$/, "");
|
|
24
|
+
|
|
25
|
+
return innerTextWithStars
|
|
26
|
+
.split(/\n/)
|
|
27
|
+
.map((line) => {
|
|
28
|
+
const trimmedLine = line.trimStart();
|
|
29
|
+
|
|
30
|
+
if (trimmedLine[0] !== "*") {
|
|
31
|
+
return line;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const textStartPos = trimmedLine[1] === " " ? 2 : 1;
|
|
35
|
+
|
|
36
|
+
return trimmedLine.substring(textStartPos);
|
|
37
|
+
})
|
|
38
|
+
.join("\n");
|
|
39
|
+
}
|
|
@@ -11,14 +11,14 @@ import pkg from "./package.json";
|
|
|
11
11
|
|
|
12
12
|
const args = new Command()
|
|
13
13
|
.option("-p, --path <path>", "Source file path")
|
|
14
|
-
.option("-t, --type <name
|
|
14
|
+
.option("-t, --type <name...>", "Type name(s)")
|
|
15
15
|
.option("-i, --id <name>", "$id for generated schema")
|
|
16
16
|
.option("-f, --tsconfig <path>", "Custom tsconfig.json path")
|
|
17
17
|
.addOption(
|
|
18
18
|
new Option("-e, --expose <expose>", "Type exposing").choices(["all", "none", "export"]).default("export"),
|
|
19
19
|
)
|
|
20
20
|
.addOption(
|
|
21
|
-
new Option("-j, --jsDoc <extended>", "Read
|
|
21
|
+
new Option("-j, --jsDoc <extended>", "Read JSDoc annotations")
|
|
22
22
|
.choices(["none", "basic", "extended"])
|
|
23
23
|
.default("extended"),
|
|
24
24
|
)
|
|
@@ -27,6 +27,14 @@ const args = new Command()
|
|
|
27
27
|
jsDoc: "extended",
|
|
28
28
|
}),
|
|
29
29
|
)
|
|
30
|
+
.addOption(
|
|
31
|
+
new Option(
|
|
32
|
+
"--full-description",
|
|
33
|
+
"Include the full raw JSDoc comment as `fullDescription` in the schema.",
|
|
34
|
+
).implies({
|
|
35
|
+
jsDoc: "extended",
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
30
38
|
.addOption(
|
|
31
39
|
new Option(
|
|
32
40
|
"--functions <functions>",
|
|
@@ -65,6 +73,7 @@ const config: Config = {
|
|
|
65
73
|
topRef: args.topRef,
|
|
66
74
|
jsDoc: args.jsDoc,
|
|
67
75
|
markdownDescription: args.markdownDescription,
|
|
76
|
+
fullDescription: args.fullDescription,
|
|
68
77
|
sortProps: !args.unstable,
|
|
69
78
|
strictTuples: args.strictTuples,
|
|
70
79
|
skipTypeCheck: !args.typeCheck,
|
|
@@ -75,7 +84,7 @@ const config: Config = {
|
|
|
75
84
|
};
|
|
76
85
|
|
|
77
86
|
try {
|
|
78
|
-
const schema = createGenerator(config).createSchema(
|
|
87
|
+
const schema = createGenerator(config).createSchema(config.type);
|
|
79
88
|
|
|
80
89
|
const stringify = config.sortProps ? stableStringify : JSON.stringify;
|
|
81
90
|
// need as string since TS can't figure out that the string | undefined case doesn't happen
|