@overmvp/kumi 0.0.0-stage → 0.3.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 +67 -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 +387 -0
- package/builder/css-v1.test.mjs +361 -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 +127 -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 +435 -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 +2336 -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 +381 -0
- package/builder/src/native_ir.rs +65 -0
- package/builder/src/native_signal.rs +83 -0
- package/builder/src/numbers.lua +76 -0
- package/builder/src/project-front.mjs +3682 -0
- package/builder/src/records.lua +75 -0
- package/builder/src/routes.rs +124 -0
- package/builder/src/setup-store.lua +18 -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-reads.lua +22 -0
- package/builder/src/wow_client.rs +253 -0
- package/builder/src/wow_types.rs +564 -0
- package/builder/style-modules.mjs +93 -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 +59 -0
- package/eslint.mjs +74 -0
- package/form/index.ts +526 -0
- package/form/package.json +13 -0
- package/motion/index.ts +2 -0
- package/motion/package.json +10 -0
- package/package.json +26 -4
- package/router/index.ts +50 -0
- package/router/navigation.ts +22 -0
- package/router/package.json +12 -0
- package/router/router-view.ts +134 -0
- package/router/routes.ts +64 -0
- package/scripts/addon-build.mjs +364 -0
- package/scripts/addon-project.mjs +78 -0
- package/scripts/kumi-cli.mjs +105 -0
- package/scripts/link-addons.mjs +61 -0
- package/src/bindings.ts +35 -0
- package/src/context.ts +15 -0
- package/src/css-tokens.ts +239 -0
- package/src/css-v1.ts +68 -0
- package/src/cvars.ts +91 -0
- package/src/element-node.ts +32 -0
- package/src/elements.ts +505 -0
- package/src/feature-scope.ts +82 -0
- package/src/flex-layout.ts +807 -0
- package/src/frame-scheduler.ts +336 -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 +29 -0
- package/src/jsx-dev-runtime.ts +2 -0
- package/src/jsx-runtime.ts +29 -0
- package/src/jsx.d.ts +12 -0
- package/src/keyed-list.ts +366 -0
- package/src/media-query.ts +144 -0
- package/src/module-ui.ts +147 -0
- package/src/module.ts +311 -0
- package/src/node-selectors.ts +114 -0
- package/src/owned-collections.ts +52 -0
- package/src/providers.ts +241 -0
- package/src/reactive-scope.ts +34 -0
- package/src/reactivity.ts +275 -0
- package/src/result.ts +64 -0
- package/src/runtime.ts +52 -0
- package/src/scheduler.ts +185 -0
- package/src/signal-kernel.ts +63 -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 +9 -0
- package/state/package.json +12 -0
- package/state/setup-store-persistence.ts +206 -0
- package/state/setup-store-view.ts +10 -0
- package/state/setup-store.ts +342 -0
- package/state/store-bindings.ts +25 -0
- package/state/store.ts +84 -0
- package/styles/_layers.scss +14 -0
- package/templates/addon/.agents/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.agents/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.agents/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.agents/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.agents/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/.claude/skills/kumi-build-debug/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-feature/SKILL.md +50 -0
- package/templates/addon/.claude/skills/kumi-plugin/SKILL.md +36 -0
- package/templates/addon/.claude/skills/kumi-store/SKILL.md +47 -0
- package/templates/addon/.claude/skills/kumi-ui/SKILL.md +73 -0
- package/templates/addon/.claude/skills/kumi-wow-api/SKILL.md +30 -0
- package/templates/addon/AGENTS.md +34 -0
- package/templates/addon/Assets/Art/Corner10.tga +0 -0
- package/templates/addon/Assets/Art/Corner11.tga +0 -0
- package/templates/addon/Assets/Art/Corner12.tga +0 -0
- package/templates/addon/Assets/Art/Corner13.tga +0 -0
- package/templates/addon/Assets/Art/Corner14.tga +0 -0
- package/templates/addon/Assets/Art/Corner15.tga +0 -0
- package/templates/addon/Assets/Art/Corner16.tga +0 -0
- package/templates/addon/Assets/Art/Corner2.tga +0 -0
- package/templates/addon/Assets/Art/Corner3.tga +0 -0
- package/templates/addon/Assets/Art/Corner4.tga +0 -0
- package/templates/addon/Assets/Art/Corner5.tga +0 -0
- package/templates/addon/Assets/Art/Corner6.tga +0 -0
- package/templates/addon/Assets/Art/Corner7.tga +0 -0
- package/templates/addon/Assets/Art/Corner8.tga +0 -0
- package/templates/addon/Assets/Art/Corner9.tga +0 -0
- package/templates/addon/Assets/Art/Dot.tga +0 -0
- package/templates/addon/Assets/Art/Gear.tga +0 -0
- package/templates/addon/Assets/Art/Grip.tga +0 -0
- package/templates/addon/Assets/Art/Outline10.tga +0 -0
- package/templates/addon/Assets/Art/Outline12.tga +0 -0
- package/templates/addon/Assets/Art/Outline13.tga +0 -0
- package/templates/addon/Assets/Art/Outline14.tga +0 -0
- package/templates/addon/Assets/Art/Outline15.tga +0 -0
- package/templates/addon/Assets/Art/Outline2.tga +0 -0
- package/templates/addon/Assets/Art/Outline3.tga +0 -0
- package/templates/addon/Assets/Art/Outline5.tga +0 -0
- package/templates/addon/Assets/Art/Outline7.tga +0 -0
- package/templates/addon/Assets/Art/Outline9.tga +0 -0
- package/templates/addon/Assets/Art/Round11.tga +0 -0
- package/templates/addon/Assets/Art/Round11Border.tga +0 -0
- package/templates/addon/Assets/Art/Round4.tga +0 -0
- package/templates/addon/Assets/Art/Round4Border.tga +0 -0
- package/templates/addon/Assets/Art/Round6.tga +0 -0
- package/templates/addon/Assets/Art/Round6Border.tga +0 -0
- package/templates/addon/Assets/Art/Round8.tga +0 -0
- package/templates/addon/Assets/Art/Round8Border.tga +0 -0
- package/templates/addon/Assets/Art/Search.tga +0 -0
- package/templates/addon/CLAUDE.md +1 -0
- package/templates/addon/README.md +49 -0
- package/templates/addon/eslint.config.mjs +6 -0
- package/templates/addon/gitignore +9 -0
- package/templates/addon/kumi.config.ts +17 -0
- package/templates/addon/package.json +20 -0
- package/templates/addon/src/app/app.module.ts +18 -0
- package/templates/addon/src/app/commands.store.ts +11 -0
- package/templates/addon/src/app/layout/AppWindow.module.scss +92 -0
- package/templates/addon/src/app/layout/AppWindow.tsx +32 -0
- package/templates/addon/src/app/main.ts +5 -0
- package/templates/addon/src/app/routes/(window)/home/page.tsx +8 -0
- package/templates/addon/src/app/routes/(window)/plugins/page.tsx +38 -0
- package/templates/addon/src/app/routes/(window)/settings/page.tsx +8 -0
- package/templates/addon/src/app/theme.ts +6 -0
- package/templates/addon/src/modules/greeting/api/player.api.ts +29 -0
- package/templates/addon/src/modules/greeting/greeting.module.ts +18 -0
- package/templates/addon/src/modules/greeting/model/greeting.store.ts +81 -0
- package/templates/addon/src/modules/greeting/services/greeting.service.ts +38 -0
- package/templates/addon/src/modules/greeting/ui/Greeting.module.scss +105 -0
- package/templates/addon/src/modules/greeting/ui/GreetingCard.tsx +29 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingHud.tsx +54 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingOverview.tsx +18 -0
- package/templates/addon/src/modules/greeting/widgets/GreetingSettings.tsx +56 -0
- package/templates/addon/src/modules/playtime/api/clock.api.ts +6 -0
- package/templates/addon/src/modules/playtime/model/playtime.store.ts +19 -0
- package/templates/addon/src/modules/playtime/playtime.module.ts +18 -0
- package/templates/addon/src/modules/playtime/services/playtime.service.ts +18 -0
- package/templates/addon/src/modules/playtime/ui/Playtime.module.scss +25 -0
- package/templates/addon/src/modules/playtime/ui/PlaytimeBadge.tsx +10 -0
- package/templates/addon/src/modules/playtime/widgets/PlaytimePage.tsx +16 -0
- package/templates/addon/src/shared/api/native-art.ts +28 -0
- package/templates/addon/src/shared/assets/close.png +0 -0
- package/templates/addon/src/shared/lib/format.ts +20 -0
- package/templates/addon/src/shared/styles/_tokens.scss +12 -0
- package/templates/addon/src/shared/ui/Button.tsx +29 -0
- package/templates/addon/src/shared/ui/Choice.tsx +34 -0
- package/templates/addon/src/shared/ui/Controls.module.scss +93 -0
- package/templates/addon/src/shared/ui/Glyph.tsx +18 -0
- package/templates/addon/src/shared/ui/Slider.tsx +25 -0
- package/templates/addon/src/shared/ui/Toggle.tsx +29 -0
- package/templates/addon/src/shared/ui/focus-ref.ts +7 -0
- package/templates/addon/src/shared/ui/range-skin.tsx +15 -0
- package/templates/addon/tsconfig.json +63 -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/action-bar-layout.ts +108 -0
- package/wow/action-button.ts +219 -0
- package/wow/action-catalog.ts +98 -0
- package/wow/addon-manifest.ts +36 -0
- package/wow/animation.ts +428 -0
- package/wow/app-ui.ts +221 -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/cooldown-timing.ts +24 -0
- package/wow/css-box.ts +144 -0
- package/wow/css-color.ts +12 -0
- package/wow/css-style.ts +109 -0
- package/wow/css-surfaces.ts +603 -0
- package/wow/css-text.ts +399 -0
- package/wow/cursor.ts +45 -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 +53 -0
- package/wow/frame-layer.ts +5 -0
- package/wow/frame-timing.ts +13 -0
- package/wow/frames.ts +236 -0
- package/wow/heal-prediction.ts +305 -0
- package/wow/hooks.ts +133 -0
- package/wow/image-layers.ts +55 -0
- package/wow/index.ts +111 -0
- package/wow/layout-view.ts +663 -0
- package/wow/locale-font.ts +15 -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 +153 -0
- package/wow/number-format.ts +8 -0
- package/wow/number-input.ts +10 -0
- package/wow/panel-host.ts +97 -0
- package/wow/pixel.ts +35 -0
- package/wow/plugins.ts +246 -0
- package/wow/protect.ts +48 -0
- package/wow/protected-layout.ts +133 -0
- package/wow/protected-view.ts +103 -0
- package/wow/range-art.ts +81 -0
- package/wow/readable-api.ts +120 -0
- package/wow/reparent-frame.ts +99 -0
- package/wow/rounded-texture.ts +183 -0
- package/wow/saved-variables.ts +109 -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/spell-cooldown.ts +36 -0
- package/wow/start-addon.ts +122 -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 +1864 -0
- package/wow/visible-frame-job.ts +34 -0
- package/wow/width-reserve.ts +108 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { getReactiveMap } from "./jsx-lists.mjs";
|
|
4
|
+
|
|
5
|
+
const reactivity = fileURLToPath(new URL("../src/reactivity.ts", import.meta.url));
|
|
6
|
+
const reactiveScope = fileURLToPath(new URL("../src/reactive-scope.ts", import.meta.url));
|
|
7
|
+
const keyedList = fileURLToPath(new URL("../src/keyed-list.ts", import.meta.url));
|
|
8
|
+
|
|
9
|
+
function sameFile(ts, node, path) {
|
|
10
|
+
const source = resolve(node.getSourceFile().fileName);
|
|
11
|
+
return ts.sys.useCaseSensitiveFileNames ? source === path : source.toLowerCase() === path.toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function outerExpression(ts, node) {
|
|
15
|
+
let expression = node;
|
|
16
|
+
for (let parent = node.parent; parent && parent.expression === expression; parent = parent.parent) {
|
|
17
|
+
if (!ts.isParenthesizedExpression(parent) && !ts.isAsExpression(parent) && !ts.isTypeAssertionExpression(parent)
|
|
18
|
+
&& !ts.isNonNullExpression(parent) && !ts.isSatisfiesExpression(parent)) break;
|
|
19
|
+
expression = parent;
|
|
20
|
+
}
|
|
21
|
+
return expression;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isCanonicalValue(ts, checker, node) {
|
|
25
|
+
const index = ts.isElementAccessExpression(node) ? node.argumentExpression : undefined;
|
|
26
|
+
const name = ts.isPropertyAccessExpression(node) ? node.name.text
|
|
27
|
+
: index && ts.isStringLiteralLike(index) ? index.text : undefined;
|
|
28
|
+
if (name !== "value") return false;
|
|
29
|
+
|
|
30
|
+
const target = outerExpression(ts, node);
|
|
31
|
+
const parent = target.parent;
|
|
32
|
+
if (ts.isBinaryExpression(parent) && parent.left === target && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) return false;
|
|
33
|
+
|
|
34
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(node.expression));
|
|
35
|
+
const types = type.isUnion() ? type.types : [type];
|
|
36
|
+
return types.every(member => {
|
|
37
|
+
const property = checker.getPropertyOfType(checker.getApparentType(member), "value");
|
|
38
|
+
const declarations = property?.declarations;
|
|
39
|
+
return declarations?.length && declarations.every(declaration => ts.isPropertySignature(declaration)
|
|
40
|
+
&& ts.isInterfaceDeclaration(declaration.parent) && ["Ref", "ReadonlyRef"].includes(declaration.parent.name.text)
|
|
41
|
+
&& sameFile(ts, declaration, reactivity));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function tracksArgument(ts, checker, call, index) {
|
|
46
|
+
const declaration = checker.getResolvedSignature(call)?.declaration;
|
|
47
|
+
if (!declaration || !ts.isFunctionDeclaration(declaration) || !declaration.name) return false;
|
|
48
|
+
const name = declaration.name.text;
|
|
49
|
+
if (name === "jsxMap" || name === "jsxList") {
|
|
50
|
+
return index < (name === "jsxMap" ? 3 : 1) && sameFile(ts, declaration, keyedList);
|
|
51
|
+
}
|
|
52
|
+
return index === 0 && ["computed", "computedResult", "watch", "watchEffect"].includes(name)
|
|
53
|
+
&& (sameFile(ts, declaration, reactivity) || sameFile(ts, declaration, reactiveScope));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Report eager reads of the canonical Kumi ref property in original TSX. */
|
|
57
|
+
export function lintNoStaticRefRead(ts, source, checker, report) {
|
|
58
|
+
if (source.isDeclarationFile || !source.fileName.endsWith(".tsx")) return;
|
|
59
|
+
const directMaps = new Set();
|
|
60
|
+
|
|
61
|
+
function visit(node, eager, tracked) {
|
|
62
|
+
if (ts.isTypeNode(node)) return;
|
|
63
|
+
if (ts.isFunctionLike(node)) {
|
|
64
|
+
const expression = outerExpression(ts, node);
|
|
65
|
+
const call = expression.parent;
|
|
66
|
+
const invoked = call && ts.isCallExpression(call);
|
|
67
|
+
if (!invoked || call.expression !== expression) {
|
|
68
|
+
const argument = invoked ? call.arguments.indexOf(expression) : -1;
|
|
69
|
+
const callbackTracked = argument >= 0
|
|
70
|
+
&& ((argument === 0 && directMaps.has(call)) || tracksArgument(ts, checker, call, argument));
|
|
71
|
+
// A computed method name runs while its surrounding object is built.
|
|
72
|
+
ts.forEachChild(node, child => child === node.name
|
|
73
|
+
? visit(child, eager, tracked) : visit(child, false, callbackTracked));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
eager ||= ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
79
|
+
const mapping = getReactiveMap(ts, node);
|
|
80
|
+
if (mapping) {
|
|
81
|
+
directMaps.add(mapping.call);
|
|
82
|
+
visit(node.expression, eager, true);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (eager && !tracked && isCanonicalValue(ts, checker, node)) {
|
|
87
|
+
report(node, "JSX snapshots this Kumi ref. Pass the ref to a reactive prop or text child, or derive the value in computed().");
|
|
88
|
+
}
|
|
89
|
+
ts.forEachChild(node, child => visit(child, eager, tracked));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
visit(source, false, false);
|
|
93
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { transformJsx } from "./jsx-lists.mjs";
|
|
6
|
+
import { lintNoStaticRefRead } from "./no-static-ref-read.mjs";
|
|
7
|
+
|
|
8
|
+
const ts = createRequire(import.meta.url)("typescript");
|
|
9
|
+
const fixturePath = fileURLToPath(new URL("./no-static-ref-fixture.tsx", import.meta.url)).replaceAll("\\", "/");
|
|
10
|
+
const packagePath = path => fileURLToPath(new URL(path, import.meta.url)).replaceAll("\\", "/");
|
|
11
|
+
|
|
12
|
+
function diagnostics(text) {
|
|
13
|
+
const options = {
|
|
14
|
+
strict: true,
|
|
15
|
+
target: ts.ScriptTarget.ES2020,
|
|
16
|
+
module: ts.ModuleKind.ESNext,
|
|
17
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
18
|
+
jsx: ts.JsxEmit.React,
|
|
19
|
+
jsxFactory: "UI.createElement",
|
|
20
|
+
jsxFragmentFactory: "UI.Fragment",
|
|
21
|
+
types: [],
|
|
22
|
+
paths: {
|
|
23
|
+
"@kumi/core": [packagePath("../src/index.ts")],
|
|
24
|
+
"@kumi/form": [packagePath("../form/index.ts")],
|
|
25
|
+
"@kumi/state": [packagePath("../state/index.ts")],
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const host = ts.createCompilerHost(options);
|
|
29
|
+
const readFile = host.readFile;
|
|
30
|
+
host.readFile = file => file === fixturePath ? text : readFile(file);
|
|
31
|
+
const program = ts.createProgram([fixturePath], options, host);
|
|
32
|
+
assert.equal(ts.getPreEmitDiagnostics(program).map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")).join("\n"), "");
|
|
33
|
+
|
|
34
|
+
const source = program.getSourceFile(fixturePath);
|
|
35
|
+
const result = [];
|
|
36
|
+
lintNoStaticRefRead(ts, source, program.getTypeChecker(), (node, message) => {
|
|
37
|
+
assert.match(message, /JSX snapshots this Kumi ref/);
|
|
38
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
39
|
+
result.push({ line: line + 1, column: character + 1, text: node.getText(source) });
|
|
40
|
+
});
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function expected(text) {
|
|
45
|
+
return text.split("\n").flatMap((line, index) => {
|
|
46
|
+
const parts = line.split(" // error ");
|
|
47
|
+
if (parts.length !== 2) return [];
|
|
48
|
+
return parts[1].split(" ").map(expression => ({
|
|
49
|
+
line: index + 1,
|
|
50
|
+
column: parts[0].indexOf(expression) + 1,
|
|
51
|
+
text: expression,
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
test("reports eager JSX reads only for values declared by a Kumi ref", () => {
|
|
57
|
+
const fixture = `
|
|
58
|
+
import * as UI from "@kumi/core";
|
|
59
|
+
import { ref as makeRef, type ReadonlyRef as Signal, type Ref } from "@kumi/state";
|
|
60
|
+
|
|
61
|
+
const count = makeRef(1);
|
|
62
|
+
interface Inherited extends Signal<number> { readonly unit: string }
|
|
63
|
+
declare const inherited: Inherited;
|
|
64
|
+
declare const signal: Signal<number>;
|
|
65
|
+
declare const union: Ref<number> | Inherited;
|
|
66
|
+
declare const optional: Signal<number> | null | undefined;
|
|
67
|
+
declare const plain: { kind: "plain"; value: number };
|
|
68
|
+
declare const mixed: typeof plain | Inherited;
|
|
69
|
+
class OwnGetter { get value() { return 3; } }
|
|
70
|
+
function HiddenRead() { return count.value; }
|
|
71
|
+
function Probe(_props: { data: object }) { return <span />; }
|
|
72
|
+
|
|
73
|
+
<span>{count.value}</span>; // error count.value
|
|
74
|
+
<span>{signal["value"]}</span>; // error signal["value"]
|
|
75
|
+
<div width={signal.value + 1} />; // error signal.value
|
|
76
|
+
<div>{count.value > 0 ? <span>yes</span> : <span>no</span>}</div>; // error count.value
|
|
77
|
+
<div style={{ width: inherited.value }} />; // error inherited.value
|
|
78
|
+
<div {...{ width: union.value }} />; // error union.value
|
|
79
|
+
<div width={optional?.value} />; // error optional?.value
|
|
80
|
+
<div width={optional?.["value"]} />; // error optional?.["value"]
|
|
81
|
+
function Counter<T extends Signal<number>>(generic: T) {
|
|
82
|
+
return <span>{generic.value}</span>; // error generic.value
|
|
83
|
+
}
|
|
84
|
+
<Probe data={{ get [count.value]() { return signal.value; } }} />; // error count.value
|
|
85
|
+
<Probe data={{ get nested() { return <span>{count.value}</span>; } }} />; // error count.value
|
|
86
|
+
|
|
87
|
+
<span>{count} {signal}</span>;
|
|
88
|
+
<span>{plain.value} {mixed.value} {new OwnGetter().value}</span>;
|
|
89
|
+
<span>{HiddenRead()}</span>;
|
|
90
|
+
<span>{(count.value = 2)}</span>;
|
|
91
|
+
<Probe data={{ get width() { return count.value; } }} />;
|
|
92
|
+
const snapshot = count.value;
|
|
93
|
+
`;
|
|
94
|
+
assert.deepEqual(diagnostics(fixture), expected(fixture));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("keeps eager IIFEs and nested JSX separate from deferred and tracked callbacks", () => {
|
|
98
|
+
const fixture = `
|
|
99
|
+
import * as UI from "@kumi/core";
|
|
100
|
+
import { computed as derive, computedResult, ref, watch } from "@kumi/state";
|
|
101
|
+
|
|
102
|
+
const count = ref(1);
|
|
103
|
+
const change = ref(2);
|
|
104
|
+
declare const scope: UI.RuntimeScope;
|
|
105
|
+
declare function debounce(read: () => void): () => void;
|
|
106
|
+
const unknown = { computed<T>(read: () => T) { return read(); } };
|
|
107
|
+
|
|
108
|
+
<button onClick={() => { count.value++; }} />;
|
|
109
|
+
<button onClick={debounce(() => { count.value; })} />;
|
|
110
|
+
<button onClick={debounce(() => { <span>{count.value}</span>; })} />; // error count.value
|
|
111
|
+
<div width={(() => count.value)()} />; // error count.value
|
|
112
|
+
<div width={(function () { return count.value; })()} />; // error count.value
|
|
113
|
+
<span>{derive(() => count.value)}</span>;
|
|
114
|
+
derive(() => <span>{count.value}</span>);
|
|
115
|
+
computedResult(() => UI.ok(<span>{count.value}</span>));
|
|
116
|
+
scope.computed(() => <span>{count.value}</span>);
|
|
117
|
+
scope.computedResult(() => UI.ok(<span>{count.value}</span>));
|
|
118
|
+
scope.watchEffect(() => { <span>{count.value}</span>; });
|
|
119
|
+
watch(() => <span>{count.value}</span>, () => { <span>{change.value}</span>; }, true); // error change.value
|
|
120
|
+
derive(() => scope.watch(() => <span>{count.value}</span>, () => { <span>{change.value}</span>; })); // error change.value
|
|
121
|
+
derive(() => <button onClick={() => { <span>{count.value}</span>; }} />); // error count.value
|
|
122
|
+
unknown.computed(() => <span>{count.value}</span>); // error count.value
|
|
123
|
+
UI.jsxList(() => [<span>{count.value}</span>]);
|
|
124
|
+
UI.jsxMap(() => [count.value], item => item, (item, index, key) => <span key={key}>{item + count.value}</span>);
|
|
125
|
+
`;
|
|
126
|
+
assert.deepEqual(diagnostics(fixture), expected(fixture));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("shares the direct-map boundary with the JSX transform", () => {
|
|
130
|
+
const fixture = `
|
|
131
|
+
import * as UI from "@kumi/core";
|
|
132
|
+
import { ref, type ReadonlyRef } from "@kumi/state";
|
|
133
|
+
|
|
134
|
+
const rows = ref([1, 2]);
|
|
135
|
+
const caption = ref("entry");
|
|
136
|
+
const plain = [1, 2];
|
|
137
|
+
declare const optional: ReadonlyRef<number[]> | undefined;
|
|
138
|
+
declare const owner: { rows: ReadonlyRef<number[]> } | undefined;
|
|
139
|
+
declare const groupedOwner: NonNullable<typeof owner>;
|
|
140
|
+
|
|
141
|
+
<div>{rows.value.map(item => <span key={item}>{caption.value}</span>)}</div>;
|
|
142
|
+
<>{rows.value.filter(item => item > 1).map(item => <span key={item}>{caption.value}</span>)}</>;
|
|
143
|
+
<div>{rows.value.map(item => <span key={item}>{caption.value}</span>).filter(Boolean)}</div>; // error rows.value caption.value
|
|
144
|
+
<div>{optional?.value.map(item => <span key={item}>{caption.value}</span>)}</div>; // error optional?.value caption.value
|
|
145
|
+
<div>{rows.value.map?.(item => <span key={item}>{caption.value}</span>)}</div>; // error rows.value caption.value
|
|
146
|
+
<div>{owner?.rows.value.map(item => <span key={item}>{caption.value}</span>)}</div>; // error owner?.rows.value caption.value
|
|
147
|
+
<div>{(groupedOwner?.rows.value).map(item => <span key={item}>{caption.value}</span>)}</div>; // error groupedOwner?.rows.value caption.value
|
|
148
|
+
<div>{plain.map(item => <span key={item}>{caption.value}</span>)}</div>; // error caption.value
|
|
149
|
+
`;
|
|
150
|
+
const transformed = transformJsx(ts, fixturePath, fixture);
|
|
151
|
+
assert.match(transformed, /__kumiJsxMap\(\(\) => \(rows\.value\)/);
|
|
152
|
+
assert.deepEqual(diagnostics(fixture), expected(fixture));
|
|
153
|
+
assert.deepEqual(diagnostics(transformed).map(diagnostic => diagnostic.text), expected(fixture).map(diagnostic => diagnostic.text));
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("requires an arrow callback that returns a keyed JSX row directly", () => {
|
|
157
|
+
const functionRow = "<div>{rows.value.map(function (row) { return <span key={row.id} />; })}</div>";
|
|
158
|
+
const indirectRow = "<div>{rows.value.map(row => { const item = <span key={row.id} />; return item; })}</div>";
|
|
159
|
+
|
|
160
|
+
assert.throws(() => transformJsx(ts, fixturePath, functionRow), /Use an arrow callback.*returns the keyed JSX row directly/);
|
|
161
|
+
assert.throws(() => transformJsx(ts, fixturePath, indirectRow), /Return one JSX element with an explicit key directly/);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("reports the original source coordinates before Form rewrites children", () => {
|
|
165
|
+
const fixture = `
|
|
166
|
+
import * as UI from "@kumi/core";
|
|
167
|
+
import { Form as Fields, type FormController } from "@kumi/form";
|
|
168
|
+
import { ref } from "@kumi/state";
|
|
169
|
+
declare const form: FormController<{ health: number }>;
|
|
170
|
+
const health = ref(40);
|
|
171
|
+
<Fields form={form}>{health.value}</Fields>; // error health.value
|
|
172
|
+
`;
|
|
173
|
+
const transformed = transformJsx(ts, fixturePath, fixture);
|
|
174
|
+
assert.match(transformed, /\{\(\) => <>\{health\.value\}/);
|
|
175
|
+
const original = diagnostics(fixture);
|
|
176
|
+
assert.deepEqual(original, expected(fixture));
|
|
177
|
+
const after = diagnostics(transformed);
|
|
178
|
+
assert.equal(after[0].line, original[0].line);
|
|
179
|
+
assert.ok(after[0].column > original[0].column);
|
|
180
|
+
assert.equal(after[0].text, original[0].text);
|
|
181
|
+
});
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
const eventParameters = new Map([
|
|
2
|
+
["OnClick", ["self", "button", "down", "control"]],
|
|
3
|
+
["PostClick", ["self", "message", "button", "down", "control"]],
|
|
4
|
+
["OnEnter", ["self", "control"]],
|
|
5
|
+
["OnLeave", ["self", "control"]],
|
|
6
|
+
["OnShow", ["self", "control"]],
|
|
7
|
+
["OnHide", ["self", "control"]],
|
|
8
|
+
["state", ["self", "stateid", "newstate", "control"]],
|
|
9
|
+
["childUpdate", ["self", "scriptid", "message", "control"]],
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const handleMethods = new Set([
|
|
13
|
+
"GetFrameRef", "GetAttribute", "SetAttribute", "Show", "Hide", "IsShown", "GetName", "GetParent", "Raise",
|
|
14
|
+
"SetBindingClick", "SetBinding", "ClearBindings", "SetHeight", "ClearAllPoints", "SetPoint", "ChildUpdate",
|
|
15
|
+
]);
|
|
16
|
+
const globalFunctions = new Set(["format", "newtable"]);
|
|
17
|
+
const libraryFunctions = new Map([
|
|
18
|
+
["string", new Set(["byte", "char", "find", "format", "gsub", "len", "lower", "match", "sub", "upper"])],
|
|
19
|
+
["math", new Set(["abs", "ceil", "floor", "max", "min", "sqrt"])],
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
function luaString(text) {
|
|
23
|
+
const escaped = text.replace(/["\\\x00-\x1f\x7f]/g, char => {
|
|
24
|
+
if (char === '"' || char === "\\") return "\\" + char;
|
|
25
|
+
return "\\" + String(char.charCodeAt(0)).padStart(3, "0");
|
|
26
|
+
});
|
|
27
|
+
return '"' + escaped + '"';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function transformSecureSnippets(ts, path, source, { program, isMarkerSymbol }) {
|
|
31
|
+
if (!/\.tsx?$/.test(path) || path.endsWith(".d.ts") || !source.includes("secureSnippet")) return;
|
|
32
|
+
const file = program.getSourceFile(path);
|
|
33
|
+
if (!file || file.text !== source) return;
|
|
34
|
+
const checker = program.getTypeChecker();
|
|
35
|
+
const edits = [];
|
|
36
|
+
|
|
37
|
+
function error(node, reason) {
|
|
38
|
+
const { line, character } = file.getLineAndCharacterOfPosition(node.getStart(file));
|
|
39
|
+
throw new Error(`${path}:${line + 1}:${character + 1}: ${reason}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isMarker(node) {
|
|
43
|
+
if (!ts.isIdentifier(node)) return false;
|
|
44
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
45
|
+
const declaration = symbol?.declarations?.find(ts.isImportSpecifier);
|
|
46
|
+
if (!declaration || declaration.isTypeOnly) return false;
|
|
47
|
+
const clause = declaration.parent.parent;
|
|
48
|
+
if (isMarkerSymbol) return !clause.isTypeOnly && isMarkerSymbol(checker.getAliasedSymbol(symbol));
|
|
49
|
+
return !clause.isTypeOnly && clause.parent.moduleSpecifier.text === "@kumi/wow"
|
|
50
|
+
&& (declaration.propertyName ?? declaration.name).text === "secureSnippet";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function properties(node) {
|
|
54
|
+
if (!ts.isObjectLiteralExpression(node)) error(node, "secureSnippet requires inline options and refs");
|
|
55
|
+
return node.properties.map(property => {
|
|
56
|
+
if ((!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property))
|
|
57
|
+
|| (!ts.isIdentifier(property.name) && !ts.isStringLiteral(property.name))) {
|
|
58
|
+
error(property, "secureSnippet option names must be static");
|
|
59
|
+
}
|
|
60
|
+
return [property.name.text, property.initializer ?? property.name];
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function options(node) {
|
|
65
|
+
let event = "OnClick";
|
|
66
|
+
const refs = new Set();
|
|
67
|
+
if (!node) return { event, refs };
|
|
68
|
+
const seen = new Set();
|
|
69
|
+
for (const [key, value] of properties(node)) {
|
|
70
|
+
if (seen.has(key)) error(value, `Repeated secureSnippet option ${key}`);
|
|
71
|
+
seen.add(key);
|
|
72
|
+
if (key === "event") {
|
|
73
|
+
if (!ts.isStringLiteral(value) || !eventParameters.has(value.text)) error(value, "Expected a supported secure event literal");
|
|
74
|
+
event = value.text;
|
|
75
|
+
} else if (key === "refs") {
|
|
76
|
+
for (const [name, target] of properties(value)) {
|
|
77
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(name) || /^(?:kumi-secure-|frameref-)/.test(name) || refs.has(name)) {
|
|
78
|
+
error(target, "Expected a unique secure frame reference name");
|
|
79
|
+
}
|
|
80
|
+
refs.add(name);
|
|
81
|
+
}
|
|
82
|
+
} else error(value, `Unknown secureSnippet option ${key}`);
|
|
83
|
+
}
|
|
84
|
+
return { event, refs };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function visit(node) {
|
|
88
|
+
if (ts.isCallExpression(node) && isMarker(node.expression)) {
|
|
89
|
+
const [body, config] = node.arguments;
|
|
90
|
+
if (!body || !ts.isArrowFunction(body) || node.arguments.length > 2 || body.modifiers?.length) {
|
|
91
|
+
error(node, "Pass a synchronous inline arrow to secureSnippet");
|
|
92
|
+
}
|
|
93
|
+
const { event, refs } = options(config);
|
|
94
|
+
const lua = compileBody(ts, checker, body, event, refs, error);
|
|
95
|
+
const original = source.slice(body.getStart(file), body.end);
|
|
96
|
+
const lines = original.match(/\r\n|\n|\r/g) ?? [];
|
|
97
|
+
edits.push([body.getStart(file), body.end, `${JSON.stringify(lua)} as never${lines.join("")}`]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (ts.isIdentifier(node) && !ts.isImportSpecifier(node.parent) && isMarker(node)) {
|
|
101
|
+
error(node, "Use secureSnippet as a direct call");
|
|
102
|
+
}
|
|
103
|
+
ts.forEachChild(node, visit);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
visit(file);
|
|
107
|
+
if (edits.length === 0) return;
|
|
108
|
+
const diagnostics = [...program.getSyntacticDiagnostics(file), ...program.getSemanticDiagnostics(file)];
|
|
109
|
+
const invalid = diagnostics.find(diagnostic => diagnostic.category === ts.DiagnosticCategory.Error);
|
|
110
|
+
if (invalid) {
|
|
111
|
+
const { line, character } = file.getLineAndCharacterOfPosition(invalid.start ?? 0);
|
|
112
|
+
const reason = ts.flattenDiagnosticMessageText(invalid.messageText, "\n");
|
|
113
|
+
throw new Error(`${path}:${line + 1}:${character + 1}: ${reason}`);
|
|
114
|
+
}
|
|
115
|
+
for (const [start, end, replacement] of edits.reverse()) source = source.slice(0, start) + replacement + source.slice(end);
|
|
116
|
+
return source;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function compileBody(ts, checker, arrow, event, refs, error) {
|
|
120
|
+
const kind = ts.SyntaxKind;
|
|
121
|
+
const parameters = eventParameters.get(event);
|
|
122
|
+
const locals = new Map();
|
|
123
|
+
let nextLocal = 0;
|
|
124
|
+
|
|
125
|
+
function define(name, lua, writable) {
|
|
126
|
+
if (!ts.isIdentifier(name)) error(name, "Secure locals and parameters require simple names");
|
|
127
|
+
locals.set(checker.getSymbolAtLocation(name), { lua, writable });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function lookup(node) {
|
|
131
|
+
return ts.isIdentifier(node) ? locals.get(checker.getSymbolAtLocation(node)) : undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isGlobal(node) {
|
|
135
|
+
if (!ts.isIdentifier(node) || lookup(node)) return false;
|
|
136
|
+
const declarations = checker.getSymbolAtLocation(node)?.declarations ?? [];
|
|
137
|
+
return declarations.length > 0 && declarations.every(declaration => declaration.getSourceFile().isDeclarationFile
|
|
138
|
+
|| (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient) !== 0);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
arrow.parameters.forEach(({ name, initializer, dotDotDotToken }, index) => {
|
|
142
|
+
if (initializer || dotDotDotToken || !parameters[index]) error(name, `Unexpected parameter for ${event}`);
|
|
143
|
+
define(name, parameters[index], false);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
function hasType(node, flags) {
|
|
147
|
+
const type = checker.getTypeAtLocation(node);
|
|
148
|
+
return type.isUnion() ? type.types.every(part => (part.flags & flags) !== 0) : (type.flags & flags) !== 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function condition(node) {
|
|
152
|
+
if (!hasType(node, ts.TypeFlags.BooleanLike)) error(node, "Secure conditions must be boolean");
|
|
153
|
+
return expression(node);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function number(node) {
|
|
157
|
+
if (!hasType(node, ts.TypeFlags.NumberLike)) error(node, "Secure arithmetic requires numbers");
|
|
158
|
+
return expression(node);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function call(node) {
|
|
162
|
+
const callee = node.expression;
|
|
163
|
+
if (node.questionDotToken || (ts.isPropertyAccessExpression(callee) && callee.questionDotToken)) {
|
|
164
|
+
error(node, "Optional calls are unsupported in a secure snippet");
|
|
165
|
+
}
|
|
166
|
+
let name;
|
|
167
|
+
if (ts.isIdentifier(callee) && globalFunctions.has(callee.text) && isGlobal(callee)) name = callee.text;
|
|
168
|
+
else if (ts.isPropertyAccessExpression(callee)) {
|
|
169
|
+
const receiver = callee.expression;
|
|
170
|
+
const method = callee.name.text;
|
|
171
|
+
if (ts.isIdentifier(receiver) && libraryFunctions.get(receiver.text)?.has(method) && isGlobal(receiver)) {
|
|
172
|
+
name = `${receiver.text}.${method}`;
|
|
173
|
+
} else if (handleMethods.has(method)) {
|
|
174
|
+
const [key] = node.arguments;
|
|
175
|
+
if (method === "GetFrameRef" && (!key || !ts.isStringLiteral(key) || !refs.has(key.text))) {
|
|
176
|
+
error(key ?? node, "GetFrameRef requires a literal key declared in refs");
|
|
177
|
+
}
|
|
178
|
+
if (method === "GetFrameRef" && lookup(receiver)?.lua !== "control"
|
|
179
|
+
&& !(event === "state" && lookup(receiver)?.lua === "self")) {
|
|
180
|
+
error(receiver, "Read declared frame references through the control parameter");
|
|
181
|
+
}
|
|
182
|
+
if (method === "SetAttribute" && key && ts.isStringLiteral(key) && /^(?:_|kumi-secure-|frameref-)/.test(key.text)) {
|
|
183
|
+
error(key, "Secure handlers cannot replace scripts, their markers or frame references");
|
|
184
|
+
}
|
|
185
|
+
name = `${expression(receiver)}:${method}`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!name) error(callee, "Expected an allowed restricted function or frame handle method");
|
|
189
|
+
return `${name}(${node.arguments.map(expression).join(", ")})`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function binary(node) {
|
|
193
|
+
const { left, right } = node;
|
|
194
|
+
const operator = node.operatorToken.kind;
|
|
195
|
+
if (operator === kind.EqualsEqualsEqualsToken || operator === kind.ExclamationEqualsEqualsToken) {
|
|
196
|
+
const op = operator === kind.EqualsEqualsEqualsToken ? "==" : "~=";
|
|
197
|
+
if (ts.isTypeOfExpression(left) || ts.isTypeOfExpression(right)) {
|
|
198
|
+
const [test, literal] = ts.isTypeOfExpression(left) ? [left, right] : [right, left];
|
|
199
|
+
if (!ts.isStringLiteral(literal) || !["number", "string", "boolean", "undefined"].includes(literal.text)) {
|
|
200
|
+
error(literal, "Use typeof with number, string, boolean or undefined in a secure snippet");
|
|
201
|
+
}
|
|
202
|
+
return `(type(${expression(test.expression)}) ${op} ${luaString(literal.text === "undefined" ? "nil" : literal.text)})`;
|
|
203
|
+
}
|
|
204
|
+
return `(${expression(left)} ${op} ${expression(right)})`;
|
|
205
|
+
}
|
|
206
|
+
if (operator === kind.AmpersandAmpersandToken || operator === kind.BarBarToken) {
|
|
207
|
+
return `(${condition(left)} ${operator === kind.AmpersandAmpersandToken ? "and" : "or"} ${condition(right)})`;
|
|
208
|
+
}
|
|
209
|
+
if (operator === kind.PlusToken && (hasType(left, ts.TypeFlags.StringLike) || hasType(right, ts.TypeFlags.StringLike))) {
|
|
210
|
+
const flags = ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike;
|
|
211
|
+
if (!hasType(left, flags) || !hasType(right, flags)) error(node, "Secure concatenation accepts only strings and numbers");
|
|
212
|
+
return `(${expression(left)} .. ${expression(right)})`;
|
|
213
|
+
}
|
|
214
|
+
const numeric = [kind.PlusToken, kind.MinusToken, kind.AsteriskToken, kind.SlashToken, kind.LessThanToken,
|
|
215
|
+
kind.LessThanEqualsToken, kind.GreaterThanToken, kind.GreaterThanEqualsToken];
|
|
216
|
+
if (!numeric.includes(operator)) error(node.operatorToken, "Unsupported secure operator");
|
|
217
|
+
return `(${number(left)} ${node.operatorToken.getText()} ${number(right)})`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function expression(node) {
|
|
221
|
+
if (ts.isIdentifier(node)) {
|
|
222
|
+
const local = lookup(node);
|
|
223
|
+
if (local) return local.lua;
|
|
224
|
+
if (node.text === "undefined" && !checker.getSymbolAtLocation(node)?.declarations?.length) return "nil";
|
|
225
|
+
error(node, `Secure snippet cannot capture ${node.text}`);
|
|
226
|
+
}
|
|
227
|
+
if (ts.isStringLiteralLike(node)) return luaString(node.text);
|
|
228
|
+
if (ts.isNumericLiteral(node)) {
|
|
229
|
+
const value = Number(node.text);
|
|
230
|
+
if (!Number.isFinite(value)) error(node, "Secure number literals must be finite");
|
|
231
|
+
return String(value);
|
|
232
|
+
}
|
|
233
|
+
if (node.kind === kind.TrueKeyword) return "true";
|
|
234
|
+
if (node.kind === kind.FalseKeyword) return "false";
|
|
235
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node)) return expression(node.expression);
|
|
236
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === kind.ExclamationToken) return `(not ${condition(node.operand)})`;
|
|
237
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === kind.MinusToken) return `(-${number(node.operand)})`;
|
|
238
|
+
if (ts.isBinaryExpression(node)) return binary(node);
|
|
239
|
+
if (ts.isCallExpression(node)) return call(node);
|
|
240
|
+
error(node, `Unsupported secure expression ${kind[node.kind]}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function declarations(list) {
|
|
244
|
+
const writable = (list.flags & ts.NodeFlags.Let) !== 0;
|
|
245
|
+
if (!writable && (list.flags & ts.NodeFlags.Const) === 0) error(list, "Declare secure locals with let or const");
|
|
246
|
+
return list.declarations.map(declaration => {
|
|
247
|
+
const value = declaration.initializer ? expression(declaration.initializer) : "nil";
|
|
248
|
+
const lua = `local${++nextLocal}_${declaration.name.getText()}`;
|
|
249
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(lua)) error(declaration.name, "Use an ASCII identifier for a secure local");
|
|
250
|
+
define(declaration.name, lua, writable);
|
|
251
|
+
return `local ${lua} = ${value}`;
|
|
252
|
+
}).join("\n");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function change(node) {
|
|
256
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === kind.EqualsToken) {
|
|
257
|
+
const receiver = lookup(node.left);
|
|
258
|
+
if (!receiver?.writable) error(node.left, "Secure assignments require a mutable local");
|
|
259
|
+
return `${receiver.lua} = ${expression(node.right)}`;
|
|
260
|
+
}
|
|
261
|
+
if (ts.isPostfixUnaryExpression(node)) {
|
|
262
|
+
const receiver = lookup(node.operand);
|
|
263
|
+
if (!receiver?.writable) error(node.operand, "Secure increments require a mutable local");
|
|
264
|
+
return `${receiver.lua} = ${number(node.operand)} ${node.operator === kind.PlusPlusToken ? "+" : "-"} 1`;
|
|
265
|
+
}
|
|
266
|
+
if (ts.isCallExpression(node)) return call(node);
|
|
267
|
+
error(node, "Expected a restricted call or local assignment");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function returns(node) {
|
|
271
|
+
if (!node) return "return";
|
|
272
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
273
|
+
if (event !== "OnClick" || node.elements.length !== 2) error(node, "Only OnClick handlers can return a pair");
|
|
274
|
+
return `return ${node.elements.map(expression).join(", ")}`;
|
|
275
|
+
}
|
|
276
|
+
if (event === "state" || event === "childUpdate" || event === "PostClick") error(node, `${event} handlers do not return a value`);
|
|
277
|
+
return `return ${expression(node)}`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function indent(text) {
|
|
281
|
+
return text.split("\n").map(line => line ? " " + line : "").join("\n");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function branch(node) {
|
|
285
|
+
if (!ts.isBlock(node)) return statement(node);
|
|
286
|
+
return node.statements.map((value, index) => {
|
|
287
|
+
if (index > 0 && ts.isReturnStatement(node.statements[index - 1])) error(value, "Move the statement before the secure return");
|
|
288
|
+
return statement(value);
|
|
289
|
+
}).join("\n");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function statement(node) {
|
|
293
|
+
if (ts.isVariableStatement(node)) return declarations(node.declarationList);
|
|
294
|
+
if (ts.isExpressionStatement(node)) return change(node.expression);
|
|
295
|
+
if (ts.isReturnStatement(node)) return returns(node.expression);
|
|
296
|
+
if (ts.isBlock(node)) return `do\n${indent(branch(node))}\nend`;
|
|
297
|
+
if (ts.isIfStatement(node)) {
|
|
298
|
+
const otherwise = node.elseStatement ? `\nelse\n${indent(branch(node.elseStatement))}` : "";
|
|
299
|
+
return `if ${condition(node.expression)} then\n${indent(branch(node.thenStatement))}${otherwise}\nend`;
|
|
300
|
+
}
|
|
301
|
+
if (ts.isWhileStatement(node)) return `while ${condition(node.expression)} do\n${indent(branch(node.statement))}\nend`;
|
|
302
|
+
if (ts.isForStatement(node)) {
|
|
303
|
+
const { initializer, condition: test, incrementor } = node;
|
|
304
|
+
if (!initializer || !ts.isVariableDeclarationList(initializer) || !test || !incrementor) {
|
|
305
|
+
error(node, "Use a local counter, condition and increment in a secure loop");
|
|
306
|
+
}
|
|
307
|
+
const start = declarations(initializer);
|
|
308
|
+
const check = condition(test);
|
|
309
|
+
const loop = [`do\n${indent(branch(node.statement))}\nend`, change(incrementor)].join("\n");
|
|
310
|
+
return `do\n${indent(start)}\n while ${check} do\n${indent(indent(loop))}\n end\nend`;
|
|
311
|
+
}
|
|
312
|
+
error(node, `Unsupported secure statement ${kind[node.kind]}`);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (ts.isBlock(arrow.body)) return branch(arrow.body);
|
|
316
|
+
return hasType(arrow.body, ts.TypeFlags.Void) ? change(arrow.body) : returns(arrow.body);
|
|
317
|
+
}
|