fix(powershell): strip only a known module/assembly extension from a using name
Follow-up to #4847. Its fix stripped any trailing dotted segment, so a dotted module name with no file extension (using module Az.Accounts) resolved to 'az' (dropping .Accounts). Restrict the strip to real module/assembly suffixes (.psm1/.psd1/.ps1/.dll/.exe) so file names like Helpers.psm1 still resolve to 'helpers' while Az.Accounts keeps its full name. Adds a regression test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
d157b98ced
commit
e71d5ed63e
2 files changed
+28
-5
No files matched your search
@@ -301,11 +301,16 @@ def extract_powershell(path: Path) -> dict:
|
||||
module_name = raw.split(".")[-1]
|
||||
else:
|
||||
# `module`/`assembly` name a file or library
|
||||
# (Helpers.psm1, System.Data.dll): the dot there is
|
||||
# an extension, not a namespace separator, so
|
||||
# splitting on it like the namespace form returned
|
||||
# the extension itself instead of the name.
|
||||
module_name = re.sub(r'\.[^.]+$', '', raw).split('/')[-1].split('\\')[-1]
|
||||
# (Helpers.psm1, System.Data.dll): a trailing module
|
||||
# or assembly extension is not a namespace separator,
|
||||
# so splitting on it like the namespace form returned
|
||||
# the extension itself instead of the name. Strip only
|
||||
# a known extension, so a dotted module name with no
|
||||
# file extension (Az.Accounts) keeps its full name
|
||||
# rather than losing its last segment.
|
||||
module_name = re.sub(
|
||||
r'\.(psm1|psd1|ps1|dll|exe)$', '', raw, flags=re.IGNORECASE
|
||||
).split('/')[-1].split('\\')[-1]
|
||||
if module_name:
|
||||
add_edge(file_nid, _make_id(module_name), "imports_from",
|
||||
node.start_point[0] + 1)
|
||||
|
||||
@@ -3305,6 +3305,24 @@ def test_powershell_using_namespace_keeps_last_segment(tmp_path):
|
||||
)
|
||||
|
||||
|
||||
def test_powershell_using_module_without_extension_keeps_full_name(tmp_path):
|
||||
"""A dotted module name with no file extension must keep its full name.
|
||||
|
||||
Only a real module/assembly extension (.psm1/.psd1/.ps1/.dll/.exe) is a file
|
||||
suffix; a dotted name like `Az.Accounts` is the module name itself, so the
|
||||
last segment must not be stripped (it would otherwise resolve to 'az')."""
|
||||
f = tmp_path / "main.ps1"
|
||||
f.write_text("using module Az.Accounts\n")
|
||||
r = extract_powershell(f)
|
||||
targets = {e["target"] for e in r["edges"] if e["relation"] == "imports_from"}
|
||||
assert not any(t == "az" for t in targets), (
|
||||
f"using-module dropped the dotted name's last segment; targets={targets}"
|
||||
)
|
||||
assert any("accounts" in t for t in targets), (
|
||||
f"using-module lost the Az.Accounts name; targets={targets}"
|
||||
)
|
||||
|
||||
|
||||
# ── PowerShell manifest (.psd1) (#1331) ──────────────────────────────────────
|
||||
|
||||
def test_powershell_psd1_dispatched():
|
||||
|
||||
Reference in new issue
Block a user