@overmvp/kumi 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +14 -2
- package/animation/color-transition.ts +196 -0
- package/animation/cycle.ts +151 -0
- package/animation/ease.ts +29 -0
- package/animation/index.ts +13 -0
- package/animation/package.json +12 -0
- package/animation/presets.ts +376 -0
- package/animation/spec.ts +197 -0
- package/builder/Cargo.lock +192 -0
- package/builder/Cargo.toml +7 -0
- package/builder/css-v1.mjs +334 -0
- package/builder/css-v1.test.mjs +321 -0
- package/builder/generated-output.mjs +61 -0
- package/builder/generated-output.test.mjs +132 -0
- package/builder/jsx-form.mjs +93 -0
- package/builder/jsx-form.test.mjs +48 -0
- package/builder/jsx-lists.mjs +209 -0
- package/builder/link-addon-package.mjs +86 -0
- package/builder/lua-assets.mjs +74 -0
- package/builder/lua-builtin-values.mjs +57 -0
- package/builder/lua-builtin-values.test.mjs +155 -0
- package/builder/lua-loop-closures.mjs +183 -0
- package/builder/lua-loop-closures.test.mjs +89 -0
- package/builder/lua-truthiness.mjs +61 -0
- package/builder/lua-truthiness.test.mjs +113 -0
- package/builder/module-declarations.mjs +594 -0
- package/builder/module-declarations.test.mjs +444 -0
- package/builder/native-kernels.mjs +60 -0
- package/builder/native-lua.d.ts +57 -0
- package/builder/native-routes.mjs +51 -0
- package/builder/no-static-ref-read.mjs +93 -0
- package/builder/no-static-ref-read.test.mjs +181 -0
- package/builder/secure-snippet.mjs +317 -0
- package/builder/secure-snippet.test.mjs +101 -0
- package/builder/shared-lua-runtime.mjs +150 -0
- package/builder/src/addon-link.lua +29 -0
- package/builder/src/arrays.lua +260 -0
- package/builder/src/assets.rs +177 -0
- package/builder/src/classes.lua +75 -0
- package/builder/src/collections.lua +190 -0
- package/builder/src/compiler.rs +2164 -0
- package/builder/src/config.rs +42 -0
- package/builder/src/entries.rs +166 -0
- package/builder/src/errors.lua +15 -0
- package/builder/src/lua_data.rs +231 -0
- package/builder/src/main.rs +367 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +47 -0
- package/builder/src/numbers.lua +62 -0
- package/builder/src/project-front.mjs +3331 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +127 -0
- package/builder/src/signal-kernel.fingerprint +1 -0
- package/builder/src/strings.lua +103 -0
- package/builder/src/ts-lower.mjs +68 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-states.mjs +100 -0
- package/builder/style-states.test.mjs +103 -0
- package/builder/vite-assets.mjs +53 -0
- package/builder/vite-project.mjs +10 -0
- package/config.ts +17 -0
- package/form/index.ts +522 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +22 -4
- package/router/index.ts +43 -0
- package/router/package.json +12 -0
- package/router/routes.ts +64 -0
- package/scripts/kumi-cli.mjs +70 -0
- package/scripts/link-addons.mjs +51 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +234 -0
- package/src/css-v1.ts +58 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +479 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +334 -0
- package/src/grid-tracks.ts +165 -0
- package/src/image-imports.d.ts +24 -0
- package/src/images.ts +79 -0
- package/src/index.ts +28 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +22 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +363 -0
- package/src/media-query.ts +144 -0
- package/src/module.ts +277 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +44 -0
- package/src/providers.ts +235 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +254 -0
- package/src/result.ts +58 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +30 -0
- package/src/style-classes.ts +51 -0
- package/src/style-states.ts +134 -0
- package/src/style-variables.ts +53 -0
- package/src/text-children.ts +42 -0
- package/src/theme.ts +50 -0
- package/src/transition.ts +18 -0
- package/state/index.ts +3 -0
- package/state/package.json +12 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +82 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/README.md +14 -0
- package/templates/addon/__ADDON_NAME__.toc +4 -0
- package/templates/addon/eslint.config.mjs +12 -0
- package/templates/addon/gitignore +8 -0
- package/templates/addon/package.json +21 -0
- package/templates/addon/scripts/build.mjs +25 -0
- package/templates/addon/src/app/main.tsx +13 -0
- package/templates/addon/tsconfig.json +24 -0
- package/translate/index.ts +14 -0
- package/translate/package.json +9 -0
- package/utils/colors.ts +76 -0
- package/utils/format.ts +37 -0
- package/utils/index.ts +4 -0
- package/utils/number.ts +17 -0
- package/utils/package.json +7 -0
- package/utils/text.ts +6 -0
- package/validate/index.ts +119 -0
- package/validate/package.json +9 -0
- package/wow/absolute-position.ts +142 -0
- package/wow/animation.ts +428 -0
- package/wow/aura-button.ts +76 -0
- package/wow/aura-container.ts +591 -0
- package/wow/aura-countdown.ts +201 -0
- package/wow/aura-slot.ts +153 -0
- package/wow/bar-shade.ts +93 -0
- package/wow/borrowed-slot.ts +216 -0
- package/wow/button-drag.ts +48 -0
- package/wow/cast-clock.ts +153 -0
- package/wow/cast-text.ts +83 -0
- package/wow/class-colors.ts +126 -0
- package/wow/clock.ts +7 -0
- package/wow/color-picker.ts +128 -0
- package/wow/commands.ts +17 -0
- package/wow/contained-frame.ts +128 -0
- package/wow/contract.ts +42 -0
- package/wow/css-box.ts +110 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +398 -0
- package/wow/css-text.ts +399 -0
- package/wow/cvar-schema.ts +32 -0
- package/wow/cvars.ts +98 -0
- package/wow/dropdown-art.ts +39 -0
- package/wow/element-click.ts +20 -0
- package/wow/element-event-coordinator.ts +219 -0
- package/wow/element-events.ts +317 -0
- package/wow/event-dispatcher.ts +151 -0
- package/wow/events.ts +50 -0
- package/wow/font-cache.ts +92 -0
- package/wow/frame-access.ts +31 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +97 -0
- package/wow/layout-view.ts +651 -0
- package/wow/lua-string.ts +18 -0
- package/wow/lua-table.ts +18 -0
- package/wow/macro-book.ts +84 -0
- package/wow/map-position.ts +27 -0
- package/wow/media-query.ts +65 -0
- package/wow/module-events.ts +11 -0
- package/wow/native-hooks.ts +168 -0
- package/wow/native-placement.ts +88 -0
- package/wow/native.ts +135 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/protect.ts +24 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/range-art.ts +74 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +156 -0
- package/wow/saved-variables.ts +60 -0
- package/wow/scroll-view.ts +64 -0
- package/wow/secret-ref.ts +14 -0
- package/wow/secure-bindings.ts +89 -0
- package/wow/secure-clicks.ts +432 -0
- package/wow/secure-snippet.ts +235 -0
- package/wow/secure-state.ts +130 -0
- package/wow/secure-target-visibility.ts +89 -0
- package/wow/secure-transaction.ts +331 -0
- package/wow/special-window.ts +269 -0
- package/wow/start-addon.ts +99 -0
- package/wow/state-paint.ts +256 -0
- package/wow/transition.ts +86 -0
- package/wow/unit-button.ts +39 -0
- package/wow/unit-cast.ts +92 -0
- package/wow/unit-health-text.ts +47 -0
- package/wow/view-nodes.ts +304 -0
- package/wow/view.ts +1746 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
const moduleMarkers = new Set(["Module", "ModuleType", "Injectable", "Inject"]);
|
|
2
|
+
|
|
3
|
+
export function transformModuleDeclarations(ts, path, source, { program, rejectUnknown = false, onModule, isMarkerSymbol } = {}) {
|
|
4
|
+
if (!/\.tsx?$/.test(path) || path.endsWith(".d.ts") || !source.includes("@")) return;
|
|
5
|
+
const original = program?.getSourceFile(path);
|
|
6
|
+
const file = original?.text === source ? original : ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true);
|
|
7
|
+
const checker = file === original ? program.getTypeChecker() : undefined;
|
|
8
|
+
const constants = new Map();
|
|
9
|
+
const imports = new Map();
|
|
10
|
+
const markerImports = [];
|
|
11
|
+
const addedImports = new Map();
|
|
12
|
+
const loweredProviders = new Set();
|
|
13
|
+
const loweredProviderObjects = new Set();
|
|
14
|
+
const usedNames = new Set();
|
|
15
|
+
const changes = [];
|
|
16
|
+
const loweredModule = freshName("__kumiDefineModule");
|
|
17
|
+
let hasModule = false;
|
|
18
|
+
let scopeImport;
|
|
19
|
+
let eventImports;
|
|
20
|
+
|
|
21
|
+
for (const node of file.statements) {
|
|
22
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
23
|
+
const clause = node.importClause;
|
|
24
|
+
const bindings = clause?.namedBindings;
|
|
25
|
+
if (!clause || clause.isTypeOnly || !bindings || !ts.isNamedImports(bindings)) continue;
|
|
26
|
+
const special = bindings.elements.filter(element => markerImport(element) !== undefined);
|
|
27
|
+
if (special.length) {
|
|
28
|
+
markerImports.push({ node, clause, bindings, special });
|
|
29
|
+
for (const element of special) imports.set(element.name.text, markerImport(element));
|
|
30
|
+
}
|
|
31
|
+
} else if (ts.isVariableStatement(node) && (node.declarationList.flags & ts.NodeFlags.Const) !== 0) {
|
|
32
|
+
for (const declaration of node.declarationList.declarations) {
|
|
33
|
+
if (ts.isIdentifier(declaration.name) && declaration.initializer) constants.set(declaration.name.text, declaration.initializer);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function freshName(base) {
|
|
39
|
+
let name = base;
|
|
40
|
+
while (usedNames.has(name) || source.includes(name)) name += "_";
|
|
41
|
+
usedNames.add(name);
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function error(node, message) {
|
|
46
|
+
const origin = node.getSourceFile();
|
|
47
|
+
const { line, character } = origin.getLineAndCharacterOfPosition(node.getStart(origin));
|
|
48
|
+
throw new Error((origin === file ? path : origin.fileName) + ":" + (line + 1) + ":" + (character + 1) + ": " + message);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function unwrap(node) {
|
|
52
|
+
while (ts.isAsExpression(node) || ts.isSatisfiesExpression(node) || ts.isParenthesizedExpression(node)) node = node.expression;
|
|
53
|
+
return node;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function markerImport(node) {
|
|
57
|
+
if (ts.isTypeOnlyImportOrExportDeclaration(node)) return;
|
|
58
|
+
const declaration = node.parent.parent.parent;
|
|
59
|
+
if (!ts.isImportDeclaration(declaration)) return;
|
|
60
|
+
const imported = (node.propertyName ?? node.name).text;
|
|
61
|
+
const recognized = declaration.moduleSpecifier.text === "@kumi/core" && moduleMarkers.has(imported)
|
|
62
|
+
|| declaration.moduleSpecifier.text === "@kumi/wow" && imported === "OnEvent";
|
|
63
|
+
if (!recognized) return;
|
|
64
|
+
if (isMarkerSymbol && (!checker || !isMarkerSymbol(checker.getAliasedSymbol(checker.getSymbolAtLocation(node.name)), imported))) return;
|
|
65
|
+
return imported;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function marker(identifier) {
|
|
69
|
+
if (!ts.isIdentifier(identifier)) return;
|
|
70
|
+
if (checker === undefined) return imports.get(identifier.text);
|
|
71
|
+
const declaration = checker.getSymbolAtLocation(identifier)?.declarations?.find(ts.isImportSpecifier);
|
|
72
|
+
if (declaration !== undefined) return markerImport(declaration);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function decorated(node) {
|
|
76
|
+
const expression = node.expression;
|
|
77
|
+
const call = ts.isCallExpression(expression) ? expression : undefined;
|
|
78
|
+
return { call, name: marker(call?.expression ?? expression) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function preserveLines(start, end, replacement) {
|
|
82
|
+
const breaks = source.slice(start, end).match(/\r\n|\n|\r/g) ?? [];
|
|
83
|
+
const used = replacement.match(/\r\n|\n|\r/g)?.length ?? 0;
|
|
84
|
+
return replacement + breaks.slice(used).join("");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function change(node, replacement) {
|
|
88
|
+
const start = node.getStart(file);
|
|
89
|
+
changes.push([start, node.end, preserveLines(start, node.end, replacement)]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function erase(node) {
|
|
93
|
+
const start = node.getStart(file);
|
|
94
|
+
changes.push([start, node.end, source.slice(start, node.end).replace(/[^\r\n]/g, " ")]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function validate(node) {
|
|
98
|
+
node = unwrap(node);
|
|
99
|
+
if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node)
|
|
100
|
+
|| node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) return;
|
|
101
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
102
|
+
validate(node.expression);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
106
|
+
for (const element of node.elements) validate(element);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
110
|
+
for (const field of node.properties) {
|
|
111
|
+
if (ts.isShorthandPropertyAssignment(field) && !field.objectAssignmentInitializer) continue;
|
|
112
|
+
if (!ts.isPropertyAssignment(field) || ts.isComputedPropertyName(field.name)) {
|
|
113
|
+
error(field, "Use static properties in @Module metadata");
|
|
114
|
+
}
|
|
115
|
+
validate(field.initializer);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
error(node, "Use literals or named references in @Module metadata. Move function expressions and calls outside the declaration");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function runtimeSymbol(symbol) {
|
|
123
|
+
if (symbol === undefined) return;
|
|
124
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
125
|
+
// The checker also detects type-only export stars, whose exported symbols can skip the re-export declaration.
|
|
126
|
+
if (checker.getTypeOnlyAliasDeclaration?.(symbol)
|
|
127
|
+
|| symbol.declarations?.some(ts.isTypeOnlyImportOrExportDeclaration)) return;
|
|
128
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
129
|
+
}
|
|
130
|
+
if (symbol.flags & ts.SymbolFlags.Value) return checker.getExportSymbolOfSymbol(symbol);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function reference(node) {
|
|
134
|
+
node = unwrap(node);
|
|
135
|
+
const names = [];
|
|
136
|
+
while (ts.isQualifiedName(node) || ts.isPropertyAccessExpression(node) && !node.questionDotToken) {
|
|
137
|
+
const qualified = ts.isQualifiedName(node);
|
|
138
|
+
const member = qualified ? node.right : node.name;
|
|
139
|
+
if (!ts.isIdentifier(member)) return;
|
|
140
|
+
names.unshift(member.text);
|
|
141
|
+
node = qualified ? node.left : node.expression;
|
|
142
|
+
}
|
|
143
|
+
if (!ts.isIdentifier(node)) return;
|
|
144
|
+
const binding = checker.getSymbolAtLocation(node);
|
|
145
|
+
const root = runtimeSymbol(binding);
|
|
146
|
+
if (root !== undefined) return { binding, root, name: node.text, names };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function copiedImport(dependency, location) {
|
|
150
|
+
const sourceBinding = dependency.binding.declarations?.find(ts.isImportSpecifier);
|
|
151
|
+
if (sourceBinding === undefined) return;
|
|
152
|
+
const sourceImport = sourceBinding.parent.parent.parent;
|
|
153
|
+
const specifier = sourceImport.moduleSpecifier;
|
|
154
|
+
if (!ts.isStringLiteral(specifier) || specifier.text.startsWith(".") || specifier.text.startsWith("/")) return;
|
|
155
|
+
const imported = checker.getSymbolAtLocation(specifier);
|
|
156
|
+
if (imported === undefined) return;
|
|
157
|
+
|
|
158
|
+
// Copy only from a non-relative import whose module resolution the original program already proved in this file.
|
|
159
|
+
const targetImport = file.statements.find(node => ts.isImportDeclaration(node) && !node.attributes
|
|
160
|
+
&& ts.isStringLiteral(node.moduleSpecifier) && node.moduleSpecifier.text === specifier.text
|
|
161
|
+
&& checker.getSymbolAtLocation(node.moduleSpecifier) === imported);
|
|
162
|
+
if (targetImport === undefined || sourceImport.attributes) return;
|
|
163
|
+
let bindings = addedImports.get(targetImport);
|
|
164
|
+
if (bindings === undefined) {
|
|
165
|
+
bindings = new Map();
|
|
166
|
+
addedImports.set(targetImport, bindings);
|
|
167
|
+
}
|
|
168
|
+
const exported = (sourceBinding.propertyName ?? sourceBinding.name).getText(sourceBinding.getSourceFile());
|
|
169
|
+
let local = bindings.get(exported);
|
|
170
|
+
if (local === undefined) {
|
|
171
|
+
const name = dependency.name;
|
|
172
|
+
const meaning = ts.SymbolFlags.Value | ts.SymbolFlags.Type | ts.SymbolFlags.Namespace | ts.SymbolFlags.Alias;
|
|
173
|
+
local = usedNames.has(name) || checker.resolveName(name, location, meaning, false)
|
|
174
|
+
? freshName("__kumi_" + name) : name;
|
|
175
|
+
usedNames.add(local);
|
|
176
|
+
bindings.set(exported, local);
|
|
177
|
+
}
|
|
178
|
+
return local;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function tokenAt(dependency, location, parameter) {
|
|
182
|
+
const meaning = ts.SymbolFlags.Value | ts.SymbolFlags.Alias;
|
|
183
|
+
let name = dependency.name;
|
|
184
|
+
if (runtimeSymbol(checker.resolveName(name, location, meaning, true)) !== dependency.root) {
|
|
185
|
+
const binding = checker.getSymbolsInScope(location, meaning).find(symbol => runtimeSymbol(symbol) === dependency.root);
|
|
186
|
+
name = binding?.name ?? copiedImport(dependency, location);
|
|
187
|
+
}
|
|
188
|
+
if (name === undefined) {
|
|
189
|
+
error(parameter, `Import a runtime binding for '${dependency.name}' in the module that provides this class`);
|
|
190
|
+
}
|
|
191
|
+
return [name, ...dependency.names].join(".");
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function constructorDependencies(service, point) {
|
|
195
|
+
const markerNode = ts.getDecorators(service)?.find(node => decorated(node).name === "Injectable");
|
|
196
|
+
if (markerNode === undefined) return;
|
|
197
|
+
const { call } = decorated(markerNode);
|
|
198
|
+
if (!call || call.arguments.length !== 0) error(markerNode, "@Injectable() does not take arguments");
|
|
199
|
+
if (!service.name || !ts.isSourceFile(service.parent) || service.getSourceFile().isDeclarationFile
|
|
200
|
+
|| ts.getModifiers(service)?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword || modifier.kind === ts.SyntaxKind.AbstractKeyword)) {
|
|
201
|
+
error(markerNode, "@Injectable() requires a named, concrete top-level class");
|
|
202
|
+
}
|
|
203
|
+
if (checker === undefined) error(markerNode, "@Injectable() requires the original TypeScript Program");
|
|
204
|
+
|
|
205
|
+
const constructors = service.members.filter(ts.isConstructorDeclaration);
|
|
206
|
+
const constructor = constructors.find(member => member.body !== undefined);
|
|
207
|
+
if (constructor === undefined) {
|
|
208
|
+
const symbol = checker.getSymbolAtLocation(service.name);
|
|
209
|
+
if (constructors.length || symbol && requiresArguments(checker.getTypeOfSymbolAtLocation(symbol, service.name))) {
|
|
210
|
+
error(markerNode, "Declare the constructor explicitly on an @Injectable() class with dependencies");
|
|
211
|
+
}
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
if (constructors.length !== 1) error(constructor, "@Injectable() requires a single constructor declaration");
|
|
215
|
+
return constructor.parameters.map(parameter => {
|
|
216
|
+
if (parameter.dotDotDotToken) error(parameter, "@Injectable() does not support rest parameters");
|
|
217
|
+
const injections = ts.getDecorators(parameter)?.filter(node => decorated(node).name === "Inject") ?? [];
|
|
218
|
+
let dependency;
|
|
219
|
+
if (injections.length !== 0) {
|
|
220
|
+
if (injections.length > 1) error(injections[1], "A constructor parameter cannot have multiple @Inject tokens");
|
|
221
|
+
const { call } = decorated(injections[0]);
|
|
222
|
+
if (!call || call.arguments.length !== 1) error(parameter, "@Inject(token) requires one runtime token");
|
|
223
|
+
dependency = reference(call.arguments[0]);
|
|
224
|
+
if (dependency === undefined || runtimeSymbol(checker.getSymbolAtLocation(unwrap(call.arguments[0]))) === undefined) {
|
|
225
|
+
error(parameter, "@Inject(token) requires an in-scope runtime binding");
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
const annotation = parameter.type === undefined ? undefined : unwrapType(parameter.type);
|
|
229
|
+
const type = annotation !== undefined && ts.isTypeReferenceNode(annotation) ? annotation : undefined;
|
|
230
|
+
const symbol = type === undefined ? undefined : runtimeSymbol(checker.getSymbolAtLocation(type.typeName));
|
|
231
|
+
const concreteClass = symbol?.declarations?.find(ts.isClassDeclaration);
|
|
232
|
+
if (type !== undefined && type.typeArguments === undefined && concreteClass !== undefined
|
|
233
|
+
&& concreteClass.typeParameters === undefined && !parameter.questionToken && !parameter.initializer
|
|
234
|
+
&& !ts.getModifiers(concreteClass)?.some(modifier => modifier.kind === ts.SyntaxKind.AbstractKeyword)) {
|
|
235
|
+
dependency = reference(type.typeName);
|
|
236
|
+
}
|
|
237
|
+
if (dependency === undefined) {
|
|
238
|
+
error(parameter, "Cannot infer a concrete class value for this constructor parameter. Use @Inject(token)");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return point === undefined ? undefined : tokenAt(dependency, point, parameter);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function unwrapType(node) {
|
|
246
|
+
while (ts.isParenthesizedTypeNode(node)) node = node.type;
|
|
247
|
+
return node;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function requiresArguments(type) {
|
|
251
|
+
const signatures = checker.getSignaturesOfType(type, ts.SignatureKind.Construct);
|
|
252
|
+
return signatures.length > 0 && signatures.every(signature => signature.minArgumentCount > 0);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function unitFilters(argument) {
|
|
256
|
+
if (argument === undefined) return {};
|
|
257
|
+
const object = unwrap(argument);
|
|
258
|
+
if (!ts.isObjectLiteralExpression(object)) error(argument, "@OnEvent filters must be an object literal");
|
|
259
|
+
if (!object.properties.length) return {};
|
|
260
|
+
if (object.properties.length !== 1) error(argument, "@OnEvent accepts either a unit or a units filter");
|
|
261
|
+
const option = object.properties[0];
|
|
262
|
+
if (!ts.isPropertyAssignment(option) || !["unit", "units"].includes(option.name.text)) {
|
|
263
|
+
error(option, "@OnEvent filters must use a static unit or units property");
|
|
264
|
+
}
|
|
265
|
+
const selected = unwrap(option.initializer);
|
|
266
|
+
const key = option.name.text;
|
|
267
|
+
if (key === "units" && !ts.isArrayLiteralExpression(selected)) error(selected, "@OnEvent units must be an array literal");
|
|
268
|
+
const list = key === "unit" ? [selected] : selected.elements;
|
|
269
|
+
const units = list.map(element => {
|
|
270
|
+
const value = unwrap(element);
|
|
271
|
+
if (!ts.isStringLiteralLike(value) || value.text === "") error(value, "@OnEvent unit filters require nonempty string literals");
|
|
272
|
+
return value.text;
|
|
273
|
+
});
|
|
274
|
+
const normalized = [...new Set(units.map(unit => unit.toLowerCase()))].sort();
|
|
275
|
+
const strings = units.map(unit => JSON.stringify(unit));
|
|
276
|
+
const value = key === "unit" ? strings[0] : "[" + strings.join(", ") + "]";
|
|
277
|
+
return { normalized, options: "{ " + key + ": " + value + " }" };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function eventDeclaration(decorator, method) {
|
|
281
|
+
validateMethod(method, "@OnEvent");
|
|
282
|
+
const { call } = decorated(decorator);
|
|
283
|
+
if (!call || call.arguments.length < 1 || call.arguments.length > 2) {
|
|
284
|
+
error(decorator, "@OnEvent requires an event name and optional unit filters");
|
|
285
|
+
}
|
|
286
|
+
const event = unwrap(call.arguments[0]);
|
|
287
|
+
if (!ts.isStringLiteralLike(event) || event.text === "") error(event, "@OnEvent requires a WoW event name string literal");
|
|
288
|
+
const { normalized, options } = unitFilters(call.arguments[1]);
|
|
289
|
+
const parameters = method.parameters.filter(parameter => !ts.isIdentifier(parameter.name) || parameter.name.text !== "this");
|
|
290
|
+
const args = parameters.some(parameter => parameter.dotDotDotToken)
|
|
291
|
+
? "...payload" : parameters.map((_, index) => "__arg" + index).join(", ");
|
|
292
|
+
return {
|
|
293
|
+
key: JSON.stringify([event.text, normalized ?? null]),
|
|
294
|
+
event: JSON.stringify(event.text),
|
|
295
|
+
options,
|
|
296
|
+
method: ts.isIdentifier(method.name) ? "." + method.name.text : "[" + JSON.stringify(method.name.text) + "]",
|
|
297
|
+
args,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function validateMethod(method, label) {
|
|
302
|
+
if (!ts.isMethodDeclaration(method) || !method.body || method.asteriskToken
|
|
303
|
+
|| !ts.isIdentifier(method.name) && !ts.isStringLiteral(method.name)
|
|
304
|
+
|| ts.getModifiers(method)?.some(modifier => [ts.SyntaxKind.AbstractKeyword, ts.SyntaxKind.AsyncKeyword,
|
|
305
|
+
ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.StaticKeyword].includes(modifier.kind))) {
|
|
306
|
+
error(method, label + " requires a public synchronous instance method");
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function classLifecycle(service) {
|
|
311
|
+
const hooks = new Set();
|
|
312
|
+
const events = [];
|
|
313
|
+
for (const member of service.members) {
|
|
314
|
+
const name = member.name?.text;
|
|
315
|
+
if (["onModuleInit", "onModuleDestroy", "onEnable", "onDisable"].includes(name)) {
|
|
316
|
+
validateMethod(member, name);
|
|
317
|
+
const parameters = member.parameters.filter(parameter => parameter.name.getText() !== "this");
|
|
318
|
+
if (name === "onEnable") {
|
|
319
|
+
if (parameters.length !== 1 || parameters[0].dotDotDotToken) error(member, "onEnable requires one feature scope parameter");
|
|
320
|
+
} else if (parameters.length !== 0) {
|
|
321
|
+
error(member, name + " does not take arguments");
|
|
322
|
+
}
|
|
323
|
+
hooks.add(name);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const seen = new Set();
|
|
327
|
+
for (const decorator of ts.getDecorators(member) ?? []) {
|
|
328
|
+
if (decorated(decorator).name !== "OnEvent") continue;
|
|
329
|
+
const event = eventDeclaration(decorator, member);
|
|
330
|
+
if (seen.has(event.key)) error(decorator, "Duplicate @OnEvent subscription on this method");
|
|
331
|
+
seen.add(event.key);
|
|
332
|
+
events.push(event);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return { hooks, events };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function lifecycleDescriptor(service, name, feature, binding) {
|
|
339
|
+
const { hooks, events } = classLifecycle(service);
|
|
340
|
+
const enable = hooks.has("onEnable");
|
|
341
|
+
const disable = hooks.has("onDisable");
|
|
342
|
+
if (feature === false && (enable || disable)) {
|
|
343
|
+
error(binding, "Class onEnable/onDisable hooks require feature: true in this @Module");
|
|
344
|
+
}
|
|
345
|
+
const fields = [];
|
|
346
|
+
const instance = "instance: InstanceType<typeof " + name + ">";
|
|
347
|
+
if (hooks.has("onModuleInit")) fields.push("initialize(this: void, " + instance + ") { return instance.onModuleInit(); }");
|
|
348
|
+
if (hooks.has("onModuleDestroy")) fields.push("destroy(this: void, " + instance + ") { instance.onModuleDestroy(); }");
|
|
349
|
+
if (enable || events.length) scopeImport ??= freshName("__kumiRuntimeScope");
|
|
350
|
+
if (enable) fields.push("enable(this: void, " + instance + ", scope: " + scopeImport + ") { return instance.onEnable(scope); }");
|
|
351
|
+
if (disable) fields.push("disable(this: void, " + instance + ") { instance.onDisable(); }");
|
|
352
|
+
if (events.length) {
|
|
353
|
+
eventImports ??= { token: freshName("__kumiModuleEvents"), input: freshName("__kumiResolveProviderInput") };
|
|
354
|
+
const subscriptions = events.map(({ event, method, options, args }, index) => {
|
|
355
|
+
const filter = options === undefined ? "" : ", " + options;
|
|
356
|
+
const registered = "registered" + index;
|
|
357
|
+
return "const " + registered + " = events.value.on(scope, " + event
|
|
358
|
+
+ ", (" + args + ") => { if (ready()) instance" + method + "(" + args + "); }" + filter
|
|
359
|
+
+ "); if (!" + registered + ".ok) return " + registered + ";";
|
|
360
|
+
});
|
|
361
|
+
fields.push("subscribe(this: void, " + instance + ", scope: " + scopeImport
|
|
362
|
+
+ ", ready: (this: void) => boolean, input: " + eventImports.input + ") { const events = input("
|
|
363
|
+
+ eventImports.token + "); if (!events.ok) return events; " + subscriptions.join(" ") + " }");
|
|
364
|
+
}
|
|
365
|
+
return fields.length ? "lifecycle: { " + fields.join(", ") + " }" : "";
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function classDeclaration(expression) {
|
|
369
|
+
const symbol = runtimeSymbol(checker.getSymbolAtLocation(expression));
|
|
370
|
+
return symbol?.declarations?.find(ts.isClassDeclaration);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function explicitClassLifecycle(entry, expression, feature, replacements) {
|
|
374
|
+
const binding = ts.isIdentifier(expression) ? runtimeSymbol(checker.getSymbolAtLocation(expression))?.valueDeclaration : undefined;
|
|
375
|
+
const descriptor = binding !== undefined && ts.isVariableDeclaration(binding) && binding.initializer !== undefined
|
|
376
|
+
? unwrap(binding.initializer) : expression;
|
|
377
|
+
if (!ts.isObjectLiteralExpression(descriptor)) return;
|
|
378
|
+
|
|
379
|
+
const useClass = descriptor.properties.find(field => field.name?.text === "useClass");
|
|
380
|
+
if (useClass === undefined) return;
|
|
381
|
+
const reference = ts.isShorthandPropertyAssignment(useClass) ? useClass.name : useClass.initializer;
|
|
382
|
+
if (reference === undefined) return;
|
|
383
|
+
const service = classDeclaration(unwrap(reference));
|
|
384
|
+
if (service === undefined || !ts.getDecorators(service)?.some(node => decorated(node).name === "Injectable")) return;
|
|
385
|
+
|
|
386
|
+
const lifecycle = lifecycleDescriptor(service, unwrap(reference).getText(), feature, entry);
|
|
387
|
+
if (lifecycle === "") return;
|
|
388
|
+
if (descriptor.getSourceFile() !== file || descriptor.properties.some(field =>
|
|
389
|
+
ts.isSpreadAssignment(field) || field.name !== undefined && ts.isComputedPropertyName(field.name))) {
|
|
390
|
+
error(entry, "Declare this class provider next to the module or use @Injectable() shorthand to lower its lifecycle");
|
|
391
|
+
}
|
|
392
|
+
const conflict = descriptor.properties.find(field => field.name?.text === "lifecycle");
|
|
393
|
+
if (conflict !== undefined) error(conflict, "A class provider cannot combine an explicit lifecycle with @Injectable() lifecycle declarations");
|
|
394
|
+
if (loweredProviderObjects.has(descriptor)) return;
|
|
395
|
+
loweredProviderObjects.add(descriptor);
|
|
396
|
+
const position = descriptor.end - 1;
|
|
397
|
+
const separator = descriptor.properties.hasTrailingComma ? " " : ", ";
|
|
398
|
+
replacements.push([position, position, separator + lifecycle + " "]);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function moduleFeature(metadata) {
|
|
402
|
+
const field = metadata.properties.find(property => property.name?.text === "feature");
|
|
403
|
+
if (field === undefined) return false;
|
|
404
|
+
let value = ts.isShorthandPropertyAssignment(field) ? field.name : field.initializer;
|
|
405
|
+
const visited = new Set();
|
|
406
|
+
while (value !== undefined) {
|
|
407
|
+
value = unwrap(value);
|
|
408
|
+
if (value.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
409
|
+
if (value.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
410
|
+
if (!ts.isIdentifier(value)) return;
|
|
411
|
+
const binding = ts.isShorthandPropertyAssignment(value.parent)
|
|
412
|
+
? checker.getShorthandAssignmentValueSymbol(value.parent) : checker.getSymbolAtLocation(value);
|
|
413
|
+
const declaration = runtimeSymbol(binding)?.valueDeclaration;
|
|
414
|
+
if (declaration === undefined || !ts.isVariableDeclaration(declaration)
|
|
415
|
+
|| !(declaration.parent.flags & ts.NodeFlags.Const) || visited.has(declaration)) return;
|
|
416
|
+
visited.add(declaration);
|
|
417
|
+
value = declaration.initializer;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function providerChanges(declaration) {
|
|
422
|
+
if (checker === undefined) return [];
|
|
423
|
+
const property = declaration.properties.find(field => field.name?.text === "providers");
|
|
424
|
+
if (property === undefined) return [];
|
|
425
|
+
let list = ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer;
|
|
426
|
+
const seen = new Set();
|
|
427
|
+
while (list !== undefined && ts.isIdentifier(unwrap(list)) && !seen.has(unwrap(list).text)) {
|
|
428
|
+
const name = unwrap(list).text;
|
|
429
|
+
seen.add(name);
|
|
430
|
+
list = constants.get(name);
|
|
431
|
+
}
|
|
432
|
+
if (list === undefined || !ts.isArrayLiteralExpression(unwrap(list))) return [];
|
|
433
|
+
const providers = unwrap(list);
|
|
434
|
+
const feature = moduleFeature(declaration);
|
|
435
|
+
const lowered = loweredProviders.has(providers);
|
|
436
|
+
if (lowered && feature !== false) return [];
|
|
437
|
+
loweredProviders.add(providers);
|
|
438
|
+
|
|
439
|
+
const replacements = [];
|
|
440
|
+
for (const entry of providers.elements) {
|
|
441
|
+
const expression = unwrap(entry);
|
|
442
|
+
const service = classDeclaration(expression);
|
|
443
|
+
if (service === undefined) {
|
|
444
|
+
explicitClassLifecycle(entry, expression, feature, replacements);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
const inject = constructorDependencies(service, expression);
|
|
448
|
+
if (inject === undefined) {
|
|
449
|
+
if (requiresArguments(checker.getTypeAtLocation(expression))) {
|
|
450
|
+
error(entry, "A bare provider with constructor dependencies requires @Injectable() or an explicit provider declaration");
|
|
451
|
+
}
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const name = expression.getText(file);
|
|
455
|
+
const start = entry.getStart(file);
|
|
456
|
+
const lifecycle = lifecycleDescriptor(service, name, feature, entry);
|
|
457
|
+
if (lowered) continue;
|
|
458
|
+
const extra = lifecycle === "" ? "" : ", " + lifecycle;
|
|
459
|
+
const replacement = preserveLines(start, entry.end,
|
|
460
|
+
`{ provide: ${name}, inject: [${inject.join(", ")}], useClass: ${name}${extra} }`);
|
|
461
|
+
replacements.push([start, entry.end, replacement]);
|
|
462
|
+
}
|
|
463
|
+
return replacements;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function replace(text, offset, replacements) {
|
|
467
|
+
for (const [start, end, replacement] of replacements.sort(([left], [right]) => right - left)) {
|
|
468
|
+
text = text.slice(0, start - offset) + replacement + text.slice(end - offset);
|
|
469
|
+
}
|
|
470
|
+
return text;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function lower(decorator, call) {
|
|
474
|
+
const node = decorator.parent;
|
|
475
|
+
if (!ts.isClassDeclaration(node) || node.parent !== file || !node.name || node.members.length) {
|
|
476
|
+
error(decorator, "@Module requires an empty named top-level class");
|
|
477
|
+
}
|
|
478
|
+
if (ts.getDecorators(node)?.length !== 1) error(decorator, "@Module must be the only decorator on its class");
|
|
479
|
+
const heritage = node.heritageClauses?.[0]?.types[0];
|
|
480
|
+
if (node.heritageClauses?.length !== 1 || node.heritageClauses[0].token !== ts.SyntaxKind.ExtendsKeyword || !heritage
|
|
481
|
+
|| marker(heritage.expression) !== "ModuleType" || heritage.typeArguments?.length !== 1) {
|
|
482
|
+
error(node, "Extend ModuleType with the metadata type to infer public exports");
|
|
483
|
+
}
|
|
484
|
+
if (node.typeParameters?.length || ts.getModifiers(node)?.some(modifier =>
|
|
485
|
+
modifier.kind !== ts.SyntaxKind.ExportKeyword && modifier.kind !== ts.SyntaxKind.DefaultKeyword)) {
|
|
486
|
+
error(node, "Use an ordinary class declaration for @Module");
|
|
487
|
+
}
|
|
488
|
+
if (!call || call.arguments.length !== 1) error(decorator, "@Module expects one metadata object");
|
|
489
|
+
const argument = call.arguments[0];
|
|
490
|
+
const expression = unwrap(argument);
|
|
491
|
+
const declaration = ts.isIdentifier(expression) ? constants.get(expression.text) : expression;
|
|
492
|
+
if (!declaration || !ts.isObjectLiteralExpression(unwrap(declaration))) {
|
|
493
|
+
error(argument, "Pass a metadata object or a top-level const object to @Module");
|
|
494
|
+
}
|
|
495
|
+
const metadata = unwrap(declaration);
|
|
496
|
+
validate(metadata);
|
|
497
|
+
const insideArgument = [];
|
|
498
|
+
const insideMetadata = [];
|
|
499
|
+
for (const replacement of providerChanges(metadata)) {
|
|
500
|
+
const [start, end] = replacement;
|
|
501
|
+
if (start >= metadata.getStart(file) && end <= metadata.end) insideMetadata.push(replacement);
|
|
502
|
+
if (start >= argument.getStart(file) && end <= argument.end) insideArgument.push(replacement);
|
|
503
|
+
else changes.push(replacement);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const modifiers = ts.getModifiers(node);
|
|
507
|
+
const exported = modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
508
|
+
const isDefault = modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
509
|
+
const name = node.name.text;
|
|
510
|
+
const lowered = replace(argument.getText(file), argument.getStart(file), insideArgument);
|
|
511
|
+
const statement = (exported && !isDefault ? "export " : "") + "const " + name + " = " + loweredModule
|
|
512
|
+
+ "(" + lowered + ");" + (isDefault ? " export default " + name + ";" : "");
|
|
513
|
+
change(node, statement);
|
|
514
|
+
hasModule = true;
|
|
515
|
+
const { line } = file.getLineAndCharacterOfPosition(decorator.getStart(file));
|
|
516
|
+
onModule?.({ name, path, line: line + 1, definition: replace(metadata.getText(file), metadata.getStart(file), insideMetadata) });
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function visit(node) {
|
|
520
|
+
if (ts.isDecorator(node)) {
|
|
521
|
+
const { call, name } = decorated(node);
|
|
522
|
+
if (name === "Module") {
|
|
523
|
+
lower(node, call);
|
|
524
|
+
} else if (name === "Injectable") {
|
|
525
|
+
if (!ts.isClassDeclaration(node.parent)) error(node, "@Injectable() decorates a service class");
|
|
526
|
+
if (ts.getDecorators(node.parent)?.filter(decorator => decorated(decorator).name === "Injectable").length !== 1) {
|
|
527
|
+
error(node, "A service class cannot have multiple @Injectable decorators");
|
|
528
|
+
}
|
|
529
|
+
constructorDependencies(node.parent);
|
|
530
|
+
classLifecycle(node.parent);
|
|
531
|
+
erase(node);
|
|
532
|
+
} else if (name === "Inject") {
|
|
533
|
+
const parameter = node.parent;
|
|
534
|
+
if (!ts.isParameter(parameter) || !ts.isConstructorDeclaration(parameter.parent)
|
|
535
|
+
|| !ts.getDecorators(parameter.parent.parent)?.some(decorator => decorated(decorator).name === "Injectable")) {
|
|
536
|
+
error(node, "@Inject(token) belongs on the constructor of an @Injectable() class");
|
|
537
|
+
}
|
|
538
|
+
erase(node);
|
|
539
|
+
} else if (name === "OnEvent") {
|
|
540
|
+
const method = node.parent;
|
|
541
|
+
if (!ts.isMethodDeclaration(method) || !ts.isClassDeclaration(method.parent)
|
|
542
|
+
|| !ts.getDecorators(method.parent)?.some(decorator => decorated(decorator).name === "Injectable")) {
|
|
543
|
+
error(node, "@OnEvent belongs on a method of an @Injectable() class");
|
|
544
|
+
}
|
|
545
|
+
erase(node);
|
|
546
|
+
} else if (rejectUnknown) {
|
|
547
|
+
error(node, "Unsupported WoW decorator. Import markers from @kumi/core or @kumi/wow");
|
|
548
|
+
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
ts.forEachChild(node, visit);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
visit(file);
|
|
555
|
+
if (!changes.length) return;
|
|
556
|
+
|
|
557
|
+
const moduleImport = markerImports.find(({ node }) => node.moduleSpecifier.text === "@kumi/core")?.node;
|
|
558
|
+
for (const { node, clause, bindings, special } of markerImports) {
|
|
559
|
+
const rest = bindings.elements.filter(element => !special.includes(element)).map(element => element.getText(file));
|
|
560
|
+
if (hasModule && node === moduleImport) rest.push("defineModule as " + loweredModule);
|
|
561
|
+
const names = [clause.name?.text, rest.length ? "{ " + rest.join(", ") + " }" : undefined].filter(Boolean).join(", ");
|
|
562
|
+
const named = addedImports.get(node);
|
|
563
|
+
let extra = named === undefined ? "" : importAdded(node, named);
|
|
564
|
+
if (node === moduleImport && scopeImport !== undefined) {
|
|
565
|
+
let names = "RuntimeScope as " + scopeImport;
|
|
566
|
+
if (eventImports !== undefined) {
|
|
567
|
+
extra += ' import { moduleEvents as ' + eventImports.token + ' } from "@kumi/wow";';
|
|
568
|
+
names += ", ResolveProviderInput as " + eventImports.input;
|
|
569
|
+
}
|
|
570
|
+
extra += " import type { " + names + ' } from "@kumi/core";';
|
|
571
|
+
}
|
|
572
|
+
addedImports.delete(node);
|
|
573
|
+
change(node, (names ? "import " + names + " from " + node.moduleSpecifier.getText(file) + ";" : "") + extra);
|
|
574
|
+
}
|
|
575
|
+
for (const [node, bindings] of addedImports) {
|
|
576
|
+
changes.push([node.end, node.end, importAdded(node, bindings)]);
|
|
577
|
+
}
|
|
578
|
+
return replace(source, 0, changes);
|
|
579
|
+
|
|
580
|
+
function importAdded(node, bindings) {
|
|
581
|
+
const names = [...bindings].map(([name, local]) => name === local ? name : name + " as " + local);
|
|
582
|
+
return " import { " + names.join(", ") + " } from " + node.moduleSpecifier.getText(file) + ";";
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export function viteModuleDeclarations(ts) {
|
|
587
|
+
return {
|
|
588
|
+
name: "kumi-module-declarations",
|
|
589
|
+
enforce: "pre",
|
|
590
|
+
transform(source, id) {
|
|
591
|
+
return transformModuleDeclarations(ts, id.split("?", 1)[0], source);
|
|
592
|
+
},
|
|
593
|
+
};
|
|
594
|
+
}
|