@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,444 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { test } from "node:test";
|
|
5
|
+
import { transformModuleDeclarations, viteModuleDeclarations } from "./module-declarations.mjs";
|
|
6
|
+
|
|
7
|
+
const load = createRequire(import.meta.url);
|
|
8
|
+
const ts = load("typescript");
|
|
9
|
+
const core = `
|
|
10
|
+
declare module "@kumi/core" {
|
|
11
|
+
export interface RuntimeScope { readonly scope: true }
|
|
12
|
+
export const moduleScope: { readonly name: "module scope" };
|
|
13
|
+
export function Module(metadata: unknown): ClassDecorator;
|
|
14
|
+
export class ModuleType<T> {}
|
|
15
|
+
export function Injectable(): ClassDecorator;
|
|
16
|
+
export function Inject(token: object): ParameterDecorator;
|
|
17
|
+
export function defineModule<Metadata>(metadata: Metadata): Metadata;
|
|
18
|
+
export type Result<Value> = { readonly ok: true; readonly value: Value } | { readonly ok: false; readonly reason: string };
|
|
19
|
+
export interface ServiceToken<Value extends object> { readonly name: string; readonly valueType?: Value }
|
|
20
|
+
export type ResolveProviderInput = <Value extends object>(this: void, token: ServiceToken<Value>) => Result<Value>;
|
|
21
|
+
}
|
|
22
|
+
declare module "@kumi/wow" {
|
|
23
|
+
import type { Result, RuntimeScope, ServiceToken } from "@kumi/core";
|
|
24
|
+
export interface WoWEventMap {
|
|
25
|
+
PLAYER_LOGIN: [];
|
|
26
|
+
UNIT_AURA: [unit: string, update: { readonly full: boolean }];
|
|
27
|
+
UNIT_FLAGS: [unit: string, flags?: string];
|
|
28
|
+
}
|
|
29
|
+
interface Filters { unit?: string; units?: string[] }
|
|
30
|
+
interface Events {
|
|
31
|
+
on<E extends keyof WoWEventMap>(this: void, scope: RuntimeScope, event: E,
|
|
32
|
+
handler: (this: void, ...payload: WoWEventMap[E]) => void, filters?: Filters): Result<() => void>;
|
|
33
|
+
}
|
|
34
|
+
export const moduleEvents: ServiceToken<Events>;
|
|
35
|
+
export function OnEvent<E extends keyof WoWEventMap>(event: E, filters?: Filters): MethodDecorator;
|
|
36
|
+
}
|
|
37
|
+
`;
|
|
38
|
+
const controls = 'import { Injectable, Inject, Module, ModuleType, moduleScope, type RuntimeScope } from "@kumi/core";';
|
|
39
|
+
const ending = 'const metadata = { id: "fixture", providers: [Service], exports: { service: Service } } as const;\n'
|
|
40
|
+
+ '@Module(metadata)\nexport class Fixture extends ModuleType<typeof metadata> {}\n';
|
|
41
|
+
|
|
42
|
+
function fixture(sources, experimentalDecorators = true, compilerOptions = {}) {
|
|
43
|
+
const path = name => resolve("virtual-module-declarations", name).replaceAll("\\", "/");
|
|
44
|
+
const files = new Map(Object.entries({ "core.d.ts": core, ...sources }).map(([name, data]) => [path(name), data]));
|
|
45
|
+
const output = new Map();
|
|
46
|
+
const options = {
|
|
47
|
+
target: ts.ScriptTarget.ES2020,
|
|
48
|
+
module: ts.ModuleKind.ESNext,
|
|
49
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
50
|
+
experimentalDecorators,
|
|
51
|
+
strict: true,
|
|
52
|
+
types: [],
|
|
53
|
+
...compilerOptions,
|
|
54
|
+
};
|
|
55
|
+
const host = ts.createCompilerHost(options);
|
|
56
|
+
const readFile = host.readFile;
|
|
57
|
+
const fileExists = host.fileExists;
|
|
58
|
+
const directoryExists = host.directoryExists;
|
|
59
|
+
const getSourceFile = host.getSourceFile;
|
|
60
|
+
host.fileExists = name => files.has(name) || fileExists(name);
|
|
61
|
+
host.directoryExists = name => [...files.keys()].some(file => file.startsWith(name + "/")) || directoryExists(name);
|
|
62
|
+
host.readFile = name => files.get(name) ?? readFile(name);
|
|
63
|
+
host.getSourceFile = (name, languageVersion, ...args) => {
|
|
64
|
+
const source = files.get(name);
|
|
65
|
+
return source === undefined ? getSourceFile(name, languageVersion, ...args) : ts.createSourceFile(name, source, languageVersion, true);
|
|
66
|
+
};
|
|
67
|
+
host.writeFile = (name, data) => output.set(name, data);
|
|
68
|
+
const program = ts.createProgram([...files.keys()], options, host);
|
|
69
|
+
return {
|
|
70
|
+
path, program, output,
|
|
71
|
+
transform: name => transformModuleDeclarations(ts, path(name), sources[name], { program, rejectUnknown: true }),
|
|
72
|
+
emit() {
|
|
73
|
+
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
74
|
+
assert.equal(diagnostics.map(item => ts.flattenDiagnosticMessageText(item.messageText, "\n")).join("\n"), "");
|
|
75
|
+
assert.equal(program.emit().emitSkipped, false);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
test("lowers constructor dependencies and retains a class import previously used only as a type", () => {
|
|
81
|
+
const source = `
|
|
82
|
+
import { Module as Define, ModuleType as Base, Injectable as ServiceClass, Inject as Use, moduleScope as scopeToken, type RuntimeScope } from "@kumi/core";
|
|
83
|
+
import { Store as LoadedStore } from "./store";
|
|
84
|
+
@ServiceClass()
|
|
85
|
+
export class Service {
|
|
86
|
+
constructor(readonly store: LoadedStore, @Use(scopeToken) readonly scope: RuntimeScope) {}
|
|
87
|
+
}
|
|
88
|
+
const providers = [Service] as const;
|
|
89
|
+
const metadata = { id: "fixture", providers, exports: { service: Service } } as const;
|
|
90
|
+
@Define(metadata)
|
|
91
|
+
export class Fixture extends Base<typeof metadata> {}
|
|
92
|
+
`;
|
|
93
|
+
const store = "export class Store {}";
|
|
94
|
+
const original = fixture({ "fixture.ts": source, "store.ts": store });
|
|
95
|
+
original.emit();
|
|
96
|
+
assert.doesNotMatch(original.output.get(original.path("fixture.js")), /from "\.\/store"/);
|
|
97
|
+
const lowered = original.transform("fixture.ts");
|
|
98
|
+
assert.match(lowered, /providers = \[\{ provide: Service, inject: \[LoadedStore, scopeToken\], useClass: Service \}\] as const/);
|
|
99
|
+
assert.match(lowered, /export const Fixture = __kumiDefineModule\(metadata\)/);
|
|
100
|
+
assert.doesNotMatch(lowered, /lifecycle|moduleEvents/);
|
|
101
|
+
assert.doesNotMatch(lowered, /@(?:ServiceClass|Use|Define)|import \{[^}]*\b(?:Inject|Injectable|ModuleType)\b/);
|
|
102
|
+
assert.equal(lowered.split("\n").length, source.split("\n").length);
|
|
103
|
+
const checked = fixture({ "fixture.ts": lowered, "store.ts": store }, false);
|
|
104
|
+
checked.emit();
|
|
105
|
+
assert.match(checked.output.get(checked.path("fixture.js")), /import \{ Store as LoadedStore \} from ".\/store"/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("resolves cross-file classes in the module and copies an existing proven package binding without a collision", () => {
|
|
109
|
+
const consumer = `
|
|
110
|
+
import { Module, ModuleType } from "@kumi/core";
|
|
111
|
+
import { RuntimeService as Service } from "./service";
|
|
112
|
+
import { Store as ProviderStore } from "./store";
|
|
113
|
+
const moduleScope = { name: "local" };
|
|
114
|
+
const metadata = { id: "module", providers: [ProviderStore, Service], exports: { service: Service } } as const;
|
|
115
|
+
@Module(metadata)
|
|
116
|
+
export class Fixture extends ModuleType<typeof metadata> {}
|
|
117
|
+
`;
|
|
118
|
+
const service = `
|
|
119
|
+
import { Injectable, Inject, moduleScope, type RuntimeScope } from "@kumi/core";
|
|
120
|
+
import { Store } from "./store";
|
|
121
|
+
@Injectable()
|
|
122
|
+
export class RuntimeService {
|
|
123
|
+
constructor(readonly store: Store, @Inject(moduleScope) readonly scope: RuntimeScope) {}
|
|
124
|
+
}
|
|
125
|
+
`;
|
|
126
|
+
const store = "export class Store {}";
|
|
127
|
+
const original = fixture({ "module.ts": consumer, "service.ts": service, "store.ts": store });
|
|
128
|
+
const loweredModule = original.transform("module.ts");
|
|
129
|
+
const loweredService = original.transform("service.ts");
|
|
130
|
+
assert.match(loweredModule, /import \{ moduleScope as (__kumi_moduleScope) \} from "@kumi\/core"/);
|
|
131
|
+
assert.match(loweredModule, /\{ provide: Service, inject: \[ProviderStore, __kumi_moduleScope\], useClass: Service \}/);
|
|
132
|
+
assert.doesNotMatch(loweredService, /\b(?:Inject|Injectable)\b|@(?:Inject|Injectable)/);
|
|
133
|
+
fixture({ "module.ts": loweredModule, "service.ts": loweredService, "store.ts": store }, false).emit();
|
|
134
|
+
|
|
135
|
+
const missing = fixture({ "module.ts": consumer.replace('import { Store as ProviderStore } from "./store";', "")
|
|
136
|
+
.replace("[ProviderStore, Service]", "[Service]"), "service.ts": service, "store.ts": store });
|
|
137
|
+
assert.throws(() => missing.transform("module.ts"), {
|
|
138
|
+
message: missing.path("service.ts") + ":6:17: Import a runtime binding for 'Store' in the module that provides this class",
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("reports a type-only named import and a type-only re-export at the constructor parameter", () => {
|
|
143
|
+
const service = 'class Holder {}\n@Injectable()\nclass Service {\n constructor(readonly store: Store) {}\n}\n';
|
|
144
|
+
for (const typeOnly of [
|
|
145
|
+
['import type { Store } from "./store";', undefined],
|
|
146
|
+
['import { Store } from "./types";', 'export type { Store } from "./store";'],
|
|
147
|
+
['import { Store } from "./types";', 'export type * from "./store";'],
|
|
148
|
+
]) {
|
|
149
|
+
const [importStatement, reexport] = typeOnly;
|
|
150
|
+
const original = fixture({ "fixture.ts": controls + "\n" + importStatement + "\n" + service + ending,
|
|
151
|
+
"store.ts": "export class Store {}", ...(reexport === undefined ? {} : { "types.ts": reexport }) });
|
|
152
|
+
assert.throws(() => original.transform("fixture.ts"), {
|
|
153
|
+
message: original.path("fixture.ts") + ":6:17: Cannot infer a concrete class value for this constructor parameter. Use @Inject(token)",
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("requires explicit tokens for erased, nonconcrete and ambiguous parameter types", () => {
|
|
159
|
+
for (const parameter of ["value: RuntimeScope", "value: Abstract", "value: GenericStore<number>", "value: GenericStore",
|
|
160
|
+
"value: Store | undefined", "value?: Store", "value: Store = new Store()"]) {
|
|
161
|
+
const source = controls + "\nabstract class Abstract {}\nclass Store {} class GenericStore<T = unknown> {}\n"
|
|
162
|
+
+ "@Injectable()\nclass Service { constructor(" + parameter + ") {} }\n" + ending;
|
|
163
|
+
const original = fixture({ "fixture.ts": source });
|
|
164
|
+
assert.throws(() => original.transform("fixture.ts"), /fixture\.ts:5:29: Cannot infer a concrete class value.*@Inject\(token\)/);
|
|
165
|
+
const explicit = fixture({ "fixture.ts": source.replace("constructor(", "constructor(@Inject(moduleScope) ") });
|
|
166
|
+
assert.match(explicit.transform("fixture.ts"), /inject: \[moduleScope\]/);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("lowers an empty injectable constructor and preserves an undecorated no-argument provider", () => {
|
|
171
|
+
const source = controls + "\n@Injectable()\nclass Service {}\nclass Ordinary {}\n"
|
|
172
|
+
+ '@Module({ id: "fixture", providers: [Service, Ordinary], exports: { service: Service } })\n'
|
|
173
|
+
+ 'export class Fixture extends ModuleType<{ exports: { service: typeof Service } }> {}';
|
|
174
|
+
const original = fixture({ "fixture.ts": source });
|
|
175
|
+
assert.match(original.transform("fixture.ts"), /providers: \[\{ provide: Service, inject: \[\], useClass: Service \}, Ordinary\]/);
|
|
176
|
+
const required = fixture({ "fixture.ts": source.replace("class Ordinary {}", "class Ordinary { constructor(scope: RuntimeScope) {} }") });
|
|
177
|
+
assert.throws(() => required.transform("fixture.ts"),
|
|
178
|
+
/A bare provider with constructor dependencies requires @Injectable\(\) or an explicit provider declaration/);
|
|
179
|
+
|
|
180
|
+
const inherited = fixture({ "fixture.ts": source.replace("@Injectable()\nclass Service {}",
|
|
181
|
+
"class Base { constructor(scope: RuntimeScope) {} }\n@Injectable()\nclass Service extends Base {}") });
|
|
182
|
+
assert.throws(() => inherited.transform("fixture.ts"), /Declare the constructor explicitly/);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("retains AST-only Vite module lowering and rejects unknown decorators in the checked transform", () => {
|
|
186
|
+
const source = 'import { Module, ModuleType } from "@kumi/core";\nclass Service {}\n' + ending;
|
|
187
|
+
const output = viteModuleDeclarations(ts).transform(source, "/fixture.ts?source");
|
|
188
|
+
assert.match(output, /providers: \[Service\]/);
|
|
189
|
+
assert.match(output, /defineModule as __kumiDefineModule/);
|
|
190
|
+
const unknown = fixture({ "fixture.ts": source.replace("class Service", "@Unrelated()\nclass Service") });
|
|
191
|
+
assert.throws(() => unknown.transform("fixture.ts"), /fixture\.ts:2:1: Unsupported WoW decorator/);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("lowers declared lifecycle and aliased events to typed scoped native calls", () => {
|
|
195
|
+
const service = `
|
|
196
|
+
import { OnEvent as Listen, type WoWEventMap } from "@kumi/wow";
|
|
197
|
+
import { Injectable, type Result } from "@kumi/core";
|
|
198
|
+
@Injectable()
|
|
199
|
+
export class Active {
|
|
200
|
+
saw = "";
|
|
201
|
+
onModuleInit(): Result<void> { return { ok: true, value: undefined }; }
|
|
202
|
+
onModuleDestroy(): void { this.saw = ""; }
|
|
203
|
+
dispose(): void {}
|
|
204
|
+
@Listen("PLAYER_LOGIN")
|
|
205
|
+
start(): void { this.saw = "ready"; }
|
|
206
|
+
@Listen("UNIT_AURA", { unit: "player" })
|
|
207
|
+
@Listen("UNIT_AURA", { units: ["party1", "party2"] })
|
|
208
|
+
refresh(...[unit, update]: WoWEventMap["UNIT_AURA"]): void { this.saw = update.full ? unit : ""; }
|
|
209
|
+
@Listen("UNIT_AURA")
|
|
210
|
+
observe(this: Active, unit: string): void { this.saw = unit; }
|
|
211
|
+
@Listen("UNIT_AURA")
|
|
212
|
+
ignore(): void { this.saw = ""; }
|
|
213
|
+
@Listen("UNIT_FLAGS")
|
|
214
|
+
flags(unit: string, flags?: string): void { this.saw = flags ?? unit; }
|
|
215
|
+
}
|
|
216
|
+
`;
|
|
217
|
+
const module = `
|
|
218
|
+
import { Module, ModuleType } from "@kumi/core";
|
|
219
|
+
import { Active as Service } from "./service";
|
|
220
|
+
const __kumiModuleEvents = false;
|
|
221
|
+
const metadata = { id: "fixture", providers: [Service], exports: { service: Service } } as const;
|
|
222
|
+
@Module(metadata)
|
|
223
|
+
export class Fixture extends ModuleType<typeof metadata> {}
|
|
224
|
+
`;
|
|
225
|
+
const fixtureFiles = { "fixture.ts": module, "service.ts": service };
|
|
226
|
+
const original = fixture(fixtureFiles);
|
|
227
|
+
const loweredModule = original.transform("fixture.ts");
|
|
228
|
+
const loweredService = original.transform("service.ts");
|
|
229
|
+
assert.equal(loweredService.split("\n").length, service.split("\n").length);
|
|
230
|
+
assert.doesNotMatch(loweredService, /@Listen|@Injectable|\bOnEvent\b/);
|
|
231
|
+
assert.match(loweredModule, /import \{ moduleEvents as __kumiModuleEvents_ \} from "@kumi\/wow"/);
|
|
232
|
+
const callback = (event, method, args, options = "") => "events.value.on(scope, \"" + event
|
|
233
|
+
+ "\", (" + args + ") => { if (ready()) instance." + method + "(" + args + "); }" + options + ")";
|
|
234
|
+
const descriptor = [
|
|
235
|
+
"{ provide: Service, inject: [], useClass: Service, lifecycle: {",
|
|
236
|
+
"initialize(this: void, instance: InstanceType<typeof Service>) { return instance.onModuleInit(); },",
|
|
237
|
+
"destroy(this: void, instance: InstanceType<typeof Service>) { instance.onModuleDestroy(); },",
|
|
238
|
+
"subscribe(this: void, instance: InstanceType<typeof Service>, scope: __kumiRuntimeScope, ready: (this: void) => boolean,",
|
|
239
|
+
"input: __kumiResolveProviderInput) { const events = input(__kumiModuleEvents_); if (!events.ok) return events;",
|
|
240
|
+
"const registered0 = " + callback("PLAYER_LOGIN", "start", "") + "; if (!registered0.ok) return registered0;",
|
|
241
|
+
"const registered1 = " + callback("UNIT_AURA", "refresh", "...payload", ', { unit: "player" }') + "; if (!registered1.ok) return registered1;",
|
|
242
|
+
"const registered2 = " + callback("UNIT_AURA", "refresh", "...payload", ', { units: ["party1", "party2"] }') + "; if (!registered2.ok) return registered2;",
|
|
243
|
+
"const registered3 = " + callback("UNIT_AURA", "observe", "__arg0") + "; if (!registered3.ok) return registered3;",
|
|
244
|
+
"const registered4 = " + callback("UNIT_AURA", "ignore", "") + "; if (!registered4.ok) return registered4;",
|
|
245
|
+
"const registered5 = " + callback("UNIT_FLAGS", "flags", "__arg0, __arg1") + "; if (!registered5.ok) return registered5;",
|
|
246
|
+
"} } }",
|
|
247
|
+
].join(" ");
|
|
248
|
+
assert.ok(loweredModule.includes(descriptor), loweredModule);
|
|
249
|
+
assert.doesNotMatch(loweredModule, /instance\.dispose/);
|
|
250
|
+
|
|
251
|
+
const checked = fixture({ "fixture.ts": loweredModule, "service.ts": loweredService }, false);
|
|
252
|
+
checked.emit();
|
|
253
|
+
assert.doesNotMatch(checked.output.get(checked.path("service.js")), /__decorate|OnEvent|Injectable/);
|
|
254
|
+
const wrongPayload = fixture({ "fixture.ts": loweredModule,
|
|
255
|
+
"service.ts": loweredService.replace('WoWEventMap["UNIT_AURA"]', "[unit: number, update: { readonly full: boolean }]") }, false);
|
|
256
|
+
assert.ok(ts.getPreEmitDiagnostics(wrongPayload.program).some(diagnostic =>
|
|
257
|
+
/string.*not assignable.*number/.test(ts.flattenDiagnosticMessageText(diagnostic.messageText, " "))));
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("keeps lifecycle-only providers independent of the event host", () => {
|
|
261
|
+
const declarations = controls + "\n"
|
|
262
|
+
+ "@Injectable()\nclass Service { onModuleInit(): void {} }\nclass Ordinary { onModuleDestroy(): void {} }\n";
|
|
263
|
+
const original = fixture({ "fixture.ts": declarations + ending.replace("[Service]", "[Service, Ordinary]") });
|
|
264
|
+
const lowered = original.transform("fixture.ts");
|
|
265
|
+
assert.match(lowered, /lifecycle: \{ initialize\(this: void, instance: InstanceType<typeof Service>\) \{ return instance\.onModuleInit\(\); \} \} \}, Ordinary/);
|
|
266
|
+
assert.doesNotMatch(lowered, /@kumi\/wow|subscribe|destroy\(/);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("rejects semantically duplicate and dynamic subscriptions at their declarations", () => {
|
|
270
|
+
const withEvent = (decorators, method = "refresh() {}") => controls + "\n"
|
|
271
|
+
+ 'import { OnEvent as Listen } from "@kumi/wow";\n'
|
|
272
|
+
+ '@Injectable()\nclass Service {\n' + decorators + "\n" + method + "\n}\n" + ending;
|
|
273
|
+
for (const decorators of [
|
|
274
|
+
'@Listen("UNIT_AURA", { units: ["PARTY2", "party1"] })\n@Listen("UNIT_AURA", { units: ["party1", "party2", "party1"] })',
|
|
275
|
+
'@Listen("UNIT_AURA", { unit: "player" })\n@Listen("UNIT_AURA", { units: ["PLAYER"] })',
|
|
276
|
+
'@Listen("PLAYER_LOGIN")\n@Listen("PLAYER_LOGIN", {})',
|
|
277
|
+
]) {
|
|
278
|
+
const original = fixture({ "fixture.ts": withEvent(decorators) });
|
|
279
|
+
assert.throws(() => original.transform("fixture.ts"), {
|
|
280
|
+
message: original.path("fixture.ts") + ":6:1: Duplicate @OnEvent subscription on this method",
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const [decorator, failure] of [
|
|
285
|
+
["@Listen(event)", /requires a WoW event name string literal/],
|
|
286
|
+
['@Listen("UNIT_AURA", options)', /filters must be an object literal/],
|
|
287
|
+
['@Listen("UNIT_AURA", { units: units })', /units must be an array literal/],
|
|
288
|
+
['@Listen("UNIT_AURA", { units: ["player", unit] })', /unit filters require nonempty string literals/],
|
|
289
|
+
['@Listen("UNIT_AURA", { unit: "player", units: ["player"] })', /accepts either a unit or a units filter/],
|
|
290
|
+
]) {
|
|
291
|
+
const original = fixture({ "fixture.ts": withEvent(decorator) });
|
|
292
|
+
assert.throws(() => original.transform("fixture.ts"), failure);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("rejects unsupported placements and inaccessible lifecycle methods", () => {
|
|
297
|
+
const source = controls + '\nimport { OnEvent } from "@kumi/wow";\n';
|
|
298
|
+
const scenarios = [
|
|
299
|
+
['class Service { @OnEvent("PLAYER_LOGIN") refresh() {} }\n',
|
|
300
|
+
/@OnEvent belongs on a method of an @Injectable\(\) class/],
|
|
301
|
+
['@Injectable()\nclass Service { @OnEvent("PLAYER_LOGIN") static refresh() {} }\n',
|
|
302
|
+
/@OnEvent requires a public synchronous instance method/],
|
|
303
|
+
['@Injectable()\nclass Service { @OnEvent("PLAYER_LOGIN") private refresh() {} }\n',
|
|
304
|
+
/@OnEvent requires a public synchronous instance method/],
|
|
305
|
+
['@Injectable()\nclass Service { protected onModuleDestroy() {} }\n',
|
|
306
|
+
/onModuleDestroy requires a public synchronous instance method/],
|
|
307
|
+
['@Injectable()\nclass Service { onModuleInit(name: string) {} }\n',
|
|
308
|
+
/onModuleInit does not take arguments/],
|
|
309
|
+
];
|
|
310
|
+
for (const [declaration, failure] of scenarios) {
|
|
311
|
+
const original = fixture({ "fixture.ts": source + declaration + ending });
|
|
312
|
+
assert.throws(() => original.transform("fixture.ts"), failure);
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("lowers lifecycle in local explicit class descriptors without rewriting their chosen DI", () => {
|
|
317
|
+
const provider = "{ provide: token, inject: [override], useClass: Service }";
|
|
318
|
+
const definitions = controls + `
|
|
319
|
+
const token = { name: "service token" };
|
|
320
|
+
const override = { name: "override" };
|
|
321
|
+
@Injectable()
|
|
322
|
+
class Service {
|
|
323
|
+
constructor(@Inject(moduleScope) readonly scope: RuntimeScope) {}
|
|
324
|
+
onModuleInit(): void {}
|
|
325
|
+
}
|
|
326
|
+
`;
|
|
327
|
+
const descriptor = ending.replace("[Service]", "[" + provider + "]").replace("service: Service", "service: token");
|
|
328
|
+
const inline = fixture({ "fixture.ts": definitions + descriptor });
|
|
329
|
+
const result = inline.transform("fixture.ts");
|
|
330
|
+
assert.match(result, /provide: token, inject: \[override\], useClass: Service\s*, lifecycle: \{ initialize/);
|
|
331
|
+
assert.doesNotMatch(result, /inject: \[moduleScope\]/);
|
|
332
|
+
const named = fixture({ "fixture.ts": definitions + "const explicit = " + provider + ";\n"
|
|
333
|
+
+ descriptor.replace(provider, "explicit") });
|
|
334
|
+
assert.match(named.transform("fixture.ts"), /const explicit = \{ provide: token, inject: \[override\], useClass: Service\s*, lifecycle/);
|
|
335
|
+
|
|
336
|
+
const conflict = fixture({ "fixture.ts": definitions + descriptor.replace("useClass: Service", "useClass: Service, lifecycle: {}") });
|
|
337
|
+
assert.throws(() => conflict.transform("fixture.ts"), /cannot combine an explicit lifecycle with @Injectable\(\) lifecycle/);
|
|
338
|
+
const foreign = fixture({
|
|
339
|
+
"fixture.ts": controls + '\nimport { explicit as Service } from "./foreign";\n' + ending,
|
|
340
|
+
"foreign.ts": definitions + "export const explicit = " + provider + ";\n",
|
|
341
|
+
});
|
|
342
|
+
assert.throws(() => foreign.transform("fixture.ts"), /Declare this class provider next to the module/);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test("emits the same Lua 5.1 class and provider as the manual constructor descriptor", () => {
|
|
346
|
+
const body = `
|
|
347
|
+
class Store { key = "ready"; }
|
|
348
|
+
|
|
349
|
+
export class Service {
|
|
350
|
+
constructor(readonly store: Store, readonly scope: RuntimeScope) {}
|
|
351
|
+
read(this: Service) { return this.store.key; }
|
|
352
|
+
}
|
|
353
|
+
`;
|
|
354
|
+
const decorated = controls + body
|
|
355
|
+
.replace("export class Service", "@Injectable()\nexport class Service")
|
|
356
|
+
.replace("readonly scope", "@Inject(moduleScope) readonly scope")
|
|
357
|
+
+ ending.replace("[Service]", "[Store, Service]");
|
|
358
|
+
const manual = 'import { moduleScope, type RuntimeScope, defineModule as __kumiDefineModule } from "@kumi/core";\n'
|
|
359
|
+
+ body + 'const metadata = { id: "fixture", providers: [Store, { provide: Service, inject: [Store, moduleScope], useClass: Service }], '
|
|
360
|
+
+ 'exports: { service: Service } } as const;\nexport const Fixture = __kumiDefineModule(metadata);\n';
|
|
361
|
+
const original = fixture({ "fixture.ts": decorated });
|
|
362
|
+
const lowered = original.transform("fixture.ts");
|
|
363
|
+
const { Transpiler } = load("typescript-to-lua");
|
|
364
|
+
function lua(source) {
|
|
365
|
+
const emitted = fixture({ "fixture.ts": source }, false, {
|
|
366
|
+
luaTarget: "5.1", luaLibImport: "require-minimal", noImplicitSelf: true, noResolvePaths: ["@kumi/core"],
|
|
367
|
+
});
|
|
368
|
+
const result = new Transpiler().emit({ program: emitted.program, writeFile: (name, data) => emitted.output.set(name, data) });
|
|
369
|
+
const diagnostics = [...ts.getPreEmitDiagnostics(emitted.program), ...result.diagnostics];
|
|
370
|
+
assert.equal(diagnostics.map(item => ts.flattenDiagnosticMessageText(item.messageText, "\n")).join("\n"), "");
|
|
371
|
+
assert.equal(result.emitSkipped, false);
|
|
372
|
+
const unit = [...emitted.output].find(([name]) => name.replaceAll("\\", "/").split("/").pop() === "fixture.lua");
|
|
373
|
+
assert.ok(unit, "Expected fixture.lua in " + [...emitted.output.keys()].join(", "));
|
|
374
|
+
return unit[1];
|
|
375
|
+
}
|
|
376
|
+
const output = lua(lowered);
|
|
377
|
+
assert.equal(output, lua(manual));
|
|
378
|
+
assert.match(output, /function Service\.prototype\.read\(self\)/);
|
|
379
|
+
assert.match(output, /inject\s*=\s*\{\s*Store,\s*moduleScope\s*\}/);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("lowers feature hooks with the child scope and keeps module hooks separate", () => {
|
|
383
|
+
const source = controls + `
|
|
384
|
+
import type { Result } from "@kumi/core";
|
|
385
|
+
@Injectable()
|
|
386
|
+
class Service {
|
|
387
|
+
onModuleInit(): void {}
|
|
388
|
+
onModuleDestroy(): void {}
|
|
389
|
+
onEnable(this: Service, scope: RuntimeScope): Result<void> {
|
|
390
|
+
return scope.scope ? { ok: true, value: undefined } : { ok: false, reason: "missing scope" };
|
|
391
|
+
}
|
|
392
|
+
onDisable(): void {}
|
|
393
|
+
}
|
|
394
|
+
` + ending.replace('id: "fixture",', 'id: "fixture", feature: true,');
|
|
395
|
+
const original = fixture({ "fixture.ts": source });
|
|
396
|
+
const lowered = original.transform("fixture.ts");
|
|
397
|
+
assert.match(lowered, /import type \{ RuntimeScope as __kumiRuntimeScope \} from "@kumi\/core"/);
|
|
398
|
+
assert.match(lowered, /feature: true, providers: \[/);
|
|
399
|
+
assert.match(lowered, /initialize\(this: void, instance: InstanceType<typeof Service>\) \{ return instance\.onModuleInit\(\); \}/);
|
|
400
|
+
assert.match(lowered, /destroy\(this: void, instance: InstanceType<typeof Service>\) \{ instance\.onModuleDestroy\(\); \}/);
|
|
401
|
+
assert.match(lowered, /enable\(this: void, instance: InstanceType<typeof Service>, scope: __kumiRuntimeScope\) \{ return instance\.onEnable\(scope\); \}/);
|
|
402
|
+
assert.match(lowered, /disable\(this: void, instance: InstanceType<typeof Service>\) \{ instance\.onDisable\(\); \}/);
|
|
403
|
+
assert.doesNotMatch(lowered, /@kumi\/wow|moduleEvents|ResolveProviderInput/);
|
|
404
|
+
fixture({ "fixture.ts": lowered }, false).emit();
|
|
405
|
+
|
|
406
|
+
const explicit = fixture({ "fixture.ts": source.replace("[Service]", "[{ provide: Service, inject: [], useClass: Service }]") });
|
|
407
|
+
assert.match(explicit.transform("fixture.ts"), /provide: Service, inject: \[\], useClass: Service\s*, lifecycle: \{/);
|
|
408
|
+
|
|
409
|
+
const ordinary = fixture({ "fixture.ts": source.replace("@Injectable()\n", "") });
|
|
410
|
+
assert.doesNotMatch(ordinary.transform("fixture.ts"), /lifecycle|__kumiRuntimeScope/);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test("diagnoses feature hooks in provable non-feature modules including reused provider lists", () => {
|
|
414
|
+
const service = controls + "\n@Injectable()\nclass Service { onEnable(scope: RuntimeScope): void {} }\n";
|
|
415
|
+
const nonFeature = /Class onEnable\/onDisable hooks require feature: true in this @Module/;
|
|
416
|
+
for (const source of [
|
|
417
|
+
service + ending,
|
|
418
|
+
service + ending.replace('id: "fixture",', 'id: "fixture", feature: false,'),
|
|
419
|
+
service + "const feature = false as const;\n" + ending.replace('id: "fixture",', 'id: "fixture", feature,'),
|
|
420
|
+
service + ending.replace("[Service]", "[{ provide: Service, inject: [], useClass: Service }]"),
|
|
421
|
+
service + 'const providers = [Service] as const;\n'
|
|
422
|
+
+ ending.replace('id: "fixture", providers: [Service]', 'id: "first", feature: true, providers')
|
|
423
|
+
+ ending.replaceAll("metadata", "other").replace("class Fixture", "class Other")
|
|
424
|
+
.replace('id: "fixture", providers: [Service]', 'id: "second", providers'),
|
|
425
|
+
]) {
|
|
426
|
+
const original = fixture({ "fixture.ts": source });
|
|
427
|
+
assert.throws(() => original.transform("fixture.ts"), nonFeature);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("checks arity and accessibility of explicit feature hooks", () => {
|
|
432
|
+
const source = controls + "\n@Injectable()\nclass Service { HOOK }\n"
|
|
433
|
+
+ ending.replace('id: "fixture",', 'id: "fixture", feature: true,');
|
|
434
|
+
for (const [hook, failure] of [
|
|
435
|
+
["onEnable() {}", /onEnable requires one feature scope parameter/],
|
|
436
|
+
["onEnable(...scopes: RuntimeScope[]) {}", /onEnable requires one feature scope parameter/],
|
|
437
|
+
["private onEnable(scope: RuntimeScope) {}", /onEnable requires a public synchronous instance method/],
|
|
438
|
+
["onDisable(scope: RuntimeScope) {}", /onDisable does not take arguments/],
|
|
439
|
+
["static onDisable() {}", /onDisable requires a public synchronous instance method/],
|
|
440
|
+
]) {
|
|
441
|
+
const original = fixture({ "fixture.ts": source.replace("HOOK", hook) });
|
|
442
|
+
assert.throws(() => original.transform("fixture.ts"), failure);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { mkdtemp, readFile, rm, rmdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { lowerSignalFactory, sha256, signalIntrinsic } from "./src/ts-lower.mjs";
|
|
6
|
+
|
|
7
|
+
export async function prepareSignalKernel({ ts, program, sdkRoot, output, pack }) {
|
|
8
|
+
const lowered = lowerSignalFactory(ts, program, sdkRoot);
|
|
9
|
+
const scratch = await mkdtemp(join(output, "kumi-native-"));
|
|
10
|
+
let lua;
|
|
11
|
+
try {
|
|
12
|
+
const ir = join(scratch, "signal.ir");
|
|
13
|
+
const target = join(scratch, "signal.lua");
|
|
14
|
+
await writeFile(ir, lowered.ir);
|
|
15
|
+
execFileSync(pack, ["emit-native", "--ir", ir, "--out", target], { windowsHide: true });
|
|
16
|
+
lua = await readFile(target, "utf8");
|
|
17
|
+
} finally {
|
|
18
|
+
// Only files created above belong to this invocation.
|
|
19
|
+
await rm(join(scratch, "signal.ir"), { force: true });
|
|
20
|
+
await rm(join(scratch, "signal.lua"), { force: true });
|
|
21
|
+
await rmdir(scratch);
|
|
22
|
+
}
|
|
23
|
+
if (!lua.startsWith(`-- kumi-native-1 / SignalFactoryV1 / ${lowered.source}\n`)) {
|
|
24
|
+
throw new Error("Signal emitter returned a mismatched native module");
|
|
25
|
+
}
|
|
26
|
+
let replaced = false;
|
|
27
|
+
return {
|
|
28
|
+
ir: lowered.ir,
|
|
29
|
+
lua,
|
|
30
|
+
plugin: {
|
|
31
|
+
afterPrint(_program, _options, _host, files) {
|
|
32
|
+
const candidates = files.filter(file => program.getSourceFile(file.fileName)
|
|
33
|
+
&& realpathSync(file.fileName) === lowered.canonical);
|
|
34
|
+
if (replaced || candidates.length !== 1) throw new Error("Expected one printed Signal factory in its provider");
|
|
35
|
+
candidates[0].code = lua;
|
|
36
|
+
candidates[0].sourceMapNode = undefined;
|
|
37
|
+
replaced = true;
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
manifest(bundle, destination) {
|
|
41
|
+
if (!replaced) throw new Error("Native Signal factory was not emitted");
|
|
42
|
+
return {
|
|
43
|
+
schema: "kumi-native-1",
|
|
44
|
+
target: "5.1",
|
|
45
|
+
intrinsic: signalIntrinsic.operation,
|
|
46
|
+
abi: { export: "createSignalFactory", receiver: "none", arity: 3, returns: "scalar" },
|
|
47
|
+
module: "kumi.src.signal-kernel",
|
|
48
|
+
imports: [],
|
|
49
|
+
sdk: signalIntrinsic.file,
|
|
50
|
+
compiler: "kumi-pack/0.1.0",
|
|
51
|
+
source: lowered.source,
|
|
52
|
+
fingerprint: lowered.fingerprint,
|
|
53
|
+
ir: sha256(lowered.ir),
|
|
54
|
+
lua: sha256(lua),
|
|
55
|
+
provider: destination,
|
|
56
|
+
bundle: sha256(bundle),
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
declare type AnyNotNil = {};
|
|
2
|
+
declare type LuaMultiReturn<T extends unknown[]> = T & { readonly __tstlMultiReturn: never };
|
|
3
|
+
|
|
4
|
+
declare const $multi: <T extends unknown[]>(this: void, ...values: T) => LuaMultiReturn<T>;
|
|
5
|
+
declare const $vararg: string[];
|
|
6
|
+
|
|
7
|
+
declare type KumiLuaIterator<T, S> = S extends undefined
|
|
8
|
+
? (this: void) => T
|
|
9
|
+
: LuaMultiReturn<[(this: void, state: S, key: T extends LuaMultiReturn<infer Slots> ? Slots[0] : T) => T,
|
|
10
|
+
S, T extends LuaMultiReturn<infer Slots> ? Slots[0] : T]>;
|
|
11
|
+
|
|
12
|
+
declare type LuaIterable<T, S = undefined> = Iterable<T> & KumiLuaIterator<T, S> & {
|
|
13
|
+
readonly __kumiLuaIteration: "iterator";
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
declare type LuaPairsIterable<K extends {}, V> = Iterable<[K, V]> & { readonly __kumiLuaIteration: "pairs" };
|
|
17
|
+
declare type LuaPairsKeyIterable<K extends {}> = Iterable<K> & { readonly __kumiLuaIteration: "keys" };
|
|
18
|
+
|
|
19
|
+
declare interface LuaTable<K extends {} = {}, V = any> extends LuaPairsIterable<K, V> {
|
|
20
|
+
length(this: LuaTable<K, V>): number;
|
|
21
|
+
get(this: LuaTable<K, V>, key: K): V;
|
|
22
|
+
set(this: LuaTable<K, V>, key: K, value: V): void;
|
|
23
|
+
has(this: LuaTable<K, V>, key: K): boolean;
|
|
24
|
+
delete(this: LuaTable<K, V>, key: K): boolean;
|
|
25
|
+
isEmpty(this: LuaTable<K, V>): boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare interface ReadonlyLuaMap<K extends {} = {}, V = any> extends LuaPairsIterable<K, V> {
|
|
29
|
+
get(this: ReadonlyLuaMap<K, V>, key: K): V | undefined;
|
|
30
|
+
has(this: ReadonlyLuaMap<K, V>, key: K): boolean;
|
|
31
|
+
isEmpty(this: ReadonlyLuaMap<K, V>): boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare interface LuaMap<K extends {} = {}, V = any> extends ReadonlyLuaMap<K, V> {
|
|
35
|
+
set(this: LuaMap<K, V>, key: K, value: V): void;
|
|
36
|
+
delete(this: LuaMap<K, V>, key: K): boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare interface ReadonlyLuaSet<K extends {} = {}> extends LuaPairsKeyIterable<K> {
|
|
40
|
+
has(this: ReadonlyLuaSet<K>, key: K): boolean;
|
|
41
|
+
isEmpty(this: ReadonlyLuaSet<K>): boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare interface LuaSet<K extends {} = {}> extends ReadonlyLuaSet<K> {
|
|
45
|
+
add(this: LuaSet<K>, key: K): void;
|
|
46
|
+
delete(this: LuaSet<K>, key: K): boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare const LuaTable: new <K extends {} = {}, V = any>() => LuaTable<K, V>;
|
|
50
|
+
declare const LuaMap: new <K extends {} = {}, V = any>() => LuaMap<K, V>;
|
|
51
|
+
declare const LuaSet: new <K extends {} = {}>() => LuaSet<K>;
|
|
52
|
+
|
|
53
|
+
declare interface ObjectConstructor {
|
|
54
|
+
keys<K extends {}, V>(value: LuaPairsIterable<K, V> | LuaPairsKeyIterable<K>): K[];
|
|
55
|
+
values<K extends {}, V>(value: LuaPairsIterable<K, V>): V[];
|
|
56
|
+
entries<K extends {}, V>(value: LuaPairsIterable<K, V>): [K, V][];
|
|
57
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { basename, isAbsolute, relative, sep } from "node:path";
|
|
2
|
+
|
|
3
|
+
function identifiers(ts, nodes) {
|
|
4
|
+
const names = new Set();
|
|
5
|
+
function visit(node) {
|
|
6
|
+
if (ts.isIdentifier(node)) names.add(node.text);
|
|
7
|
+
ts.forEachChild(node, visit);
|
|
8
|
+
}
|
|
9
|
+
for (const node of nodes) visit(node);
|
|
10
|
+
return names;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function importNames(ts, { importClause }) {
|
|
14
|
+
const names = [];
|
|
15
|
+
if (importClause?.name) names.push(importClause.name.text);
|
|
16
|
+
const bindings = importClause?.namedBindings;
|
|
17
|
+
if (bindings && ts.isNamespaceImport(bindings)) names.push(bindings.name.text);
|
|
18
|
+
if (bindings && ts.isNamedImports(bindings)) names.push(...bindings.elements.map(({ name }) => name.text));
|
|
19
|
+
return names;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useNativeRoutes(ts, host, routesDir) {
|
|
23
|
+
const { readFile } = host;
|
|
24
|
+
host.readFile = path => {
|
|
25
|
+
const text = readFile(path);
|
|
26
|
+
if (text === undefined || basename(path) !== "page.tsx") return text;
|
|
27
|
+
const route = relative(routesDir, path);
|
|
28
|
+
if (isAbsolute(route) || route.split(sep)[0] === "..") return text;
|
|
29
|
+
|
|
30
|
+
const file = ts.createSourceFile(path, text, ts.ScriptTarget.Latest);
|
|
31
|
+
const preview = file.statements.find(statement => ts.isVariableStatement(statement)
|
|
32
|
+
&& statement.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword)
|
|
33
|
+
&& statement.declarationList.declarations.length === 1
|
|
34
|
+
&& statement.declarationList.declarations.some(({ name }) => ts.isIdentifier(name) && name.text === "preview"));
|
|
35
|
+
if (!preview) return text;
|
|
36
|
+
|
|
37
|
+
const imports = file.statements.filter(ts.isImportDeclaration);
|
|
38
|
+
const browserNames = identifiers(ts, [preview]);
|
|
39
|
+
const nativeNames = identifiers(ts, file.statements.filter(node => node !== preview && !ts.isImportDeclaration(node)));
|
|
40
|
+
const browserImports = imports.filter(statement => {
|
|
41
|
+
const names = importNames(ts, statement);
|
|
42
|
+
return names.length > 0 && names.every(name => browserNames.has(name) && !nativeNames.has(name));
|
|
43
|
+
});
|
|
44
|
+
// Keep offsets stable so diagnostics still point to the source file.
|
|
45
|
+
return [preview, ...browserImports].reduce((source, node) => {
|
|
46
|
+
const start = node.getStart(file);
|
|
47
|
+
return source.slice(0, start) + source.slice(start, node.end).replace(/[^\r\n]/g, " ") + source.slice(node.end);
|
|
48
|
+
}, text);
|
|
49
|
+
};
|
|
50
|
+
return host;
|
|
51
|
+
}
|