@xlr-lib/xlr-sdk 0.2.0--canary.2.131 → 1.0.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.
@@ -1,5 +1,5 @@
1
1
  import { test, expect, describe } from "vitest";
2
- import type { NamedType, TransformFunction, OrType } from "@xlr-lib/xlr";
2
+ import type { NamedType, OrType, ObjectType } from "@xlr-lib/xlr";
3
3
  import { parseTree } from "jsonc-parser";
4
4
  import { Types, ReferenceAssetsWebPluginManifest } from "@xlr-lib/static-xlrs";
5
5
  import type { Filters } from "../registry";
@@ -70,6 +70,19 @@ describe("Object Recall", () => {
70
70
 
71
71
  expect(sdk.getType("InputAsset")).toMatchSnapshot();
72
72
  });
73
+
74
+ test("Test Correct Generic Cascading", () => {
75
+ const sdk = new XLRSDK();
76
+ sdk.loadDefinitionsFromModule(Types);
77
+ sdk.loadDefinitionsFromModule(ReferenceAssetsWebPluginManifest);
78
+ const Flow = sdk.getType("Flow") as ObjectType;
79
+ const Schema = Flow.properties["schema"].node as ObjectType;
80
+ const Node = Schema.additionalProperties as ObjectType;
81
+ const DataTypes = Node.additionalProperties as OrType;
82
+ const DataType = DataTypes.or[0] as ObjectType;
83
+ const df = DataType.properties["default"].node;
84
+ expect(df.type).toBe("unknown");
85
+ });
73
86
  });
74
87
 
75
88
  describe("Validation", () => {
@@ -142,85 +155,6 @@ describe("Validation", () => {
142
155
  });
143
156
  });
144
157
 
145
- describe("Export Test", () => {
146
- test("Exports Typescript types", () => {
147
- const importMap = new Map([
148
- [
149
- "@player-ui/types",
150
- ["Expression", "Asset", "Binding", "AssetWrapper", "Schema.DataType"],
151
- ],
152
- ]);
153
-
154
- const sdk = new XLRSDK();
155
- sdk.loadDefinitionsFromModule(Types);
156
- sdk.loadDefinitionsFromModule(ReferenceAssetsWebPluginManifest);
157
- const results = sdk.exportRegistry("TypeScript", importMap);
158
- expect(results[0][0]).toBe("out.d.ts");
159
- expect(results[0][1]).toMatchSnapshot();
160
- });
161
-
162
- test("Exports Typescript Types With Filters", () => {
163
- const importMap = new Map([
164
- [
165
- "@player-ui/types",
166
- ["Expression", "Asset", "Binding", "AssetWrapper", "Schema.DataType"],
167
- ],
168
- ]);
169
-
170
- const sdk = new XLRSDK();
171
- sdk.loadDefinitionsFromModule(Types);
172
- sdk.loadDefinitionsFromModule(ReferenceAssetsWebPluginManifest);
173
- const results = sdk.exportRegistry("TypeScript", importMap, {
174
- typeFilter: "Transformed",
175
- pluginFilter: "Types",
176
- });
177
- expect(results[0][0]).toBe("out.d.ts");
178
- expect(results[0][1]).toMatchSnapshot();
179
- });
180
-
181
- test("Exports Typescript Types With Transforms", () => {
182
- const importMap = new Map([
183
- [
184
- "@player-ui/types",
185
- ["Expression", "Asset", "Binding", "AssetWrapper", "Schema.DataType"],
186
- ],
187
- ]);
188
-
189
- /**
190
- *
191
- */
192
- const transformFunction: TransformFunction = (input, capability) => {
193
- if (capability === "Assets") {
194
- const ret = { ...input };
195
- if (ret.type === "object") {
196
- ret.properties.transformed = {
197
- required: false,
198
- node: { type: "boolean", const: true },
199
- };
200
- }
201
-
202
- return ret;
203
- }
204
-
205
- return input;
206
- };
207
-
208
- const sdk = new XLRSDK();
209
- sdk.loadDefinitionsFromModule(Types);
210
- sdk.loadDefinitionsFromModule(ReferenceAssetsWebPluginManifest);
211
- const results = sdk.exportRegistry(
212
- "TypeScript",
213
- importMap,
214
- {
215
- pluginFilter: "Types",
216
- },
217
- [transformFunction],
218
- );
219
- expect(results[0][0]).toBe("out.d.ts");
220
- expect(results[0][1]).toMatchSnapshot();
221
- });
222
- });
223
-
224
158
  describe("Or Type Validation", () => {
225
159
  test("Outputs helpful type error messages", () => {
226
160
  const mockAsset = parseTree(`
package/src/sdk.ts CHANGED
@@ -8,7 +8,6 @@ import type {
8
8
  TransformFunction,
9
9
  TSManifest,
10
10
  } from "@xlr-lib/xlr";
11
- import type { TopLevelDeclaration } from "@xlr-lib/xlr-utils";
12
11
  import {
13
12
  computeEffectiveObject,
14
13
  resolveConditional,
@@ -16,14 +15,11 @@ import {
16
15
  } from "@xlr-lib/xlr-utils";
17
16
  import { fillInGenerics } from "@xlr-lib/xlr-utils";
18
17
  import type { Node } from "jsonc-parser";
19
- import { TSWriter } from "@xlr-lib/xlr-converters";
20
18
  import fs from "fs";
21
19
  import path from "path";
22
- import ts from "typescript";
23
20
 
24
21
  import type { XLRRegistry, Filters } from "./registry";
25
22
  import { BasicXLRRegistry } from "./registry";
26
- import type { ExportTypes } from "./types";
27
23
  import { XLRValidator } from "./validator";
28
24
  import { TransformFunctionMap, xlrTransformWalker } from "./utils";
29
25
 
@@ -40,14 +36,12 @@ export interface GetTypeOptions {
40
36
  export class XLRSDK {
41
37
  private registry: XLRRegistry;
42
38
  private validator: XLRValidator;
43
- private tsWriter: TSWriter;
44
39
  private computedNodeCache: Map<string, NodeType>;
45
40
  private externalTransformFunctions: Map<string, TransformFunction>;
46
41
 
47
42
  constructor(customRegistry?: XLRRegistry) {
48
43
  this.registry = customRegistry ?? new BasicXLRRegistry();
49
44
  this.validator = new XLRValidator(this.getType.bind(this));
50
- this.tsWriter = new TSWriter();
51
45
  this.computedNodeCache = new Map();
52
46
  this.externalTransformFunctions = new Map();
53
47
  }
@@ -265,43 +259,6 @@ export class XLRSDK {
265
259
  return this.validator.validateType(rootNode, type);
266
260
  }
267
261
 
268
- /**
269
- * Exports the types loaded into the registry to the specified format
270
- *
271
- * @param exportType - what format to export as
272
- * @param importMap - a map of primitive packages to types exported from that package to add import statements
273
- * @param filters - filter out plugins/capabilities/types you don't want to export
274
- * @param transforms - transforms to apply to types before exporting them
275
- * @returns [filename, content][] - Tuples of filenames and content to write
276
- */
277
- public exportRegistry(
278
- exportType: ExportTypes,
279
- importMap: Map<string, string[]>,
280
- filters?: Filters,
281
- transforms?: Array<TransformFunction>
282
- ): [string, string][] {
283
- const typesToExport = this.registry.list(filters).map((type) => {
284
- const effectiveType =
285
- transforms?.reduce(
286
- (typeAccumulator: NamedType<NodeType>, transformFn) =>
287
- transformFn(
288
- typeAccumulator,
289
- this.registry.info(type.name)?.capability as string
290
- ) as NamedType<NodeType>,
291
- type
292
- ) ?? type;
293
-
294
- return effectiveType;
295
- });
296
-
297
- if (exportType === "TypeScript") {
298
- const outputString = this.exportToTypeScript(typesToExport, importMap);
299
- return [["out.d.ts", outputString]];
300
- }
301
-
302
- throw new Error(`Unknown export format ${exportType}`);
303
- }
304
-
305
262
  /**
306
263
  * Transforms a generated XLR node into its final representation by resolving all `extends` properties.
307
264
  * If `optimize` is set to true the following operations are also performed:
@@ -311,7 +268,7 @@ export class XLRSDK {
311
268
  * - filing in any remaining generics with their default value
312
269
  */
313
270
  private resolveType(type: NamedType, optimize = true): NamedType {
314
- const resolvedObject = fillInGenerics(type);
271
+ const resolvedObject = fillInGenerics(type, new Map(), true);
315
272
 
316
273
  let transformMap: TransformFunctionMap = {
317
274
  object: [(objectNode: ObjectType) => {
@@ -391,68 +348,4 @@ export class XLRSDK {
391
348
  return xlrTransformWalker(transformMap)(resolvedObject) as NamedType
392
349
  }
393
350
 
394
- private exportToTypeScript(
395
- typesToExport: NamedType[],
396
- importMap: Map<string, string[]>
397
- ): string {
398
- const referencedImports: Set<string> = new Set();
399
- const exportedTypes: Map<string, TopLevelDeclaration> = new Map();
400
- const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
401
-
402
- let resultFile = ts.createSourceFile(
403
- "output.d.ts",
404
- "",
405
- ts.ScriptTarget.ES2017,
406
- false, // setParentNodes
407
- ts.ScriptKind.TS
408
- );
409
-
410
- typesToExport.forEach((typeNode) => {
411
- const { type, referencedTypes, additionalTypes } =
412
- this.tsWriter.convertNamedType(typeNode);
413
- exportedTypes.set(typeNode.name, type);
414
- additionalTypes?.forEach((additionalType, name) =>
415
- exportedTypes.set(name, additionalType)
416
- );
417
- referencedTypes?.forEach((referencedType) =>
418
- referencedImports.add(referencedType)
419
- );
420
- });
421
-
422
- const typesToPrint: Array<string> = [];
423
-
424
- exportedTypes.forEach((type) =>
425
- typesToPrint.push(
426
- printer.printNode(ts.EmitHint.Unspecified, type, resultFile)
427
- )
428
- );
429
-
430
- importMap.forEach((imports, packageName) => {
431
- const applicableImports = imports.filter((i) => referencedImports.has(i));
432
- resultFile = ts.factory.updateSourceFile(resultFile, [
433
- ts.factory.createImportDeclaration(
434
- /* modifiers */ undefined,
435
- ts.factory.createImportClause(
436
- false,
437
- undefined,
438
- ts.factory.createNamedImports(
439
- applicableImports.map((i) =>
440
- ts.factory.createImportSpecifier(
441
- false,
442
- undefined,
443
- ts.factory.createIdentifier(i)
444
- )
445
- )
446
- )
447
- ),
448
- ts.factory.createStringLiteral(packageName)
449
- ),
450
- ...resultFile.statements,
451
- ]);
452
- });
453
-
454
- const headerText = printer.printFile(resultFile);
455
- const nodeText = typesToPrint.join("\n");
456
- return `${headerText}\n${nodeText}`;
457
- }
458
351
  }
package/src/types.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import type { Node } from "jsonc-parser";
2
2
 
3
- /** Support Export Formats */
4
- export type ExportTypes = "TypeScript";
5
-
6
3
  export interface BaseValidationMessage<ErrorType extends string = string> {
7
4
  /** Validation Type */
8
5
  type: ErrorType;
package/types/sdk.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { NamedType, NodeType, TransformFunction, TSManifest } from "@xlr-lib/xlr";
2
2
  import type { Node } from "jsonc-parser";
3
3
  import type { XLRRegistry, Filters } from "./registry";
4
- import type { ExportTypes } from "./types";
5
4
  export interface GetTypeOptions {
6
5
  /** Resolves `extends` fields in objects */
7
6
  getRawType?: boolean;
@@ -14,7 +13,6 @@ export interface GetTypeOptions {
14
13
  export declare class XLRSDK {
15
14
  private registry;
16
15
  private validator;
17
- private tsWriter;
18
16
  private computedNodeCache;
19
17
  private externalTransformFunctions;
20
18
  constructor(customRegistry?: XLRRegistry);
@@ -87,16 +85,6 @@ export declare class XLRSDK {
87
85
  * @returns `Array<ValidationErrors>`
88
86
  */
89
87
  validateByType(type: NodeType, rootNode: Node): import("./types").ValidationMessage[];
90
- /**
91
- * Exports the types loaded into the registry to the specified format
92
- *
93
- * @param exportType - what format to export as
94
- * @param importMap - a map of primitive packages to types exported from that package to add import statements
95
- * @param filters - filter out plugins/capabilities/types you don't want to export
96
- * @param transforms - transforms to apply to types before exporting them
97
- * @returns [filename, content][] - Tuples of filenames and content to write
98
- */
99
- exportRegistry(exportType: ExportTypes, importMap: Map<string, string[]>, filters?: Filters, transforms?: Array<TransformFunction>): [string, string][];
100
88
  /**
101
89
  * Transforms a generated XLR node into its final representation by resolving all `extends` properties.
102
90
  * If `optimize` is set to true the following operations are also performed:
@@ -106,6 +94,5 @@ export declare class XLRSDK {
106
94
  * - filing in any remaining generics with their default value
107
95
  */
108
96
  private resolveType;
109
- private exportToTypeScript;
110
97
  }
111
98
  //# sourceMappingURL=sdk.d.ts.map
package/types/types.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  import type { Node } from "jsonc-parser";
2
- /** Support Export Formats */
3
- export type ExportTypes = "TypeScript";
4
2
  export interface BaseValidationMessage<ErrorType extends string = string> {
5
3
  /** Validation Type */
6
4
  type: ErrorType;