quicktype 18.0.5 → 19.0.4

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 (174) hide show
  1. package/README.md +1 -1
  2. package/dist/{cli/CompressedJSONFromStream.d.ts → CompressedJSONFromStream.d.ts} +1 -1
  3. package/dist/{cli/CompressedJSONFromStream.js → CompressedJSONFromStream.js} +2 -2
  4. package/dist/{cli/GraphQLIntrospection.d.ts → GraphQLIntrospection.d.ts} +0 -0
  5. package/dist/{cli/GraphQLIntrospection.js → GraphQLIntrospection.js} +1 -1
  6. package/dist/{cli/TypeSource.d.ts → TypeSource.d.ts} +2 -2
  7. package/dist/{cli/TypeSource.js → TypeSource.js} +0 -0
  8. package/dist/{cli/URLGrammar.d.ts → URLGrammar.d.ts} +0 -0
  9. package/dist/{cli/URLGrammar.js → URLGrammar.js} +1 -1
  10. package/dist/{cli/index.d.ts → index.d.ts} +1 -1
  11. package/dist/{cli/index.js → index.js} +4 -5
  12. package/package.json +61 -77
  13. package/dist/quicktype-core/Annotation.d.ts +0 -8
  14. package/dist/quicktype-core/Annotation.js +0 -16
  15. package/dist/quicktype-core/ConvenienceRenderer.d.ts +0 -138
  16. package/dist/quicktype-core/ConvenienceRenderer.js +0 -668
  17. package/dist/quicktype-core/DateTime.d.ts +0 -10
  18. package/dist/quicktype-core/DateTime.js +0 -57
  19. package/dist/quicktype-core/DeclarationIR.d.ts +0 -14
  20. package/dist/quicktype-core/DeclarationIR.js +0 -167
  21. package/dist/quicktype-core/EncodedMarkovChain.d.ts +0 -1
  22. package/dist/quicktype-core/EncodedMarkovChain.js +0 -4
  23. package/dist/quicktype-core/GatherNames.d.ts +0 -2
  24. package/dist/quicktype-core/GatherNames.js +0 -333
  25. package/dist/quicktype-core/Graph.d.ts +0 -12
  26. package/dist/quicktype-core/Graph.js +0 -220
  27. package/dist/quicktype-core/GraphRewriting.d.ts +0 -87
  28. package/dist/quicktype-core/GraphRewriting.js +0 -421
  29. package/dist/quicktype-core/MakeTransformations.d.ts +0 -4
  30. package/dist/quicktype-core/MakeTransformations.js +0 -193
  31. package/dist/quicktype-core/MarkovChain.d.ts +0 -15
  32. package/dist/quicktype-core/MarkovChain.js +0 -154
  33. package/dist/quicktype-core/Messages.d.ts +0 -294
  34. package/dist/quicktype-core/Messages.js +0 -105
  35. package/dist/quicktype-core/Naming.d.ts +0 -64
  36. package/dist/quicktype-core/Naming.js +0 -356
  37. package/dist/quicktype-core/Renderer.d.ts +0 -50
  38. package/dist/quicktype-core/Renderer.js +0 -274
  39. package/dist/quicktype-core/RendererOptions.d.ts +0 -71
  40. package/dist/quicktype-core/RendererOptions.js +0 -141
  41. package/dist/quicktype-core/Run.d.ts +0 -174
  42. package/dist/quicktype-core/Run.js +0 -393
  43. package/dist/quicktype-core/Source.d.ts +0 -65
  44. package/dist/quicktype-core/Source.js +0 -204
  45. package/dist/quicktype-core/TargetLanguage.d.ts +0 -34
  46. package/dist/quicktype-core/TargetLanguage.js +0 -66
  47. package/dist/quicktype-core/Transformers.d.ts +0 -209
  48. package/dist/quicktype-core/Transformers.js +0 -836
  49. package/dist/quicktype-core/Type.d.ts +0 -206
  50. package/dist/quicktype-core/Type.js +0 -716
  51. package/dist/quicktype-core/TypeBuilder.d.ts +0 -61
  52. package/dist/quicktype-core/TypeBuilder.js +0 -324
  53. package/dist/quicktype-core/TypeGraph.d.ts +0 -75
  54. package/dist/quicktype-core/TypeGraph.js +0 -420
  55. package/dist/quicktype-core/TypeUtils.d.ts +0 -33
  56. package/dist/quicktype-core/TypeUtils.js +0 -230
  57. package/dist/quicktype-core/UnifyClasses.d.ts +0 -16
  58. package/dist/quicktype-core/UnifyClasses.js +0 -159
  59. package/dist/quicktype-core/UnionBuilder.d.ts +0 -49
  60. package/dist/quicktype-core/UnionBuilder.js +0 -282
  61. package/dist/quicktype-core/attributes/AccessorNames.d.ts +0 -18
  62. package/dist/quicktype-core/attributes/AccessorNames.js +0 -182
  63. package/dist/quicktype-core/attributes/Constraints.d.ts +0 -38
  64. package/dist/quicktype-core/attributes/Constraints.js +0 -172
  65. package/dist/quicktype-core/attributes/Description.d.ts +0 -9
  66. package/dist/quicktype-core/attributes/Description.js +0 -97
  67. package/dist/quicktype-core/attributes/EnumValues.d.ts +0 -8
  68. package/dist/quicktype-core/attributes/EnumValues.js +0 -31
  69. package/dist/quicktype-core/attributes/StringTypes.d.ts +0 -28
  70. package/dist/quicktype-core/attributes/StringTypes.js +0 -189
  71. package/dist/quicktype-core/attributes/TypeAttributes.d.ts +0 -34
  72. package/dist/quicktype-core/attributes/TypeAttributes.js +0 -119
  73. package/dist/quicktype-core/attributes/TypeNames.d.ts +0 -46
  74. package/dist/quicktype-core/attributes/TypeNames.js +0 -274
  75. package/dist/quicktype-core/attributes/URIAttributes.d.ts +0 -8
  76. package/dist/quicktype-core/attributes/URIAttributes.js +0 -85
  77. package/dist/quicktype-core/index.d.ts +0 -48
  78. package/dist/quicktype-core/index.js +0 -180
  79. package/dist/quicktype-core/input/CompressedJSON.d.ts +0 -69
  80. package/dist/quicktype-core/input/CompressedJSON.js +0 -276
  81. package/dist/quicktype-core/input/FetchingJSONSchemaStore.d.ts +0 -6
  82. package/dist/quicktype-core/input/FetchingJSONSchemaStore.js +0 -18
  83. package/dist/quicktype-core/input/Inference.d.ts +0 -22
  84. package/dist/quicktype-core/input/Inference.js +0 -240
  85. package/dist/quicktype-core/input/Inputs.d.ts +0 -48
  86. package/dist/quicktype-core/input/Inputs.js +0 -145
  87. package/dist/quicktype-core/input/JSONSchemaInput.d.ts +0 -91
  88. package/dist/quicktype-core/input/JSONSchemaInput.js +0 -1041
  89. package/dist/quicktype-core/input/JSONSchemaStore.d.ts +0 -8
  90. package/dist/quicktype-core/input/JSONSchemaStore.js +0 -36
  91. package/dist/quicktype-core/input/PostmanCollection.d.ts +0 -5
  92. package/dist/quicktype-core/input/PostmanCollection.js +0 -60
  93. package/dist/quicktype-core/input/io/NodeIO.d.ts +0 -3
  94. package/dist/quicktype-core/input/io/NodeIO.js +0 -87
  95. package/dist/quicktype-core/input/io/get-stream/buffer-stream.d.ts +0 -2
  96. package/dist/quicktype-core/input/io/get-stream/buffer-stream.js +0 -45
  97. package/dist/quicktype-core/input/io/get-stream/index.d.ts +0 -9
  98. package/dist/quicktype-core/input/io/get-stream/index.js +0 -52
  99. package/dist/quicktype-core/language/All.d.ts +0 -3
  100. package/dist/quicktype-core/language/All.js +0 -57
  101. package/dist/quicktype-core/language/CPlusPlus.d.ts +0 -241
  102. package/dist/quicktype-core/language/CPlusPlus.js +0 -1847
  103. package/dist/quicktype-core/language/CSharp.d.ts +0 -201
  104. package/dist/quicktype-core/language/CSharp.js +0 -1850
  105. package/dist/quicktype-core/language/Crystal.d.ts +0 -37
  106. package/dist/quicktype-core/language/Crystal.js +0 -337
  107. package/dist/quicktype-core/language/Dart.d.ts +0 -66
  108. package/dist/quicktype-core/language/Dart.js +0 -576
  109. package/dist/quicktype-core/language/Elm.d.ts +0 -58
  110. package/dist/quicktype-core/language/Elm.js +0 -522
  111. package/dist/quicktype-core/language/Golang.d.ts +0 -51
  112. package/dist/quicktype-core/language/Golang.js +0 -460
  113. package/dist/quicktype-core/language/Haskell.d.ts +0 -56
  114. package/dist/quicktype-core/language/Haskell.js +0 -411
  115. package/dist/quicktype-core/language/JSONSchema.d.ts +0 -31
  116. package/dist/quicktype-core/language/JSONSchema.js +0 -169
  117. package/dist/quicktype-core/language/Java.d.ts +0 -125
  118. package/dist/quicktype-core/language/Java.js +0 -1081
  119. package/dist/quicktype-core/language/JavaScript.d.ts +0 -68
  120. package/dist/quicktype-core/language/JavaScript.js +0 -435
  121. package/dist/quicktype-core/language/JavaScriptPropTypes.d.ts +0 -40
  122. package/dist/quicktype-core/language/JavaScriptPropTypes.js +0 -213
  123. package/dist/quicktype-core/language/JavaScriptUnicodeMaps.d.ts +0 -2
  124. package/dist/quicktype-core/language/JavaScriptUnicodeMaps.js +0 -134
  125. package/dist/quicktype-core/language/Kotlin.d.ts +0 -109
  126. package/dist/quicktype-core/language/Kotlin.js +0 -832
  127. package/dist/quicktype-core/language/Objective-C.d.ts +0 -79
  128. package/dist/quicktype-core/language/Objective-C.js +0 -777
  129. package/dist/quicktype-core/language/Php.d.ts +0 -80
  130. package/dist/quicktype-core/language/Php.js +0 -702
  131. package/dist/quicktype-core/language/Pike.d.ts +0 -37
  132. package/dist/quicktype-core/language/Pike.js +0 -276
  133. package/dist/quicktype-core/language/Python.d.ts +0 -107
  134. package/dist/quicktype-core/language/Python.js +0 -969
  135. package/dist/quicktype-core/language/Rust.d.ts +0 -56
  136. package/dist/quicktype-core/language/Rust.js +0 -309
  137. package/dist/quicktype-core/language/Swift.d.ts +0 -96
  138. package/dist/quicktype-core/language/Swift.js +0 -1211
  139. package/dist/quicktype-core/language/TypeScriptFlow.d.ts +0 -77
  140. package/dist/quicktype-core/language/TypeScriptFlow.js +0 -286
  141. package/dist/quicktype-core/language/ruby/index.d.ts +0 -52
  142. package/dist/quicktype-core/language/ruby/index.js +0 -557
  143. package/dist/quicktype-core/language/ruby/keywords.d.ts +0 -3
  144. package/dist/quicktype-core/language/ruby/keywords.js +0 -358
  145. package/dist/quicktype-core/rewrites/CombineClasses.d.ts +0 -3
  146. package/dist/quicktype-core/rewrites/CombineClasses.js +0 -125
  147. package/dist/quicktype-core/rewrites/ExpandStrings.d.ts +0 -4
  148. package/dist/quicktype-core/rewrites/ExpandStrings.js +0 -143
  149. package/dist/quicktype-core/rewrites/FlattenStrings.d.ts +0 -3
  150. package/dist/quicktype-core/rewrites/FlattenStrings.js +0 -45
  151. package/dist/quicktype-core/rewrites/FlattenUnions.d.ts +0 -3
  152. package/dist/quicktype-core/rewrites/FlattenUnions.js +0 -47
  153. package/dist/quicktype-core/rewrites/InferMaps.d.ts +0 -3
  154. package/dist/quicktype-core/rewrites/InferMaps.js +0 -117
  155. package/dist/quicktype-core/rewrites/ReplaceObjectType.d.ts +0 -3
  156. package/dist/quicktype-core/rewrites/ReplaceObjectType.js +0 -60
  157. package/dist/quicktype-core/rewrites/ResolveIntersections.d.ts +0 -3
  158. package/dist/quicktype-core/rewrites/ResolveIntersections.js +0 -244
  159. package/dist/quicktype-core/support/Acronyms.d.ts +0 -10
  160. package/dist/quicktype-core/support/Acronyms.js +0 -1128
  161. package/dist/quicktype-core/support/Chance.d.ts +0 -38
  162. package/dist/quicktype-core/support/Chance.js +0 -2146
  163. package/dist/quicktype-core/support/Converters.d.ts +0 -6
  164. package/dist/quicktype-core/support/Converters.js +0 -16
  165. package/dist/quicktype-core/support/Strings.d.ts +0 -44
  166. package/dist/quicktype-core/support/Strings.js +0 -525
  167. package/dist/quicktype-core/support/Support.d.ts +0 -29
  168. package/dist/quicktype-core/support/Support.js +0 -162
  169. package/dist/quicktype-graphql-input/GraphQLSchema.d.ts +0 -645
  170. package/dist/quicktype-graphql-input/GraphQLSchema.js +0 -39
  171. package/dist/quicktype-graphql-input/index.d.ts +0 -17
  172. package/dist/quicktype-graphql-input/index.js +0 -407
  173. package/dist/quicktype-typescript-input/index.d.ts +0 -2
  174. package/dist/quicktype-typescript-input/index.js +0 -98
@@ -1,68 +0,0 @@
1
- import { Type, ClassProperty, ClassType } from "../Type";
2
- import { AcronymStyleOptions } from "../support/Acronyms";
3
- import { ConvertersOptions } from "../support/Converters";
4
- import { Sourcelike } from "../Source";
5
- import { Namer, Name } from "../Naming";
6
- import { ConvenienceRenderer } from "../ConvenienceRenderer";
7
- import { TargetLanguage } from "../TargetLanguage";
8
- import { StringTypeMapping } from "../TypeBuilder";
9
- import { BooleanOption, Option, OptionValues, EnumOption } from "../RendererOptions";
10
- import { RenderContext } from "../Renderer";
11
- export declare const javaScriptOptions: {
12
- acronymStyle: EnumOption<AcronymStyleOptions>;
13
- runtimeTypecheck: BooleanOption;
14
- runtimeTypecheckIgnoreUnknownProperties: BooleanOption;
15
- converters: EnumOption<ConvertersOptions>;
16
- rawType: EnumOption<"any" | "json">;
17
- };
18
- export type JavaScriptTypeAnnotations = {
19
- any: string;
20
- anyArray: string;
21
- anyMap: string;
22
- string: string;
23
- stringArray: string;
24
- boolean: string;
25
- never: string;
26
- };
27
- export declare class JavaScriptTargetLanguage extends TargetLanguage {
28
- constructor(displayName?: string, names?: string[], extension?: string);
29
- protected getOptions(): Option<any>[];
30
- get stringTypeMapping(): StringTypeMapping;
31
- get supportsOptionalClassProperties(): boolean;
32
- get supportsFullObjectType(): boolean;
33
- protected makeRenderer(renderContext: RenderContext, untypedOptionValues: {
34
- [name: string]: any;
35
- }): JavaScriptRenderer;
36
- }
37
- export declare const legalizeName: (s: string) => string;
38
- export declare class JavaScriptRenderer extends ConvenienceRenderer {
39
- private readonly _jsOptions;
40
- constructor(targetLanguage: TargetLanguage, renderContext: RenderContext, _jsOptions: OptionValues<typeof javaScriptOptions>);
41
- protected nameStyle(original: string, upper: boolean): string;
42
- protected makeNamedTypeNamer(): Namer;
43
- protected namerForObjectProperty(): Namer;
44
- protected makeUnionMemberNamer(): null;
45
- protected makeEnumCaseNamer(): Namer;
46
- protected namedTypeToNameForTopLevel(type: Type): Type | undefined;
47
- protected makeNameForProperty(c: ClassType, className: Name, p: ClassProperty, jsonName: string, _assignedName: string | undefined): Name | undefined;
48
- protected emitDescriptionBlock(lines: Sourcelike[]): void;
49
- typeMapTypeFor(t: Type): Sourcelike;
50
- typeMapTypeForProperty(p: ClassProperty): Sourcelike;
51
- emitBlock(source: Sourcelike, end: Sourcelike, emit: () => void): void;
52
- emitTypeMap(): void;
53
- protected deserializerFunctionName(name: Name): Sourcelike;
54
- protected deserializerFunctionLine(_t: Type, name: Name): Sourcelike;
55
- protected serializerFunctionName(name: Name): Sourcelike;
56
- protected serializerFunctionLine(_t: Type, name: Name): Sourcelike;
57
- protected get moduleLine(): string | undefined;
58
- protected get castFunctionLines(): [string, string];
59
- protected get typeAnnotations(): JavaScriptTypeAnnotations;
60
- protected emitConvertModuleBody(): void;
61
- protected emitConvertModuleHelpers(): void;
62
- protected emitConvertModule(): void;
63
- protected emitTypes(): void;
64
- protected emitUsageImportComment(): void;
65
- protected emitUsageComments(): void;
66
- protected emitModuleExports(): void;
67
- protected emitSourceStructure(): void;
68
- }
@@ -1,435 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JavaScriptRenderer = exports.legalizeName = exports.JavaScriptTargetLanguage = exports.javaScriptOptions = void 0;
4
- const collection_utils_1 = require("collection-utils");
5
- const TypeUtils_1 = require("../TypeUtils");
6
- const Acronyms_1 = require("../support/Acronyms");
7
- const Converters_1 = require("../support/Converters");
8
- const Strings_1 = require("../support/Strings");
9
- const Support_1 = require("../support/Support");
10
- const Source_1 = require("../Source");
11
- const Naming_1 = require("../Naming");
12
- const ConvenienceRenderer_1 = require("../ConvenienceRenderer");
13
- const TargetLanguage_1 = require("../TargetLanguage");
14
- const RendererOptions_1 = require("../RendererOptions");
15
- const JavaScriptUnicodeMaps_1 = require("./JavaScriptUnicodeMaps");
16
- exports.javaScriptOptions = {
17
- acronymStyle: (0, Acronyms_1.acronymOption)(Acronyms_1.AcronymStyleOptions.Pascal),
18
- runtimeTypecheck: new RendererOptions_1.BooleanOption("runtime-typecheck", "Verify JSON.parse results at runtime", true),
19
- runtimeTypecheckIgnoreUnknownProperties: new RendererOptions_1.BooleanOption("runtime-typecheck-ignore-unknown-properties", "Ignore unknown properties when verifying at runtime", false, "secondary"),
20
- converters: (0, Converters_1.convertersOption)(),
21
- rawType: new RendererOptions_1.EnumOption("raw-type", "Type of raw input (json by default)", [
22
- ["json", "json"],
23
- ["any", "any"]
24
- ], "json", "secondary")
25
- };
26
- class JavaScriptTargetLanguage extends TargetLanguage_1.TargetLanguage {
27
- constructor(displayName = "JavaScript", names = ["javascript", "js", "jsx"], extension = "js") {
28
- super(displayName, names, extension);
29
- }
30
- getOptions() {
31
- return [
32
- exports.javaScriptOptions.runtimeTypecheck,
33
- exports.javaScriptOptions.runtimeTypecheckIgnoreUnknownProperties,
34
- exports.javaScriptOptions.acronymStyle,
35
- exports.javaScriptOptions.converters,
36
- exports.javaScriptOptions.rawType
37
- ];
38
- }
39
- get stringTypeMapping() {
40
- const mapping = new Map();
41
- const dateTimeType = "date-time";
42
- mapping.set("date", dateTimeType);
43
- mapping.set("date-time", dateTimeType);
44
- return mapping;
45
- }
46
- get supportsOptionalClassProperties() {
47
- return true;
48
- }
49
- get supportsFullObjectType() {
50
- return true;
51
- }
52
- makeRenderer(renderContext, untypedOptionValues) {
53
- return new JavaScriptRenderer(this, renderContext, (0, RendererOptions_1.getOptionValues)(exports.javaScriptOptions, untypedOptionValues));
54
- }
55
- }
56
- exports.JavaScriptTargetLanguage = JavaScriptTargetLanguage;
57
- exports.legalizeName = (0, Strings_1.utf16LegalizeCharacters)(JavaScriptUnicodeMaps_1.isES3IdentifierPart);
58
- const identityNamingFunction = (0, Naming_1.funPrefixNamer)("properties", s => s);
59
- class JavaScriptRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
60
- _jsOptions;
61
- constructor(targetLanguage, renderContext, _jsOptions) {
62
- super(targetLanguage, renderContext);
63
- this._jsOptions = _jsOptions;
64
- }
65
- nameStyle(original, upper) {
66
- const acronyms = (0, Acronyms_1.acronymStyle)(this._jsOptions.acronymStyle);
67
- const words = (0, Strings_1.splitIntoWords)(original);
68
- return (0, Strings_1.combineWords)(words, exports.legalizeName, upper ? Strings_1.firstUpperWordStyle : Strings_1.allLowerWordStyle, Strings_1.firstUpperWordStyle, upper ? s => (0, Strings_1.capitalize)(acronyms(s)) : Strings_1.allLowerWordStyle, acronyms, "", JavaScriptUnicodeMaps_1.isES3IdentifierStart);
69
- }
70
- makeNamedTypeNamer() {
71
- return (0, Naming_1.funPrefixNamer)("types", s => this.nameStyle(s, true));
72
- }
73
- namerForObjectProperty() {
74
- return identityNamingFunction;
75
- }
76
- makeUnionMemberNamer() {
77
- return null;
78
- }
79
- makeEnumCaseNamer() {
80
- return (0, Naming_1.funPrefixNamer)("enum-cases", s => this.nameStyle(s, true));
81
- }
82
- namedTypeToNameForTopLevel(type) {
83
- return (0, TypeUtils_1.directlyReachableSingleNamedType)(type);
84
- }
85
- makeNameForProperty(c, className, p, jsonName, _assignedName) {
86
- // Ignore the assigned name
87
- return super.makeNameForProperty(c, className, p, jsonName, undefined);
88
- }
89
- emitDescriptionBlock(lines) {
90
- this.emitCommentLines(lines, " * ", "/**", " */");
91
- }
92
- typeMapTypeFor(t) {
93
- if (["class", "object", "enum"].indexOf(t.kind) >= 0) {
94
- return ['r("', this.nameForNamedType(t), '")'];
95
- }
96
- return (0, TypeUtils_1.matchType)(t, _anyType => '"any"', _nullType => `null`, _boolType => `true`, _integerType => `0`, _doubleType => `3.14`, _stringType => `""`, arrayType => ["a(", this.typeMapTypeFor(arrayType.items), ")"], _classType => (0, Support_1.panic)("We handled this above"), mapType => ["m(", this.typeMapTypeFor(mapType.values), ")"], _enumType => (0, Support_1.panic)("We handled this above"), unionType => {
97
- const children = Array.from(unionType.getChildren()).map((type) => this.typeMapTypeFor(type));
98
- return ["u(", ...(0, collection_utils_1.arrayIntercalate)(", ", children), ")"];
99
- }, transformedStringType => {
100
- if (transformedStringType.kind === "date-time") {
101
- return "Date";
102
- }
103
- return `""`;
104
- });
105
- }
106
- typeMapTypeForProperty(p) {
107
- const typeMap = this.typeMapTypeFor(p.type);
108
- if (!p.isOptional) {
109
- return typeMap;
110
- }
111
- return ["u(undefined, ", typeMap, ")"];
112
- }
113
- emitBlock(source, end, emit) {
114
- this.emitLine(source, "{");
115
- this.indent(emit);
116
- this.emitLine("}", end);
117
- }
118
- emitTypeMap() {
119
- const { any: anyAnnotation } = this.typeAnnotations;
120
- this.emitBlock(`const typeMap${anyAnnotation} = `, ";", () => {
121
- this.forEachObject("none", (t, name) => {
122
- const additionalProperties = t.getAdditionalProperties();
123
- const additional = additionalProperties !== undefined ? this.typeMapTypeFor(additionalProperties) : "false";
124
- this.emitLine('"', name, '": o([');
125
- this.indent(() => {
126
- this.forEachClassProperty(t, "none", (propName, jsonName, property) => {
127
- this.emitLine('{ json: "', (0, Strings_1.utf16StringEscape)(jsonName), '", js: "', (0, Source_1.modifySource)(Strings_1.utf16StringEscape, propName), '", typ: ', this.typeMapTypeForProperty(property), " },");
128
- });
129
- });
130
- this.emitLine("], ", additional, "),");
131
- });
132
- this.forEachEnum("none", (e, name) => {
133
- this.emitLine('"', name, '": [');
134
- this.indent(() => {
135
- this.forEachEnumCase(e, "none", (_caseName, jsonName) => {
136
- this.emitLine(`"${(0, Strings_1.utf16StringEscape)(jsonName)}",`);
137
- });
138
- });
139
- this.emitLine("],");
140
- });
141
- });
142
- }
143
- deserializerFunctionName(name) {
144
- return ["to", name];
145
- }
146
- deserializerFunctionLine(_t, name) {
147
- return ["function ", this.deserializerFunctionName(name), "(json)"];
148
- }
149
- serializerFunctionName(name) {
150
- const camelCaseName = (0, Source_1.modifySource)(Strings_1.camelCase, name);
151
- return [camelCaseName, "ToJson"];
152
- }
153
- serializerFunctionLine(_t, name) {
154
- return ["function ", this.serializerFunctionName(name), "(value)"];
155
- }
156
- get moduleLine() {
157
- return undefined;
158
- }
159
- get castFunctionLines() {
160
- return ["function cast(val, typ)", "function uncast(val, typ)"];
161
- }
162
- get typeAnnotations() {
163
- return {
164
- any: "",
165
- anyArray: "",
166
- anyMap: "",
167
- string: "",
168
- stringArray: "",
169
- boolean: "",
170
- never: ""
171
- };
172
- }
173
- emitConvertModuleBody() {
174
- const converter = (t, name) => {
175
- const typeMap = this.typeMapTypeFor(t);
176
- this.emitBlock([this.deserializerFunctionLine(t, name), " "], "", () => {
177
- const parsedJson = this._jsOptions.rawType === "json" ? "JSON.parse(json)" : "json";
178
- if (!this._jsOptions.runtimeTypecheck) {
179
- this.emitLine("return ", parsedJson, ";");
180
- }
181
- else {
182
- this.emitLine("return cast(", parsedJson, ", ", typeMap, ");");
183
- }
184
- });
185
- this.ensureBlankLine();
186
- this.emitBlock([this.serializerFunctionLine(t, name), " "], "", () => {
187
- if (this._jsOptions.rawType === "json") {
188
- if (!this._jsOptions.runtimeTypecheck) {
189
- this.emitLine("return JSON.stringify(value);");
190
- }
191
- else {
192
- this.emitLine("return JSON.stringify(uncast(value, ", typeMap, "), null, 2);");
193
- }
194
- }
195
- else {
196
- if (!this._jsOptions.runtimeTypecheck) {
197
- this.emitLine("return value;");
198
- }
199
- else {
200
- this.emitLine("return uncast(value, ", typeMap, ");");
201
- }
202
- }
203
- });
204
- };
205
- switch (this._jsOptions.converters) {
206
- case Converters_1.ConvertersOptions.AllObjects:
207
- this.forEachObject("interposing", converter);
208
- break;
209
- default:
210
- this.forEachTopLevel("interposing", converter);
211
- break;
212
- }
213
- }
214
- emitConvertModuleHelpers() {
215
- if (this._jsOptions.runtimeTypecheck) {
216
- const { any: anyAnnotation, anyArray: anyArrayAnnotation, anyMap: anyMapAnnotation, string: stringAnnotation, stringArray: stringArrayAnnotation, never: neverAnnotation } = this.typeAnnotations;
217
- this.ensureBlankLine();
218
- this
219
- .emitMultiline(`function invalidValue(typ${anyAnnotation}, val${anyAnnotation}, key${anyAnnotation}, parent${anyAnnotation} = '')${neverAnnotation} {
220
- const prettyTyp = prettyTypeName(typ);
221
- const parentText = parent ? \` on \${parent}\` : '';
222
- const keyText = key ? \` for key "\${key}"\` : '';
223
- throw Error(\`Invalid value\${keyText}\${parentText}. Expected \${prettyTyp} but got \${JSON.stringify(val)}\`);
224
- }
225
-
226
- function prettyTypeName(typ${anyAnnotation})${stringAnnotation} {
227
- if (Array.isArray(typ)) {
228
- if (typ.length === 2 && typ[0] === undefined) {
229
- return \`an optional \${prettyTypeName(typ[1])}\`;
230
- } else {
231
- return \`one of [\${typ.map(a => { return prettyTypeName(a); }).join(", ")}]\`;
232
- }
233
- } else if (typeof typ === "object" && typ.literal !== undefined) {
234
- return typ.literal;
235
- } else {
236
- return typeof typ;
237
- }
238
- }
239
-
240
- function jsonToJSProps(typ${anyAnnotation})${anyAnnotation} {
241
- if (typ.jsonToJS === undefined) {
242
- const map${anyAnnotation} = {};
243
- typ.props.forEach((p${anyAnnotation}) => map[p.json] = { key: p.js, typ: p.typ });
244
- typ.jsonToJS = map;
245
- }
246
- return typ.jsonToJS;
247
- }
248
-
249
- function jsToJSONProps(typ${anyAnnotation})${anyAnnotation} {
250
- if (typ.jsToJSON === undefined) {
251
- const map${anyAnnotation} = {};
252
- typ.props.forEach((p${anyAnnotation}) => map[p.js] = { key: p.json, typ: p.typ });
253
- typ.jsToJSON = map;
254
- }
255
- return typ.jsToJSON;
256
- }
257
-
258
- function transform(val${anyAnnotation}, typ${anyAnnotation}, getProps${anyAnnotation}, key${anyAnnotation} = '', parent${anyAnnotation} = '')${anyAnnotation} {
259
- function transformPrimitive(typ${stringAnnotation}, val${anyAnnotation})${anyAnnotation} {
260
- if (typeof typ === typeof val) return val;
261
- return invalidValue(typ, val, key, parent);
262
- }
263
-
264
- function transformUnion(typs${anyArrayAnnotation}, val${anyAnnotation})${anyAnnotation} {
265
- // val must validate against one typ in typs
266
- const l = typs.length;
267
- for (let i = 0; i < l; i++) {
268
- const typ = typs[i];
269
- try {
270
- return transform(val, typ, getProps);
271
- } catch (_) {}
272
- }
273
- return invalidValue(typs, val, key, parent);
274
- }
275
-
276
- function transformEnum(cases${stringArrayAnnotation}, val${anyAnnotation})${anyAnnotation} {
277
- if (cases.indexOf(val) !== -1) return val;
278
- return invalidValue(cases.map(a => { return l(a); }), val, key, parent);
279
- }
280
-
281
- function transformArray(typ${anyAnnotation}, val${anyAnnotation})${anyAnnotation} {
282
- // val must be an array with no invalid elements
283
- if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent);
284
- return val.map(el => transform(el, typ, getProps));
285
- }
286
-
287
- function transformDate(val${anyAnnotation})${anyAnnotation} {
288
- if (val === null) {
289
- return null;
290
- }
291
- const d = new Date(val);
292
- if (isNaN(d.valueOf())) {
293
- return invalidValue(l("Date"), val, key, parent);
294
- }
295
- return d;
296
- }
297
-
298
- function transformObject(props${anyMapAnnotation}, additional${anyAnnotation}, val${anyAnnotation})${anyAnnotation} {
299
- if (val === null || typeof val !== "object" || Array.isArray(val)) {
300
- return invalidValue(l(ref || "object"), val, key, parent);
301
- }
302
- const result${anyAnnotation} = {};
303
- Object.getOwnPropertyNames(props).forEach(key => {
304
- const prop = props[key];
305
- const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
306
- result[prop.key] = transform(v, prop.typ, getProps, key, ref);
307
- });
308
- Object.getOwnPropertyNames(val).forEach(key => {
309
- if (!Object.prototype.hasOwnProperty.call(props, key)) {
310
- result[key] = ${this._jsOptions.runtimeTypecheckIgnoreUnknownProperties
311
- ? `val[key]`
312
- : `transform(val[key], additional, getProps, key, ref)`};
313
- }
314
- });
315
- return result;
316
- }
317
-
318
- if (typ === "any") return val;
319
- if (typ === null) {
320
- if (val === null) return val;
321
- return invalidValue(typ, val, key, parent);
322
- }
323
- if (typ === false) return invalidValue(typ, val, key, parent);
324
- let ref${anyAnnotation} = undefined;
325
- while (typeof typ === "object" && typ.ref !== undefined) {
326
- ref = typ.ref;
327
- typ = typeMap[typ.ref];
328
- }
329
- if (Array.isArray(typ)) return transformEnum(typ, val);
330
- if (typeof typ === "object") {
331
- return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
332
- : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
333
- : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
334
- : invalidValue(typ, val, key, parent);
335
- }
336
- // Numbers can be parsed by Date but shouldn't be.
337
- if (typ === Date && typeof val !== "number") return transformDate(val);
338
- return transformPrimitive(typ, val);
339
- }
340
-
341
- ${this.castFunctionLines[0]} {
342
- return transform(val, typ, jsonToJSProps);
343
- }
344
-
345
- ${this.castFunctionLines[1]} {
346
- return transform(val, typ, jsToJSONProps);
347
- }
348
-
349
- function l(typ${anyAnnotation}) {
350
- return { literal: typ };
351
- }
352
-
353
- function a(typ${anyAnnotation}) {
354
- return { arrayItems: typ };
355
- }
356
-
357
- function u(...typs${anyArrayAnnotation}) {
358
- return { unionMembers: typs };
359
- }
360
-
361
- function o(props${anyArrayAnnotation}, additional${anyAnnotation}) {
362
- return { props, additional };
363
- }
364
-
365
- function m(additional${anyAnnotation}) {
366
- return { props: [], additional };
367
- }
368
-
369
- function r(name${stringAnnotation}) {
370
- return { ref: name };
371
- }
372
- `);
373
- this.emitTypeMap();
374
- }
375
- }
376
- emitConvertModule() {
377
- this.ensureBlankLine();
378
- this.emitMultiline(`// Converts JSON ${this._jsOptions.rawType === "json" ? "strings" : "types"} to/from your types`);
379
- if (this._jsOptions.runtimeTypecheck) {
380
- this.emitMultiline(`// and asserts the results${this._jsOptions.rawType === "json" ? " of JSON.parse" : ""} at runtime`);
381
- }
382
- const moduleLine = this.moduleLine;
383
- if (moduleLine === undefined) {
384
- this.emitConvertModuleBody();
385
- }
386
- else {
387
- this.emitBlock([moduleLine, " "], "", () => this.emitConvertModuleBody());
388
- }
389
- }
390
- emitTypes() {
391
- return;
392
- }
393
- emitUsageImportComment() {
394
- this.emitLine('// const Convert = require("./file");');
395
- }
396
- emitUsageComments() {
397
- this.emitMultiline(`// To parse this data:
398
- //`);
399
- this.emitUsageImportComment();
400
- this.emitLine("//");
401
- this.forEachTopLevel("none", (_t, name) => {
402
- const camelCaseName = (0, Source_1.modifySource)(Strings_1.camelCase, name);
403
- this.emitLine("// const ", camelCaseName, " = Convert.to", name, "(json);");
404
- });
405
- if (this._jsOptions.runtimeTypecheck) {
406
- this.emitLine("//");
407
- this.emitLine("// These functions will throw an error if the JSON doesn't");
408
- this.emitLine("// match the expected interface, even if the JSON is valid.");
409
- }
410
- }
411
- emitModuleExports() {
412
- this.ensureBlankLine();
413
- this.emitBlock("module.exports = ", ";", () => {
414
- this.forEachTopLevel("none", (_, name) => {
415
- const serializer = this.serializerFunctionName(name);
416
- const deserializer = this.deserializerFunctionName(name);
417
- this.emitLine('"', serializer, '": ', serializer, ",");
418
- this.emitLine('"', deserializer, '": ', deserializer, ",");
419
- });
420
- });
421
- }
422
- emitSourceStructure() {
423
- if (this.leadingComments !== undefined) {
424
- this.emitCommentLines(this.leadingComments);
425
- }
426
- else {
427
- this.emitUsageComments();
428
- }
429
- this.emitTypes();
430
- this.emitConvertModule();
431
- this.emitConvertModuleHelpers();
432
- this.emitModuleExports();
433
- }
434
- }
435
- exports.JavaScriptRenderer = JavaScriptRenderer;
@@ -1,40 +0,0 @@
1
- import { TargetLanguage } from "../TargetLanguage";
2
- import { Option, OptionValues, EnumOption } from "../RendererOptions";
3
- import { RenderContext } from "../Renderer";
4
- import { ConvenienceRenderer } from "../ConvenienceRenderer";
5
- import { Name, Namer } from "../Naming";
6
- import { AcronymStyleOptions } from "../support/Acronyms";
7
- import { ClassProperty, ClassType, Sourcelike, Type } from "..";
8
- export declare const javaScriptPropTypesOptions: {
9
- acronymStyle: EnumOption<AcronymStyleOptions>;
10
- converters: EnumOption<import("../support/Converters").ConvertersOptions>;
11
- moduleSystem: EnumOption<boolean>;
12
- };
13
- export declare class JavaScriptPropTypesTargetLanguage extends TargetLanguage {
14
- protected getOptions(): Option<any>[];
15
- constructor(displayName?: string, names?: string[], extension?: string);
16
- protected makeRenderer(renderContext: RenderContext, untypedOptionValues: {
17
- [name: string]: any;
18
- }): JavaScriptPropTypesRenderer;
19
- }
20
- export declare class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
21
- private readonly _jsOptions;
22
- constructor(targetLanguage: TargetLanguage, renderContext: RenderContext, _jsOptions: OptionValues<typeof javaScriptPropTypesOptions>);
23
- protected nameStyle(original: string, upper: boolean): string;
24
- protected makeNamedTypeNamer(): Namer;
25
- protected namerForObjectProperty(): Namer;
26
- protected makeUnionMemberNamer(): null;
27
- protected makeEnumCaseNamer(): Namer;
28
- protected namedTypeToNameForTopLevel(type: Type): Type | undefined;
29
- protected makeNameForProperty(c: ClassType, className: Name, p: ClassProperty, jsonName: string, _assignedName: string | undefined): Name | undefined;
30
- typeMapTypeFor(t: Type, required?: boolean): Sourcelike;
31
- typeMapTypeForProperty(p: ClassProperty): Sourcelike;
32
- private importStatement;
33
- protected emitUsageComments(): void;
34
- protected emitBlock(source: Sourcelike, end: Sourcelike, emit: () => void): void;
35
- protected emitImports(): void;
36
- private emitExport;
37
- protected emitTypes(): void;
38
- private emitObject;
39
- protected emitSourceStructure(): void;
40
- }