@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,155 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { test } from "node:test";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { lintLuaBuiltinValues } from "./lua-builtin-values.mjs";
|
|
7
|
+
|
|
8
|
+
const ts = createRequire(import.meta.url)("typescript");
|
|
9
|
+
const directory = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const normalize = file => resolve(file).replaceAll("\\", "/");
|
|
11
|
+
const fixturePath = normalize(join(directory, "lua-builtin-values-fixture.ts"));
|
|
12
|
+
|
|
13
|
+
function diagnostics(text, extra = {}) {
|
|
14
|
+
const options = {
|
|
15
|
+
strict: true,
|
|
16
|
+
target: ts.ScriptTarget.ES2020,
|
|
17
|
+
module: ts.ModuleKind.ESNext,
|
|
18
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
19
|
+
types: [],
|
|
20
|
+
};
|
|
21
|
+
const files = new Map([
|
|
22
|
+
[fixturePath, text],
|
|
23
|
+
...Object.entries(extra).map(([name, source]) => [normalize(join(directory, name)), source]),
|
|
24
|
+
]);
|
|
25
|
+
const host = ts.createCompilerHost(options);
|
|
26
|
+
const { fileExists, readFile } = host;
|
|
27
|
+
host.fileExists = file => files.has(normalize(file)) || fileExists(file);
|
|
28
|
+
host.readFile = file => files.get(normalize(file)) ?? readFile(file);
|
|
29
|
+
const program = ts.createProgram([fixturePath], options, host);
|
|
30
|
+
assert.deepEqual(ts.getPreEmitDiagnostics(program).map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")), []);
|
|
31
|
+
const source = program.getSourceFile(fixturePath);
|
|
32
|
+
const result = [];
|
|
33
|
+
lintLuaBuiltinValues(ts, source, program.getTypeChecker(), (node, message) => {
|
|
34
|
+
const reference = node.getText(source);
|
|
35
|
+
if (reference === "Boolean" || reference === '"Boolean"') {
|
|
36
|
+
assert.equal(message, "Lua does not support the global Boolean conversion. Use an explicit comparison suited to the value type.");
|
|
37
|
+
} else {
|
|
38
|
+
assert.match(message, /use value => (Number|String)\(value\)/);
|
|
39
|
+
}
|
|
40
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
41
|
+
result.push([line + 1, character + 1, reference]);
|
|
42
|
+
}, program);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test("reports global constructor values at their source references", () => {
|
|
47
|
+
const fixture = [
|
|
48
|
+
"[1].filter(Boolean);",
|
|
49
|
+
"[1].map(String);",
|
|
50
|
+
'["1"].map(Number);',
|
|
51
|
+
"const alias = Number;",
|
|
52
|
+
"const options = { Boolean };",
|
|
53
|
+
"const qualified = globalThis.Number;",
|
|
54
|
+
'const indexed = globalThis["String"];',
|
|
55
|
+
"const wrapped = (String as StringConstructor)!;",
|
|
56
|
+
].join("\n");
|
|
57
|
+
assert.deepEqual(diagnostics(fixture), [
|
|
58
|
+
[1, 12, "Boolean"],
|
|
59
|
+
[2, 9, "String"],
|
|
60
|
+
[3, 11, "Number"],
|
|
61
|
+
[4, 15, "Number"],
|
|
62
|
+
[5, 19, "Boolean"],
|
|
63
|
+
[6, 30, "Number"],
|
|
64
|
+
[7, 28, '"String"'],
|
|
65
|
+
[8, 18, "String"],
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("reports destructured properties only when they resolve to a global builtin", () => {
|
|
70
|
+
const fixture = [
|
|
71
|
+
"export {};",
|
|
72
|
+
"const { Number: convert } = globalThis;",
|
|
73
|
+
"const { String } = globalThis;",
|
|
74
|
+
'const { "Boolean": toBoolean } = globalThis;',
|
|
75
|
+
"const local = { Number(value: string) { return value.length; } };",
|
|
76
|
+
"const { Number } = local;",
|
|
77
|
+
].join("\n");
|
|
78
|
+
assert.deepEqual(diagnostics(fixture), [
|
|
79
|
+
[2, 9, "Number"],
|
|
80
|
+
[3, 9, "String"],
|
|
81
|
+
[4, 9, '"Boolean"'],
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("reports calls and construction of the global Boolean builtin at the reference", () => {
|
|
86
|
+
const fixture = [
|
|
87
|
+
"Boolean(true);",
|
|
88
|
+
"globalThis.Boolean(false);",
|
|
89
|
+
'globalThis["Boolean"](0);',
|
|
90
|
+
"(((Boolean)))(true);",
|
|
91
|
+
"(Boolean as BooleanConstructor)(1);",
|
|
92
|
+
"Boolean!(1);",
|
|
93
|
+
"(Boolean satisfies BooleanConstructor)(1);",
|
|
94
|
+
"Boolean?.(0);",
|
|
95
|
+
"globalThis.Boolean?.(false);",
|
|
96
|
+
'globalThis["Boolean"]?.(0);',
|
|
97
|
+
"new Boolean(true);",
|
|
98
|
+
'new (globalThis["Boolean"])(1);',
|
|
99
|
+
].join("\n");
|
|
100
|
+
assert.deepEqual(diagnostics(fixture), [
|
|
101
|
+
[1, 1, "Boolean"],
|
|
102
|
+
[2, 12, "Boolean"],
|
|
103
|
+
[3, 12, '"Boolean"'],
|
|
104
|
+
[4, 4, "Boolean"],
|
|
105
|
+
[5, 2, "Boolean"],
|
|
106
|
+
[6, 1, "Boolean"],
|
|
107
|
+
[7, 2, "Boolean"],
|
|
108
|
+
[8, 1, "Boolean"],
|
|
109
|
+
[9, 12, "Boolean"],
|
|
110
|
+
[10, 12, '"Boolean"'],
|
|
111
|
+
[11, 5, "Boolean"],
|
|
112
|
+
[12, 17, '"Boolean"'],
|
|
113
|
+
]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("allows Number and String calls, static properties, writes, and type positions", () => {
|
|
117
|
+
const fixture = [
|
|
118
|
+
'const converted = [Number("1"), String(1)];',
|
|
119
|
+
"[1].map(value => String(value));",
|
|
120
|
+
'(Number as NumberConstructor)!("1");',
|
|
121
|
+
"(String satisfies StringConstructor)(1);",
|
|
122
|
+
"Number.isFinite(1);",
|
|
123
|
+
"Boolean.prototype.valueOf;",
|
|
124
|
+
'globalThis["Boolean"].prototype;',
|
|
125
|
+
'const checks = { Boolean: Number["isNaN"], Number: Number.isFinite, String: 1 };',
|
|
126
|
+
'const value: String = "one";',
|
|
127
|
+
"type Globals = [typeof Boolean, Number, typeof String];",
|
|
128
|
+
"interface Numeric extends Number {}",
|
|
129
|
+
"declare const replacement: BooleanConstructor;",
|
|
130
|
+
"globalThis.Boolean = replacement;",
|
|
131
|
+
'globalThis["Boolean"] = replacement;',
|
|
132
|
+
].join("\n");
|
|
133
|
+
assert.deepEqual(diagnostics(fixture), []);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("allows values shadowed by local and imported symbols", () => {
|
|
137
|
+
const fixture = [
|
|
138
|
+
'import { Boolean, String } from "./lua-builtin-values-local";',
|
|
139
|
+
"function local(Boolean: (value: number) => boolean) { return Boolean(1) && [1].filter(Boolean).length > 0; }",
|
|
140
|
+
"Boolean(1);",
|
|
141
|
+
"(Boolean satisfies (value: number) => boolean)?.(1);",
|
|
142
|
+
"{ const Number = (value: string) => value.length; const alias = Number; }",
|
|
143
|
+
"const formatters = { String };",
|
|
144
|
+
"const values = [1].map(String);",
|
|
145
|
+
"const own = { Boolean(value: number) { return value !== 0; } };",
|
|
146
|
+
"[1].filter(own.Boolean);",
|
|
147
|
+
'own["Boolean"]?.(1);',
|
|
148
|
+
"{ const globalThis = own; globalThis.Boolean(0); }",
|
|
149
|
+
].join("\n");
|
|
150
|
+
const extra = {
|
|
151
|
+
"lua-builtin-values-local.ts": "export function String(value: number) { return value.toFixed(); }\n"
|
|
152
|
+
+ "export function Boolean(value: number) { return value !== 0; }",
|
|
153
|
+
};
|
|
154
|
+
assert.deepEqual(diagnostics(fixture, extra), []);
|
|
155
|
+
});
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { basename, dirname, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
const framework = fileURLToPath(new URL("../", import.meta.url));
|
|
5
|
+
const resultFile = resolve(framework, "src/result.ts");
|
|
6
|
+
const protectFile = resolve(framework, "wow/protect.ts");
|
|
7
|
+
const arrayCallbacks = new Set([
|
|
8
|
+
"every", "filter", "find", "findIndex", "findLast", "findLastIndex", "flatMap", "forEach", "map",
|
|
9
|
+
"reduce", "reduceRight", "some", "sort", "toSorted",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export function lintLuaLoopClosures(ts, source, checker, report) {
|
|
13
|
+
const bindings = new Map();
|
|
14
|
+
|
|
15
|
+
function collectLoops(node) {
|
|
16
|
+
if (ts.isForStatement(node) && node.initializer && ts.isVariableDeclarationList(node.initializer)
|
|
17
|
+
&& (node.initializer.flags & ts.NodeFlags.Let)) {
|
|
18
|
+
function addBinding(name) {
|
|
19
|
+
if (ts.isIdentifier(name)) {
|
|
20
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
21
|
+
if (symbol) bindings.set(symbol, node);
|
|
22
|
+
} else {
|
|
23
|
+
for (const element of name.elements) {
|
|
24
|
+
if (ts.isBindingElement(element)) addBinding(element.name);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const declaration of node.initializer.declarations) addBinding(declaration.name);
|
|
29
|
+
}
|
|
30
|
+
ts.forEachChild(node, collectLoops);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
collectLoops(source);
|
|
34
|
+
if (!bindings.size) return;
|
|
35
|
+
|
|
36
|
+
const references = new Map();
|
|
37
|
+
function collectReferences(node) {
|
|
38
|
+
if (ts.isTypeNode(node)) return;
|
|
39
|
+
if (ts.isIdentifier(node)) {
|
|
40
|
+
const parent = node.parent;
|
|
41
|
+
const symbol = ts.isShorthandPropertyAssignment(parent) && parent.name === node
|
|
42
|
+
? checker.getShorthandAssignmentValueSymbol(parent)
|
|
43
|
+
: checker.getSymbolAtLocation(node);
|
|
44
|
+
if (symbol && !symbol.declarations?.some(declaration => declaration.name === node)) {
|
|
45
|
+
if (!references.has(symbol)) references.set(symbol, []);
|
|
46
|
+
references.get(symbol).push(node);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
ts.forEachChild(node, collectReferences);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
collectReferences(source);
|
|
53
|
+
const proofs = new Map();
|
|
54
|
+
|
|
55
|
+
function atCall(node, boundary) {
|
|
56
|
+
const expression = outside(node);
|
|
57
|
+
const call = expression.parent;
|
|
58
|
+
if (!ts.isCallExpression(call)) return false;
|
|
59
|
+
const index = call.arguments.indexOf(expression);
|
|
60
|
+
return (call.expression === expression || (index !== -1 && synchronousParameter(call, index)))
|
|
61
|
+
&& synchronousWithin(call, boundary);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function synchronousParameter(call, index) {
|
|
65
|
+
if (index === 0 && isSynchronousApi(ts, checker.getResolvedSignature(call)?.declaration)) return true;
|
|
66
|
+
const target = ts.skipOuterExpressions(call.expression);
|
|
67
|
+
const declaration = ts.isIdentifier(target) ? checker.getSymbolAtLocation(target)?.valueDeclaration : undefined;
|
|
68
|
+
const fn = declaration && ts.isFunctionDeclaration(declaration) ? declaration
|
|
69
|
+
: declaration && ts.isVariableDeclaration(declaration) && isConst(declaration)
|
|
70
|
+
? declaration.initializer && ts.skipOuterExpressions(declaration.initializer)
|
|
71
|
+
: target;
|
|
72
|
+
if (!fn || fn.getSourceFile() !== source || !isSynchronousSyntax(ts, fn)) return false;
|
|
73
|
+
const receiver = fn.parameters[0]?.name;
|
|
74
|
+
const offset = receiver && ts.isIdentifier(receiver) && receiver.text === "this" ? 1 : 0;
|
|
75
|
+
const parameter = fn.parameters[index + offset];
|
|
76
|
+
if (!parameter || parameter.dotDotDotToken || !ts.isIdentifier(parameter.name)) return false;
|
|
77
|
+
if (call.arguments.slice(0, index).some(ts.isSpreadElement)) return false;
|
|
78
|
+
return prove(parameter, fn, () => usesOf(parameter.name).every(use => atCall(use, fn)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function synchronousWithin(node, boundary) {
|
|
82
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
83
|
+
if (current === boundary) return true;
|
|
84
|
+
if (ts.isFunctionLike(current)) {
|
|
85
|
+
return prove(current, boundary, () => synchronousFunction(current, boundary));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function synchronousFunction(fn, boundary) {
|
|
92
|
+
if (!isSynchronousSyntax(ts, fn)) return false;
|
|
93
|
+
if (ts.isFunctionDeclaration(fn)) {
|
|
94
|
+
return !!fn.name && usesOf(fn.name).every(use => atCall(use, boundary));
|
|
95
|
+
}
|
|
96
|
+
if (ts.isFunctionExpression(fn) && fn.name && usesOf(fn.name).length) return false;
|
|
97
|
+
const expression = outside(fn);
|
|
98
|
+
const declaration = expression.parent;
|
|
99
|
+
if (ts.isVariableDeclaration(declaration) && declaration.initializer === expression && isConst(declaration)) {
|
|
100
|
+
return ts.isIdentifier(declaration.name) && usesOf(declaration.name).every(use => atCall(use, boundary));
|
|
101
|
+
}
|
|
102
|
+
return atCall(expression, boundary);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function usesOf(name) {
|
|
106
|
+
return references.get(checker.getSymbolAtLocation(name)) ?? [];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function prove(node, boundary, check) {
|
|
110
|
+
if (!proofs.has(boundary)) proofs.set(boundary, new Map());
|
|
111
|
+
const status = proofs.get(boundary);
|
|
112
|
+
if (status.has(node)) return status.get(node);
|
|
113
|
+
// Treat cycles as unknown instead of assuming a recursive call stays synchronous.
|
|
114
|
+
status.set(node, false);
|
|
115
|
+
const proven = check();
|
|
116
|
+
status.set(node, proven);
|
|
117
|
+
return proven;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isConst(declaration) {
|
|
121
|
+
return ts.isVariableDeclarationList(declaration.parent) && (declaration.parent.flags & ts.NodeFlags.Const) !== 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function outside(node) {
|
|
125
|
+
while (node.parent && ts.isOuterExpression(node.parent) && node.parent.expression === node) node = node.parent;
|
|
126
|
+
return node;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const [symbol, loop] of bindings) {
|
|
130
|
+
const uses = references.get(symbol) ?? [];
|
|
131
|
+
if (!uses.some(use => isWrite(ts, use))) continue;
|
|
132
|
+
for (const use of uses) {
|
|
133
|
+
if (!synchronousWithin(use, loop)) {
|
|
134
|
+
report(use, "Lua 5.1 reuses the '"
|
|
135
|
+
+ use.text + "' binding across iterations. Capture a const declared inside the loop.");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function isSynchronousSyntax(ts, fn) {
|
|
142
|
+
return (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn) || ts.isFunctionDeclaration(fn))
|
|
143
|
+
&& !!fn.body && !fn.asteriskToken && !fn.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function isSynchronousApi(ts, signature) {
|
|
147
|
+
if (!signature) return false;
|
|
148
|
+
const source = signature.getSourceFile();
|
|
149
|
+
const file = resolve(source.fileName);
|
|
150
|
+
if (file === resultFile || file === protectFile) {
|
|
151
|
+
const owner = signature.parent;
|
|
152
|
+
return (file === resultFile && ts.isFunctionTypeNode(signature) && ts.isTypeAliasDeclaration(owner)
|
|
153
|
+
&& owner.name.text === "Protect")
|
|
154
|
+
|| (ts.isFunctionDeclaration(signature) && (file === resultFile ? signature.name?.text === "direct"
|
|
155
|
+
: signature.name?.text === "protect" || signature.name?.text === "pcall"));
|
|
156
|
+
}
|
|
157
|
+
const owner = signature.parent;
|
|
158
|
+
return source.isDeclarationFile && dirname(file) === dirname(resolve(ts.getDefaultLibFilePath({})))
|
|
159
|
+
&& /^lib\..+\.d\.ts$/.test(basename(file))
|
|
160
|
+
&& ts.isMethodSignature(signature) && ts.isIdentifier(signature.name) && arrayCallbacks.has(signature.name.text)
|
|
161
|
+
&& ts.isInterfaceDeclaration(owner) && (owner.name.text === "Array" || owner.name.text === "ReadonlyArray");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isWrite(ts, node) {
|
|
165
|
+
// Object and array assignment patterns use the expression nodes until they reach the assignment.
|
|
166
|
+
for (let parent = node.parent; parent; node = parent, parent = parent.parent) {
|
|
167
|
+
if (ts.isBinaryExpression(parent)) {
|
|
168
|
+
return parent.left === node && parent.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
|
|
169
|
+
&& parent.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
|
|
170
|
+
}
|
|
171
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) {
|
|
172
|
+
return parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken;
|
|
173
|
+
}
|
|
174
|
+
if (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) return parent.initializer === node;
|
|
175
|
+
if ((ts.isOuterExpression(parent) && parent.expression === node)
|
|
176
|
+
|| ts.isArrayLiteralExpression(parent) || ts.isObjectLiteralExpression(parent)
|
|
177
|
+
|| ts.isSpreadElement(parent) || ts.isSpreadAssignment(parent)
|
|
178
|
+
|| (ts.isPropertyAssignment(parent) && parent.initializer === node)
|
|
179
|
+
|| (ts.isShorthandPropertyAssignment(parent) && parent.name === node)) continue;
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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 { lintLuaLoopClosures } from "./lua-loop-closures.mjs";
|
|
6
|
+
|
|
7
|
+
const ts = createRequire(import.meta.url)("typescript");
|
|
8
|
+
|
|
9
|
+
const fixture = `
|
|
10
|
+
import type { Protect } from "../src/result";
|
|
11
|
+
|
|
12
|
+
declare const host: { protect: Protect };
|
|
13
|
+
declare const defer: (run: () => unknown) => void;
|
|
14
|
+
const callbacks: (() => unknown)[] = [];
|
|
15
|
+
const values: readonly number[] = [0, 1];
|
|
16
|
+
const other = { map: defer };
|
|
17
|
+
|
|
18
|
+
function run(fn: () => unknown) { return fn(); }
|
|
19
|
+
function forward(_tag: string, fn: () => unknown) { return run(fn); }
|
|
20
|
+
function protect(fn: () => unknown) { defer(fn); }
|
|
21
|
+
|
|
22
|
+
for (let index = 0; index < 2; index++) {
|
|
23
|
+
const snapshot = index;
|
|
24
|
+
callbacks.push(() => snapshot);
|
|
25
|
+
callbacks.push(() => index); // error
|
|
26
|
+
defer(() => ({ index })); // error
|
|
27
|
+
defer((index = 1) => index);
|
|
28
|
+
(() => index)();
|
|
29
|
+
(() => () => index)(); // error
|
|
30
|
+
values.map(() => index);
|
|
31
|
+
other.map(() => index); // error
|
|
32
|
+
protect(() => index); // error
|
|
33
|
+
host.protect(() => index);
|
|
34
|
+
forward("read", () => index);
|
|
35
|
+
defer(() => run(() => index)); // error
|
|
36
|
+
function direct() { return index; }
|
|
37
|
+
direct();
|
|
38
|
+
const alsoDirect = () => index;
|
|
39
|
+
alsoDirect();
|
|
40
|
+
const late = () => index; // error
|
|
41
|
+
defer(() => late());
|
|
42
|
+
function stored() { return index; } // error
|
|
43
|
+
const alias = stored;
|
|
44
|
+
callbacks.push(alias);
|
|
45
|
+
const object = { get value() { return index; } }; // error
|
|
46
|
+
defer(() => { index++; }); // error
|
|
47
|
+
defer(() => { type Index = typeof index; });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const index of values) defer(() => index);
|
|
51
|
+
for (let index of values) defer(() => index);
|
|
52
|
+
for (let index = 0; values.length; ) defer(() => index);
|
|
53
|
+
for (let index = 0; values.length; ) {
|
|
54
|
+
[index] = [1];
|
|
55
|
+
defer(() => index); // error
|
|
56
|
+
}
|
|
57
|
+
for (let [index] = values; index < 2; index++) {
|
|
58
|
+
defer(() => index); // error
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
for (let index = 0; (defer(() => index), index++ < 2); ) {} // error
|
|
62
|
+
for (let index = 0; index < 2; defer(() => index), index++) {} // error
|
|
63
|
+
`;
|
|
64
|
+
|
|
65
|
+
test("diagnoses Lua 5.1 loop bindings captured by callbacks that can escape", () => {
|
|
66
|
+
const path = fileURLToPath(new URL("./lua-loop-fixture.ts", import.meta.url)).replaceAll("\\", "/");
|
|
67
|
+
const options = {
|
|
68
|
+
strict: true,
|
|
69
|
+
target: ts.ScriptTarget.ES2020,
|
|
70
|
+
module: ts.ModuleKind.ESNext,
|
|
71
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
72
|
+
types: [],
|
|
73
|
+
};
|
|
74
|
+
const host = ts.createCompilerHost(options);
|
|
75
|
+
const readFile = host.readFile;
|
|
76
|
+
host.readFile = file => file === path ? fixture : readFile(file);
|
|
77
|
+
const program = ts.createProgram([path], options, host);
|
|
78
|
+
assert.deepEqual(ts.getPreEmitDiagnostics(program), []);
|
|
79
|
+
|
|
80
|
+
const source = program.getSourceFile(path);
|
|
81
|
+
const references = [];
|
|
82
|
+
lintLuaLoopClosures(ts, source, program.getTypeChecker(), (reference, message) => {
|
|
83
|
+
assert.equal(reference.getText(source), "index");
|
|
84
|
+
assert.match(message, /Lua 5\.1.*const declared inside the loop/);
|
|
85
|
+
references.push(source.getLineAndCharacterOfPosition(reference.getStart(source)).line + 1);
|
|
86
|
+
});
|
|
87
|
+
const expected = fixture.split("\n").flatMap((line, index) => line.endsWith("// error") ? [index + 1] : []);
|
|
88
|
+
assert.deepEqual(references.sort((a, b) => a - b), expected);
|
|
89
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Compare JavaScript truthiness with Lua's treatment of 0 and "" using the
|
|
2
|
+
// narrowed types from the caller's TypeScript program.
|
|
3
|
+
export function lintLuaTruthiness(ts, source, checker, report) {
|
|
4
|
+
const { NumberLike, StringLike, TypeParameter } = ts.TypeFlags;
|
|
5
|
+
|
|
6
|
+
function primitiveFlags(type) {
|
|
7
|
+
if (type.isUnionOrIntersection()) {
|
|
8
|
+
return type.types.reduce((flags, part) => flags | primitiveFlags(part), 0);
|
|
9
|
+
}
|
|
10
|
+
if (type.flags & TypeParameter) {
|
|
11
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
12
|
+
return constraint && constraint !== type ? primitiveFlags(constraint) : 0;
|
|
13
|
+
}
|
|
14
|
+
return type.flags & (NumberLike | StringLike);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function logicalOperator(node) {
|
|
18
|
+
if (!ts.isBinaryExpression(node)) return;
|
|
19
|
+
if (node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken) return "&&";
|
|
20
|
+
if (node.operatorToken.kind === ts.SyntaxKind.BarBarToken) return "||";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function check(node, context) {
|
|
24
|
+
while (ts.isParenthesizedExpression(node)) node = node.expression;
|
|
25
|
+
const operator = logicalOperator(node);
|
|
26
|
+
if (operator) {
|
|
27
|
+
// The visitor checks the left operand on its own. This result use
|
|
28
|
+
// additionally tests the right operand when short-circuiting allows it.
|
|
29
|
+
check(node.right, operator + " right operand");
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const type = checker.getTypeAtLocation(node);
|
|
34
|
+
const flags = primitiveFlags(type);
|
|
35
|
+
if (!flags) return;
|
|
36
|
+
const category = flags & NumberLike
|
|
37
|
+
? flags & StringLike ? "number|string" : "number"
|
|
38
|
+
: "string";
|
|
39
|
+
const narrowedType = checker.typeToString(type, node);
|
|
40
|
+
const message = "Lua truthiness in " + context + " for type " + narrowedType
|
|
41
|
+
+ ". Use an explicit comparison suited to the value type.";
|
|
42
|
+
report(node, message, { category, context, narrowedType });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function visit(node) {
|
|
46
|
+
if (ts.isTypeNode(node)) return;
|
|
47
|
+
if (ts.isIfStatement(node)) check(node.expression, "if condition");
|
|
48
|
+
if (ts.isWhileStatement(node)) check(node.expression, "while condition");
|
|
49
|
+
if (ts.isDoStatement(node)) check(node.expression, "do-while condition");
|
|
50
|
+
if (ts.isForStatement(node) && node.condition) check(node.condition, "for condition");
|
|
51
|
+
if (ts.isConditionalExpression(node)) check(node.condition, "ternary condition");
|
|
52
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.ExclamationToken) {
|
|
53
|
+
check(node.operand, "! operand");
|
|
54
|
+
}
|
|
55
|
+
const operator = logicalOperator(node);
|
|
56
|
+
if (operator) check(node.left, operator + " left operand");
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
visit(source);
|
|
61
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { lintLuaTruthiness } from "./lua-truthiness.mjs";
|
|
7
|
+
|
|
8
|
+
const ts = createRequire(import.meta.url)("typescript");
|
|
9
|
+
const normalize = file => resolve(file).replaceAll("\\", "/");
|
|
10
|
+
const path = normalize(fileURLToPath(new URL("./lua-truthiness-fixture.ts", import.meta.url)));
|
|
11
|
+
|
|
12
|
+
function diagnostics(input) {
|
|
13
|
+
const options = { strict: true, target: ts.ScriptTarget.ES2020, types: [] };
|
|
14
|
+
const host = ts.createCompilerHost(options);
|
|
15
|
+
const { readFile } = host;
|
|
16
|
+
host.readFile = file => normalize(file) === path ? input : readFile(file);
|
|
17
|
+
const program = ts.createProgram([path], options, host);
|
|
18
|
+
assert.deepEqual(ts.getPreEmitDiagnostics(program), []);
|
|
19
|
+
const source = program.getSourceFile(path);
|
|
20
|
+
const findings = [];
|
|
21
|
+
lintLuaTruthiness(ts, source, program.getTypeChecker(), (node, message, details) => {
|
|
22
|
+
assert.match(message, /explicit comparison suited to the value type/);
|
|
23
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
24
|
+
findings.push({
|
|
25
|
+
line: line + 1, column: character + 1, expression: node.getText(source), ...details,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
return findings.sort((a, b) => a.line - b.line || a.column - b.column);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("reports only operands used for truth testing, at the source expression", () => {
|
|
32
|
+
const input = [
|
|
33
|
+
"declare const n: number, s: string, bool: boolean, maybe: number | undefined;",
|
|
34
|
+
"if (n) {}",
|
|
35
|
+
"while ((s)) break;",
|
|
36
|
+
"do {} while (n);",
|
|
37
|
+
"for (; s; ) break;",
|
|
38
|
+
"const decision = n ? 'yes' : 'no';",
|
|
39
|
+
"const inverted = !s;",
|
|
40
|
+
"const fallback = n || s;",
|
|
41
|
+
"const value = bool && s;",
|
|
42
|
+
"if (bool && s) {}",
|
|
43
|
+
"if (n || s) {}",
|
|
44
|
+
"if ((n || s) && bool) {}",
|
|
45
|
+
"const initial = maybe ?? 0;",
|
|
46
|
+
"if (maybe ?? 0) {}",
|
|
47
|
+
"const nested = bool && (n || s);",
|
|
48
|
+
"const negated = !(bool || s);",
|
|
49
|
+
"for (;;) break;",
|
|
50
|
+
].join("\n");
|
|
51
|
+
assert.deepEqual(diagnostics(input).map(({ line, column, expression, context }) => [line, column, expression, context]), [
|
|
52
|
+
[2, 5, "n", "if condition"],
|
|
53
|
+
[3, 9, "s", "while condition"],
|
|
54
|
+
[4, 14, "n", "do-while condition"],
|
|
55
|
+
[5, 8, "s", "for condition"],
|
|
56
|
+
[6, 18, "n", "ternary condition"],
|
|
57
|
+
[7, 19, "s", "! operand"],
|
|
58
|
+
[8, 18, "n", "|| left operand"],
|
|
59
|
+
[10, 13, "s", "&& right operand"],
|
|
60
|
+
[11, 5, "n", "|| left operand"],
|
|
61
|
+
[11, 10, "s", "|| right operand"],
|
|
62
|
+
[12, 6, "n", "|| left operand"],
|
|
63
|
+
[12, 11, "s", "|| right operand"],
|
|
64
|
+
[14, 5, "maybe ?? 0", "if condition"],
|
|
65
|
+
[15, 25, "n", "|| left operand"],
|
|
66
|
+
[16, 27, "s", "|| right operand"],
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("uses narrowed types, nullable unions, literal members and constraints", () => {
|
|
71
|
+
const input = [
|
|
72
|
+
"type Branded = number & { readonly color: unique symbol };",
|
|
73
|
+
"type T = string;",
|
|
74
|
+
"declare let maybe: number | string | undefined;",
|
|
75
|
+
"declare const zero: 0, code: '', brand: Branded;",
|
|
76
|
+
"if (maybe) {}",
|
|
77
|
+
"if (typeof maybe === 'string' && maybe) {}",
|
|
78
|
+
"if (zero || code) {}",
|
|
79
|
+
"if (brand) {}",
|
|
80
|
+
"function boxed<T extends string | null>(value: T) { if (value) {} }",
|
|
81
|
+
"function pair<T extends number | string>(value: T) { if (value) {} }",
|
|
82
|
+
"function unconstrained<T>(value: T) { if (value) {} }",
|
|
83
|
+
"function shadow<T extends boolean>(value: T) { if (value) {} }",
|
|
84
|
+
].join("\n");
|
|
85
|
+
assert.deepEqual(diagnostics(input).map(({ line, expression, category, narrowedType }) => [line, expression, category, narrowedType]), [
|
|
86
|
+
[5, "maybe", "number|string", "string | number | undefined"],
|
|
87
|
+
[6, "maybe", "string", "string"],
|
|
88
|
+
[7, "zero", "number", "0"],
|
|
89
|
+
[7, "code", "string", "\"\""],
|
|
90
|
+
[8, "brand", "number", "Branded"],
|
|
91
|
+
[9, "value", "string", "T"],
|
|
92
|
+
[10, "value", "number|string", "T"],
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("leaves boolean, object, unknown, any, and type-only syntax alone", () => {
|
|
97
|
+
const input = [
|
|
98
|
+
"type Choose<T> = T extends string ? number : boolean;",
|
|
99
|
+
"type Secret<T> = { readonly opaque: T };",
|
|
100
|
+
"declare function isSecret(value: unknown): boolean;",
|
|
101
|
+
"declare const u: unknown, a: any, object: { some: number } | null;",
|
|
102
|
+
"declare let mixed: boolean | string;",
|
|
103
|
+
"declare const opaque: Secret<number> | undefined;",
|
|
104
|
+
"if (object && !isSecret(object.some)) {}",
|
|
105
|
+
"if (u || a) {}",
|
|
106
|
+
"if (opaque) {}",
|
|
107
|
+
"if (typeof mixed === 'boolean') { if (mixed) {} }",
|
|
108
|
+
"declare const value: number;",
|
|
109
|
+
"function shadow(value: boolean) { if (!value) {} }",
|
|
110
|
+
"const payload = object?.some ?? 0;",
|
|
111
|
+
].join("\n");
|
|
112
|
+
assert.deepEqual(diagnostics(input), []);
|
|
113
|
+
});
|