Skip to content

Official Codebase of 3DCodeVerse

Python

cleanup: C2 — languages/_js_lint.py

gaoypeng18b941f1cc67
diff --git a/harness/codeverse/languages/_js_lint.py b/harness/codeverse/languages/_js_lint.py
new file mode 100644
index 00000000..e87c33ff
--- /dev/null
+++ b/harness/codeverse/languages/_js_lint.py
@@ -0,0 +1,134 @@
+"""Shared node-side lint primitives for the JS authoring languages (threejs, scene_threejs).
+
+Replaces two copies of the same algorithm: ``threejs/lint.py:check_syntax`` +
+``_lint_imports`` and ``scene_threejs/lint.py:_node_check`` + ``_check_imports``.
+Both ran ``node --input-type=module --check`` over stdin and scraped the same
+``[stdin]:N`` / ``SyntaxError:`` lines; both matched static, re-export and dynamic
+import specifiers with near-identical regexes and applied the same allowlist
+(``three``, ``three/addons/*``, ``three/examples/jsm/*``, relative files under
+``src/``). They differed only in message text (owned by the caller via
+``make_finding``, as in :mod:`_ast_lint`) and in two defects this module fixes:
+threejs's fallback message was a ``list`` (``splitlines()[-1:]``), and scene's
+escape check was a ``str.startswith`` prefix test that let ``src2/`` pass.
+Escape is decided before existence so a symlink/``..`` hop out of ``src/`` is
+reported as an escape, never as "missing".
+"""
+
+from __future__ import annotations
+
+import re
+import subprocess
+from collections.abc import Callable
+from dataclasses import dataclass
+from enum import StrEnum
+from pathlib import Path
+
+from codeverse.contracts.artifacts import GateFinding
+
+# threejs's regexes (the superset: side-effect ``import './x.js'``, ``export * from``,
+# ``export * as ns from`` and multi-line ``import {\n a,\n b\n} from`` all match)
+_IMPORT_RE = re.compile(
+ r"""(?:^|\n)\s*(?:import\s+(?:[^'";]*?\s+from\s+)?|export\s+(?:\*(?:\s+as\s+\w+)?|\{[^}]*\})\s+from\s+)['"]([^'"]+)['"]"""
+)
+_DYN_IMPORT_RE = re.compile(r"""\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)""")
+_STDIN_LINE_RE = re.compile(r"\[stdin\]:(\d+)|:(\d+)\n")
+_URL_RE = re.compile(r"https?://")
+
+
+@dataclass(frozen=True)
+class SyntaxProblem:
+ """What ``node --check`` reported: ``line`` is None when it could not be parsed
+ (or node could not run), ``stderr_tail`` is the last 600 chars of stderr."""
+
+ line: int | None
+ message: str
+ stderr_tail: str
+
+
+def node_check_syntax(path: Path, node_bin: str) -> SyntaxProblem | None:
+ """``None`` when ``path`` parses as ESM under ``node --input-type=module --check``.
+
+ Stdin is used because ``node --check file.js`` treats a bare ``.js`` as
+ CommonJS-or-detect.
+ """
+ try:
+ proc = subprocess.run(
+ [node_bin, "--input-type=module", "--check"],
+ input=path.read_bytes(), capture_output=True, timeout=30,
+ )
+ except (OSError, subprocess.TimeoutExpired) as e:
+ return SyntaxProblem(None, f"node --check could not run: {e}", "")
+ if proc.returncode == 0:
+ return None
+ err = proc.stderr.decode(errors="replace").strip()
+ m = _STDIN_LINE_RE.search(err)
+ line = int(m.group(1) or m.group(2)) if m else None
+ lines = err.splitlines()
+ msg = next((ln for ln in lines if "Error" in ln), lines[0] if lines else "syntax error")
+ return SyntaxProblem(line, msg, err[-600:])
+
+
+def import_specs(src: str) -> list[tuple[str, int]]:
+ """``(specifier, 1-based line)`` for every static, re-export and dynamic import in ``src``."""
+ out = [(m.group(1), m.start(1)) for m in _IMPORT_RE.finditer(src)]
+ out += [(m.group(1), m.start(1)) for m in _DYN_IMPORT_RE.finditer(src)]
+ return [(spec, src.count("\n", 0, pos) + 1) for spec, pos in out]
+
+
+class ImportKind(StrEnum):
+ ALLOWED = "allowed" # 'three', 'three/addons/*', 'three/examples/jsm/*', or an existing file under src/
+ ESCAPES = "escapes" # relative path resolves outside src/
+ MISSING = "missing" # relative path stays under src/ but is not a file
+ ABSOLUTE = "absolute" # '/x.js'
+ URL = "url" # http(s)://...
+ PACKAGE = "package" # any other bare specifier (npm package)
+
+
+@dataclass(frozen=True)
+class ImportVerdict:
+ kind: ImportKind
+ #: resolved relative target (set for the three relative kinds), else None
+ target: Path | None = None
+
+
+def classify_import(spec: str, path: Path, src_root: Path) -> ImportVerdict:
+ """Apply the JS import policy to one specifier found in ``path`` (a file under ``src_root``)."""
+ if spec == "three" or spec.startswith("three/addons/") or spec.startswith("three/examples/jsm/"):
+ return ImportVerdict(ImportKind.ALLOWED)
+ if spec.startswith("./") or spec.startswith("../"):
+ target = (path.parent / spec).resolve()
+ if not target.is_relative_to(src_root.resolve()):
+ return ImportVerdict(ImportKind.ESCAPES, target)
+ if not target.is_file():
+ return ImportVerdict(ImportKind.MISSING, target)
+ return ImportVerdict(ImportKind.ALLOWED, target)
+ if spec.startswith("/"):
+ return ImportVerdict(ImportKind.ABSOLUTE)
+ if _URL_RE.match(spec):
+ return ImportVerdict(ImportKind.URL)
+ return ImportVerdict(ImportKind.PACKAGE)
+
+
+def check_imports(
+ src: str,
+ path: Path,
+ src_root: Path,
+ *,
+ make_finding: Callable[[ImportVerdict, str, int], GateFinding],
+) -> tuple[list[GateFinding], set[Path]]:
+ """Classify every import in ``src``; return the findings for the non-allowed ones
+ and the set of resolved relative files it imports.
+
+ ``make_finding(verdict, spec, line)`` builds the finding — the caller owns
+ message, severity and hint text.
+ """
+ findings: list[GateFinding] = []
+ targets: set[Path] = set()
+ for spec, line in import_specs(src):
+ verdict = classify_import(spec, path, src_root)
+ if verdict.kind is ImportKind.ALLOWED:
+ if verdict.target is not None:
+ targets.add(verdict.target)
+ else:
+ findings.append(make_finding(verdict, spec, line))
+ return findings, targets
diff --git a/harness/codeverse/languages/scene_threejs/lint.py b/harness/codeverse/languages/scene_threejs/lint.py
index c007e7ce..a4af0193 100644
--- a/harness/codeverse/languages/scene_threejs/lint.py
+++ b/harness/codeverse/languages/scene_threejs/lint.py
@@ -1,26 +1,26 @@
"""Static lint for ``scene_threejs`` workspaces (no browser).
Checks: ``node --check`` syntax per file, import allowlist ('three',
-'three/addons/*', relative), forbidden network/DOM/render-loop usage, required
-exports (``createScene`` in src/scene.js, ``build`` in zones), removed three.js
-APIs, oversized files. Returns a GateReport ``lint:scene_threejs``.
+'three/addons/*', relative — side-effect ``import './x.js'`` is matched too, so a
+bare import of a missing file is flagged), forbidden network/DOM/render-loop
+usage, required exports (``createScene`` in src/scene.js, ``build`` in zones),
+removed three.js APIs, oversized files. Returns a GateReport ``lint:scene_threejs``.
"""
from __future__ import annotations
import re
-import subprocess
import time
from pathlib import Path
from codeverse.config import get_settings
from codeverse.contracts.artifacts import GateFinding, GateReport, Severity
+from codeverse.languages._js_lint import ImportKind, ImportVerdict, check_imports, node_check_syntax
from codeverse.workspace import Workspace
GATE = "lint:scene_threejs"
MAX_LINES = 800
-_IMPORT_RE = re.compile(r"""(?:^|\n)\s*(?:import|export)\s[^;'"\n]*?from\s*['"]([^'"]+)['"]|import\s*\(\s*['"]([^'"]+)['"]\s*\)""")
_CREATE_SCENE_RE = re.compile(r"export\s+(?:async\s+)?function\s+createScene\b|export\s+(?:const|let)\s+createScene\b|export\s*\{[^}]*\bcreateScene\b[^}]*\}")
_BUILD_RE = re.compile(r"export\s+(?:async\s+)?function\s+build\b|export\s+(?:const|let)\s+build\b|export\s*\{[^}]*\bbuild\b[^}]*\}")
_BUILD_ANY_RE = re.compile(r"export\s+(?:async\s+)?function\s+build[A-Za-z0-9_]*\b|export\s+(?:const|let)\s+build[A-Za-z0-9_]*\b")
@@ -53,47 +53,27 @@ def _js_files(ws: Workspace) -> list[Path]:
def _node_check(path: Path, rel: str) -> GateFinding | None:
- node = get_settings().binaries.node or "node"
- try:
- # ESM syntax check via stdin: `node --check file.js` treats a bare .js as CommonJS-or-detect
- proc = subprocess.run([node, "--input-type=module", "--check"], input=path.read_text(errors="replace"),
- capture_output=True, text=True, timeout=30)
- except (OSError, subprocess.TimeoutExpired) as e:
- return _f(Severity.ERROR, f"node --check could not run: {e}", target=rel, hint="harness problem (node missing?)")
- if proc.returncode == 0:
+ p = node_check_syntax(path, get_settings().binaries.node or "node")
+ if p is None:
return None
- err = proc.stderr.strip()
- line = None
- m = re.search(r"\[stdin\]:(\d+)", err) or re.search(r":(\d+)\n", err)
- if m:
- line = int(m.group(1))
- msg = next((ln for ln in err.splitlines() if "Error" in ln), err.splitlines()[0] if err else "syntax error")
- return _f(Severity.ERROR, f"syntax: {msg}", target=f"{rel}:{line}" if line else rel,
- hint="fix the syntax error at the quoted line (node --check)", line=line, detail=err[-600:])
+ return _f(Severity.ERROR, f"syntax: {p.message}", target=f"{rel}:{p.line}" if p.line else rel,
+ hint="fix the syntax error at the quoted line (node --check)", line=p.line, detail=p.stderr_tail)
def _check_imports(rel: str, text: str, ws: Workspace, path: Path) -> list[GateFinding]:
- out: list[GateFinding] = []
- for m in _IMPORT_RE.finditer(text):
- spec = m.group(1) or m.group(2)
- line = text.count("\n", 0, m.start()) + 1
+ def make_finding(v: ImportVerdict, spec: str, line: int) -> GateFinding:
tgt = f"{rel}:{line}"
- if spec == "three" or spec.startswith("three/addons/") or spec.startswith("three/examples/jsm/"):
- continue
- if spec.startswith("./") or spec.startswith("../"):
- dest = (path.parent / spec).resolve()
- if not dest.is_file():
- out.append(_f(Severity.ERROR, f"import of missing file '{spec}'", target=tgt,
- hint="create the file or fix the path (relative imports need the .js extension)"))
- elif not str(dest).startswith(str(ws.src.resolve())):
- out.append(_f(Severity.ERROR, f"import '{spec}' escapes src/", target=tgt, hint="keep all code under src/"))
- continue
- if spec.startswith("/"):
- out.append(_f(Severity.ERROR, f"absolute import '{spec}'", target=tgt, hint="use relative imports ('./x.js')"))
- continue
- out.append(_f(Severity.ERROR, f"import '{spec}' is not allowed (only 'three', 'three/addons/*', relative files)", target=tgt,
- hint="no CDN / npm packages; write the helper yourself in src/"))
- return out
+ if v.kind is ImportKind.ESCAPES:
+ return _f(Severity.ERROR, f"import '{spec}' escapes src/", target=tgt, hint="keep all code under src/")
+ if v.kind is ImportKind.MISSING:
+ return _f(Severity.ERROR, f"import of missing file '{spec}'", target=tgt,
+ hint="create the file or fix the path (relative imports need the .js extension)")
+ if v.kind is ImportKind.ABSOLUTE:
+ return _f(Severity.ERROR, f"absolute import '{spec}'", target=tgt, hint="use relative imports ('./x.js')")
+ return _f(Severity.ERROR, f"import '{spec}' is not allowed (only 'three', 'three/addons/*', relative files)", target=tgt,
+ hint="no CDN / npm packages; write the helper yourself in src/")
+
+ return check_imports(text, path, ws.src, make_finding=make_finding)[0]
def lint(ws: Workspace) -> GateReport:
diff --git a/harness/codeverse/languages/threejs/lint.py b/harness/codeverse/languages/threejs/lint.py
index 4fbb6220..6d27d5e7 100644
--- a/harness/codeverse/languages/threejs/lint.py
+++ b/harness/codeverse/languages/threejs/lint.py
@@ -9,7 +9,6 @@ that every planned part has a file and is assembled by ``object.js``.
from __future__ import annotations
import re
-import subprocess
import time
from pathlib import Path
@@ -17,15 +16,15 @@ from codeverse.config import get_settings
from codeverse.contracts.artifacts import GateFinding, GateReport, Severity
from codeverse.contracts.plan import StaticPlan
from codeverse.conventions import to_pascal, to_snake
+from codeverse.languages._js_lint import ImportKind, ImportVerdict, check_imports
+from codeverse.languages._js_lint import (
+ node_check_syntax as check_syntax, # module-level name: tests monkeypatch it
+)
from codeverse.proc import read_json_or_none
from codeverse.workspace import Workspace
GATE = "lint:threejs"
-_IMPORT_RE = re.compile(
- r"""(?:^|\n)\s*(?:import\s+(?:[^'";]*?\s+from\s+)?|export\s+(?:\*|\{[^}]*\})\s+from\s+)['"]([^'"]+)['"]"""
-)
-_DYN_IMPORT_RE = re.compile(r"""\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)""")
_EXPORT_BUILD_RE = re.compile(r"\bexport\s+(?:async\s+)?function\s+(build[A-Za-z0-9_]*)\s*\(")
_EXPORT_CONST_BUILD_RE = re.compile(r"\bexport\s+(?:const|let|var)\s+(build[A-Za-z0-9_]*)\s*=")
_EXPORT_LIST_RE = re.compile(r"\bexport\s*\{([^}]*)\}")
@@ -81,49 +80,26 @@ def list_sources(ws: Workspace) -> list[Path]:
return sorted(p for p in ws.src.rglob("*.js") if "node_modules" not in p.parts and not p.name.startswith("."))
-def check_syntax(path: Path, node_bin: str) -> tuple[int | None, str] | None:
- """``None`` when the file parses as ESM, else ``(line, message)``."""
- try:
- proc = subprocess.run(
- [node_bin, "--input-type=module", "--check"],
- input=path.read_bytes(), capture_output=True, timeout=30,
- )
- except (OSError, subprocess.TimeoutExpired) as e:
- return (None, f"syntax check could not run: {e}")
- if proc.returncode == 0:
- return None
- err = proc.stderr.decode(errors="replace")
- m = re.search(r"\[stdin\]:(\d+)", err)
- msg = re.search(r"^(SyntaxError:.*)$", err, re.M)
- return (int(m.group(1)) if m else None, msg.group(1) if msg else err.strip().splitlines()[-1:] or "syntax error")
-
-
def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFinding]) -> set[Path]:
"""Validate import specifiers; return the set of resolved relative targets."""
- targets: set[Path] = set()
- specs = [(m.group(1), m.start(1)) for m in _IMPORT_RE.finditer(src)]
- specs += [(m.group(1), m.start(1)) for m in _DYN_IMPORT_RE.finditer(src)]
rel = _rel(ws, path)
- for spec, pos in specs:
- line = _line_of(src, pos)
- if spec == "three" or spec.startswith("three/addons/") or spec.startswith("three/examples/jsm/"):
- continue
- if spec.startswith("./") or spec.startswith("../"):
- target = (path.parent / spec).resolve()
- if not target.is_relative_to(ws.src.resolve()):
- findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
- message=f"{rel}:{line}: import '{spec}' escapes src/", fix_hint="keep all files under src/"))
- elif not target.is_file():
- findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
- message=f"{rel}:{line}: imported file does not exist: '{spec}'",
- fix_hint=f"create {_rel(ws, target)} or fix the path (extension '.js' is required)"))
- else:
- targets.add(target)
- continue
- kind = "URL" if re.match(r"https?://", spec) else "package"
- findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
+
+ def make_finding(v: ImportVerdict, spec: str, line: int) -> GateFinding:
+ if v.kind is ImportKind.ESCAPES:
+ return GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
+ message=f"{rel}:{line}: import '{spec}' escapes src/", fix_hint="keep all files under src/")
+ if v.kind is ImportKind.MISSING:
+ assert v.target is not None
+ return GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
+ message=f"{rel}:{line}: imported file does not exist: '{spec}'",
+ fix_hint=f"create {_rel(ws, v.target)} or fix the path (extension '.js' is required)")
+ kind = "URL" if v.kind is ImportKind.URL else "package"
+ return GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
message=f"{rel}:{line}: import of {kind} '{spec}' is not allowed",
- fix_hint="only 'three', 'three/addons/...' and relative './' imports are available"))
+ fix_hint="only 'three', 'three/addons/...' and relative './' imports are available")
+
+ new, targets = check_imports(src, path, ws.src, make_finding=make_finding)
+ findings.extend(new)
return targets
@@ -179,9 +155,8 @@ def lint_workspace(ws: Workspace) -> GateReport:
rel = _rel(ws, path)
syn = check_syntax(path, node_bin)
if syn is not None:
- line, msg = syn
findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,
- message=f"{rel}:{line or '?'}: {msg}", fix_hint="fix the syntax error at that line"))
+ message=f"{rel}:{syn.line or '?'}: {syn.message}", fix_hint="fix the syntax error at that line"))
continue # other checks are noise on a file that does not parse
imported |= _lint_imports(ws, path, src, findings)
_lint_forbidden(ws, path, src, findings)