@travetto/transformer 7.0.0-rc.1 → 7.0.0-rc.3
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 +2 -2
- package/src/importer.ts +55 -55
- package/src/manager.ts +9 -4
- package/src/register.ts +62 -62
- package/src/resolver/builder.ts +34 -28
- package/src/resolver/coerce.ts +6 -6
- package/src/resolver/service.ts +9 -8
- package/src/resolver/types.ts +4 -0
- package/src/state.ts +68 -68
- package/src/types/shared.ts +3 -3
- package/src/types/visitor.ts +3 -3
- package/src/util/core.ts +21 -19
- package/src/util/declaration.ts +24 -24
- package/src/util/decorator.ts +16 -16
- package/src/util/doc.ts +9 -9
- package/src/util/import.ts +11 -11
- package/src/util/literal.ts +84 -84
- package/src/util/log.ts +10 -10
- package/src/visitor.ts +18 -18
package/src/resolver/builder.ts
CHANGED
|
@@ -19,8 +19,8 @@ const isMappedType = (type: string | undefined): type is MappedType['operation']
|
|
|
19
19
|
const getMappedFields = (type: ts.Type): string[] | undefined => {
|
|
20
20
|
if (type.isStringLiteral()) {
|
|
21
21
|
return [type.value];
|
|
22
|
-
} else if (type.isUnion() && type.types.every(
|
|
23
|
-
return type.types.map(
|
|
22
|
+
} else if (type.isUnion() && type.types.every(subType => subType.isStringLiteral())) {
|
|
23
|
+
return type.types.map(subType => subType.value);
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
|
|
@@ -82,7 +82,7 @@ export function TypeCategorize(resolver: TransformResolver, type: ts.Type): { ca
|
|
|
82
82
|
try {
|
|
83
83
|
const source = DeclarationUtil.getPrimaryDeclarationNode(type).getSourceFile();
|
|
84
84
|
const sourceFile = source.fileName;
|
|
85
|
-
if (sourceFile && ManifestModuleUtil.
|
|
85
|
+
if (sourceFile && ManifestModuleUtil.TYPINGS_EXT_REGEX.test(sourceFile) && !resolver.isKnownFile(sourceFile)) {
|
|
86
86
|
return { category: 'foreign', type };
|
|
87
87
|
}
|
|
88
88
|
} catch { }
|
|
@@ -101,7 +101,7 @@ export function TypeCategorize(resolver: TransformResolver, type: ts.Type): { ca
|
|
|
101
101
|
const sourceFile = source.fileName;
|
|
102
102
|
if (sourceFile?.includes('typescript/lib')) {
|
|
103
103
|
return { category: 'literal', type };
|
|
104
|
-
} else if (sourceFile && ManifestModuleUtil.
|
|
104
|
+
} else if (sourceFile && ManifestModuleUtil.TYPINGS_EXT_REGEX.test(sourceFile) && !resolver.isKnownFile(sourceFile)) {
|
|
105
105
|
return { category: 'foreign', type: resolvedType };
|
|
106
106
|
} else if (!resolvedType.isClass()) { // Not a real type
|
|
107
107
|
return { category: 'shape', type: resolvedType };
|
|
@@ -113,7 +113,7 @@ export function TypeCategorize(resolver: TransformResolver, type: ts.Type): { ca
|
|
|
113
113
|
} else if (type.isLiteral()) {
|
|
114
114
|
return { category: 'shape', type };
|
|
115
115
|
} else if (objectFlags & ts.ObjectFlags.Mapped) { // Mapped types
|
|
116
|
-
if (type.getProperties().some(
|
|
116
|
+
if (type.getProperties().some(property => property.declarations || property.valueDeclaration)) {
|
|
117
117
|
return { category: 'mapped', type };
|
|
118
118
|
}
|
|
119
119
|
}
|
|
@@ -160,7 +160,7 @@ export const TypeBuilder: {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
if (values.length > 0) {
|
|
163
|
-
return ({ key: 'template', template: {
|
|
163
|
+
return ({ key: 'template', template: { operation: 'and', values }, ctor: String });
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
}
|
|
@@ -207,7 +207,7 @@ export const TypeBuilder: {
|
|
|
207
207
|
build: (resolver, type) => {
|
|
208
208
|
const name = CoreUtil.getSymbol(type)?.getName();
|
|
209
209
|
const source = DeclarationUtil.getPrimaryDeclarationNode(type).getSourceFile();
|
|
210
|
-
return { key: 'foreign', name, source: source.fileName };
|
|
210
|
+
return { key: 'foreign', name, source: source.fileName, classId: `${source.fileName.split('node_modules/')[1]}+${name}` };
|
|
211
211
|
}
|
|
212
212
|
},
|
|
213
213
|
managed: {
|
|
@@ -223,11 +223,11 @@ export const TypeBuilder: {
|
|
|
223
223
|
let undefinable = false;
|
|
224
224
|
let nullable = false;
|
|
225
225
|
const remainder = uType.types.filter(ut => {
|
|
226
|
-
const
|
|
227
|
-
const
|
|
228
|
-
undefinable ||=
|
|
229
|
-
nullable ||=
|
|
230
|
-
return !(
|
|
226
|
+
const isUndefined = (ut.getFlags() & (ts.TypeFlags.Undefined)) > 0;
|
|
227
|
+
const isNull = (ut.getFlags() & (ts.TypeFlags.Null)) > 0;
|
|
228
|
+
undefinable ||= isUndefined;
|
|
229
|
+
nullable ||= isNull;
|
|
230
|
+
return !(isUndefined || isNull);
|
|
231
231
|
});
|
|
232
232
|
const name = CoreUtil.getSymbol(uType)?.getName();
|
|
233
233
|
return { key: 'composition', name, undefinable, nullable, tsSubTypes: remainder, subTypes: [], operation: uType.isUnion() ? 'or' : 'and' };
|
|
@@ -247,14 +247,19 @@ export const TypeBuilder: {
|
|
|
247
247
|
ctor: String,
|
|
248
248
|
nullable: type.nullable,
|
|
249
249
|
undefinable: type.undefinable,
|
|
250
|
-
template: {
|
|
250
|
+
template: { operation: 'or', values: subTypes.map(subType => transformCast<TemplateType>(subType).template!) }
|
|
251
251
|
};
|
|
252
252
|
} else if (subTypes.length === 1) {
|
|
253
253
|
return { undefinable, nullable, ...first };
|
|
254
|
-
} else if (first.key === 'literal' && subTypes.every(
|
|
254
|
+
} else if (first.key === 'literal' && subTypes.every(item => item.name === first.name)) { // We have a common
|
|
255
255
|
type.commonType = first;
|
|
256
|
-
} else if (type.operation === 'and' && first.key === 'shape' && subTypes.every(
|
|
257
|
-
return {
|
|
256
|
+
} else if (type.operation === 'and' && first.key === 'shape' && subTypes.every(item => item.key === 'shape')) { // All shapes
|
|
257
|
+
return {
|
|
258
|
+
importName: first.importName,
|
|
259
|
+
name: first.name,
|
|
260
|
+
key: 'shape',
|
|
261
|
+
fieldTypes: subTypes.reduce((map, subType) => ({ ...map, ...subType.fieldTypes }), {})
|
|
262
|
+
};
|
|
258
263
|
}
|
|
259
264
|
return type;
|
|
260
265
|
}
|
|
@@ -266,8 +271,8 @@ export const TypeBuilder: {
|
|
|
266
271
|
let operation: string | undefined;
|
|
267
272
|
let name: string | undefined;
|
|
268
273
|
|
|
269
|
-
const
|
|
270
|
-
const ref =
|
|
274
|
+
const declarations = DeclarationUtil.getDeclarations(type).filter(declaration => ts.isTypeAliasDeclaration(declaration));
|
|
275
|
+
const ref = declarations[0]?.type;
|
|
271
276
|
|
|
272
277
|
if (ref && ts.isTypeReferenceNode(ref) && ref.typeArguments && ref.typeArguments.length > 0) {
|
|
273
278
|
const [first, second] = ref.typeArguments;
|
|
@@ -298,20 +303,20 @@ export const TypeBuilder: {
|
|
|
298
303
|
const name = CoreUtil.getSymbol(context?.alias ?? type)?.getName();
|
|
299
304
|
const importName = resolver.getTypeImportName(type) ?? '<unknown>';
|
|
300
305
|
const tsTypeArguments = resolver.getAllTypeArguments(type);
|
|
301
|
-
const
|
|
302
|
-
if (
|
|
306
|
+
const properties = resolver.getPropertiesOfType(type);
|
|
307
|
+
if (properties.length === 0) {
|
|
303
308
|
return { key: 'literal', name: 'Object', ctor: Object, importName };
|
|
304
309
|
}
|
|
305
310
|
|
|
306
|
-
for (const member of
|
|
307
|
-
const
|
|
308
|
-
if (DeclarationUtil.isPublic(
|
|
309
|
-
const memberType = resolver.getType(
|
|
311
|
+
for (const member of properties) {
|
|
312
|
+
const decorator = DeclarationUtil.getPrimaryDeclarationNode(member);
|
|
313
|
+
if (DeclarationUtil.isPublic(decorator)) { // If public
|
|
314
|
+
const memberType = resolver.getType(decorator);
|
|
310
315
|
if (
|
|
311
316
|
!member.getName().includes('@') && // if not a symbol
|
|
312
317
|
!memberType.getCallSignatures().length // if not a function
|
|
313
318
|
) {
|
|
314
|
-
if ((ts.isPropertySignature(
|
|
319
|
+
if ((ts.isPropertySignature(decorator) || ts.isPropertyDeclaration(decorator)) && !!decorator.questionToken) {
|
|
315
320
|
Object.defineProperty(memberType, UNDEFINED, { value: true });
|
|
316
321
|
}
|
|
317
322
|
tsFieldTypes[member.getName()] = memberType;
|
|
@@ -333,7 +338,7 @@ export const TypeBuilder: {
|
|
|
333
338
|
// Resolving relative to source file
|
|
334
339
|
if (!importName || importName.startsWith('.')) {
|
|
335
340
|
const rawSourceFile: string = DeclarationUtil.getDeclarations(type)
|
|
336
|
-
?.find(
|
|
341
|
+
?.find(declaration => ts.getAllJSDocTags(declaration, (node): node is ts.JSDocTag => node.tagName.getText() === 'concrete').length)
|
|
337
342
|
?.getSourceFile().fileName ?? '';
|
|
338
343
|
|
|
339
344
|
if (!importName || importName === '.') {
|
|
@@ -346,8 +351,9 @@ export const TypeBuilder: {
|
|
|
346
351
|
|
|
347
352
|
// Convert name to $Concrete suffix if not provided
|
|
348
353
|
if (!name) {
|
|
349
|
-
const [
|
|
350
|
-
|
|
354
|
+
const [primaryDeclaration] = DeclarationUtil.getDeclarations(type)
|
|
355
|
+
.filter(declaration => ts.isInterfaceDeclaration(declaration) || ts.isTypeAliasDeclaration(declaration));
|
|
356
|
+
name = `${primaryDeclaration.name.text}$Concrete`;
|
|
351
357
|
}
|
|
352
358
|
|
|
353
359
|
return { key: 'managed', name, importName };
|
package/src/resolver/coerce.ts
CHANGED
|
@@ -4,12 +4,12 @@ export class CoerceUtil {
|
|
|
4
4
|
/**
|
|
5
5
|
* Is a value a plain JS object, created using {}
|
|
6
6
|
*/
|
|
7
|
-
static #isPlainObject(
|
|
8
|
-
return typeof
|
|
9
|
-
&&
|
|
10
|
-
&&
|
|
11
|
-
&&
|
|
12
|
-
&& Object.prototype.toString.call(
|
|
7
|
+
static #isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
8
|
+
return typeof value === 'object' // separate from primitives
|
|
9
|
+
&& value !== undefined
|
|
10
|
+
&& value !== null // is obvious
|
|
11
|
+
&& value.constructor === Object // separate instances (Array, DOM, ...)
|
|
12
|
+
&& Object.prototype.toString.call(value) === '[object Object]'; // separate build-in like Math
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
package/src/resolver/service.ts
CHANGED
|
@@ -73,10 +73,10 @@ export class SimpleResolver implements TransformResolver {
|
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* Get type from element
|
|
76
|
-
* @param
|
|
76
|
+
* @param value
|
|
77
77
|
*/
|
|
78
|
-
getType(
|
|
79
|
-
return 'getSourceFile' in
|
|
78
|
+
getType(value: ts.Type | ts.Node): ts.Type {
|
|
79
|
+
return 'getSourceFile' in value ? this.#tsChecker.getTypeAtLocation(value) : value;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/**
|
|
@@ -106,7 +106,8 @@ export class SimpleResolver implements TransformResolver {
|
|
|
106
106
|
* Get list of properties
|
|
107
107
|
*/
|
|
108
108
|
getPropertiesOfType(type: ts.Type): ts.Symbol[] {
|
|
109
|
-
return this.#tsChecker.getPropertiesOfType(type)
|
|
109
|
+
return this.#tsChecker.getPropertiesOfType(type)
|
|
110
|
+
.filter(property => property.getName() !== '__proto__' && property.getName() !== 'prototype');
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
/**
|
|
@@ -161,11 +162,11 @@ export class SimpleResolver implements TransformResolver {
|
|
|
161
162
|
|
|
162
163
|
try {
|
|
163
164
|
return resolve(this.getType(node));
|
|
164
|
-
} catch (
|
|
165
|
-
if (!(
|
|
166
|
-
throw
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (!(error instanceof Error)) {
|
|
167
|
+
throw error;
|
|
167
168
|
}
|
|
168
|
-
console.error(`Unable to resolve type in ${importName}`,
|
|
169
|
+
console.error(`Unable to resolve type in ${importName}`, error.stack);
|
|
169
170
|
return { key: 'literal', ctor: Object, name: 'object' };
|
|
170
171
|
}
|
|
171
172
|
}
|
package/src/resolver/types.ts
CHANGED
package/src/state.ts
CHANGED
|
@@ -15,16 +15,16 @@ import { CoreUtil } from './util/core.ts';
|
|
|
15
15
|
import { LiteralUtil } from './util/literal.ts';
|
|
16
16
|
import { SystemUtil } from './util/system.ts';
|
|
17
17
|
|
|
18
|
-
function hasOriginal(
|
|
19
|
-
return !!
|
|
18
|
+
function hasOriginal(node: ts.Node): node is ts.Node & { original: ts.Node } {
|
|
19
|
+
return !!node && !node.parent && 'original' in node && !!node.original;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function hasEscapedName(
|
|
23
|
-
return !!
|
|
22
|
+
function hasEscapedName(node: ts.Node): node is ts.Node & { name: { escapedText: string } } {
|
|
23
|
+
return !!node && 'name' in node && typeof node.name === 'object' && !!node.name && 'escapedText' in node.name && !!node.name.escapedText;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function isRedefinableDeclaration(
|
|
27
|
-
return ts.isFunctionDeclaration(
|
|
26
|
+
function isRedefinableDeclaration(node: ts.Node): node is ts.InterfaceDeclaration | ts.ClassDeclaration | ts.FunctionDeclaration {
|
|
27
|
+
return ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const FOREIGN_TYPE_REGISTRY_FILE = '@travetto/runtime/src/function';
|
|
@@ -35,7 +35,7 @@ const FOREIGN_TYPE_REGISTRY_FILE = '@travetto/runtime/src/function';
|
|
|
35
35
|
export class TransformerState implements State {
|
|
36
36
|
#resolver: SimpleResolver;
|
|
37
37
|
#imports: ImportManager;
|
|
38
|
-
#
|
|
38
|
+
#moduleIdentifier: ts.Identifier;
|
|
39
39
|
#manifestIndex: ManifestIndex;
|
|
40
40
|
#syntheticIdentifiers = new Map<string, ts.Identifier>();
|
|
41
41
|
#decorators = new Map<string, ts.PropertyAccessExpression>();
|
|
@@ -93,8 +93,8 @@ export class TransformerState implements State {
|
|
|
93
93
|
const resolved = this.resolveType(node);
|
|
94
94
|
if (resolved.key !== 'managed') {
|
|
95
95
|
const file = node.getSourceFile().fileName;
|
|
96
|
-
const
|
|
97
|
-
throw new Error(`Unable to import non-external type: ${node.getText()} ${resolved.key}: ${
|
|
96
|
+
const source = this.#resolver.getFileImportName(file);
|
|
97
|
+
throw new Error(`Unable to import non-external type: ${node.getText()} ${resolved.key}: ${source}`);
|
|
98
98
|
}
|
|
99
99
|
return resolved;
|
|
100
100
|
}
|
|
@@ -136,9 +136,9 @@ export class TransformerState implements State {
|
|
|
136
136
|
*/
|
|
137
137
|
readDocTagList(node: ts.Declaration, name: string): string[] {
|
|
138
138
|
return this.readDocTag(node, name)
|
|
139
|
-
.flatMap(
|
|
140
|
-
.map(
|
|
141
|
-
.filter(
|
|
139
|
+
.flatMap(tag => tag.split(/\s*,\s*/g))
|
|
140
|
+
.map(tag => tag.replace(/`/g, ''))
|
|
141
|
+
.filter(tag => !!tag);
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
/**
|
|
@@ -147,8 +147,8 @@ export class TransformerState implements State {
|
|
|
147
147
|
importDecorator(pth: string, name: string): ts.PropertyAccessExpression | undefined {
|
|
148
148
|
if (!this.#decorators.has(`${pth}:${name}`)) {
|
|
149
149
|
const ref = this.#imports.importFile(pth);
|
|
150
|
-
const
|
|
151
|
-
this.#decorators.set(name, this.factory.createPropertyAccessExpression(ref.
|
|
150
|
+
const identifier = this.factory.createIdentifier(name);
|
|
151
|
+
this.#decorators.set(name, this.factory.createPropertyAccessExpression(ref.identifier, identifier));
|
|
152
152
|
}
|
|
153
153
|
return this.#decorators.get(name);
|
|
154
154
|
}
|
|
@@ -164,22 +164,22 @@ export class TransformerState implements State {
|
|
|
164
164
|
/**
|
|
165
165
|
* Read a decorator's metadata
|
|
166
166
|
*/
|
|
167
|
-
getDecoratorMeta(
|
|
168
|
-
const
|
|
169
|
-
const type = this.#resolver.getType(
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
const mod =
|
|
167
|
+
getDecoratorMeta(decorator: ts.Decorator): DecoratorMeta | undefined {
|
|
168
|
+
const identifier = DecoratorUtil.getDecoratorIdentifier(decorator);
|
|
169
|
+
const type = this.#resolver.getType(identifier);
|
|
170
|
+
const declaration = DeclarationUtil.getOptionalPrimaryDeclarationNode(type);
|
|
171
|
+
const source = declaration?.getSourceFile().fileName;
|
|
172
|
+
const mod = source ? this.#resolver.getFileImportName(source, true) : undefined;
|
|
173
173
|
const file = this.#manifestIndex.getFromImport(mod ?? '')?.outputFile;
|
|
174
174
|
const targets = DocUtil.readAugments(type);
|
|
175
175
|
const example = DocUtil.readExample(type);
|
|
176
176
|
const module = file ? mod : undefined;
|
|
177
|
-
const name =
|
|
178
|
-
|
|
177
|
+
const name = identifier ?
|
|
178
|
+
identifier.escapedText?.toString()! :
|
|
179
179
|
undefined;
|
|
180
180
|
|
|
181
|
-
if (
|
|
182
|
-
return {
|
|
181
|
+
if (identifier && name) {
|
|
182
|
+
return { decorator, identifier, file, module, targets, name, options: example };
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -188,8 +188,8 @@ export class TransformerState implements State {
|
|
|
188
188
|
*/
|
|
189
189
|
getDecoratorList(node: ts.Node): DecoratorMeta[] {
|
|
190
190
|
return ts.canHaveDecorators(node) ? (ts.getDecorators(node) ?? [])
|
|
191
|
-
.map(
|
|
192
|
-
.filter(
|
|
191
|
+
.map(decorator => this.getDecoratorMeta(decorator))
|
|
192
|
+
.filter(meta => !!meta) : [];
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
/**
|
|
@@ -201,26 +201,26 @@ export class TransformerState implements State {
|
|
|
201
201
|
|
|
202
202
|
/**
|
|
203
203
|
* Register statement for inclusion in final output
|
|
204
|
-
* @param
|
|
204
|
+
* @param added
|
|
205
205
|
* @param before
|
|
206
206
|
*/
|
|
207
207
|
addStatements(added: ts.Statement[], before?: ts.Node | number): void {
|
|
208
|
-
const
|
|
209
|
-
let idx =
|
|
208
|
+
const statements = this.source.statements.slice(0);
|
|
209
|
+
let idx = statements.length + 1000;
|
|
210
210
|
|
|
211
211
|
if (before && typeof before !== 'number') {
|
|
212
|
-
let
|
|
213
|
-
if (hasOriginal(
|
|
214
|
-
|
|
212
|
+
let node = before;
|
|
213
|
+
if (hasOriginal(node)) {
|
|
214
|
+
node = node.original;
|
|
215
215
|
}
|
|
216
|
-
while (
|
|
217
|
-
|
|
216
|
+
while (node && !ts.isSourceFile(node.parent) && node !== node.parent) {
|
|
217
|
+
node = node.parent;
|
|
218
218
|
}
|
|
219
|
-
if (!ts.isStatement(
|
|
219
|
+
if (!ts.isStatement(node)) {
|
|
220
220
|
throw new Error('Unable to find statement at top level');
|
|
221
221
|
}
|
|
222
|
-
if (
|
|
223
|
-
idx =
|
|
222
|
+
if (node && ts.isSourceFile(node.parent) && statements.indexOf(node) >= 0) {
|
|
223
|
+
idx = statements.indexOf(node) - 1;
|
|
224
224
|
}
|
|
225
225
|
} else if (before !== undefined) {
|
|
226
226
|
idx = before;
|
|
@@ -241,21 +241,21 @@ export class TransformerState implements State {
|
|
|
241
241
|
/**
|
|
242
242
|
* From literal
|
|
243
243
|
*/
|
|
244
|
-
fromLiteral<T extends ts.Expression>(
|
|
245
|
-
fromLiteral(
|
|
246
|
-
fromLiteral(
|
|
247
|
-
fromLiteral(
|
|
248
|
-
fromLiteral(
|
|
249
|
-
fromLiteral(
|
|
250
|
-
fromLiteral(
|
|
251
|
-
return LiteralUtil.fromLiteral(this.factory,
|
|
244
|
+
fromLiteral<T extends ts.Expression>(value: T): T;
|
|
245
|
+
fromLiteral(value: undefined): ts.Identifier;
|
|
246
|
+
fromLiteral(value: null): ts.NullLiteral;
|
|
247
|
+
fromLiteral(value: object): ts.ObjectLiteralExpression;
|
|
248
|
+
fromLiteral(value: unknown[]): ts.ArrayLiteralExpression;
|
|
249
|
+
fromLiteral(value: string | boolean | number): ts.LiteralExpression;
|
|
250
|
+
fromLiteral(value: unknown): ts.Node {
|
|
251
|
+
return LiteralUtil.fromLiteral(this.factory, value!);
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
/**
|
|
255
255
|
* Extend
|
|
256
256
|
*/
|
|
257
|
-
extendObjectLiteral(
|
|
258
|
-
return LiteralUtil.extendObjectLiteral(this.factory,
|
|
257
|
+
extendObjectLiteral(source: object | ts.Expression, ...rest: (object | ts.Expression)[]): ts.ObjectLiteralExpression {
|
|
258
|
+
return LiteralUtil.extendObjectLiteral(this.factory, source, ...rest);
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
/**
|
|
@@ -268,8 +268,8 @@ export class TransformerState implements State {
|
|
|
268
268
|
/**
|
|
269
269
|
* Create a static field for a class
|
|
270
270
|
*/
|
|
271
|
-
createStaticField(name: string,
|
|
272
|
-
return CoreUtil.createStaticField(this.factory, name,
|
|
271
|
+
createStaticField(name: string, value: ts.Expression): ts.PropertyDeclaration {
|
|
272
|
+
return CoreUtil.createStaticField(this.factory, name, value);
|
|
273
273
|
}
|
|
274
274
|
|
|
275
275
|
/**
|
|
@@ -284,17 +284,17 @@ export class TransformerState implements State {
|
|
|
284
284
|
* Get filename identifier, regardless of module system
|
|
285
285
|
*/
|
|
286
286
|
getModuleIdentifier(): ts.Expression {
|
|
287
|
-
if (this.#
|
|
288
|
-
this.#
|
|
287
|
+
if (this.#moduleIdentifier === undefined) {
|
|
288
|
+
this.#moduleIdentifier = this.factory.createUniqueName('mod');
|
|
289
289
|
const entry = this.#resolver.getFileImport(this.source.fileName);
|
|
290
|
-
const
|
|
290
|
+
const declaration = this.factory.createVariableDeclaration(this.#moduleIdentifier, undefined, undefined,
|
|
291
291
|
this.fromLiteral([entry?.module, entry?.relativeFile ?? ''])
|
|
292
292
|
);
|
|
293
293
|
this.addStatements([
|
|
294
|
-
this.factory.createVariableStatement([], this.factory.createVariableDeclarationList([
|
|
294
|
+
this.factory.createVariableStatement([], this.factory.createVariableDeclarationList([declaration]))
|
|
295
295
|
], -1);
|
|
296
296
|
}
|
|
297
|
-
return this.#
|
|
297
|
+
return this.#moduleIdentifier;
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
/**
|
|
@@ -309,7 +309,7 @@ export class TransformerState implements State {
|
|
|
309
309
|
mod = typeof mod === 'string' ? mod : mod[ModuleNameSymbol]!;
|
|
310
310
|
const target = `${mod}:${name}`;
|
|
311
311
|
const list = this.getDecoratorList(node);
|
|
312
|
-
return list.find(
|
|
312
|
+
return list.find(meta => meta.targets?.includes(target) && (!module || meta.name === name && meta.module === module))?.decorator;
|
|
313
313
|
}
|
|
314
314
|
|
|
315
315
|
/**
|
|
@@ -379,14 +379,14 @@ export class TransformerState implements State {
|
|
|
379
379
|
findMethodByName(cls: ts.ClassLikeDeclaration | ts.Type, method: string): ts.MethodDeclaration | undefined {
|
|
380
380
|
if ('getSourceFile' in cls) {
|
|
381
381
|
return cls.members.find(
|
|
382
|
-
(
|
|
382
|
+
(value): value is ts.MethodDeclaration => ts.isMethodDeclaration(value) && ts.isIdentifier(value.name) && value.name.escapedText === method
|
|
383
383
|
);
|
|
384
384
|
} else {
|
|
385
|
-
const
|
|
386
|
-
for (const
|
|
387
|
-
const
|
|
388
|
-
if (
|
|
389
|
-
return
|
|
385
|
+
const properties = this.#resolver.getPropertiesOfType(cls);
|
|
386
|
+
for (const property of properties) {
|
|
387
|
+
const declaration = property.declarations?.[0];
|
|
388
|
+
if (declaration && property.escapedName === method && ts.isMethodDeclaration(declaration)) {
|
|
389
|
+
return declaration;
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
392
|
}
|
|
@@ -403,13 +403,13 @@ export class TransformerState implements State {
|
|
|
403
403
|
/**
|
|
404
404
|
* Produce a foreign target type
|
|
405
405
|
*/
|
|
406
|
-
getForeignTarget(
|
|
406
|
+
getForeignTarget(type: ForeignType): ts.Expression {
|
|
407
407
|
const file = this.importFile(FOREIGN_TYPE_REGISTRY_FILE);
|
|
408
408
|
return this.factory.createCallExpression(this.createAccess(
|
|
409
|
-
file.
|
|
409
|
+
file.identifier,
|
|
410
410
|
this.factory.createIdentifier('foreignType'),
|
|
411
411
|
), [], [
|
|
412
|
-
this.fromLiteral(
|
|
412
|
+
this.fromLiteral(type.classId)
|
|
413
413
|
]);
|
|
414
414
|
}
|
|
415
415
|
|
|
@@ -425,8 +425,8 @@ export class TransformerState implements State {
|
|
|
425
425
|
return this.getForeignTarget(type);
|
|
426
426
|
} else {
|
|
427
427
|
const file = node.getSourceFile().fileName;
|
|
428
|
-
const
|
|
429
|
-
throw new Error(`Unable to import non-external type: ${node.getText()} ${type.key}: ${
|
|
428
|
+
const source = this.getFileImportName(file);
|
|
429
|
+
throw new Error(`Unable to import non-external type: ${node.getText()} ${type.key}: ${source}`);
|
|
430
430
|
}
|
|
431
431
|
}
|
|
432
432
|
|
|
@@ -435,7 +435,7 @@ export class TransformerState implements State {
|
|
|
435
435
|
*/
|
|
436
436
|
getApparentTypeOfField(value: ts.Type, field: string): AnyType | undefined {
|
|
437
437
|
const checker = this.#resolver.getChecker();
|
|
438
|
-
const
|
|
439
|
-
return
|
|
438
|
+
const properties = checker.getApparentType(value).getApparentProperties().find(property => property.escapedName === field);
|
|
439
|
+
return properties ? this.resolveType(checker.getTypeOfSymbol(properties)) : undefined;
|
|
440
440
|
}
|
|
441
441
|
}
|
package/src/types/shared.ts
CHANGED
|
@@ -22,13 +22,13 @@ export interface DeclDocumentation {
|
|
|
22
22
|
*/
|
|
23
23
|
export type Import = {
|
|
24
24
|
path: string;
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
identifier: ts.Identifier;
|
|
26
|
+
statement?: ts.ImportDeclaration;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
/** Template Literal Types */
|
|
30
30
|
export type TemplateLiteralPart = string | NumberConstructor | StringConstructor | BooleanConstructor;
|
|
31
|
-
export type TemplateLiteral = {
|
|
31
|
+
export type TemplateLiteral = { operation: 'and' | 'or', values: (TemplateLiteralPart | TemplateLiteral)[] };
|
|
32
32
|
|
|
33
33
|
export function transformCast<T>(input: unknown): T {
|
|
34
34
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
package/src/types/visitor.ts
CHANGED
|
@@ -4,8 +4,8 @@ import ts from 'typescript';
|
|
|
4
4
|
* Decorator metadata
|
|
5
5
|
*/
|
|
6
6
|
export type DecoratorMeta = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
decorator: ts.Decorator;
|
|
8
|
+
identifier: ts.Identifier;
|
|
9
9
|
module?: string;
|
|
10
10
|
file?: string;
|
|
11
11
|
targets?: string[];
|
|
@@ -18,7 +18,7 @@ export type State = {
|
|
|
18
18
|
importName: string;
|
|
19
19
|
added: Map<number, ts.Statement[]>;
|
|
20
20
|
getDecoratorList(node: ts.Node): DecoratorMeta[];
|
|
21
|
-
finalize(
|
|
21
|
+
finalize(source: ts.SourceFile): ts.SourceFile;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
export type TransformPhase = 'before' | 'after';
|
package/src/util/core.ts
CHANGED
|
@@ -8,25 +8,24 @@ export class CoreUtil {
|
|
|
8
8
|
/**
|
|
9
9
|
* See if inbound node has an original property
|
|
10
10
|
*/
|
|
11
|
-
static hasOriginal(
|
|
12
|
-
return 'original' in
|
|
11
|
+
static hasOriginal(value: ts.Node): value is (ts.Node & { original: ts.Node }) {
|
|
12
|
+
return 'original' in value && !!value.original;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* See if type has target
|
|
17
17
|
*/
|
|
18
|
-
static hasTarget(
|
|
19
|
-
return 'target' in
|
|
18
|
+
static hasTarget(value: ts.Type): value is (ts.Type & { target: ts.Type }) {
|
|
19
|
+
return 'target' in value && !!value.target;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Get code range of node
|
|
24
|
-
* @param m
|
|
25
24
|
*/
|
|
26
|
-
static getRangeOf<T extends ts.Node>(source: ts.SourceFile,
|
|
27
|
-
if (
|
|
28
|
-
const start = ts.getLineAndCharacterOfPosition(source,
|
|
29
|
-
const end = ts.getLineAndCharacterOfPosition(source,
|
|
25
|
+
static getRangeOf<T extends ts.Node>(source: ts.SourceFile, value: T | undefined): [start: number, end: number] | undefined {
|
|
26
|
+
if (value && value.pos >= 0) {
|
|
27
|
+
const start = ts.getLineAndCharacterOfPosition(source, value.getStart(source));
|
|
28
|
+
const end = ts.getLineAndCharacterOfPosition(source, value.getEnd());
|
|
30
29
|
return [start.line + 1, end.line + 1];
|
|
31
30
|
}
|
|
32
31
|
}
|
|
@@ -34,7 +33,10 @@ export class CoreUtil {
|
|
|
34
33
|
/**
|
|
35
34
|
* Find the primary argument of a call expression, or decorator.
|
|
36
35
|
*/
|
|
37
|
-
static findArgument<T extends ts.Expression = ts.Expression>(
|
|
36
|
+
static findArgument<T extends ts.Expression = ts.Expression>(
|
|
37
|
+
node: ts.CallExpression | undefined,
|
|
38
|
+
pred: (expr: ts.Expression) => expr is T
|
|
39
|
+
): T | undefined {
|
|
38
40
|
if (node && node.arguments && node.arguments.length) {
|
|
39
41
|
return node.arguments.find(pred);
|
|
40
42
|
}
|
|
@@ -52,10 +54,10 @@ export class CoreUtil {
|
|
|
52
54
|
/**
|
|
53
55
|
* Create a static field for a class
|
|
54
56
|
*/
|
|
55
|
-
static createStaticField(factory: ts.NodeFactory, name: string,
|
|
57
|
+
static createStaticField(factory: ts.NodeFactory, name: string, value: ts.Expression): ts.PropertyDeclaration {
|
|
56
58
|
return factory.createPropertyDeclaration(
|
|
57
59
|
[factory.createToken(ts.SyntaxKind.StaticKeyword)],
|
|
58
|
-
name, undefined, undefined,
|
|
60
|
+
name, undefined, undefined, value
|
|
59
61
|
);
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -74,12 +76,12 @@ export class CoreUtil {
|
|
|
74
76
|
|
|
75
77
|
/**
|
|
76
78
|
* Updates source
|
|
77
|
-
* @param
|
|
79
|
+
* @param source
|
|
78
80
|
* @param statements
|
|
79
81
|
*/
|
|
80
|
-
static updateSource(factory: ts.NodeFactory,
|
|
82
|
+
static updateSource(factory: ts.NodeFactory, source: ts.SourceFile, statements: ts.NodeArray<ts.Statement> | ts.Statement[]): ts.SourceFile {
|
|
81
83
|
return factory.updateSourceFile(
|
|
82
|
-
|
|
84
|
+
source, statements, source.isDeclarationFile, source.referencedFiles, source.typeReferenceDirectives, source.hasNoDefaultLib
|
|
83
85
|
);
|
|
84
86
|
}
|
|
85
87
|
|
|
@@ -96,9 +98,9 @@ export class CoreUtil {
|
|
|
96
98
|
first = factory.createIdentifier(first);
|
|
97
99
|
}
|
|
98
100
|
return items.reduce<ts.Expression>(
|
|
99
|
-
(
|
|
100
|
-
factory.createElementAccessExpression(
|
|
101
|
-
factory.createPropertyAccessExpression(
|
|
101
|
+
(expr, value) => typeof value === 'number' ?
|
|
102
|
+
factory.createElementAccessExpression(expr, value) :
|
|
103
|
+
factory.createPropertyAccessExpression(expr, value),
|
|
102
104
|
factory.createPropertyAccessExpression(first, second)
|
|
103
105
|
);
|
|
104
106
|
}
|
|
@@ -111,7 +113,7 @@ export class CoreUtil {
|
|
|
111
113
|
factory.createCallExpression(
|
|
112
114
|
name,
|
|
113
115
|
undefined,
|
|
114
|
-
contents.filter(
|
|
116
|
+
contents.filter(expr => !!expr)
|
|
115
117
|
)
|
|
116
118
|
);
|
|
117
119
|
}
|