restfuncs-transformer 0.9.7 → 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.
- package/AddRemoteMethodsMeta.d.ts +67 -0
- package/AddRemoteMethodsMeta.d.ts.map +1 -0
- package/AddRemoteMethodsMeta.js +221 -0
- package/AddRemoteMethodsMeta.js.map +1 -0
- package/AddRemoteMethodsMeta.ts +403 -0
- package/devPlayground.d.ts +2 -0
- package/devPlayground.d.ts.map +1 -0
- package/devPlayground.js +31 -0
- package/devPlayground.js.map +1 -0
- package/devPlayground.ts +15 -0
- package/index.d.ts +7 -2
- package/index.d.ts.map +1 -1
- package/index.js +58 -29
- package/index.js.map +1 -1
- package/index.ts +89 -4
- package/package.json +10 -6
- package/readme.md +43 -2
- package/transformerUtil.d.ts +50 -0
- package/transformerUtil.d.ts.map +1 -0
- package/transformerUtil.js +104 -0
- package/transformerUtil.js.map +1 -0
- package/transformerUtil.ts +104 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import ts, {
|
|
2
|
+
ClassDeclaration,
|
|
3
|
+
MethodDeclaration,
|
|
4
|
+
Node,
|
|
5
|
+
ObjectLiteralExpression,
|
|
6
|
+
PropertyAccessExpression,
|
|
7
|
+
SyntaxKind
|
|
8
|
+
} from 'typescript';
|
|
9
|
+
import {FileTransformRun} from "./transformerUtil";
|
|
10
|
+
import {transformerVersion} from "./index";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Transformer that adds the following statement to the method body (like in readme.md#how-it-works)
|
|
15
|
+
* <code><pre>
|
|
16
|
+
* static getRemoteMethodsMeta() {
|
|
17
|
+
* ....
|
|
18
|
+
* }
|
|
19
|
+
* </pre></code>
|
|
20
|
+
*/
|
|
21
|
+
export class AddRemoteMethodsMeta extends FileTransformRun {
|
|
22
|
+
|
|
23
|
+
diagnosis_currentClass_instanceMethodsSeen?: Set<string>;
|
|
24
|
+
currentClass_instanceMethodsMeta?: Record<string, ObjectLiteralExpression>; // The {...} that should be added below... see example in readme.md
|
|
25
|
+
astWasModified = false;
|
|
26
|
+
|
|
27
|
+
/* Visitor Function */
|
|
28
|
+
visit(node: Node): Node {
|
|
29
|
+
if (node.kind === SyntaxKind.MethodDeclaration) { // @remote ?
|
|
30
|
+
const methodDeclaration = (node as MethodDeclaration)
|
|
31
|
+
if(this.getChilds(methodDeclaration).some(n => n.kind == SyntaxKind.StaticKeyword)) { // static ?
|
|
32
|
+
return node;
|
|
33
|
+
}
|
|
34
|
+
if(this.getParent().kind !== SyntaxKind.ClassDeclaration) { // Method not under a class (i.e. an anonymous object ?}
|
|
35
|
+
return node;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const methodName = (methodDeclaration.name as any).escapedText;
|
|
39
|
+
try {
|
|
40
|
+
let remoteDecorator = this.getChilds(methodDeclaration).find(d => d.kind === SyntaxKind.Decorator);
|
|
41
|
+
if (!remoteDecorator) { // no @remote found ?
|
|
42
|
+
return node;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Diagnosis: Check for overloads:
|
|
46
|
+
if(this.diagnosis_currentClass_instanceMethodsSeen?.has(methodName)) { // Seen twice ?
|
|
47
|
+
throw new Error(`@remote methods cannot have multiple/overloaded signatures: ${methodName}`) // TODO: add diagnosis
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.currentClass_instanceMethodsMeta![methodName] = this.createMethodMetaExpression(methodDeclaration, methodName) // create the code:
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
this.diagnosis_currentClass_instanceMethodsSeen!.add(methodName);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
else if(node.kind === SyntaxKind.ClassDeclaration) {
|
|
59
|
+
this.diagnosis_currentClass_instanceMethodsSeen = new Set()
|
|
60
|
+
this.currentClass_instanceMethodsMeta = {}
|
|
61
|
+
try {
|
|
62
|
+
let result = this.visitChilds(node) as ClassDeclaration
|
|
63
|
+
|
|
64
|
+
if(Object.keys(this.currentClass_instanceMethodsMeta).length > 0) { // Current class has @remote methods ?
|
|
65
|
+
// add "static getRemoteMethodsMeta() {...}" method:
|
|
66
|
+
// @ts-ignore yes, a bit hacky, but with factory.crateClassDeclation we might also miss some properties
|
|
67
|
+
result.members = this.context.factory.createNodeArray([...result.members.values(), this.create_static_getRemoteMethodsMeta_expression()])
|
|
68
|
+
this.astWasModified = true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
return result
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
this.currentClass_instanceMethodsMeta = undefined
|
|
76
|
+
this.diagnosis_currentClass_instanceMethodsSeen = undefined
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return this.visitChilds(node);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Crates the following expression like in readme.md#how-it-works
|
|
87
|
+
* <code><pre>
|
|
88
|
+
* {
|
|
89
|
+
* arguments: {
|
|
90
|
+
* ...
|
|
91
|
+
* }
|
|
92
|
+
* jsDoc: {
|
|
93
|
+
* ...
|
|
94
|
+
* }
|
|
95
|
+
* }
|
|
96
|
+
* </pre></code>
|
|
97
|
+
*
|
|
98
|
+
* @param methodName
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
createMethodMetaExpression(node: MethodDeclaration, methodName: string) {
|
|
102
|
+
const factory = this.context.factory;
|
|
103
|
+
|
|
104
|
+
const arguments_typiaFuncs:Record<string, PropertyAccessExpression> = {
|
|
105
|
+
/**
|
|
106
|
+
* typia.validateEquals
|
|
107
|
+
*/
|
|
108
|
+
validateEquals: factory.createPropertyAccessExpression(
|
|
109
|
+
factory.createIdentifier("typia"),
|
|
110
|
+
factory.createIdentifier("validateEquals")
|
|
111
|
+
),
|
|
112
|
+
"validatePrune": factory.createPropertyAccessExpression(factory.createPropertyAccessExpression(
|
|
113
|
+
factory.createIdentifier("typia"),
|
|
114
|
+
factory.createIdentifier("misc")
|
|
115
|
+
), factory.createIdentifier("validatePrune"))
|
|
116
|
+
}
|
|
117
|
+
const result_typiaFuncs = {...arguments_typiaFuncs};
|
|
118
|
+
|
|
119
|
+
// Example from readme.md#how-it-works Copy&pasted through the [AST viewer tool](https://ts-ast-viewer.com/)
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
return factory.createObjectLiteralExpression(
|
|
122
|
+
[
|
|
123
|
+
// arguments: {...}
|
|
124
|
+
factory.createPropertyAssignment(
|
|
125
|
+
factory.createIdentifier("arguments"),
|
|
126
|
+
factory.createObjectLiteralExpression(
|
|
127
|
+
Object.keys(arguments_typiaFuncs).map((typiaFnName) =>
|
|
128
|
+
factory.createPropertyAssignment(
|
|
129
|
+
factory.createIdentifier(typiaFnName),
|
|
130
|
+
factory.createArrowFunction(
|
|
131
|
+
undefined,
|
|
132
|
+
undefined,
|
|
133
|
+
[factory.createParameterDeclaration(
|
|
134
|
+
undefined,
|
|
135
|
+
undefined,
|
|
136
|
+
factory.createIdentifier("args"),
|
|
137
|
+
undefined,
|
|
138
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
|
|
139
|
+
undefined
|
|
140
|
+
)],
|
|
141
|
+
undefined,
|
|
142
|
+
factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
|
|
143
|
+
factory.createCallExpression(arguments_typiaFuncs[typiaFnName],
|
|
144
|
+
[factory.createTypeReferenceNode(
|
|
145
|
+
factory.createIdentifier("Parameters"),
|
|
146
|
+
[factory.createIndexedAccessTypeNode(
|
|
147
|
+
factory.createTypeQueryNode(
|
|
148
|
+
factory.createQualifiedName(
|
|
149
|
+
factory.createIdentifier("this"),
|
|
150
|
+
factory.createIdentifier("prototype")
|
|
151
|
+
),
|
|
152
|
+
undefined
|
|
153
|
+
),
|
|
154
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(methodName))
|
|
155
|
+
)]
|
|
156
|
+
)],
|
|
157
|
+
[factory.createIdentifier("args")]
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
)),
|
|
161
|
+
true
|
|
162
|
+
)
|
|
163
|
+
),
|
|
164
|
+
// result: {...}
|
|
165
|
+
factory.createPropertyAssignment(
|
|
166
|
+
factory.createIdentifier("result"),
|
|
167
|
+
factory.createObjectLiteralExpression(
|
|
168
|
+
Object.keys(result_typiaFuncs).map((typiaFnName) =>
|
|
169
|
+
factory.createPropertyAssignment(
|
|
170
|
+
factory.createIdentifier(typiaFnName),
|
|
171
|
+
factory.createArrowFunction(
|
|
172
|
+
undefined,
|
|
173
|
+
undefined,
|
|
174
|
+
[factory.createParameterDeclaration(
|
|
175
|
+
undefined,
|
|
176
|
+
undefined,
|
|
177
|
+
factory.createIdentifier("value"),
|
|
178
|
+
undefined,
|
|
179
|
+
factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
|
|
180
|
+
undefined
|
|
181
|
+
)],
|
|
182
|
+
undefined,
|
|
183
|
+
factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
|
|
184
|
+
factory.createCallExpression(arguments_typiaFuncs[typiaFnName],
|
|
185
|
+
[factory.createTypeReferenceNode(
|
|
186
|
+
factory.createIdentifier("Awaited"), [factory.createTypeReferenceNode(
|
|
187
|
+
factory.createIdentifier("ReturnType"),
|
|
188
|
+
[factory.createIndexedAccessTypeNode(
|
|
189
|
+
factory.createTypeQueryNode(
|
|
190
|
+
factory.createQualifiedName(
|
|
191
|
+
factory.createIdentifier("this"),
|
|
192
|
+
factory.createIdentifier("prototype")
|
|
193
|
+
),
|
|
194
|
+
undefined
|
|
195
|
+
),
|
|
196
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(methodName))
|
|
197
|
+
)]
|
|
198
|
+
)]
|
|
199
|
+
)],
|
|
200
|
+
[factory.createIdentifier("value")]
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
)),
|
|
204
|
+
true
|
|
205
|
+
)
|
|
206
|
+
),
|
|
207
|
+
factory.createPropertyAssignment(
|
|
208
|
+
factory.createIdentifier("jsDoc"), this.createJsDocExpression(node, methodName)
|
|
209
|
+
)
|
|
210
|
+
],
|
|
211
|
+
true
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Crates the following expression like in readme.md#how-it-works
|
|
217
|
+
* <code><pre>
|
|
218
|
+
* {
|
|
219
|
+
* comment: "...",
|
|
220
|
+
* params: {...},
|
|
221
|
+
* ...
|
|
222
|
+
* }
|
|
223
|
+
* </pre></code>
|
|
224
|
+
*
|
|
225
|
+
* or
|
|
226
|
+
*
|
|
227
|
+
* <code><pre>
|
|
228
|
+
* undefined
|
|
229
|
+
* </pre></code>
|
|
230
|
+
*
|
|
231
|
+
*
|
|
232
|
+
* @param methodName
|
|
233
|
+
* @private
|
|
234
|
+
*/
|
|
235
|
+
private createJsDocExpression(node: MethodDeclaration, diagnosis_methodName: string) {
|
|
236
|
+
const factory = this.context.factory;
|
|
237
|
+
|
|
238
|
+
if(!node.jsDoc || node.jsDoc.length == 0 ) { // no jsdoc
|
|
239
|
+
return factory.createIdentifier("undefined");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const jsdoc = node.jsDoc[node.jsDoc.length - 1]; // use last jsDoc
|
|
243
|
+
|
|
244
|
+
const comment = jsdoc.comment || "";
|
|
245
|
+
if(typeof comment !== "string") {
|
|
246
|
+
throw new Error(`Expected comment to be a string. Got: ${comment}` ); // TODO add to diagnostics instead
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Collect params and tags
|
|
250
|
+
const params: Record<string, string> = {}
|
|
251
|
+
const tags: {name:string, comment?: string}[] = []
|
|
252
|
+
jsdoc.tags?.forEach(tag => {
|
|
253
|
+
const name = (tag.tagName.escapedText as string).toLowerCase()
|
|
254
|
+
const comment = tag.comment;
|
|
255
|
+
if(comment && typeof comment !== "string") {
|
|
256
|
+
throw new Error(`Expected comment to be a string. Got: ${comment}` ); // TODO add to diagnostics instead
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if(name === "param") {
|
|
260
|
+
const paramname = (tag as any).name?.escapedText;
|
|
261
|
+
if(paramname && comment) {
|
|
262
|
+
params[paramname] = comment;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const tagName: Node | undefined = (tag as any).name;
|
|
267
|
+
tags.push({name, comment})
|
|
268
|
+
}
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
// JSDoc. Example from readme.md#how-it-works Copy&pasted through the [AST viewer tool](https://ts-ast-viewer.com/)
|
|
272
|
+
let jsDocExpression = factory.createObjectLiteralExpression(
|
|
273
|
+
[
|
|
274
|
+
factory.createPropertyAssignment(
|
|
275
|
+
factory.createIdentifier("comment"),
|
|
276
|
+
factory.createStringLiteral(comment)
|
|
277
|
+
),
|
|
278
|
+
factory.createPropertyAssignment(
|
|
279
|
+
factory.createIdentifier("params"),
|
|
280
|
+
factory.createObjectLiteralExpression(
|
|
281
|
+
Object.keys(params).map(paramName =>
|
|
282
|
+
factory.createPropertyAssignment(
|
|
283
|
+
factory.createIdentifier(paramName),
|
|
284
|
+
factory.createStringLiteral(params[paramName])
|
|
285
|
+
)),
|
|
286
|
+
false
|
|
287
|
+
)
|
|
288
|
+
),
|
|
289
|
+
factory.createPropertyAssignment(
|
|
290
|
+
factory.createIdentifier("tags"),
|
|
291
|
+
factory.createArrayLiteralExpression(
|
|
292
|
+
tags.map(tag => factory.createObjectLiteralExpression(
|
|
293
|
+
[
|
|
294
|
+
factory.createPropertyAssignment(
|
|
295
|
+
factory.createIdentifier("name"),
|
|
296
|
+
factory.createStringLiteral(tag.name)
|
|
297
|
+
),
|
|
298
|
+
factory.createPropertyAssignment(
|
|
299
|
+
factory.createIdentifier("comment"),
|
|
300
|
+
tag.comment !== undefined?factory.createStringLiteral(tag.comment):factory.createIdentifier("undefined")
|
|
301
|
+
)
|
|
302
|
+
],
|
|
303
|
+
false
|
|
304
|
+
)),
|
|
305
|
+
false
|
|
306
|
+
)
|
|
307
|
+
)
|
|
308
|
+
],
|
|
309
|
+
true
|
|
310
|
+
)
|
|
311
|
+
return jsDocExpression;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Crates the following expression like in readme.md#how-it-works
|
|
316
|
+
* <code><pre>
|
|
317
|
+
* static getRemoteMethodsMeta() {
|
|
318
|
+
* ....
|
|
319
|
+
* }
|
|
320
|
+
* </pre></code>
|
|
321
|
+
*
|
|
322
|
+
* @param methodName
|
|
323
|
+
* @private
|
|
324
|
+
*/
|
|
325
|
+
private create_static_getRemoteMethodsMeta_expression() : MethodDeclaration {
|
|
326
|
+
const factory = this.context.factory;
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
let instanceMethods = factory.createObjectLiteralExpression(
|
|
330
|
+
Object.keys(this.currentClass_instanceMethodsMeta!).map(methodName => factory.createPropertyAssignment(
|
|
331
|
+
factory.createStringLiteral(methodName), this.currentClass_instanceMethodsMeta![methodName]
|
|
332
|
+
)),
|
|
333
|
+
true
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
// Example from readme.md#how-it-works Copy&pasted through the [AST viewer tool](https://ts-ast-viewer.com/)
|
|
337
|
+
return factory.createMethodDeclaration(
|
|
338
|
+
[factory.createToken(ts.SyntaxKind.StaticKeyword)],
|
|
339
|
+
undefined,
|
|
340
|
+
factory.createIdentifier("getRemoteMethodsMeta"),
|
|
341
|
+
undefined,
|
|
342
|
+
undefined,
|
|
343
|
+
[],
|
|
344
|
+
factory.createParenthesizedType(factory.createTypeQueryNode(
|
|
345
|
+
factory.createQualifiedName(
|
|
346
|
+
factory.createIdentifier("this"),
|
|
347
|
+
factory.createIdentifier("type_remoteMethodsMeta")
|
|
348
|
+
),
|
|
349
|
+
undefined
|
|
350
|
+
)),
|
|
351
|
+
factory.createBlock(
|
|
352
|
+
[
|
|
353
|
+
factory.createExpressionStatement(factory.createPropertyAccessExpression(
|
|
354
|
+
factory.createThis(),
|
|
355
|
+
factory.createIdentifier("__hello_developer__make_sure_your_class_is_a_subclass_of_ServerSession")
|
|
356
|
+
)),
|
|
357
|
+
factory.createVariableStatement(
|
|
358
|
+
undefined,
|
|
359
|
+
factory.createVariableDeclarationList(
|
|
360
|
+
[factory.createVariableDeclaration(
|
|
361
|
+
factory.createIdentifier("typia"),
|
|
362
|
+
undefined,
|
|
363
|
+
undefined,
|
|
364
|
+
factory.createPropertyAccessExpression(
|
|
365
|
+
factory.createThis(),
|
|
366
|
+
factory.createIdentifier("typiaRuntime")
|
|
367
|
+
)
|
|
368
|
+
)],
|
|
369
|
+
ts.NodeFlags.Let
|
|
370
|
+
)
|
|
371
|
+
),
|
|
372
|
+
factory.createReturnStatement(factory.createObjectLiteralExpression(
|
|
373
|
+
[
|
|
374
|
+
factory.createPropertyAssignment(
|
|
375
|
+
factory.createIdentifier("transformerVersion"),
|
|
376
|
+
factory.createObjectLiteralExpression(
|
|
377
|
+
[
|
|
378
|
+
factory.createPropertyAssignment(
|
|
379
|
+
factory.createIdentifier("major"),
|
|
380
|
+
factory.createIdentifier(`${transformerVersion.major}`)
|
|
381
|
+
),
|
|
382
|
+
factory.createPropertyAssignment(
|
|
383
|
+
factory.createIdentifier("feature"),
|
|
384
|
+
factory.createIdentifier(`${transformerVersion.feature}`)
|
|
385
|
+
)
|
|
386
|
+
],
|
|
387
|
+
false
|
|
388
|
+
)
|
|
389
|
+
),
|
|
390
|
+
factory.createPropertyAssignment(
|
|
391
|
+
factory.createIdentifier("instanceMethods"),
|
|
392
|
+
instanceMethods
|
|
393
|
+
)
|
|
394
|
+
],
|
|
395
|
+
true
|
|
396
|
+
))
|
|
397
|
+
],
|
|
398
|
+
true
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
}
|
|
403
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devPlayground.d.ts","sourceRoot":"","sources":["devPlayground.ts"],"names":[],"mappings":""}
|
package/devPlayground.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const ts = __importStar(require("typescript"));
|
|
27
|
+
const typescript_1 = require("typescript");
|
|
28
|
+
var ScriptKind = ts.ScriptKind;
|
|
29
|
+
let sourceFile = ts.createSourceFile("dev/some.ts", "const i:string = 'myStr';", typescript_1.ScriptTarget.ES2020, false, ScriptKind.JS);
|
|
30
|
+
let i = undefined;
|
|
31
|
+
//# sourceMappingURL=devPlayground.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devPlayground.js","sourceRoot":"","sources":["devPlayground.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAiC;AACjC,2CAAwC;AACxC,IAAO,UAAU,GAAG,EAAE,CAAC,UAAU,CAAC;AAGlC,IAAI,UAAU,GAAG,EAAE,CAAC,gBAAgB,CAAC,aAAa,EAAC,2BAA2B,EAAE,yBAAY,CAAC,MAAM,EAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC,CAAC;AAS1H,IAAI,CAAC,GAA2B,SAAS,CAAA"}
|
package/devPlayground.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import {ScriptTarget} from 'typescript';
|
|
3
|
+
import ScriptKind = ts.ScriptKind;
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
let sourceFile = ts.createSourceFile("dev/some.ts","const i:string = 'myStr';", ScriptTarget.ES2020,false, ScriptKind.JS);
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
type X = {
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
let i: Awaited<X> | undefined = undefined
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { CompilerHost, Program } from 'typescript';
|
|
2
|
+
import { PluginConfig, ProgramTransformerExtras } from "ts-patch";
|
|
3
|
+
export declare const transformerVersion: {
|
|
4
|
+
major: number;
|
|
5
|
+
feature: number;
|
|
6
|
+
};
|
|
7
|
+
export default function transformProgram(program: Program, host: CompilerHost | undefined, config: PluginConfig, extras?: ProgramTransformerExtras): Program;
|
|
3
8
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAW,EAAC,YAAY,EAAmB,OAAO,EAAa,MAAM,YAAY,CAAC;AAClF,OAAO,EAAC,YAAY,EAAE,wBAAwB,EAAC,MAAM,UAAU,CAAC;AAMhE,eAAO,MAAM,kBAAkB;;;CAA2B,CAAA;AAwC1D,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACpC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,YAAY,GAAG,SAAS,EAC9B,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,wBAAwB,GAClC,OAAO,CAkCT"}
|
package/index.js
CHANGED
|
@@ -1,33 +1,62 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
exports.transformerVersion = void 0;
|
|
4
|
+
const AddRemoteMethodsMeta_1 = require("./AddRemoteMethodsMeta");
|
|
5
|
+
const transformerUtil_1 = require("./transformerUtil");
|
|
6
|
+
// From: https://github.com/nonara/ts-patch/discussions/29#discussioncomment-325979
|
|
7
|
+
exports.transformerVersion = { major: 1, feature: 1 };
|
|
8
|
+
/* ****************************************************************************************************************** */
|
|
9
|
+
// region: Helpers
|
|
10
|
+
/* ****************************************************************************************************************** */
|
|
11
|
+
/**
|
|
12
|
+
* Patches existing Compiler Host (or creates new one) to allow feeding updated file content from cache
|
|
13
|
+
*/
|
|
14
|
+
function getPatchedHost(maybeHost, tsInstance, compilerOptions) {
|
|
15
|
+
const fileCache = new Map();
|
|
16
|
+
const compilerHost = maybeHost !== null && maybeHost !== void 0 ? maybeHost : tsInstance.createCompilerHost(compilerOptions, true);
|
|
17
|
+
const originalGetSourceFile = compilerHost.getSourceFile;
|
|
18
|
+
return Object.assign(compilerHost, {
|
|
19
|
+
getSourceFile(fileName, languageVersion) {
|
|
20
|
+
fileName = tsInstance.normalizePath(fileName);
|
|
21
|
+
if (fileCache.has(fileName))
|
|
22
|
+
return fileCache.get(fileName);
|
|
23
|
+
const sourceFile = originalGetSourceFile.apply(void 0, Array.from(arguments));
|
|
24
|
+
fileCache.set(fileName, sourceFile);
|
|
25
|
+
return sourceFile;
|
|
26
|
+
},
|
|
27
|
+
fileCache
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// endregion
|
|
31
|
+
/* ****************************************************************************************************************** */
|
|
32
|
+
// region: Program Transformer
|
|
33
|
+
/* ****************************************************************************************************************** */
|
|
34
|
+
function transformProgram(program, host, config, extras) {
|
|
35
|
+
if (!extras) {
|
|
36
|
+
throw new Error(`Please add the flag "transformProgram": true to the transformer inside tsconfig.json`);
|
|
37
|
+
}
|
|
38
|
+
const { ts: tsInstance } = extras;
|
|
39
|
+
const compilerOptions = program.getCompilerOptions();
|
|
40
|
+
const compilerHost = getPatchedHost(host, tsInstance, compilerOptions);
|
|
41
|
+
const rootFileNames = program.getRootFileNames().map(tsInstance.normalizePath);
|
|
42
|
+
const transformerFactoryOOP = new transformerUtil_1.TransformerFactoryOOP(AddRemoteMethodsMeta_1.AddRemoteMethodsMeta);
|
|
43
|
+
/* Transform AST */
|
|
44
|
+
tsInstance.transform(
|
|
45
|
+
/* sourceFiles */ program.getSourceFiles().filter(sourceFile => rootFileNames.includes(sourceFile.fileName)),
|
|
46
|
+
/* transformerFactoryOOP */ [transformerFactoryOOP.asFunction], compilerOptions);
|
|
47
|
+
transformerFactoryOOP.transformRunsDone.forEach(transformRun => {
|
|
48
|
+
if (transformRun.astWasModified) {
|
|
49
|
+
/* Render modified files and create new SourceFiles for them to use in host's cache */
|
|
50
|
+
const { printFile } = tsInstance.createPrinter();
|
|
51
|
+
const sourceFile = transformRun.sourceFile;
|
|
52
|
+
const updatedSourceFile = tsInstance.createSourceFile(sourceFile.fileName, printFile(sourceFile), sourceFile.languageVersion);
|
|
53
|
+
updatedSourceFile.version = `${sourceFile.version}_restfuncs_${exports.transformerVersion.major}.${exports.transformerVersion.feature}`;
|
|
54
|
+
compilerHost.fileCache.set(sourceFile.fileName, updatedSourceFile);
|
|
14
55
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}, oe: (o, k) => {
|
|
22
|
-
try {
|
|
23
|
-
return o[k];
|
|
24
|
-
}
|
|
25
|
-
catch (e) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
}, t: {} };
|
|
29
|
-
// Just pass-through the default export of typescript-rtti/dist/transformer
|
|
30
|
-
// May be later, we need to extend the transformer for runtime inspection JSDoc information
|
|
31
|
-
const transformer_1 = __importDefault(require("typescript-rtti/dist/transformer"));
|
|
32
|
-
exports.default = transformer_1.default;
|
|
56
|
+
});
|
|
57
|
+
/* Re-create Program instance */
|
|
58
|
+
return tsInstance.createProgram(rootFileNames, compilerOptions, compilerHost);
|
|
59
|
+
}
|
|
60
|
+
exports.default = transformProgram;
|
|
61
|
+
// endregion
|
|
33
62
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAEA,iEAA4D;AAC5D,uDAA0E;AAE1E,mFAAmF;AAEtE,QAAA,kBAAkB,GAAG,EAAC,KAAK,EAAE,CAAC,EAAG,OAAO,EAAE,CAAC,EAAE,CAAA;AAE1D,wHAAwH;AACxH,kBAAkB;AAClB,wHAAwH;AAExH;;GAEG;AACH,SAAS,cAAc,CACnB,SAAmC,EACnC,UAAqB,EAC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