@player-lang/functional-dsl-generator 0.0.2-next.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 (42) hide show
  1. package/dist/cjs/index.cjs +2146 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/index.legacy-esm.js +2075 -0
  4. package/dist/index.mjs +2075 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/package.json +38 -0
  7. package/src/__tests__/__snapshots__/generator.test.ts.snap +886 -0
  8. package/src/__tests__/builder-class-generator.test.ts +627 -0
  9. package/src/__tests__/cli.test.ts +685 -0
  10. package/src/__tests__/default-value-generator.test.ts +365 -0
  11. package/src/__tests__/generator.test.ts +2860 -0
  12. package/src/__tests__/import-generator.test.ts +444 -0
  13. package/src/__tests__/path-utils.test.ts +174 -0
  14. package/src/__tests__/type-collector.test.ts +674 -0
  15. package/src/__tests__/type-transformer.test.ts +934 -0
  16. package/src/__tests__/utils.test.ts +597 -0
  17. package/src/builder-class-generator.ts +254 -0
  18. package/src/cli.ts +285 -0
  19. package/src/default-value-generator.ts +307 -0
  20. package/src/generator.ts +257 -0
  21. package/src/import-generator.ts +331 -0
  22. package/src/index.ts +38 -0
  23. package/src/path-utils.ts +155 -0
  24. package/src/ts-morph-type-finder.ts +319 -0
  25. package/src/type-categorizer.ts +131 -0
  26. package/src/type-collector.ts +296 -0
  27. package/src/type-resolver.ts +266 -0
  28. package/src/type-transformer.ts +487 -0
  29. package/src/utils.ts +762 -0
  30. package/types/builder-class-generator.d.ts +56 -0
  31. package/types/cli.d.ts +6 -0
  32. package/types/default-value-generator.d.ts +74 -0
  33. package/types/generator.d.ts +102 -0
  34. package/types/import-generator.d.ts +77 -0
  35. package/types/index.d.ts +12 -0
  36. package/types/path-utils.d.ts +65 -0
  37. package/types/ts-morph-type-finder.d.ts +73 -0
  38. package/types/type-categorizer.d.ts +46 -0
  39. package/types/type-collector.d.ts +62 -0
  40. package/types/type-resolver.d.ts +49 -0
  41. package/types/type-transformer.d.ts +74 -0
  42. package/types/utils.d.ts +205 -0
@@ -0,0 +1,74 @@
1
+ import type { NodeType, ObjectType, RefType } from "@xlr-lib/xlr";
2
+ /**
3
+ * Interface for tracking Asset import needs and namespace mappings.
4
+ */
5
+ export interface TypeTransformContext {
6
+ /** Set to true when Asset type needs to be imported */
7
+ setNeedsAssetImport(value: boolean): void;
8
+ /** Get the current Asset import need state */
9
+ getNeedsAssetImport(): boolean;
10
+ /** Track a referenced type for import */
11
+ trackReferencedType(typeName: string): void;
12
+ /** Track a namespace import */
13
+ trackNamespaceImport(namespaceName: string): void;
14
+ /** Get the namespace member map for type resolution */
15
+ getNamespaceMemberMap(): Map<string, string>;
16
+ /** Get the generic parameter symbols */
17
+ getGenericParamSymbols(): Set<string>;
18
+ /** Get the AssetWrapper ancestor's RefType for a type extending AssetWrapper (via registry) */
19
+ getAssetWrapperExtendsRef(typeName: string): RefType | undefined;
20
+ }
21
+ /**
22
+ * Transforms XLR types to TypeScript type strings.
23
+ */
24
+ export declare class TypeTransformer {
25
+ private readonly context;
26
+ constructor(context: TypeTransformContext);
27
+ /**
28
+ * Determines if a type name should be tracked for import.
29
+ * A type should be tracked if it's not a generic parameter and not a builtin type.
30
+ * Note: PLAYER_BUILTINS (Asset, AssetWrapper, Binding, Expression) are filtered
31
+ * by isBuiltinType(), so no explicit Asset check is needed here.
32
+ */
33
+ private shouldTrackTypeForImport;
34
+ /**
35
+ * Transform an XLR type to a TypeScript type string.
36
+ * This is the core recursive transformation that adds TaggedTemplateValue support.
37
+ */
38
+ transformType(node: NodeType, forParameter?: boolean): string;
39
+ /**
40
+ * Transform a type for use in generic constraints and defaults.
41
+ * Unlike transformType(), this returns raw type names without FunctionalBuilder unions,
42
+ * since constraints define type bounds, not parameter types that accept builders.
43
+ *
44
+ * @param node - The type node to transform
45
+ * @returns The raw TypeScript type string
46
+ */
47
+ transformTypeForConstraint(node: NodeType): string;
48
+ /**
49
+ * Transform a reference type to TypeScript.
50
+ */
51
+ private transformRefType;
52
+ /**
53
+ * Transform a type that extends AssetWrapper into a combined union.
54
+ * Produces: InnerType | FunctionalBuilder<InnerType> | TypeName | FunctionalBuilder<TypeName> | FunctionalPartial<TypeName>
55
+ */
56
+ private transformAssetWrapperExtension;
57
+ /**
58
+ * Generate an inline object type for anonymous objects.
59
+ */
60
+ generateInlineObjectType(node: ObjectType, forParameter: boolean): string;
61
+ /**
62
+ * Check if a property name needs to be quoted in TypeScript.
63
+ * Property names with special characters like hyphens must be quoted.
64
+ */
65
+ private needsQuoting;
66
+ /**
67
+ * Get the full qualified name for a type if it's a namespace member.
68
+ * For example, "CrossfieldReference" -> "Validation.CrossfieldReference"
69
+ * if we've seen "Validation.CrossfieldReference" in the source.
70
+ * Returns the original name if no namespace mapping exists.
71
+ */
72
+ private resolveTypeName;
73
+ }
74
+ //# sourceMappingURL=type-transformer.d.ts.map
@@ -0,0 +1,205 @@
1
+ import type { NodeType, ObjectType, StringType, NumberType, BooleanType, TupleType, RefType } from "@xlr-lib/xlr";
2
+ import { isStringType, isNumberType, isBooleanType, isObjectType, isArrayType, isRefType, isOrType, isAndType, isRecordType, isNamedType } from "@xlr-lib/xlr-utils";
3
+ export { isStringType, isNumberType, isBooleanType, isObjectType, isArrayType, isRefType, isOrType, isAndType, isRecordType, isNamedType, };
4
+ /**
5
+ * Type guard for tuple type nodes
6
+ */
7
+ export declare function isTupleType(node: NodeType): node is TupleType;
8
+ /**
9
+ * Check if a primitive type has a const value (literal type)
10
+ */
11
+ export declare function isPrimitiveConst(node: NodeType): node is (StringType | NumberType | BooleanType) & {
12
+ const: unknown;
13
+ };
14
+ /**
15
+ * Check if a ref type is an AssetWrapper
16
+ */
17
+ export declare function isAssetWrapperRef(node: NodeType): boolean;
18
+ /**
19
+ * Check if a NodeType resolves to a type that extends AssetWrapper.
20
+ * Handles:
21
+ * - RefType nodes resolved via type registry
22
+ * - Inline ObjectType nodes with an `extends` field
23
+ * - Transitive chains: e.g., ListItem → ListItemBase → AssetWrapper
24
+ * Uses cycle detection to prevent infinite recursion on circular type hierarchies.
25
+ *
26
+ * @param node - The node to check
27
+ * @param typeRegistry - Map of type names to their ObjectType definitions
28
+ * @param visited - Set of already-visited type names for cycle detection
29
+ * @returns true if the node resolves to a type extending AssetWrapper
30
+ */
31
+ export declare function extendsAssetWrapper(node: NodeType, typeRegistry: TypeRegistry, visited?: Set<string>): boolean;
32
+ /**
33
+ * Get the AssetWrapper ancestor's RefType for a node that extends AssetWrapper.
34
+ * Handles both inline ObjectTypes and RefType lookups via registry.
35
+ * This allows extracting the generic argument (e.g., ImageAsset from AssetWrapper<ImageAsset>).
36
+ *
37
+ * @param node - The node to inspect
38
+ * @param typeRegistry - Map of type names to their ObjectType definitions
39
+ * @param visited - Set of already-visited type names for cycle detection
40
+ * @returns The RefType of the AssetWrapper ancestor, or undefined if not found
41
+ */
42
+ export declare function getAssetWrapperExtendsRef(node: NodeType, typeRegistry: TypeRegistry, visited?: Set<string>): RefType | undefined;
43
+ /**
44
+ * Look up the AssetWrapper ancestor's RefType by type name via the registry.
45
+ * Convenience wrapper around getAssetWrapperExtendsRef for name-based lookups.
46
+ */
47
+ export declare function getAssetWrapperExtendsRefByName(typeName: string, typeRegistry: TypeRegistry): RefType | undefined;
48
+ /**
49
+ * Check if a ref type is an Expression
50
+ */
51
+ export declare function isExpressionRef(node: NodeType): boolean;
52
+ /**
53
+ * Check if a ref type is a Binding
54
+ */
55
+ export declare function isBindingRef(node: NodeType): boolean;
56
+ /**
57
+ * Sanitize a property name by removing surrounding quotes.
58
+ * TypeScript allows quoted property names like "mime-type" which may
59
+ * end up in XLR with quotes preserved.
60
+ *
61
+ * @example
62
+ * sanitizePropertyName("'mime-type'") // "mime-type"
63
+ * sanitizePropertyName('"content-type"') // "content-type"
64
+ * sanitizePropertyName("normalProp") // "normalProp"
65
+ */
66
+ export declare function sanitizePropertyName(name: string): string;
67
+ /**
68
+ * Convert a property name to PascalCase for method names.
69
+ * Handles camelCase, kebab-case, snake_case inputs, and quoted property names.
70
+ *
71
+ * @example
72
+ * toPascalCase("myProperty") // "MyProperty"
73
+ * toPascalCase("my-property") // "MyProperty"
74
+ * toPascalCase("my_property") // "MyProperty"
75
+ * toPascalCase("'mime-type'") // "MimeType"
76
+ */
77
+ export declare function toPascalCase(str: string): string;
78
+ /**
79
+ * Convert a type name to a factory function name (camelCase)
80
+ */
81
+ export declare function toFactoryName(typeName: string): string;
82
+ /**
83
+ * Convert a type name to a builder class name
84
+ */
85
+ export declare function toBuilderClassName(typeName: string): string;
86
+ /**
87
+ * Check if an object type is complex enough to warrant its own builder class
88
+ */
89
+ export declare function isComplexObjectType(obj: ObjectType): boolean;
90
+ /**
91
+ * Get the asset type string from extends ref
92
+ */
93
+ export declare function getAssetTypeFromExtends(obj: ObjectType): string | undefined;
94
+ /**
95
+ * Information about a property for code generation
96
+ */
97
+ export interface PropertyInfo {
98
+ name: string;
99
+ node: NodeType;
100
+ required: boolean;
101
+ isSlot: boolean;
102
+ isArraySlot: boolean;
103
+ isArray: boolean;
104
+ }
105
+ /**
106
+ * Extract property information from an ObjectType
107
+ */
108
+ export declare function getPropertiesInfo(obj: ObjectType): PropertyInfo[];
109
+ /**
110
+ * Check if a type contains an array type (directly or within a union/intersection)
111
+ * This handles cases like `Array<T> | T` where the property can be either
112
+ */
113
+ export declare function containsArrayType(node: NodeType): boolean;
114
+ /**
115
+ * Split a string by commas, but only at the top level (ignoring commas inside angle brackets).
116
+ * This is needed for parsing generic parameter lists like "T extends Foo<A, B>, U = Bar<C, D>"
117
+ *
118
+ * @example
119
+ * splitAtTopLevelCommas("T extends Foo, U = Bar") // ["T extends Foo", "U = Bar"]
120
+ * splitAtTopLevelCommas("T extends Foo<A, B>, U") // ["T extends Foo<A, B>", "U"]
121
+ */
122
+ export declare function splitAtTopLevelCommas(str: string): string[];
123
+ /**
124
+ * Extract generic usage string from generic params declaration
125
+ * Converts "T extends Foo, U = Bar" to "<T, U>"
126
+ * Handles nested generics like "T extends Foo<A, B>, U = Bar<C, D>" correctly
127
+ */
128
+ export declare function extractGenericUsage(genericParams: string | undefined): string;
129
+ /**
130
+ * Set of TypeScript built-in types that should never be imported.
131
+ * These are either global types or utility types provided by TypeScript.
132
+ */
133
+ export declare const TYPESCRIPT_BUILTINS: Set<string>;
134
+ /**
135
+ * Set of Player-specific built-in types that have special handling
136
+ * and should not be imported as regular types.
137
+ */
138
+ export declare const PLAYER_BUILTINS: Set<string>;
139
+ /**
140
+ * Check if a type name is a built-in type (TypeScript or Player-specific)
141
+ * that should not be imported.
142
+ */
143
+ export declare function isBuiltinType(typeName: string): boolean;
144
+ /**
145
+ * Extracts the base type name from a ref string, handling nested generics.
146
+ * @example
147
+ * extractBaseName("MyType") // "MyType"
148
+ * extractBaseName("MyType<T>") // "MyType"
149
+ * extractBaseName("Map<string, Array<T>>") // "Map"
150
+ */
151
+ export declare function extractBaseName(ref: string): string;
152
+ /**
153
+ * Checks if a type name is a namespaced type (e.g., "Validation.CrossfieldReference").
154
+ * Returns the namespace and member name if it is, null otherwise.
155
+ */
156
+ export declare function parseNamespacedType(typeName: string): {
157
+ namespace: string;
158
+ member: string;
159
+ } | null;
160
+ /**
161
+ * Type registry for resolving named type references.
162
+ *
163
+ * This map stores XLR ObjectType definitions keyed by their type name.
164
+ * It's used by `findAssetWrapperPaths` to resolve references to named
165
+ * interface types when searching for nested AssetWrapper properties.
166
+ *
167
+ * Example usage:
168
+ * ```typescript
169
+ * const registry: TypeRegistry = new Map([
170
+ * ["ContentCardHeader", headerObjectType],
171
+ * ["SlotConfig", slotConfigObjectType],
172
+ * ]);
173
+ *
174
+ * // Now findAssetWrapperPaths can resolve ContentCardHeader references
175
+ * const paths = findAssetWrapperPaths(contentCardType, registry);
176
+ * ```
177
+ *
178
+ * Types should be registered when:
179
+ * - They are referenced by other types in the codebase
180
+ * - They contain AssetWrapper properties that need to be discovered
181
+ * - They are part of a nested type hierarchy
182
+ */
183
+ export type TypeRegistry = Map<string, ObjectType>;
184
+ /**
185
+ * Finds all paths to AssetWrapper properties within a type, including nested interfaces.
186
+ *
187
+ * This function recursively traverses the type tree to find all property paths
188
+ * that lead to AssetWrapper fields. It supports:
189
+ * - Direct AssetWrapper properties
190
+ * - Named interface references resolved via type registry
191
+ * - Arbitrary nesting depth
192
+ * - Cycle detection for recursive types
193
+ *
194
+ * @param node - The type node to search
195
+ * @param typeRegistry - Map of type names to their ObjectType definitions
196
+ * @returns Array of paths, where each path is an array of property names
197
+ *
198
+ * @example
199
+ * // For a type like:
200
+ * // interface ContentCard { header: ContentCardHeader }
201
+ * // interface ContentCardHeader { left: AssetWrapper }
202
+ * // Returns: [["header", "left"]]
203
+ */
204
+ export declare function findAssetWrapperPaths(node: NodeType, typeRegistry: TypeRegistry): string[][];
205
+ //# sourceMappingURL=utils.d.ts.map