-
-
Notifications
You must be signed in to change notification settings - Fork 11k
fix(ts): avoid normalizing runtime dynamic imports #3219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hopstreax
wants to merge
1
commit into
Graphify-Labs:v8
Choose a base branch
from
hopstreax:investigate/3210-ts-import-normalizer-regression
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−58
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| import hashlib | ||
| import importlib | ||
| import re | ||
| from graphify.extractors.base import _LANGUAGE_BUILTIN_GLOBALS, _file_stem, _make_id, _read_text | ||
| from graphify.ids import normalize_id | ||
| from graphify.extractors.models import LanguageConfig | ||
|
|
@@ -2875,6 +2876,82 @@ def _ruby_extra_walk(node, source: bytes, file_nid: str, stem: str, str_path: st | |
| del ruby_namespace[-len(const_segments):] | ||
| return True | ||
|
|
||
| def _recover_3154_ts_import_types(tree, source: bytes) -> bytes | None: | ||
| """Rewrite TypeScript `import(...)` type arguments in malformed generic call expressions | ||
| to standard type identifiers of identical byte length (#3154 / #3210). | ||
|
|
||
| tree-sitter-typescript misparses generic call expressions whose type arguments start with | ||
| `typeof import(...)` or `import(...).Foo` (e.g. `f<typeof import("mod")>()`) as relational | ||
| binary comparison expressions (`<` and `>`), generating an ERROR node for `();` that absorbs | ||
| subsequent declarations as anonymous function/class expressions. | ||
|
|
||
| AST-first: only runs when `tree.root_node.has_error` is True, identifying exact `import(...)` | ||
| call expressions nested inside such malformed call expressions, and leaves runtime dynamic | ||
| imports completely untouched. | ||
|
|
||
| Preserves byte length, newlines, and source offsets so all downstream node source_location | ||
| metadata remains 100% accurate. | ||
| """ | ||
| if b"import(" not in source and b"import (" not in source: | ||
| return None | ||
| if not tree.root_node.has_error: | ||
| return None | ||
|
|
||
| def find_candidates(node): | ||
| candidates = [] | ||
| if node.type == "call_expression": | ||
| if node.children and node.children[0].type == "import": | ||
| curr = node.parent | ||
| while curr is not None and curr.type not in ( | ||
| "expression_statement", | ||
| "lexical_declaration", | ||
| "statement_block", | ||
| "program", | ||
| ): | ||
| curr = curr.parent | ||
| if curr is not None and curr.type == "expression_statement": | ||
| main_expr = curr.children[0] if curr.children else None | ||
| if main_expr and main_expr.type in ("binary_expression", "sequence_expression"): | ||
| def leftmost_leaf(n): | ||
| while n.children: | ||
| n = n.children[0] | ||
| return n | ||
|
|
||
| first_leaf = leftmost_leaf(main_expr) | ||
| if first_leaf.type in ("identifier", "property_identifier"): | ||
| def has_call_error(n): | ||
| if n.type == "ERROR": | ||
| raw = source[n.start_byte:n.end_byte].lstrip() | ||
| if ( | ||
| raw.startswith(b"(") | ||
| or raw.startswith(b">(") | ||
| or any( | ||
| c.type in ("formal_parameters", "arguments") | ||
| for c in n.children | ||
| ) | ||
| ): | ||
| return True | ||
| return any(has_call_error(c) for c in n.children) | ||
|
|
||
| if has_call_error(curr): | ||
| candidates.append((node.start_byte, node.end_byte)) | ||
| for c in node.children: | ||
| candidates.extend(find_candidates(c)) | ||
| return candidates | ||
|
|
||
| import_ranges = find_candidates(tree.root_node) | ||
| if not import_ranges: | ||
| return None | ||
|
|
||
| rewritten = bytearray(source) | ||
| for start, end in sorted(import_ranges, key=lambda r: r[0], reverse=True): | ||
| orig_slice = source[start:end] | ||
| repl = b"T" + re.sub(rb"[^\r\n]", b" ", orig_slice[1:]) | ||
| rewritten[start:end] = repl | ||
|
|
||
| return bytes(rewritten) | ||
|
|
||
|
|
||
| def _extract_generic( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
fans out to 26 callees (efferent coupling); 18 callers depend on it (afferent coupling). Grounded coupling-delta finding (deterministic), not an LLM guess. |
||
| path: Path, config: LanguageConfig, *, source_override: bytes | None = None | ||
| ) -> dict: | ||
|
|
@@ -2913,6 +2990,16 @@ def _extract_generic( | |
| source = path.read_bytes() if source_override is None else source_override | ||
| tree = parser.parse(source) | ||
| root = tree.root_node | ||
| if ( | ||
| root.has_error | ||
| and config.ts_module in ("tree_sitter_typescript",) | ||
| and (b"import(" in source or b"import (" in source) | ||
| ): | ||
| rewritten = _recover_3154_ts_import_types(tree, source) | ||
| if rewritten is not None: | ||
| source = rewritten | ||
| tree = parser.parse(source) | ||
| root = tree.root_node | ||
| except Exception as e: | ||
| return {"nodes": [], "edges": [], "error": str(e)} | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract_js()85 callers depend on it (afferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.