@zyno-io/ts-reflection 26.803.2224
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/README.md +13 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +966 -0
- package/dist/reflection/annotations.d.ts +15 -0
- package/dist/reflection/annotations.d.ts.map +1 -0
- package/dist/reflection/compact-metadata.d.ts +18 -0
- package/dist/reflection/compact-metadata.d.ts.map +1 -0
- package/dist/reflection/conversion.d.ts +15 -0
- package/dist/reflection/conversion.d.ts.map +1 -0
- package/dist/reflection/deserializer.d.ts +15 -0
- package/dist/reflection/deserializer.d.ts.map +1 -0
- package/dist/reflection/errors.d.ts +8 -0
- package/dist/reflection/errors.d.ts.map +1 -0
- package/dist/reflection/index.d.ts +10 -0
- package/dist/reflection/index.d.ts.map +1 -0
- package/dist/reflection/metadata-store.d.ts +20 -0
- package/dist/reflection/metadata-store.d.ts.map +1 -0
- package/dist/reflection/model.d.ts +273 -0
- package/dist/reflection/model.d.ts.map +1 -0
- package/dist/reflection/primitive-conversion.d.ts +2 -0
- package/dist/reflection/primitive-conversion.d.ts.map +1 -0
- package/dist/reflection/reflection-class.d.ts +60 -0
- package/dist/reflection/reflection-class.d.ts.map +1 -0
- package/dist/reflection/type-utils.d.ts +34 -0
- package/dist/reflection/type-utils.d.ts.map +1 -0
- package/dist/type-compiler/download-prebuilt.cjs +114 -0
- package/dist/type-compiler/go/ast_expression.go +187 -0
- package/dist/type-compiler/go/ast_metadata.go +388 -0
- package/dist/type-compiler/go/collect.go +963 -0
- package/dist/type-compiler/go/compact_metadata.go +553 -0
- package/dist/type-compiler/go/emission_plan.go +340 -0
- package/dist/type-compiler/go/emit_ast.go +557 -0
- package/dist/type-compiler/go/emit_ast_test.go +558 -0
- package/dist/type-compiler/go/go.mod +10 -0
- package/dist/type-compiler/go/plugin.go +359 -0
- package/dist/type-compiler/go/plugin_test.go +1206 -0
- package/dist/type-compiler/go/precompute.go +86 -0
- package/dist/type-compiler/go/receive_type.go +912 -0
- package/dist/type-compiler/go/resolve.go +265 -0
- package/dist/type-compiler/go/source_scan.go +51 -0
- package/dist/type-compiler/go/text_parse.go +734 -0
- package/dist/type-compiler/go/type_expr.go +1291 -0
- package/dist/type-compiler/go/typia_expr.go +2316 -0
- package/dist/type-compiler/index.cjs +43 -0
- package/dist/type-compiler/pnp.cjs +474 -0
- package/dist/type-compiler/prebuilt.cjs +324 -0
- package/dist/type-metadata-runtime.cjs +1 -0
- package/dist/type-metadata-runtime.d.ts +2 -0
- package/dist/type-metadata-runtime.d.ts.map +1 -0
- package/dist/type-metadata-runtime.js +107 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/primitives.d.ts +33 -0
- package/dist/types/primitives.d.ts.map +1 -0
- package/dist/types/runtime.d.ts +2 -0
- package/dist/types/runtime.d.ts.map +1 -0
- package/dist/types/type-annotations.d.ts +28 -0
- package/dist/types/type-annotations.d.ts.map +1 -0
- package/package.json +47 -0
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"path/filepath"
|
|
5
|
+
"sort"
|
|
6
|
+
"strconv"
|
|
7
|
+
"strings"
|
|
8
|
+
|
|
9
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
10
|
+
shimprinter "github.com/microsoft/typescript-go/shim/printer"
|
|
11
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
type astImportAsset struct {
|
|
15
|
+
moduleSpecifier *shimast.Node
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type astOptionalModuleLoader struct {
|
|
19
|
+
name string
|
|
20
|
+
expression *shimast.Node
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type astImportRegistry struct {
|
|
24
|
+
ec *shimprinter.EmitContext
|
|
25
|
+
sourceFile *shimast.SourceFile
|
|
26
|
+
commonJS bool
|
|
27
|
+
assets map[string]*astImportAsset
|
|
28
|
+
order []string
|
|
29
|
+
loaders map[string]*astOptionalModuleLoader
|
|
30
|
+
loaderOrder []string
|
|
31
|
+
loaderPrefix string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
func newAstImportRegistry(ec *shimprinter.EmitContext, sourceFile *shimast.SourceFile, commonJS bool) *astImportRegistry {
|
|
35
|
+
loaderPrefix := "__tsf_metadata_module_"
|
|
36
|
+
if sourceFile != nil {
|
|
37
|
+
for strings.Contains(sourceFile.Text(), loaderPrefix) {
|
|
38
|
+
loaderPrefix = "_" + loaderPrefix
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return &astImportRegistry{
|
|
42
|
+
ec: ec,
|
|
43
|
+
sourceFile: sourceFile,
|
|
44
|
+
commonJS: commonJS,
|
|
45
|
+
assets: map[string]*astImportAsset{},
|
|
46
|
+
loaders: map[string]*astOptionalModuleLoader{},
|
|
47
|
+
loaderPrefix: loaderPrefix,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
func (registry *astImportRegistry) resolvedSpecifier(spec string, targetFile string) string {
|
|
52
|
+
if targetFile != "" && registry.sourceFile != nil {
|
|
53
|
+
return moduleSpecifierForOutput(registry.sourceFile.FileName(), targetFile, !registry.commonJS)
|
|
54
|
+
}
|
|
55
|
+
if strings.HasPrefix(spec, ".") {
|
|
56
|
+
ext := strings.ToLower(filepath.Ext(spec))
|
|
57
|
+
switch ext {
|
|
58
|
+
case ".js", ".mjs", ".cjs", ".json", ".node":
|
|
59
|
+
return spec
|
|
60
|
+
case ".ts", ".tsx", ".mts", ".cts":
|
|
61
|
+
return strings.TrimSuffix(spec, filepath.Ext(spec)) + outputImportExtension(spec, true)
|
|
62
|
+
case "":
|
|
63
|
+
return spec + ".js"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return spec
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func (registry *astImportRegistry) asset(spec string, targetFile string) *astImportAsset {
|
|
70
|
+
resolved := registry.resolvedSpecifier(spec, targetFile)
|
|
71
|
+
if asset := registry.assets[resolved]; asset != nil {
|
|
72
|
+
return asset
|
|
73
|
+
}
|
|
74
|
+
asset := &astImportAsset{
|
|
75
|
+
moduleSpecifier: registry.ec.Factory.NewStringLiteral(resolved, shimast.TokenFlagsNone),
|
|
76
|
+
}
|
|
77
|
+
registry.assets[resolved] = asset
|
|
78
|
+
registry.order = append(registry.order, resolved)
|
|
79
|
+
return asset
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
func (registry *astImportRegistry) namespace(spec string, targetFile string) *shimast.Node {
|
|
83
|
+
if registry.commonJS {
|
|
84
|
+
return registry.safeRequire(registry.resolvedSpecifier(spec, targetFile))
|
|
85
|
+
}
|
|
86
|
+
asset := registry.asset(spec, targetFile)
|
|
87
|
+
return registry.ec.Factory.NewGeneratedNameForNode(asset.moduleSpecifier)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func (registry *astImportRegistry) member(spec string, exportName string, targetFile string) *shimast.Node {
|
|
91
|
+
if registry.commonJS {
|
|
92
|
+
return registry.lazyRequireMember(registry.resolvedSpecifier(spec, targetFile), exportName)
|
|
93
|
+
}
|
|
94
|
+
return registry.ec.Factory.NewPropertyAccessExpression(
|
|
95
|
+
registry.namespace(spec, targetFile),
|
|
96
|
+
nil,
|
|
97
|
+
registry.ec.Factory.NewIdentifier(exportName),
|
|
98
|
+
shimast.NodeFlagsNone,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// staticMember returns a normal namespace-import reference for a required
|
|
103
|
+
// runtime dependency. Unlike reflected application imports, the compact
|
|
104
|
+
// metadata decoder is always present and should be loaded once per file in both
|
|
105
|
+
// CommonJS and ESM output instead of expanded as a defensive lazy require at
|
|
106
|
+
// every metadata expression.
|
|
107
|
+
func (registry *astImportRegistry) staticMember(spec string, exportName string) *shimast.Node {
|
|
108
|
+
asset := registry.asset(spec, "")
|
|
109
|
+
return registry.ec.Factory.NewPropertyAccessExpression(
|
|
110
|
+
registry.ec.Factory.NewGeneratedNameForNode(asset.moduleSpecifier),
|
|
111
|
+
nil,
|
|
112
|
+
registry.ec.Factory.NewIdentifier(exportName),
|
|
113
|
+
shimast.NodeFlagsNone,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func (registry *astImportRegistry) optionalModuleLoader(spec string, targetFile string) *shimast.Node {
|
|
118
|
+
resolved := registry.resolvedSpecifier(spec, targetFile)
|
|
119
|
+
if loader := registry.loaders[resolved]; loader != nil {
|
|
120
|
+
return registry.ec.Factory.NewIdentifier(loader.name)
|
|
121
|
+
}
|
|
122
|
+
name := registry.loaderPrefix + strconv.Itoa(len(registry.loaderOrder))
|
|
123
|
+
var expression *shimast.Node
|
|
124
|
+
if registry.commonJS {
|
|
125
|
+
expression = registry.safeRequire(resolved)
|
|
126
|
+
} else {
|
|
127
|
+
expression = registry.namespace(spec, targetFile)
|
|
128
|
+
}
|
|
129
|
+
registry.loaders[resolved] = &astOptionalModuleLoader{name: name, expression: expression}
|
|
130
|
+
registry.loaderOrder = append(registry.loaderOrder, resolved)
|
|
131
|
+
return registry.ec.Factory.NewIdentifier(name)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
func (registry *astImportRegistry) isOptionalModuleLoader(node *shimast.Node) bool {
|
|
135
|
+
if node == nil || node.Kind != shimast.KindIdentifier {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
for _, loader := range registry.loaders {
|
|
139
|
+
if loader.name == node.Text() {
|
|
140
|
+
return true
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return false
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
func (registry *astImportRegistry) requireCall(spec string) *shimast.Node {
|
|
147
|
+
return registry.ec.Factory.NewCallExpression(
|
|
148
|
+
registry.ec.Factory.NewIdentifier("require"),
|
|
149
|
+
nil,
|
|
150
|
+
nil,
|
|
151
|
+
registry.ec.Factory.NewNodeList([]*shimast.Node{registry.ec.Factory.NewStringLiteral(spec, shimast.TokenFlagsNone)}),
|
|
152
|
+
shimast.NodeFlagsNone,
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func (registry *astImportRegistry) requireAvailable() *shimast.Node {
|
|
157
|
+
return registry.ec.Factory.NewBinaryExpression(
|
|
158
|
+
nil,
|
|
159
|
+
registry.ec.Factory.NewTypeOfExpression(registry.ec.Factory.NewIdentifier("require")),
|
|
160
|
+
nil,
|
|
161
|
+
registry.ec.Factory.NewToken(shimast.KindExclamationEqualsEqualsToken),
|
|
162
|
+
registry.ec.Factory.NewStringLiteral("undefined", shimast.TokenFlagsNone),
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
func (registry *astImportRegistry) lazyRequireMember(spec string, exportName string) *shimast.Node {
|
|
167
|
+
member := registry.ec.Factory.NewPropertyAccessExpression(
|
|
168
|
+
registry.requireCall(spec),
|
|
169
|
+
nil,
|
|
170
|
+
registry.ec.Factory.NewIdentifier(exportName),
|
|
171
|
+
shimast.NodeFlagsNone,
|
|
172
|
+
)
|
|
173
|
+
return registry.ec.Factory.NewParenthesizedExpression(registry.ec.Factory.NewConditionalExpression(
|
|
174
|
+
registry.requireAvailable(),
|
|
175
|
+
registry.ec.Factory.NewToken(shimast.KindQuestionToken),
|
|
176
|
+
member,
|
|
177
|
+
registry.ec.Factory.NewToken(shimast.KindColonToken),
|
|
178
|
+
registry.ec.Factory.NewIdentifier("undefined"),
|
|
179
|
+
))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
func (registry *astImportRegistry) safeRequire(spec string) *shimast.Node {
|
|
183
|
+
value := registry.ec.Factory.NewConditionalExpression(
|
|
184
|
+
registry.requireAvailable(),
|
|
185
|
+
registry.ec.Factory.NewToken(shimast.KindQuestionToken),
|
|
186
|
+
registry.requireCall(spec),
|
|
187
|
+
registry.ec.Factory.NewToken(shimast.KindColonToken),
|
|
188
|
+
registry.ec.Factory.NewIdentifier("undefined"),
|
|
189
|
+
)
|
|
190
|
+
tryBlock := registry.ec.Factory.NewBlock(
|
|
191
|
+
registry.ec.Factory.NewNodeList([]*shimast.Node{registry.ec.Factory.NewReturnStatement(value)}),
|
|
192
|
+
true,
|
|
193
|
+
)
|
|
194
|
+
catchBlock := registry.ec.Factory.NewBlock(
|
|
195
|
+
registry.ec.Factory.NewNodeList([]*shimast.Node{registry.ec.Factory.NewReturnStatement(registry.ec.Factory.NewIdentifier("undefined"))}),
|
|
196
|
+
true,
|
|
197
|
+
)
|
|
198
|
+
body := registry.ec.Factory.NewBlock(
|
|
199
|
+
registry.ec.Factory.NewNodeList([]*shimast.Node{registry.ec.Factory.NewTryStatement(
|
|
200
|
+
tryBlock,
|
|
201
|
+
registry.ec.Factory.NewCatchClause(nil, catchBlock),
|
|
202
|
+
nil,
|
|
203
|
+
)}),
|
|
204
|
+
true,
|
|
205
|
+
)
|
|
206
|
+
arrow := registry.ec.Factory.NewArrowFunction(
|
|
207
|
+
nil,
|
|
208
|
+
nil,
|
|
209
|
+
registry.ec.Factory.NewNodeList(nil),
|
|
210
|
+
nil,
|
|
211
|
+
nil,
|
|
212
|
+
registry.ec.Factory.NewToken(shimast.KindEqualsGreaterThanToken),
|
|
213
|
+
body,
|
|
214
|
+
)
|
|
215
|
+
return registry.ec.Factory.NewCallExpression(
|
|
216
|
+
registry.ec.Factory.NewParenthesizedExpression(arrow),
|
|
217
|
+
nil,
|
|
218
|
+
nil,
|
|
219
|
+
registry.ec.Factory.NewNodeList(nil),
|
|
220
|
+
shimast.NodeFlagsNone,
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
func (registry *astImportRegistry) statements() []*shimast.Node {
|
|
225
|
+
statements := make([]*shimast.Node, 0, len(registry.order)+len(registry.loaderOrder))
|
|
226
|
+
for _, spec := range registry.order {
|
|
227
|
+
asset := registry.assets[spec]
|
|
228
|
+
statements = append(statements, registry.ec.Factory.NewImportDeclaration(
|
|
229
|
+
nil,
|
|
230
|
+
registry.ec.Factory.NewImportClause(
|
|
231
|
+
0,
|
|
232
|
+
nil,
|
|
233
|
+
registry.ec.Factory.NewNamespaceImport(registry.ec.Factory.NewGeneratedNameForNode(asset.moduleSpecifier)),
|
|
234
|
+
),
|
|
235
|
+
asset.moduleSpecifier,
|
|
236
|
+
nil,
|
|
237
|
+
))
|
|
238
|
+
}
|
|
239
|
+
for _, spec := range registry.loaderOrder {
|
|
240
|
+
loader := registry.loaders[spec]
|
|
241
|
+
statements = append(statements, expressionFactoryDeclaration(registry.ec, loader.name, loader.expression))
|
|
242
|
+
}
|
|
243
|
+
return statements
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
func metadataTransform(plans emissionPlans) driver.PluginTransform {
|
|
247
|
+
return func(ec *shimprinter.EmitContext, sourceFile *shimast.SourceFile) *shimast.SourceFile {
|
|
248
|
+
if sourceFile == nil || sourceFile.IsDeclarationFile {
|
|
249
|
+
return sourceFile
|
|
250
|
+
}
|
|
251
|
+
plan := plans[sourceFile.FileName()]
|
|
252
|
+
if plan == nil {
|
|
253
|
+
return sourceFile
|
|
254
|
+
}
|
|
255
|
+
imports := newAstImportRegistry(ec, sourceFile, plan.commonJS)
|
|
256
|
+
runtimeReferences := newCompactMetadataRuntimeInterner(ec, sourceFile)
|
|
257
|
+
var metadataTypes *shimast.Node
|
|
258
|
+
if len(plan.metadataTypes) != 0 {
|
|
259
|
+
expression := materializeCompactMetadataRegistry(
|
|
260
|
+
ec,
|
|
261
|
+
imports,
|
|
262
|
+
runtimeReferences,
|
|
263
|
+
plan.metadataTypes,
|
|
264
|
+
plan.metadataTypeResolver,
|
|
265
|
+
)
|
|
266
|
+
metadataTypes = metadataTypeDeclaration(ec, plan.metadataTypeResolver, expression)
|
|
267
|
+
}
|
|
268
|
+
var visitor *shimast.NodeVisitor
|
|
269
|
+
visitor = ec.NewNodeVisitor(func(node *shimast.Node) *shimast.Node {
|
|
270
|
+
if node == nil {
|
|
271
|
+
return nil
|
|
272
|
+
}
|
|
273
|
+
original := ec.MostOriginal(node)
|
|
274
|
+
originalPos := node.Pos()
|
|
275
|
+
if original != nil {
|
|
276
|
+
originalPos = original.Pos()
|
|
277
|
+
}
|
|
278
|
+
if node.Kind == shimast.KindCallExpression {
|
|
279
|
+
visited := visitor.VisitEachChild(node)
|
|
280
|
+
callPlan, ok := plan.calls[originalPos]
|
|
281
|
+
if !ok || visited == nil || visited.Kind != shimast.KindCallExpression {
|
|
282
|
+
return visited
|
|
283
|
+
}
|
|
284
|
+
return updateMetadataCall(ec, visited.AsCallExpression(), callPlan, imports, runtimeReferences, plan.metadataTypeResolver)
|
|
285
|
+
}
|
|
286
|
+
if node.Kind == shimast.KindClassDeclaration {
|
|
287
|
+
visited := visitor.VisitEachChild(node)
|
|
288
|
+
classPlan, ok := plan.classes[originalPos]
|
|
289
|
+
if !ok || visited == nil || visited.Kind != shimast.KindClassDeclaration {
|
|
290
|
+
return visited
|
|
291
|
+
}
|
|
292
|
+
metadata := materializeCompactMetadataExpression(
|
|
293
|
+
ec,
|
|
294
|
+
imports,
|
|
295
|
+
runtimeReferences,
|
|
296
|
+
classPlan.metadata,
|
|
297
|
+
plan.metadataTypeResolver,
|
|
298
|
+
)
|
|
299
|
+
metadataReference := ec.Factory.NewUniqueName("_" + classPlan.name + "Metadata")
|
|
300
|
+
declaration := classMetadataDeclaration(ec, metadataReference, original)
|
|
301
|
+
withMetadata := classWithStaticMetadata(ec, visited.AsClassDeclaration(), metadataReference, metadata)
|
|
302
|
+
classTypeAssignment := classMetadataClassTypeAssignment(ec, withMetadata, original, classPlan.name, metadataReference)
|
|
303
|
+
assignment := classMetadataAssignment(ec, withMetadata, original, classPlan.name, metadataReference)
|
|
304
|
+
return ec.Factory.NewSyntaxList([]*shimast.Node{declaration, withMetadata, classTypeAssignment, assignment})
|
|
305
|
+
}
|
|
306
|
+
return visitor.VisitEachChild(node)
|
|
307
|
+
})
|
|
308
|
+
output := visitor.VisitNode(sourceFile.AsNode())
|
|
309
|
+
if output == nil {
|
|
310
|
+
return sourceFile
|
|
311
|
+
}
|
|
312
|
+
result := output.AsSourceFile()
|
|
313
|
+
if plan.aliases != nil {
|
|
314
|
+
result = appendAliasMetadata(ec, result, materializeCompactAliasMetadataExpression(
|
|
315
|
+
ec,
|
|
316
|
+
imports,
|
|
317
|
+
runtimeReferences,
|
|
318
|
+
*plan.aliases,
|
|
319
|
+
plan.metadataTypeResolver,
|
|
320
|
+
))
|
|
321
|
+
}
|
|
322
|
+
declarations := []*shimast.Node{}
|
|
323
|
+
if metadataTypes != nil {
|
|
324
|
+
declarations = append(declarations, metadataTypes)
|
|
325
|
+
}
|
|
326
|
+
if len(declarations) != 0 {
|
|
327
|
+
result = injectMetadataTypeDeclarations(ec, result, declarations)
|
|
328
|
+
}
|
|
329
|
+
if statements := imports.statements(); len(statements) != 0 {
|
|
330
|
+
result = injectAstImports(ec, result, statements)
|
|
331
|
+
}
|
|
332
|
+
return result
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
func metadataTypeDeclaration(ec *shimprinter.EmitContext, name string, metadata *shimast.Node) *shimast.Node {
|
|
337
|
+
return ec.Factory.NewVariableStatement(
|
|
338
|
+
nil,
|
|
339
|
+
ec.Factory.NewVariableDeclarationList(
|
|
340
|
+
ec.Factory.NewNodeList([]*shimast.Node{ec.Factory.NewVariableDeclaration(
|
|
341
|
+
ec.Factory.NewIdentifier(name),
|
|
342
|
+
nil,
|
|
343
|
+
nil,
|
|
344
|
+
metadata,
|
|
345
|
+
)}),
|
|
346
|
+
shimast.NodeFlagsConst,
|
|
347
|
+
),
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
func injectMetadataTypeDeclarations(ec *shimprinter.EmitContext, sourceFile *shimast.SourceFile, declarations []*shimast.Node) *shimast.SourceFile {
|
|
352
|
+
index := 0
|
|
353
|
+
for index < len(sourceFile.Statements.Nodes) {
|
|
354
|
+
statement := sourceFile.Statements.Nodes[index]
|
|
355
|
+
if statement == nil {
|
|
356
|
+
break
|
|
357
|
+
}
|
|
358
|
+
if statement.Kind == shimast.KindImportDeclaration || statement.Kind == shimast.KindImportEqualsDeclaration {
|
|
359
|
+
index++
|
|
360
|
+
continue
|
|
361
|
+
}
|
|
362
|
+
if statement.Kind == shimast.KindExpressionStatement {
|
|
363
|
+
expression := statement.AsExpressionStatement().Expression
|
|
364
|
+
if expression != nil && expression.Kind == shimast.KindStringLiteral {
|
|
365
|
+
index++
|
|
366
|
+
continue
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
break
|
|
370
|
+
}
|
|
371
|
+
statements := make([]*shimast.Node, 0, len(sourceFile.Statements.Nodes)+len(declarations))
|
|
372
|
+
statements = append(statements, sourceFile.Statements.Nodes[:index]...)
|
|
373
|
+
statements = append(statements, declarations...)
|
|
374
|
+
statements = append(statements, sourceFile.Statements.Nodes[index:]...)
|
|
375
|
+
return ec.Factory.UpdateSourceFile(sourceFile, ec.Factory.NewNodeList(statements), sourceFile.EndOfFileToken).AsSourceFile()
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// classWithStaticMetadata initializes reflection metadata as part of the class
|
|
379
|
+
// itself. TypeScript's decorator transform therefore evaluates it before class
|
|
380
|
+
// decorators run, preserving the ordering of the previous inline-class emit.
|
|
381
|
+
// The companion post-class assignment reuses the same metadata object so a
|
|
382
|
+
// decorator that mutates metadata or replaces the constructor preserves it.
|
|
383
|
+
func classWithStaticMetadata(ec *shimprinter.EmitContext, class *shimast.ClassDeclaration, metadataReference *shimast.Node, metadata *shimast.Node) *shimast.Node {
|
|
384
|
+
members := append([]*shimast.Node{}, class.Members.Nodes...)
|
|
385
|
+
initializer := ec.Factory.NewBinaryExpression(
|
|
386
|
+
nil,
|
|
387
|
+
metadataReference,
|
|
388
|
+
nil,
|
|
389
|
+
ec.Factory.NewToken(shimast.KindEqualsToken),
|
|
390
|
+
metadata,
|
|
391
|
+
)
|
|
392
|
+
members = append(members, ec.Factory.NewPropertyDeclaration(
|
|
393
|
+
ec.Factory.NewModifierList([]*shimast.Node{ec.Factory.NewModifier(shimast.KindStaticKeyword)}),
|
|
394
|
+
ec.Factory.NewIdentifier("__tsfType"),
|
|
395
|
+
nil,
|
|
396
|
+
nil,
|
|
397
|
+
initializer,
|
|
398
|
+
))
|
|
399
|
+
return ec.Factory.UpdateClassDeclaration(
|
|
400
|
+
class,
|
|
401
|
+
class.Modifiers(),
|
|
402
|
+
class.Name(),
|
|
403
|
+
class.TypeParameters,
|
|
404
|
+
class.HeritageClauses,
|
|
405
|
+
ec.Factory.NewNodeList(members),
|
|
406
|
+
)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
func classMetadataDeclaration(ec *shimprinter.EmitContext, name *shimast.Node, original *shimast.Node) *shimast.Node {
|
|
410
|
+
declaration := ec.Factory.NewVariableDeclaration(name, nil, nil, nil)
|
|
411
|
+
statement := ec.Factory.NewVariableStatement(
|
|
412
|
+
nil,
|
|
413
|
+
ec.Factory.NewVariableDeclarationList(
|
|
414
|
+
ec.Factory.NewNodeList([]*shimast.Node{declaration}),
|
|
415
|
+
shimast.NodeFlagsLet,
|
|
416
|
+
),
|
|
417
|
+
)
|
|
418
|
+
if original != nil {
|
|
419
|
+
ec.SetOriginal(statement, original)
|
|
420
|
+
}
|
|
421
|
+
return statement
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
func updateMetadataCall(
|
|
425
|
+
ec *shimprinter.EmitContext,
|
|
426
|
+
call *shimast.CallExpression,
|
|
427
|
+
plan callEmissionPlan,
|
|
428
|
+
imports *astImportRegistry,
|
|
429
|
+
runtimeReferences *compactMetadataRuntimeInterner,
|
|
430
|
+
metadataTypeResolver string,
|
|
431
|
+
) *shimast.Node {
|
|
432
|
+
arguments := []*shimast.Node{}
|
|
433
|
+
if call.Arguments != nil {
|
|
434
|
+
arguments = append(arguments, call.Arguments.Nodes...)
|
|
435
|
+
}
|
|
436
|
+
if len(arguments) > plan.metadataArgIndex {
|
|
437
|
+
return call.AsNode()
|
|
438
|
+
}
|
|
439
|
+
for len(arguments) < plan.metadataArgIndex {
|
|
440
|
+
arguments = append(arguments, ec.Factory.NewIdentifier("undefined"))
|
|
441
|
+
}
|
|
442
|
+
arguments = append(arguments, materializeCompactMetadataExpression(
|
|
443
|
+
ec,
|
|
444
|
+
imports,
|
|
445
|
+
runtimeReferences,
|
|
446
|
+
plan.metadata,
|
|
447
|
+
metadataTypeResolver,
|
|
448
|
+
))
|
|
449
|
+
return ec.Factory.UpdateCallExpression(
|
|
450
|
+
call,
|
|
451
|
+
call.Expression,
|
|
452
|
+
call.QuestionDotToken,
|
|
453
|
+
call.TypeArguments,
|
|
454
|
+
ec.Factory.NewNodeList(arguments),
|
|
455
|
+
call.Flags,
|
|
456
|
+
)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
func classMetadataAssignment(ec *shimprinter.EmitContext, visited *shimast.Node, original *shimast.Node, className string, metadataReference *shimast.Node) *shimast.Node {
|
|
460
|
+
left := ec.Factory.NewPropertyAccessExpression(
|
|
461
|
+
classNameReference(ec, visited, original, className),
|
|
462
|
+
nil,
|
|
463
|
+
ec.Factory.NewIdentifier("__tsfType"),
|
|
464
|
+
shimast.NodeFlagsNone,
|
|
465
|
+
)
|
|
466
|
+
assignment := ec.Factory.NewBinaryExpression(
|
|
467
|
+
nil,
|
|
468
|
+
left,
|
|
469
|
+
nil,
|
|
470
|
+
ec.Factory.NewToken(shimast.KindEqualsToken),
|
|
471
|
+
metadataReference,
|
|
472
|
+
)
|
|
473
|
+
statement := ec.Factory.NewExpressionStatement(assignment)
|
|
474
|
+
if original != nil {
|
|
475
|
+
ec.SetOriginal(statement, original)
|
|
476
|
+
}
|
|
477
|
+
return statement
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
func classMetadataClassTypeAssignment(ec *shimprinter.EmitContext, visited *shimast.Node, original *shimast.Node, className string, metadataReference *shimast.Node) *shimast.Node {
|
|
481
|
+
left := ec.Factory.NewPropertyAccessExpression(
|
|
482
|
+
metadataReference,
|
|
483
|
+
nil,
|
|
484
|
+
ec.Factory.NewIdentifier("classType"),
|
|
485
|
+
shimast.NodeFlagsNone,
|
|
486
|
+
)
|
|
487
|
+
assignment := ec.Factory.NewBinaryExpression(
|
|
488
|
+
nil,
|
|
489
|
+
left,
|
|
490
|
+
nil,
|
|
491
|
+
ec.Factory.NewToken(shimast.KindEqualsToken),
|
|
492
|
+
classNameReference(ec, visited, original, className),
|
|
493
|
+
)
|
|
494
|
+
statement := ec.Factory.NewExpressionStatement(assignment)
|
|
495
|
+
if original != nil {
|
|
496
|
+
ec.SetOriginal(statement, original)
|
|
497
|
+
}
|
|
498
|
+
return statement
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
func classNameReference(ec *shimprinter.EmitContext, visited *shimast.Node, original *shimast.Node, className string) *shimast.Node {
|
|
502
|
+
name := ec.Factory.NewIdentifier(className)
|
|
503
|
+
if original != nil && original.Name() != nil {
|
|
504
|
+
ec.SetOriginal(name, original.Name())
|
|
505
|
+
} else if visited.Name() != nil {
|
|
506
|
+
ec.SetOriginal(name, visited.Name())
|
|
507
|
+
}
|
|
508
|
+
return name
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
func appendAliasMetadata(ec *shimprinter.EmitContext, sourceFile *shimast.SourceFile, metadata *shimast.Node) *shimast.SourceFile {
|
|
512
|
+
declaration := ec.Factory.NewVariableDeclaration(
|
|
513
|
+
ec.Factory.NewIdentifier("__tsfTypeAliases"),
|
|
514
|
+
nil,
|
|
515
|
+
nil,
|
|
516
|
+
metadata,
|
|
517
|
+
)
|
|
518
|
+
statement := ec.Factory.NewVariableStatement(
|
|
519
|
+
ec.Factory.NewModifierList([]*shimast.Node{ec.Factory.NewModifier(shimast.KindExportKeyword)}),
|
|
520
|
+
ec.Factory.NewVariableDeclarationList(
|
|
521
|
+
ec.Factory.NewNodeList([]*shimast.Node{declaration}),
|
|
522
|
+
shimast.NodeFlagsConst,
|
|
523
|
+
),
|
|
524
|
+
)
|
|
525
|
+
statements := append([]*shimast.Node{}, sourceFile.Statements.Nodes...)
|
|
526
|
+
statements = append(statements, statement)
|
|
527
|
+
return ec.Factory.UpdateSourceFile(sourceFile, ec.Factory.NewNodeList(statements), sourceFile.EndOfFileToken).AsSourceFile()
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
func injectAstImports(ec *shimprinter.EmitContext, sourceFile *shimast.SourceFile, imports []*shimast.Node) *shimast.SourceFile {
|
|
531
|
+
index := 0
|
|
532
|
+
for index < len(sourceFile.Statements.Nodes) {
|
|
533
|
+
statement := sourceFile.Statements.Nodes[index]
|
|
534
|
+
if statement == nil || statement.Kind != shimast.KindExpressionStatement {
|
|
535
|
+
break
|
|
536
|
+
}
|
|
537
|
+
expression := statement.AsExpressionStatement().Expression
|
|
538
|
+
if expression == nil || expression.Kind != shimast.KindStringLiteral {
|
|
539
|
+
break
|
|
540
|
+
}
|
|
541
|
+
index++
|
|
542
|
+
}
|
|
543
|
+
statements := make([]*shimast.Node, 0, len(sourceFile.Statements.Nodes)+len(imports))
|
|
544
|
+
statements = append(statements, sourceFile.Statements.Nodes[:index]...)
|
|
545
|
+
statements = append(statements, imports...)
|
|
546
|
+
statements = append(statements, sourceFile.Statements.Nodes[index:]...)
|
|
547
|
+
return ec.Factory.UpdateSourceFile(sourceFile, ec.Factory.NewNodeList(statements), sourceFile.EndOfFileToken).AsSourceFile()
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
func sortedEmissionPlanFiles(plans emissionPlans) []string {
|
|
551
|
+
files := make([]string, 0, len(plans))
|
|
552
|
+
for file := range plans {
|
|
553
|
+
files = append(files, file)
|
|
554
|
+
}
|
|
555
|
+
sort.Strings(files)
|
|
556
|
+
return files
|
|
557
|
+
}
|