Skip to content

Official Codebase of 3DCodeVerse

Python

js lint: scene_threejs's forbidden-token patterns read code, not comments, through the shared strip_js

gaoypeng233c99c02457
diff --git a/harness/codeverse3d/languages/_js_lint.py b/harness/codeverse3d/languages/_js_lint.py
index 5ce5ab99..0b5b9ad7 100644
--- a/harness/codeverse3d/languages/_js_lint.py
+++ b/harness/codeverse3d/languages/_js_lint.py
@@ -36,6 +36,22 @@ _IMPORT_RE = re.compile(
)
_DYN_IMPORT_RE = re.compile(r"""\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)""")
_URL_RE = re.compile(r"https?://")
+# strings and comments in ONE alternation so a '//' inside a string is not a comment
+_TOKEN_RE = re.compile(
+ r"""'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"|`(?:\\.|[^`\\])*`|//[^\n]*|/\*.*?\*/""", re.S
+)
+
+
+def strip_js(src: str, *, strings: bool = True) -> str:
+ """Blank out comments (and, with ``strings``, string contents), keeping newlines so line
+ numbers survive: the forbidden-token lints read code, never a comment quoting the contract.
+ scene_threejs keeps strings (its network rule matches URL literals)."""
+ def repl(m: re.Match[str]) -> str:
+ tok = m.group(0)
+ if not tok.startswith("/"):
+ return '""' + "\n" * tok.count("\n") if strings else tok
+ return "\n" * tok.count("\n")
+ return _TOKEN_RE.sub(repl, src)
@dataclass(frozen=True)
diff --git a/harness/codeverse3d/languages/scene_threejs/__init__.py b/harness/codeverse3d/languages/scene_threejs/__init__.py
index 832dfc20..d34dc4fb 100644
--- a/harness/codeverse3d/languages/scene_threejs/__init__.py
+++ b/harness/codeverse3d/languages/scene_threejs/__init__.py
@@ -27,6 +27,7 @@ from codeverse3d.languages._js_lint import (
check_imports,
js_sources,
node_check_syntax,
+ strip_js,
)
from codeverse3d.workspace import Workspace
@@ -115,10 +116,11 @@ def lint(ws: Workspace) -> GateReport:
if _is_lib(path, ws):
continue
findings.extend(_check_imports(rel, text, ws, path))
+ code = strip_js(text, strings=False) # a comment quoting the contract is not a call
for pat, sev, msg, hint in _PATTERNS:
- m = pat.search(text)
+ m = pat.search(code)
if m:
- line = line_of(text, m.start())
+ line = line_of(code, m.start())
findings.append(_f(sev, msg, target=f"{rel}:{line}", hint=hint))
n_lines = text.count("\n") + 1
if n_lines > MAX_LINES:
diff --git a/harness/codeverse3d/languages/threejs/__init__.py b/harness/codeverse3d/languages/threejs/__init__.py
index 56e224e2..f5ab0e70 100644
--- a/harness/codeverse3d/languages/threejs/__init__.py
+++ b/harness/codeverse3d/languages/threejs/__init__.py
@@ -18,6 +18,7 @@ from codeverse3d.languages._js_lint import (
ImportVerdict,
check_imports,
js_sources,
+ strip_js,
)
from codeverse3d.languages._js_lint import (
node_check_syntax as check_syntax, # module-level name: tests monkeypatch it
@@ -98,11 +99,6 @@ GATE = "lint:threejs"
_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*\{([^}]*)\}")
-# strings and comments in ONE alternation so a '//' inside a string is not a comment
-_TOKEN_RE = re.compile(
- r"""'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"|`(?:\\.|[^`\\])*`|//[^\n]*|/\*.*?\*/""", re.S
-)
-
# token -> (severity, message, fix hint)
_FORBIDDEN: dict[str, tuple[Severity, str, str]] = {
r"\bfetch\s*\(": (Severity.ERROR, "network access (fetch) is not allowed in object code", "build geometry procedurally; no downloads"),
@@ -124,15 +120,6 @@ _FORBIDDEN: dict[str, tuple[Severity, str, str]] = {
}
-def _strip(src: str) -> str:
- """Blank out comments and string contents (keeps newlines so line numbers survive)."""
- def repl(m: re.Match[str]) -> str:
- tok = m.group(0)
- nl = "\n" * tok.count("\n")
- return nl if tok.startswith("/") else '""' + nl
- return _TOKEN_RE.sub(repl, src)
-
-
def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFinding]) -> set[Path]:
"""Validate import specifiers; return the set of resolved relative targets."""
rel = ws_rel(ws, path)
@@ -157,7 +144,7 @@ def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFindin
def _lint_forbidden(ws: Workspace, path: Path, src: str, findings: list[GateFinding]) -> None:
- stripped = _strip(src)
+ stripped = strip_js(src)
rel = ws_rel(ws, path)
for pattern, (sev, msg, hint) in _FORBIDDEN.items():
m = re.search(pattern, stripped)
diff --git a/harness/tests/scene_runtime/test_lint.py b/harness/tests/scene_runtime/test_lint.py
index 77ed66f9..d3c3f3bc 100644
--- a/harness/tests/scene_runtime/test_lint.py
+++ b/harness/tests/scene_runtime/test_lint.py
@@ -50,6 +50,13 @@ def test_forbidden_imports_and_globals(ws):
assert "BufferGeometry aliases" in text
# targets carry file:line
assert any(t and t.startswith("src/scene.js:") for t, _ in errs)
+ # a comment quoting the contract is not a call (audit 2026-09-24 N20, as three.js's lint);
+ # a URL string literal still is network access
+ (ws.src / "quoted.js").write_text(
+ "// never call renderer.render( or requestAnimationFrame( here: 'https://cdn.x/three.js'\n"
+ "/* new THREE.WebGLRenderer() */ export const url = 'https://cdn.x/tree.glb';\n")
+ quoted = [(t, m) for t, m in _msgs(lint(ws), Severity.ERROR) if t and t.startswith("src/quoted.js")]
+ assert quoted == [("src/quoted.js:2", "network / CDN access")], quoted
def test_syntax_error_maps_to_line(ws):