Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

build(checks): the inspector may not name a component it renders

yhhbbb77a86dbda
diff --git a/package.json b/package.json
index 3fb0caa98..81d821cb3 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,7 @@
"emsdk:setup": "node build-tools/toolchain/setup-emsdk.js",
"asset-meta": "node tools/asset-meta.js",
"migrate-asset-refs": "node tools/migrate-asset-refs.js",
- "verify": "pnpm --filter ./sdk exec tsc --noEmit && node tools/api-surface.mjs --check && node tools/check-cycles.mjs && node tools/check-layers.mjs && node tools/check-project-settings.mjs && node tools/check-workflows.mjs && node tools/check-tool-calls.mjs && node tools/component-reference.mjs --check",
+ "verify": "pnpm --filter ./sdk exec tsc --noEmit && node tools/api-surface.mjs --check && node tools/check-cycles.mjs && node tools/check-layers.mjs && node tools/check-project-settings.mjs && node tools/check-workflows.mjs && node tools/check-tool-calls.mjs && node tools/check-inspector-door.mjs && node tools/component-reference.mjs --check",
"perf": "node tools/perf-guard.mjs --check",
"prepare": "git config core.hooksPath .githooks || true"
},
diff --git a/tools/check-inspector-door.mjs b/tools/check-inspector-door.mjs
new file mode 100644
index 000000000..4075f08e6
--- /dev/null
+++ b/tools/check-inspector-door.mjs
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file check-inspector-door.mjs — per-component inspector UI has one door.
+ *
+ * Most of the Details panel is derived: the component registry says a field is a
+ * vec2 or an enum, and a control renders it by TYPE. A few components need more
+ * than their fields — an anchor grid, a shape switch, a "put me under a Canvas"
+ * button. That extra used to be added two different ways: plugins registered it,
+ * and the editor's own was a chain of `comp.name === 'UINode' ? … :
+ * comp.name === 'FlexContainer' ? …` in the panel body, sitting directly above
+ * the element that renders the plugin half.
+ *
+ * Two ways to say one thing is how they came to disagree. The panel has TWO
+ * bodies — the edit inspector and the live Game inspector — each grew its own
+ * chain, and the Game one ended up running an edit-model action against a realm
+ * runtime id, an id space its own header calls "never mixed". Nothing failed;
+ * the button was simply wired to the surface where its argument means something
+ * else, and missing from the one where it works.
+ *
+ * So: naming a built-in component in the render path is the violation. The extra
+ * goes on `componentDecorators.tsx`, which is the table — the one file where
+ * naming a component is the entire point, and the only exemption here.
+ *
+ * DELIBERATELY NARROW, on the evidence of check-layers: a rule that is wrong even
+ * occasionally teaches everyone to route around it. It flags a component name as
+ * a STRING LITERAL, nothing else. It does not police `.name` comparisons in
+ * general (a panel id, an asset type and a menu entry all have names), does not
+ * read intent, and does not care where in the file the literal sits. The names
+ * come from the same source-only enumerator the component reference uses, so a
+ * component cannot be known to one check and invisible to the other.
+ */
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { dirname, join, resolve, relative } from 'node:path';
+import { componentNamesFromSource } from './lib/componentNames.mjs';
+
+const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
+
+/** The inspector's render path: what draws an entity's components. */
+const GUARDED = [
+ join(ROOT, 'desktop', 'src', 'panels', 'Details.tsx'),
+ join(ROOT, 'desktop', 'src', 'panels', 'inspector', 'controls.tsx'),
+];
+
+/** Where naming a component IS the point — the decorator table itself. */
+const EXEMPT = join(ROOT, 'desktop', 'src', 'panels', 'inspector', 'componentDecorators.tsx');
+
+// A guard whose subject moved is a guard that protects nothing while reporting
+// success — this repo has shipped that twice. Fail loudly instead.
+for (const f of [...GUARDED, EXEMPT]) {
+ try {
+ readFileSync(f, 'utf8');
+ } catch {
+ console.error(`check-inspector-door: STALE — ${relative(ROOT, f)} does not exist.`);
+ console.error(' The inspector moved. Update GUARDED/EXEMPT to name the files that render it.');
+ process.exit(1);
+ }
+}
+
+const stripComments = (s) => s.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^[ \t]*\/\/.*$/gm, '');
+
+const names = componentNamesFromSource();
+if (names.length < 20) {
+ console.error(`check-inspector-door: STALE — only ${names.length} component names found; the enumerator no longer matches the SDK.`);
+ process.exit(1);
+}
+const nameSet = new Set(names);
+
+const violations = [];
+for (const file of GUARDED) {
+ const lines = stripComments(readFileSync(file, 'utf8')).split(/\r?\n/);
+ lines.forEach((line, i) => {
+ for (const m of line.matchAll(/'([A-Za-z0-9_]+)'/g)) {
+ if (nameSet.has(m[1])) violations.push({ file, line: i + 1, name: m[1], text: line.trim() });
+ }
+ });
+}
+
+if (violations.length) {
+ console.error(`check-inspector-door: ${violations.length} component name(s) written into the inspector's render path`);
+ for (const v of violations) {
+ console.error(` ${relative(ROOT, v.file)}:${v.line} '${v.name}'`);
+ console.error(` ${v.text.slice(0, 110)}`);
+ }
+ console.error('\n Per-component UI is a decorator. Register it in');
+ console.error(` ${relative(ROOT, EXEMPT)} under owner 'core' — a render body, the`);
+ console.error(' field keys it owns, and/or a header action — instead of branching here.');
+ process.exit(1);
+}
+
+console.log(`check-inspector-door: the inspector names none of the ${names.length} components it renders.`);
diff --git a/tools/component-reference.mjs b/tools/component-reference.mjs
index a34fa3d3b..a22637bbd 100644
--- a/tools/component-reference.mjs
+++ b/tools/component-reference.mjs
@@ -32,6 +32,7 @@
import { readFileSync, writeFileSync, readdirSync, existsSync } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join, resolve } from 'node:path';
+import { componentNamesFromSource } from './lib/componentNames.mjs';
const here = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(here, '..');
@@ -51,44 +52,10 @@ if (mode !== '--check' && mode !== '--update') {
// ── enumerating the components ───────────────────────────────────────────────
-function tsFiles(dir) {
- const out = [];
- for (const e of readdirSync(dir, { withFileTypes: true })) {
- const p = join(dir, e.name);
- if (e.isDirectory()) out.push(...tsFiles(p));
- else if (e.name.endsWith('.ts') && !e.name.endsWith('.test.ts') && !e.name.endsWith('.d.ts')) out.push(p);
- }
- return out;
-}
-
-/** Comments hold example code (`defineComponent('Patrol', …)`) that would
- * otherwise register as a component that does not exist. */
-const stripComments = (s) => s.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^[ \t]*\/\/.*$/gm, '');
-
-/**
- * Component names, from source alone — no SDK build required.
- *
- * Matches the assignment, not the bare call: every real registration is
- * `export const X = defineComponent(…)`, while the strings that merely *mention*
- * the call are prose — a `defineComponent('MyThing', …)` inside an error message
- * teaching the argument order is not a component, and comment-stripping alone
- * does not reach it.
- */
-function namesFromSource() {
- const names = new Set();
- for (const f of tsFiles(SDK_SRC)) {
- const src = stripComments(readFileSync(f, 'utf8'));
- for (const m of src.matchAll(/=\s*define(?:Component|Builtin|Tag)(?:<[^>]*>)?\(\s*'([A-Za-z0-9_]+)'/g)) {
- names.add(m[1]);
- }
- }
- // ensureBuiltinComponentsRegistered() registers every COMPONENT_META key,
- // typed const or not, so the generated metadata is part of the set.
- const gen = readFileSync(join(SDK_SRC, 'ecs', 'component.generated.ts'), 'utf8');
- const meta = gen.slice(gen.indexOf('export const COMPONENT_META'));
- for (const m of meta.matchAll(/^ {4}([A-Za-z0-9_]+): \{$/gm)) names.add(m[1]);
- return [...names].sort();
-}
+// The enumerator is shared with check-inspector-door, which refuses a component
+// name written as a literal in the Details panel's render path. One definition,
+// so a component cannot be known to one check and invisible to the other.
+const namesFromSource = componentNamesFromSource;
// ── describing one component ─────────────────────────────────────────────────
diff --git a/tools/lib/componentNames.mjs b/tools/lib/componentNames.mjs
new file mode 100644
index 000000000..377c086de
--- /dev/null
+++ b/tools/lib/componentNames.mjs
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file componentNames.mjs — the engine's component names, from source alone.
+ *
+ * No SDK build required, which is the whole point: the checks that read this run
+ * in seconds on every push, before anything is compiled.
+ *
+ * Two tools need the list and must not each carry their own idea of it. The
+ * component reference derives a documentation page per component; the inspector
+ * door check refuses a component name written as a literal in the panel's render
+ * path. A list that drifts between them fails in the direction that hurts —
+ * silently passing a component neither one knows about.
+ */
+import { readFileSync, readdirSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { dirname, join, resolve } from 'node:path';
+
+const SDK_SRC = join(resolve(dirname(fileURLToPath(import.meta.url)), '..', '..'), 'sdk', 'src');
+
+function tsFiles(dir) {
+ const out = [];
+ for (const e of readdirSync(dir, { withFileTypes: true })) {
+ const p = join(dir, e.name);
+ if (e.isDirectory()) out.push(...tsFiles(p));
+ else if (e.name.endsWith('.ts') && !e.name.endsWith('.test.ts') && !e.name.endsWith('.d.ts')) out.push(p);
+ }
+ return out;
+}
+
+/** Comments hold example code (`defineComponent('Patrol', …)`) that would
+ * otherwise register as a component that does not exist. */
+const stripComments = (s) => s.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^[ \t]*\/\/.*$/gm, '');
+
+/**
+ * Component names, from source alone — no SDK build required.
+ *
+ * Matches the assignment, not the bare call: every real registration is
+ * `export const X = defineComponent(…)`, while the strings that merely *mention*
+ * the call are prose — a `defineComponent('MyThing', …)` inside an error message
+ * teaching the argument order is not a component, and comment-stripping alone
+ * does not reach it.
+ */
+export function componentNamesFromSource() {
+ const names = new Set();
+ for (const f of tsFiles(SDK_SRC)) {
+ const src = stripComments(readFileSync(f, 'utf8'));
+ for (const m of src.matchAll(/=\s*define(?:Component|Builtin|Tag)(?:<[^>]*>)?\(\s*'([A-Za-z0-9_]+)'/g)) {
+ names.add(m[1]);
+ }
+ }
+ // ensureBuiltinComponentsRegistered() registers every COMPONENT_META key,
+ // typed const or not, so the generated metadata is part of the set.
+ const gen = readFileSync(join(SDK_SRC, 'ecs', 'component.generated.ts'), 'utf8');
+ const meta = gen.slice(gen.indexOf('export const COMPONENT_META'));
+ for (const m of meta.matchAll(/^ {4}([A-Za-z0-9_]+): \{$/gm)) names.add(m[1]);
+ return [...names].sort();
+}