-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
fix(lua): emit an imports edge for bare require() statements (#3320) #3448
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
shivamtiwari3
wants to merge
1
commit into
Graphify-Labs:v8
Choose a base branch
from
shivamtiwari3:fix/3320-lua-bare-require
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.
+200
−20
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
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 |
|---|---|---|
|
|
@@ -3274,6 +3274,14 @@ def ensure_named_node(name: str, line: int) -> str: | |
| def walk(node, parent_class_nid: str | None = None) -> None: | ||
|
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 58 callees (efferent coupling). Grounded coupling-delta finding (deterministic), not an LLM guess. |
||
| t = node.type | ||
|
|
||
| # Import-carrying call statements (Lua `require("m")` with no assignment). | ||
| # Unlike import_types this does not return: the node is an ordinary call | ||
| # expression, so class/function/call dispatch below still has to see it | ||
| # and its children (#3320). The handler decides whether the call is | ||
| # actually an import. | ||
| if t in config.import_call_types and config.import_handler: | ||
| config.import_handler(node, source, file_nid, stem, edges, str_path, scope_stack) | ||
|
|
||
| # Import types | ||
| if t in config.import_types: | ||
| if config.import_handler: | ||
|
|
@@ -5302,6 +5310,12 @@ def walk_calls( | |
| and node.type in ("lexical_declaration", "variable_declaration")): | ||
| _require_imports_js(node, source, caller_nid, stem, edges, str_path) | ||
|
|
||
| # Lua's twin of the block above: a bare `require("m")` is valid at any | ||
| # depth, and lazy requires inside a function body are as common in Neovim | ||
| # config as the top-level form the module-level walk covers (#3320). | ||
| if node.type in config.import_call_types and config.import_handler: | ||
| config.import_handler(node, source, file_nid, stem, edges, str_path, scope_stack) | ||
|
|
||
| if node.type in config.call_types: | ||
| # JS/TS dynamic imports: await import('./foo.js') | ||
| if config.ts_module in ("tree_sitter_javascript", "tree_sitter_typescript"): | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """Regression tests for bare Lua `require("m")` statements (#3320). | ||
|
|
||
| `_import_lua` only ran for `variable_declaration` nodes, so an import edge came | ||
| out of `local x = require("m")` but not out of the far more common Neovim idiom | ||
| — a `require("m")` statement with no assignment, which tree-sitter-lua parses as | ||
| a plain `function_call`. Every `init.lua` written as a list of bare requires | ||
| therefore produced zero import edges. The fix routes those call nodes through | ||
| the same handler via `import_call_types`, which (unlike `import_types`) does not | ||
| stop the walk, so Lua call extraction is unaffected. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from graphify.extract import extract | ||
|
|
||
|
|
||
| def _write(path: Path, text: str) -> Path: | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.write_text(text, encoding="utf-8") | ||
| return path | ||
|
|
||
|
|
||
| def _import_targets(result: dict, source: str) -> list[str]: | ||
| return [ | ||
| edge["target"] | ||
| for edge in result["edges"] | ||
| if edge["relation"] == "imports" and edge["source"] == source | ||
| ] | ||
|
|
||
|
|
||
| def test_bare_require_emits_import_edge(tmp_path: Path): | ||
| entry = _write(tmp_path / "init.lua", 'require("lazy")\n') | ||
| module = _write(tmp_path / "lazy.lua", "local M = {}\nreturn M\n") | ||
|
|
||
| result = extract([entry, module], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "init") == ["lazy"] | ||
|
|
||
|
|
||
| def test_bare_require_matches_the_assigned_form(tmp_path: Path): | ||
| """The two spellings of the same dependency must produce the same edge.""" | ||
| module = _write(tmp_path / "lazy.lua", "local M = {}\nreturn M\n") | ||
| bare = _write(tmp_path / "bare.lua", 'require("lazy")\n') | ||
| assigned = _write(tmp_path / "assigned.lua", 'local ok = require("lazy")\n') | ||
|
|
||
| result = extract([module, bare, assigned], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "bare") == _import_targets(result, "assigned") | ||
|
|
||
|
|
||
| def test_bare_require_without_parentheses(tmp_path: Path): | ||
| entry = _write(tmp_path / "init.lua", 'require "lazy"\n') | ||
| module = _write(tmp_path / "lazy.lua", "local M = {}\nreturn M\n") | ||
|
|
||
| result = extract([entry, module], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "init") == ["lazy"] | ||
|
|
||
|
|
||
| def test_every_bare_require_in_a_list_is_reported(tmp_path: Path): | ||
| entry = _write( | ||
| tmp_path / "init.lua", | ||
| 'require("options")\nrequire("keymaps")\nrequire("plugins")\n', | ||
| ) | ||
| modules = [ | ||
| _write(tmp_path / f"{name}.lua", "-- module\n") | ||
| for name in ("options", "keymaps", "plugins") | ||
| ] | ||
|
|
||
| result = extract([entry, *modules], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "init") == ["options", "keymaps", "plugins"] | ||
|
|
||
|
|
||
| def test_lazy_require_inside_a_function_body(tmp_path: Path): | ||
| """A require at any lexical depth is still a dependency of the file.""" | ||
| entry = _write( | ||
| tmp_path / "init.lua", | ||
| "function setup()\n require(\"lazy\")\nend\n", | ||
| ) | ||
| module = _write(tmp_path / "lazy.lua", "-- module\n") | ||
|
|
||
| result = extract([entry, module], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "init") == ["lazy"] | ||
|
|
||
|
|
||
| def test_a_non_require_call_produces_no_import_edge(tmp_path: Path): | ||
| entry = _write(tmp_path / "init.lua", "local M = {}\nfunction M.go() end\nM.go()\n") | ||
|
|
||
| result = extract([entry], cache_root=tmp_path) | ||
|
|
||
| assert _import_targets(result, "init") == [] | ||
|
|
||
|
|
||
| def test_bare_require_does_not_suppress_call_extraction(tmp_path: Path): | ||
| """`import_call_types` must not swallow the call node the way imports do.""" | ||
| entry = _write( | ||
| tmp_path / "init.lua", | ||
| 'require("lazy")\n' | ||
| "function helper() end\n" | ||
| "function setup()\n helper()\nend\n", | ||
| ) | ||
| module = _write(tmp_path / "lazy.lua", "-- module\n") | ||
|
|
||
| result = extract([entry, module], cache_root=tmp_path) | ||
|
|
||
| calls = { | ||
| (edge["source"], edge["target"]) | ||
| for edge in result["edges"] | ||
| if edge["relation"] == "calls" | ||
| } | ||
| assert ("init_setup", "init_helper") in calls | ||
| assert _import_targets(result, "init") == ["lazy"] | ||
|
|
||
|
|
||
| def test_bare_require_is_reported_once(tmp_path: Path): | ||
| """The module-level walk and the call walk must not both emit the edge.""" | ||
| entry = _write(tmp_path / "init.lua", 'require("lazy")\n') | ||
| module = _write(tmp_path / "lazy.lua", "-- module\n") | ||
|
|
||
| result = extract([entry, module], cache_root=tmp_path) | ||
|
|
||
| assert len(_import_targets(result, "init")) == 1 |
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.
walk()fans out to 58 callees (efferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.