ts-json-schema-generator 2.4.1-next.1 → 2.5.0--canary.2413.011011e.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.
Files changed (87) hide show
  1. package/README.md +22 -21
  2. package/dist/factory/generator.js +1 -1
  3. package/dist/factory/generator.js.map +1 -1
  4. package/dist/factory/parser.js +13 -2
  5. package/dist/factory/parser.js.map +1 -1
  6. package/dist/factory/program.js +5 -2
  7. package/dist/factory/program.js.map +1 -1
  8. package/dist/package.json +31 -31
  9. package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.d.ts +2 -1
  10. package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js +22 -10
  11. package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js.map +1 -1
  12. package/dist/src/Config.d.ts +5 -2
  13. package/dist/src/Config.js +1 -0
  14. package/dist/src/Config.js.map +1 -1
  15. package/dist/src/Error/BaseError.d.ts +1 -1
  16. package/dist/src/Error/BaseError.js +1 -2
  17. package/dist/src/Error/BaseError.js.map +1 -1
  18. package/dist/src/NodeParser/BinaryExpressionNodeParser.d.ts +13 -0
  19. package/dist/src/NodeParser/BinaryExpressionNodeParser.js +79 -0
  20. package/dist/src/NodeParser/BinaryExpressionNodeParser.js.map +1 -0
  21. package/dist/src/NodeParser/IdentifierNodeParser.d.ts +11 -0
  22. package/dist/src/NodeParser/IdentifierNodeParser.js +33 -0
  23. package/dist/src/NodeParser/IdentifierNodeParser.js.map +1 -0
  24. package/dist/src/NodeParser/MappedTypeNodeParser.js +1 -1
  25. package/dist/src/NodeParser/MappedTypeNodeParser.js.map +1 -1
  26. package/dist/src/NodeParser/NewExpressionParser.d.ts +13 -0
  27. package/dist/src/NodeParser/NewExpressionParser.js +41 -0
  28. package/dist/src/NodeParser/NewExpressionParser.js.map +1 -0
  29. package/dist/src/NodeParser/SpreadElementNodeParser.d.ts +10 -0
  30. package/dist/src/NodeParser/SpreadElementNodeParser.js +21 -0
  31. package/dist/src/NodeParser/SpreadElementNodeParser.js.map +1 -0
  32. package/dist/src/NodeParser/TypeOperatorNodeParser.d.ts +1 -1
  33. package/dist/src/NodeParser/TypeOperatorNodeParser.js +2 -1
  34. package/dist/src/NodeParser/TypeOperatorNodeParser.js.map +1 -1
  35. package/dist/src/NodeParser/TypeReferenceNodeParser.js +1 -1
  36. package/dist/src/NodeParser/TypeReferenceNodeParser.js.map +1 -1
  37. package/dist/src/SchemaGenerator.d.ts +2 -2
  38. package/dist/src/SchemaGenerator.js +17 -7
  39. package/dist/src/SchemaGenerator.js.map +1 -1
  40. package/dist/src/Type/AnnotatedType.d.ts +1 -3
  41. package/dist/src/Type/AnnotatedType.js.map +1 -1
  42. package/dist/src/Type/UnknownType.d.ts +1 -1
  43. package/dist/src/Type/UnknownType.js +1 -1
  44. package/dist/src/Type/UnknownType.js.map +1 -1
  45. package/dist/src/TypeFormatter/AnnotatedTypeFormatter.js +1 -1
  46. package/dist/src/TypeFormatter/AnnotatedTypeFormatter.js.map +1 -1
  47. package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js +10 -5
  48. package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js.map +1 -1
  49. package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.js +6 -2
  50. package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.js.map +1 -1
  51. package/dist/src/Utils/castArray.d.ts +1 -0
  52. package/dist/src/Utils/castArray.js +10 -0
  53. package/dist/src/Utils/castArray.js.map +1 -0
  54. package/dist/src/Utils/getFullDescription.d.ts +2 -0
  55. package/dist/src/Utils/getFullDescription.js +33 -0
  56. package/dist/src/Utils/getFullDescription.js.map +1 -0
  57. package/dist/src/Utils/narrowType.js.map +1 -1
  58. package/dist/src/Utils/nodeKey.d.ts +1 -1
  59. package/dist/src/Utils/nodeKey.js.map +1 -1
  60. package/dist/ts-json-schema-generator.js +7 -3
  61. package/dist/ts-json-schema-generator.js.map +1 -1
  62. package/dist/tsconfig.tsbuildinfo +1 -1
  63. package/factory/generator.ts +1 -1
  64. package/factory/parser.ts +19 -2
  65. package/factory/program.ts +7 -3
  66. package/package.json +31 -31
  67. package/src/AnnotationsReader/ExtendedAnnotationsReader.ts +25 -10
  68. package/src/Config.ts +124 -2
  69. package/src/Error/BaseError.ts +1 -3
  70. package/src/NodeParser/BinaryExpressionNodeParser.ts +99 -0
  71. package/src/NodeParser/IdentifierNodeParser.ts +38 -0
  72. package/src/NodeParser/MappedTypeNodeParser.ts +1 -1
  73. package/src/NodeParser/NewExpressionParser.ts +46 -0
  74. package/src/NodeParser/SpreadElementNodeParser.ts +25 -0
  75. package/src/NodeParser/TypeOperatorNodeParser.ts +2 -2
  76. package/src/NodeParser/TypeReferenceNodeParser.ts +1 -1
  77. package/src/SchemaGenerator.ts +26 -9
  78. package/src/Type/AnnotatedType.ts +1 -3
  79. package/src/Type/UnknownType.ts +1 -1
  80. package/src/TypeFormatter/AnnotatedTypeFormatter.ts +2 -2
  81. package/src/TypeFormatter/LiteralUnionTypeFormatter.ts +12 -6
  82. package/src/TypeFormatter/PrimitiveUnionTypeFormatter.ts +9 -2
  83. package/src/Utils/castArray.ts +7 -0
  84. package/src/Utils/getFullDescription.ts +39 -0
  85. package/src/Utils/narrowType.ts +1 -6
  86. package/src/Utils/nodeKey.ts +3 -1
  87. package/ts-json-schema-generator.ts +12 -3
@@ -5,7 +5,7 @@ export class UnknownType extends BaseType {
5
5
  /**
6
6
  * If the source for this UnknownType was from a failed operation than to an actual `unknown` type present in the source code.
7
7
  */
8
- readonly erroredSource: boolean,
8
+ readonly erroredSource = false,
9
9
  ) {
10
10
  super();
11
11
  }
@@ -8,7 +8,7 @@ import type { TypeFormatter } from "../TypeFormatter.js";
8
8
  import { derefType } from "../Utils/derefType.js";
9
9
 
10
10
  export function makeNullable(def: Definition): Definition {
11
- const union: Definition[] | undefined = (def.oneOf as Definition[]) || def.anyOf;
11
+ const union = (def.oneOf || def.anyOf) as Definition[] | undefined;
12
12
  if (union && union.filter((d: Definition) => d.type === "null").length === 0) {
13
13
  union.push({ type: "null" });
14
14
  } else if (def.type && def.type !== "object") {
@@ -58,7 +58,7 @@ export class AnnotatedTypeFormatter implements SubTypeFormatter {
58
58
  if ("discriminator" in annotations) {
59
59
  const deref = derefType(type.getType());
60
60
  if (deref instanceof UnionType) {
61
- deref.setDiscriminator(annotations.discriminator);
61
+ deref.setDiscriminator(annotations.discriminator as string);
62
62
  delete annotations.discriminator;
63
63
  } else {
64
64
  throw new JsonTypeError(
@@ -55,12 +55,18 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {
55
55
  appendTypeValues(type, typeValues);
56
56
  }
57
57
 
58
- const schema = {
59
- type: toEnumType(Array.from(typeNames)),
60
- enum: Array.from(typeValues),
61
- };
62
-
63
- return preserveLiterals ? { anyOf: [{ type: "string" }, schema] } : schema;
58
+ const schema =
59
+ typeNames.size === 1 && typeValues.size === 1
60
+ ? {
61
+ type: toEnumType(Array.from(typeNames)),
62
+ const: Array.from(typeValues)[0],
63
+ }
64
+ : {
65
+ type: toEnumType(Array.from(typeNames)),
66
+ enum: Array.from(typeValues),
67
+ };
68
+
69
+ return hasString ? { anyOf: [{ type: "string" }, schema] } : schema;
64
70
  }
65
71
 
66
72
  public getChildren(): BaseType[] {
@@ -6,7 +6,6 @@ import type { BaseType } from "../Type/BaseType.js";
6
6
  import { BooleanType } from "../Type/BooleanType.js";
7
7
  import { NullType } from "../Type/NullType.js";
8
8
  import { NumberType } from "../Type/NumberType.js";
9
- import { PrimitiveType } from "../Type/PrimitiveType.js";
10
9
  import { StringType } from "../Type/StringType.js";
11
10
  import { UnionType } from "../Type/UnionType.js";
12
11
  import { uniqueArray } from "../Utils/uniqueArray.js";
@@ -25,7 +24,15 @@ export class PrimitiveUnionTypeFormatter implements SubTypeFormatter {
25
24
  }
26
25
 
27
26
  protected isPrimitiveUnion(type: UnionType): boolean {
28
- return type.getTypes().every((item) => item instanceof PrimitiveType);
27
+ return type
28
+ .getTypes()
29
+ .every(
30
+ (item) =>
31
+ item instanceof StringType ||
32
+ item instanceof NumberType ||
33
+ item instanceof BooleanType ||
34
+ item instanceof NullType,
35
+ );
29
36
  }
30
37
 
31
38
  protected getPrimitiveType(item: BaseType): RawTypeName {
@@ -0,0 +1,7 @@
1
+ export function castArray<T>(input: undefined | T | T[]): undefined | T[] {
2
+ if (input === undefined) {
3
+ return undefined;
4
+ }
5
+
6
+ return Array.isArray(input) ? input : [input];
7
+ }
@@ -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
+ }
@@ -16,12 +16,7 @@ import { derefType } from "./derefType.js";
16
16
  * kept, when returning false it is removed.
17
17
  * @return The narrowed down type.
18
18
  */
19
- export function narrowType(
20
- type: BaseType,
21
- // TODO: remove the next line
22
- // eslint-disable-next-line no-shadow
23
- predicate: (type: BaseType) => boolean,
24
- ): BaseType {
19
+ export function narrowType(type: BaseType, predicate: (type: BaseType) => boolean): BaseType {
25
20
  const derefed = derefType(type);
26
21
  if (derefed instanceof UnionType || derefed instanceof EnumType) {
27
22
  let changed = false;
@@ -2,7 +2,9 @@ import stringify from "safe-stable-stringify";
2
2
  import type { Node } from "typescript";
3
3
  import type { Context } from "../NodeParser.js";
4
4
 
5
- export function hash(a: string | boolean | number | (string | boolean | number)[] | object): string | number {
5
+ export function hash(
6
+ a: string | boolean | number | (string | boolean | number | Record<string, unknown>)[] | Record<string, unknown>,
7
+ ): string | number {
6
8
  if (typeof a === "number") {
7
9
  return a;
8
10
  }
@@ -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>", "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 JsDoc annotations")
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(args.type);
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