Skip to content

The agent that grows with you

fix(plugins): ternary destructuring rebinds; ambiguous slashes never hide code

Teknium1cf3ac3cb038
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.py
index 4f39e86782a..f06b7a8a62c 100644
--- a/hermes_cli/plugin_validate_desktop.py
+++ b/hermes_cli/plugin_validate_desktop.py
@@ -108,7 +108,7 @@ _REBINDING = (
r"|\b(?:let|const|var|class|function\s*\*?)\s+{0}(?![\w$])"
# a PATTERN opens where an expression cannot (not ``obj[`` / ``f()[``), names ``X`` itself (not
# ``X.name``), and is assigned or iterated
- r"|(?:^|(?<=[;(,{{}}=\n])|\b(?:let|const|var))\s*[\[{{][^;=()]*?(?<![\w$.]){0}(?![\w$.])[^;=()]*?[}}\]][\s}}\]]*"
+ r"|(?:^|(?<=[;(,{{}}=?:&|!\n])|\b(?:let|const|var|return|yield|void))\s*[\[{{][^;=()]*?(?<![\w$.]){0}(?![\w$.])[^;=()]*?[}}\]][\s}}\]]*"
r"(?:=(?![=>])|\b(?:of|in)\b)"
r"|(?:\bfunction\s*\*?\s*[\w$]*|(?<![\w$.])(?!(?:if|for|while|switch|with|return|typeof|await)\b)[\w$]+)"
r"\s*\([^()]*?(?<![\w$.]){0}(?![\w$])[^()]*\)\s*\{{"
@@ -120,27 +120,12 @@ _REBINDING = (
_REGEX_AFTER_WORD = re.compile(r"(?:^|[^\w$.])(?:return|typeof|case|do|else|in|of|void|delete|throw|new|yield|await)\s*$")
-def _after_control_condition(source: str, close: int) -> bool:
- """The ``)`` at *close* ends an ``if``/``while``/``for``/``with`` condition (a statement may follow)."""
- depth = 0
- for k in range(close, -1, -1):
- if source[k] == ")":
- depth += 1
- elif source[k] == "(":
- depth -= 1
- if depth == 0:
- return bool(re.search(r"(?:^|[^\w$.])(?:if|while|for|with)\s*$", source[max(0, k - 12):k]))
- return False
-
-
def _regex_may_start(source: str, at: int) -> bool:
j = at - 1
while j >= 0 and source[j] in " \t\r\n":
j -= 1
if j < 0:
return True
- if source[j] == ")":
- return _after_control_condition(source, j)
if source[j] in "(,=:[!&|?{};+-*%<>~^":
return not source.startswith("/", at + 1) and not (source[j] in "+-" and j > 0 and source[j - 1] == source[j])
return bool(_REGEX_AFTER_WORD.search(source, max(0, j - 12), j + 1))
@@ -168,8 +153,11 @@ def _code_view(source: str) -> str:
"""*source* with the text of quoted strings, template literals and regex literals blanked, same
length. Only text that can never run is blanked: a template's ``${…}`` stays code, and a ``/``
starts a regex only where an expression cannot end before it (after an operator, ``(``, ``,``,
- a keyword such as ``return``, or the ``)`` of an ``if``/``while``/``for`` condition); after a
- name, number, other ``)`` or ``]`` it is division."""
+ a keyword such as ``return``); after a name, number, ``)`` or ``]`` it is division.
+
+ Every ambiguity resolves toward NOT blanking: a guess that wrongly reads code as literal text
+ hides a patch, while one that reads text as code only refuses a bundle for review. So a regex
+ statement after ``if (…)`` is scanned as code (deciding needs a parser)."""
out = list(source)
def blank(i: int) -> None:
diff --git a/tests/hermes_cli/test_plugin_validate.py b/tests/hermes_cli/test_plugin_validate.py
index 840af4afe9a..5641f9db3a7 100644
--- a/tests/hermes_cli/test_plugin_validate.py
+++ b/tests/hermes_cli/test_plugin_validate.py
@@ -353,8 +353,8 @@ class TestDesktopSurface:
also declares as a class, including a minifier-reused one-letter name, and a parameter
shadowing the class, a destructuring assignment, and writes hidden in a template ``${}`` or
between divisions. Passing the class around (``register(Q)``, ``{ Array: Q }``,
- ``registry[F.name] = F``) or text in a string, template or regex literal (including a regex
- statement after ``if (…)``) is not a rebinding."""
+ ``registry[F.name] = F``) or text in a string, template or regex literal is not a
+ rebinding. Ambiguous text never hides code: a ``/`` after ``)`` is division."""
d = self._desktop_plugin(tmp_path, (
"var Vector2 = class _Vector2 { constructor() { _Vector2.prototype.isVector2 = true } }\n"
"function Track() {}\nTrack.prototype.ValueTypeName = 'vector'\n"
@@ -372,8 +372,10 @@ class TestDesktopSurface:
"const label = `${Storage.prototype.setItem = spy}`\n"
"const n = 1 / (Storage.prototype.setItem = spy, 2) / 3\n"
"function E() {}\nconst re = /E = x/, t = `E = ${1}`; E.prototype.isE = 1\n"
- "function F() {}\nregistry[F.name] = F; if (ready) /F = x/.test(label); F.prototype.isF = 1\n"
+ "function F() {}\nregistry[F.name] = F; F.prototype.isF = 1\n"
"function G() {}\nlet {a: G} = window; G.prototype.setItem = spy\n"
+ "function J() {}\ntrue ? [J] = [window.Storage] : null; J.prototype.setItem = spy\n"
+ "const m = Number(\"if (\") / (Storage.prototype.setItem = spy, 2) / 3\n"
))
assert desktop_surface_hits(d) == [
"prototype patching (desktop/plugin.js:5)",
@@ -388,6 +390,8 @@ class TestDesktopSurface:
"prototype patching (desktop/plugin.js:29)",
"prototype patching (desktop/plugin.js:30)",
"prototype patching (desktop/plugin.js:36)",
+ "prototype patching (desktop/plugin.js:38)",
+ "prototype patching (desktop/plugin.js:39)",
]
def test_node_sidecar_and_test_mjs_outside_desktop_are_not_the_surface(self, tmp_path):