@travetto/transformer 7.0.0-rc.0 → 7.0.0-rc.1
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/package.json +1 -1
- package/src/resolver/builder.ts +27 -25
- package/src/resolver/service.ts +1 -1
- package/src/state.ts +8 -8
package/package.json
CHANGED
package/src/resolver/builder.ts
CHANGED
|
@@ -16,6 +16,14 @@ const UNDEFINED = Symbol();
|
|
|
16
16
|
|
|
17
17
|
const MAPPED_TYPE_SET = new Set(['Omit', 'Pick', 'Required', 'Partial']);
|
|
18
18
|
const isMappedType = (type: string | undefined): type is MappedType['operation'] => MAPPED_TYPE_SET.has(type!);
|
|
19
|
+
const getMappedFields = (type: ts.Type): string[] | undefined => {
|
|
20
|
+
if (type.isStringLiteral()) {
|
|
21
|
+
return [type.value];
|
|
22
|
+
} else if (type.isUnion() && type.types.every(t => t.isStringLiteral())) {
|
|
23
|
+
return type.types.map(t => t.value);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
19
27
|
/**
|
|
20
28
|
* List of global types that can be parameterized
|
|
21
29
|
*/
|
|
@@ -117,7 +125,7 @@ export function TypeCategorize(resolver: TransformResolver, type: ts.Type): { ca
|
|
|
117
125
|
*/
|
|
118
126
|
export const TypeBuilder: {
|
|
119
127
|
[K in Category]: {
|
|
120
|
-
build(resolver: TransformResolver, type: ts.Type, alias?: ts.Symbol): AnyType | undefined;
|
|
128
|
+
build(resolver: TransformResolver, type: ts.Type, context: { alias?: ts.Symbol, node?: ts.Node }): AnyType | undefined;
|
|
121
129
|
finalize?(type: Type<K>): AnyType;
|
|
122
130
|
}
|
|
123
131
|
} = {
|
|
@@ -252,48 +260,42 @@ export const TypeBuilder: {
|
|
|
252
260
|
}
|
|
253
261
|
},
|
|
254
262
|
mapped: {
|
|
255
|
-
build: (resolver, type,
|
|
263
|
+
build: (resolver, type, context) => {
|
|
256
264
|
let mainType: ts.Type | undefined;
|
|
257
265
|
let fields: string[] | undefined;
|
|
258
266
|
let operation: string | undefined;
|
|
267
|
+
let name: string | undefined;
|
|
259
268
|
|
|
260
269
|
const decls = DeclarationUtil.getDeclarations(type).filter(x => ts.isTypeAliasDeclaration(x));
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
} else {
|
|
277
|
-
mainType = type.aliasTypeArguments?.[0]!;
|
|
278
|
-
operation = type.aliasSymbol?.escapedName.toString();
|
|
279
|
-
fields = type.getApparentProperties().map(p => p.getName());
|
|
270
|
+
const ref = decls[0]?.type;
|
|
271
|
+
|
|
272
|
+
if (ref && ts.isTypeReferenceNode(ref) && ref.typeArguments && ref.typeArguments.length > 0) {
|
|
273
|
+
const [first, second] = ref.typeArguments;
|
|
274
|
+
mainType = resolver.getType(first);
|
|
275
|
+
operation = ref.typeName.getText();
|
|
276
|
+
name = resolver.getTypeAsString(type)!;
|
|
277
|
+
fields = !second ? [] : getMappedFields(resolver.getType(second));
|
|
278
|
+
} else if (type.aliasTypeArguments && type.aliasSymbol) {
|
|
279
|
+
mainType = type.aliasTypeArguments[0];
|
|
280
|
+
operation = type.aliasSymbol.escapedName.toString();
|
|
281
|
+
fields = (type.aliasTypeArguments.length > 1) ? getMappedFields(type.aliasTypeArguments[1]) : [];
|
|
282
|
+
name = `${resolver.getTypeAsString(mainType)!}_${operation}_${fields?.join('_')}`;
|
|
280
283
|
}
|
|
281
284
|
|
|
282
285
|
if (!isMappedType(operation) || fields === undefined || !mainType || !mainType.isClass()) {
|
|
283
|
-
return TypeBuilder.shape.build(resolver, type,
|
|
286
|
+
return TypeBuilder.shape.build(resolver, type, context);
|
|
284
287
|
}
|
|
285
288
|
|
|
286
289
|
const importName = resolver.getTypeImportName(mainType) ?? '<unknown>';
|
|
287
290
|
const mappedClassName = resolver.getTypeAsString(mainType)!;
|
|
288
|
-
const name = resolver.getTypeAsString(type)!;
|
|
289
291
|
|
|
290
292
|
return { key: 'mapped', name, original: mainType, operation, importName, mappedClassName, fields };
|
|
291
293
|
}
|
|
292
294
|
},
|
|
293
295
|
shape: {
|
|
294
|
-
build: (resolver, type,
|
|
296
|
+
build: (resolver, type, context) => {
|
|
295
297
|
const tsFieldTypes: Record<string, ts.Type> = {};
|
|
296
|
-
const name = CoreUtil.getSymbol(alias ?? type)?.getName();
|
|
298
|
+
const name = CoreUtil.getSymbol(context?.alias ?? type)?.getName();
|
|
297
299
|
const importName = resolver.getTypeImportName(type) ?? '<unknown>';
|
|
298
300
|
const tsTypeArguments = resolver.getAllTypeArguments(type);
|
|
299
301
|
const props = resolver.getPropertiesOfType(type);
|
package/src/resolver/service.ts
CHANGED
|
@@ -123,7 +123,7 @@ export class SimpleResolver implements TransformResolver {
|
|
|
123
123
|
const { category, type } = TypeCategorize(this, resType);
|
|
124
124
|
const { build, finalize } = TypeBuilder[category];
|
|
125
125
|
|
|
126
|
-
let result = build(this, type, alias);
|
|
126
|
+
let result = build(this, type, { alias, node: node && 'kind' in node ? node : undefined });
|
|
127
127
|
|
|
128
128
|
// Convert via cache if needed
|
|
129
129
|
result = visited.getOrSet(type, result);
|
package/src/state.ts
CHANGED
|
@@ -27,6 +27,8 @@ function isRedefinableDeclaration(x: ts.Node): x is ts.InterfaceDeclaration | ts
|
|
|
27
27
|
return ts.isFunctionDeclaration(x) || ts.isClassDeclaration(x) || ts.isInterfaceDeclaration(x);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
const FOREIGN_TYPE_REGISTRY_FILE = '@travetto/runtime/src/function';
|
|
31
|
+
|
|
30
32
|
/**
|
|
31
33
|
* Transformer runtime state
|
|
32
34
|
*/
|
|
@@ -402,14 +404,12 @@ export class TransformerState implements State {
|
|
|
402
404
|
* Produce a foreign target type
|
|
403
405
|
*/
|
|
404
406
|
getForeignTarget(ret: ForeignType): ts.Expression {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
this.fromLiteral(`${ret.source.split('node_modules/')[1]}+${ret.name}`)
|
|
412
|
-
)
|
|
407
|
+
const file = this.importFile(FOREIGN_TYPE_REGISTRY_FILE);
|
|
408
|
+
return this.factory.createCallExpression(this.createAccess(
|
|
409
|
+
file.ident,
|
|
410
|
+
this.factory.createIdentifier('foreignType'),
|
|
411
|
+
), [], [
|
|
412
|
+
this.fromLiteral(`${ret.source.split('node_modules/')[1]}+${ret.name}`)
|
|
413
413
|
]);
|
|
414
414
|
}
|
|
415
415
|
|