fix: recover chained TS decorators and report the lossy parse region (#4711)
tree-sitter-typescript 0.23.2 cannot parse a member access on a call in
decorator position. For `@(Link().toMany())` it wraps `Link().` in an ERROR
and parses `toMany()` as a sibling, and `_ts_decorator_name` returned None for
any parenthesized expression, so the decorator contributed no edge at all.
Read the outermost call of a parenthesized decorator, including one recovered
past an ERROR, and leave a genuinely unparseable expression (an invalid
`@(A, B)`) without a name.
The partial-parse warning also reported the first ERROR line, which is often an
earlier single-line recovery that kept its symbols. Report the multiline
region the warning gate selects on instead.
(cherry picked from commit 3b8d4c4c52)
This commit is contained in:
1 parent
e0fb925378
commit
6533f57e85
5 files changed
+82
-20
No files matched your search
+4
-3
@@ -9659,7 +9659,7 @@ def extract(
|
||||
# syntax the Lua grammar can't parse, #2520) extracts partially — sometimes
|
||||
# to nothing but the file node — with no other signal. Neither warning
|
||||
# above fires (nodes exist, no error marker), so surface it explicitly,
|
||||
# naming the first error line so the user can find the construct.
|
||||
# naming the error region's line so the user can find the construct.
|
||||
_syntax_error_files: list[tuple[str, int | None]] = []
|
||||
for i, _p in enumerate(paths):
|
||||
_res = per_file[i] or {}
|
||||
@@ -9687,10 +9687,11 @@ def extract(
|
||||
# one that yielded most of its symbols and lost an ERROR region is
|
||||
# partial. "May be partially extracted" rendered both identically.
|
||||
_kept = max(len(_res.get("nodes", [])) - 1, 0)
|
||||
_syntax_error_files.append((_rel, _pe.get("first_error_line"), _kept))
|
||||
_line = _pe.get("lossy_error_line") or _pe.get("first_error_line")
|
||||
_syntax_error_files.append((_rel, _line, _kept))
|
||||
if _syntax_error_files:
|
||||
def _describe_syntax_error(rel: str, line: "int | None", kept: int) -> str:
|
||||
_where = f"first error at line {line}" if line else "syntax error"
|
||||
_where = f"error region at line {line}" if line else "syntax error"
|
||||
_got = "no symbols extracted" if kept == 0 else f"{kept} symbol(s) extracted"
|
||||
return f"{rel} ({_where}, {_got})"
|
||||
|
||||
|
||||
@@ -5180,6 +5180,28 @@ def _first_parse_error_line(root) -> int:
|
||||
node = child
|
||||
|
||||
|
||||
def _first_multiline_error_line(root) -> int:
|
||||
"""1-based line of the first ERROR that spans more than one line.
|
||||
|
||||
The #2551 warning gate selects on a multiline region (or a total loss), so
|
||||
that region is the actionable location. Earlier single-line recoveries can
|
||||
sit far above it: in a decorator-heavy TS file, `@(Link().toMany())` errors
|
||||
on line 14 but keeps every symbol, while the multiline chain on line 33 is
|
||||
the one the reader needs (#4711).
|
||||
"""
|
||||
best = None
|
||||
stack = [root]
|
||||
while stack:
|
||||
n = stack.pop()
|
||||
if n.type == "ERROR" and n.end_point[0] > n.start_point[0]:
|
||||
if best is None or n.start_byte < best.start_byte:
|
||||
best = n
|
||||
stack.extend(c for c in n.children if c.has_error)
|
||||
if best is None:
|
||||
return _first_parse_error_line(root)
|
||||
return best.start_point[0] + 1
|
||||
|
||||
|
||||
def _has_multiline_error(root) -> bool:
|
||||
"""True if any materialized ERROR node spans more than one line (a
|
||||
recovery region large enough to plausibly drop symbols, vs a tiny
|
||||
@@ -10455,10 +10477,15 @@ def _extract_generic(
|
||||
# error's line so extract() can warn instead of reporting silent success.
|
||||
# Rides on the result dict, so it survives the per-file AST cache.
|
||||
if root.has_error:
|
||||
_multiline = _has_multiline_error(root)
|
||||
result["parse_errors"] = {
|
||||
"first_error_line": _first_parse_error_line(root),
|
||||
"multiline_error": _has_multiline_error(root),
|
||||
"multiline_error": _multiline,
|
||||
}
|
||||
if _multiline:
|
||||
# Report the region the warning gate selected on, not an earlier
|
||||
# fully-recovered one that would send the reader elsewhere (#4711).
|
||||
result["parse_errors"]["lossy_error_line"] = _first_multiline_error_line(root)
|
||||
# Kotlin (#2526/#2550): the declared package qualifies every node in the
|
||||
# file; the import-target and qualified-call resolvers key their per-package
|
||||
# symbol indexes off it.
|
||||
@@ -10584,26 +10611,60 @@ def _python_decorator_name(deco_node, source: bytes) -> str | None:
|
||||
return None
|
||||
return None
|
||||
|
||||
def _ts_decorator_target_name(expr, source: bytes) -> str | None:
|
||||
"""The symbol a decorator expression applies: the outermost call's name.
|
||||
|
||||
`f` -> f; `f()` / `x.f()` -> f; `@Component({...})` -> Component. A
|
||||
parenthesized decorator is unwrapped first. tree-sitter-typescript 0.23.2
|
||||
cannot parse a call chain in decorator position (`@(Link().toMany())`): it
|
||||
wraps `Link().` in an ERROR and parses `toMany()` as a SIBLING of the ERROR
|
||||
inside the parentheses. The applied decorator is still that outermost call,
|
||||
which is the last named call child, never the one buried in the ERROR
|
||||
(#4711). Without this the whole parenthesized decorator contributed no edge,
|
||||
though most of its expression parsed.
|
||||
"""
|
||||
if expr is None:
|
||||
return None
|
||||
if expr.type == "parenthesized_expression":
|
||||
calls = [c for c in expr.named_children if c.type == "call_expression"]
|
||||
if calls:
|
||||
return _ts_decorator_target_name(calls[-1], source)
|
||||
# A recovered non-call expression (an invalid `@(A, B)` sequence lands
|
||||
# here as ERROR + `B`) is not a decorator name: do not guess one.
|
||||
if any(c.type == "ERROR" for c in expr.named_children):
|
||||
return None
|
||||
named = [c for c in expr.named_children if c.type != "ERROR"]
|
||||
return _ts_decorator_target_name(named[0], source) if named else None
|
||||
if expr.type == "call_expression":
|
||||
fn = expr.child_by_field_name("function")
|
||||
if fn is None:
|
||||
return None
|
||||
if fn.type == "member_expression":
|
||||
prop = fn.child_by_field_name("property")
|
||||
return _read_text(prop, source) if prop else None
|
||||
if fn.type == "identifier":
|
||||
return _read_text(fn, source)
|
||||
return None
|
||||
if expr.type == "member_expression":
|
||||
prop = expr.child_by_field_name("property")
|
||||
return _read_text(prop, source) if prop else None
|
||||
if expr.type == "identifier":
|
||||
return _read_text(expr, source)
|
||||
return None
|
||||
|
||||
|
||||
def _ts_decorator_name(deco_node, source: bytes) -> str | None:
|
||||
"""Return the head symbol of a TS `decorator` node.
|
||||
|
||||
`@Injectable` -> the identifier; `@Component({...})` / `@Input()` -> the
|
||||
`function` of the call_expression; `@ng.Component()` / `@core.Injectable` ->
|
||||
the `property` of the member_expression (the imported symbol, not the
|
||||
namespace alias).
|
||||
namespace alias); `@(Link().toMany())` -> the outermost call, `toMany`.
|
||||
"""
|
||||
for child in deco_node.children:
|
||||
if not child.is_named:
|
||||
continue
|
||||
target = child
|
||||
if target.type == "call_expression":
|
||||
target = target.child_by_field_name("function") or target
|
||||
if target.type == "member_expression":
|
||||
prop = target.child_by_field_name("property")
|
||||
return _read_text(prop, source) if prop else None
|
||||
if target.type == "identifier":
|
||||
return _read_text(target, source)
|
||||
return None
|
||||
return _ts_decorator_target_name(child, source)
|
||||
return None
|
||||
|
||||
def _ts_method_name(method_node, source: bytes) -> str | None:
|
||||
|
||||
@@ -345,7 +345,7 @@ def test_kotlin_partial_parse_warns_with_file_and_line(tmp_path, capsys):
|
||||
err = capsys.readouterr().err
|
||||
assert "syntax errors" in err
|
||||
assert "Broken.kt" in err
|
||||
assert re.search(r"first error at line \d+", err)
|
||||
assert re.search(r"error region at line \d+", err)
|
||||
# The marker must survive the per-file AST cache: a warm re-run (same
|
||||
# cache_root) warns again.
|
||||
old = os.getcwd()
|
||||
|
||||
@@ -60,11 +60,11 @@ def test_warning_names_the_file_and_how_much_survived(tmp_path, capsys):
|
||||
f"the warning does not say how much survived: {err!r}")
|
||||
|
||||
|
||||
def test_warning_still_reports_the_first_error_line(tmp_path, capsys):
|
||||
"""The line number was the one actionable thing the old message had; it must
|
||||
def test_warning_still_reports_the_error_region_line(tmp_path, capsys):
|
||||
"""The line number is the one actionable thing the old message had; it must
|
||||
survive the rewrite."""
|
||||
err = _run(tmp_path, [_partial_parse_fixture(tmp_path)], capsys)
|
||||
assert re.search(r"first error at line \d+", err), err
|
||||
assert re.search(r"error region at line \d+", err), err
|
||||
|
||||
|
||||
def test_syntax_warning_survives_cross_drive_paths_without_changing_the_graph(
|
||||
@@ -95,7 +95,7 @@ def test_syntax_warning_survives_cross_drive_paths_without_changing_the_graph(
|
||||
warning = capsys.readouterr().err
|
||||
assert "partially extracted" in warning
|
||||
assert source.as_posix() in warning
|
||||
assert re.search(r"first error at line \d+", warning)
|
||||
assert re.search(r"error region at line \d+", warning)
|
||||
assert "yielded no symbols" not in warning
|
||||
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ def test_ts_genuinely_broken_file_still_warns(tmp_path, capsys):
|
||||
err = capsys.readouterr().err
|
||||
assert "syntax errors" in err
|
||||
assert "broken.ts" in err
|
||||
assert re.search(r"first error at line 1", err)
|
||||
assert re.search(r"error region at line 1", err)
|
||||
|
||||
|
||||
def test_ts_midfile_breakage_warns_and_keeps_intact_functions(tmp_path, capsys):
|
||||
|
||||
Reference in new issue
Block a user