Skip to content

Commit 6fd8e7f

Browse files
committed
Support require() too
1 parent ad600e4 commit 6fd8e7f

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

crates/ark/src/lsp/diagnostics.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,9 @@ fn recurse_call(
806806
let fun = fun.as_str();
807807

808808
match fun {
809-
"library" => {
810-
// Track symbols exported by `library()` calls
811-
handle_library_call(node, context)?;
809+
"library" | "require" => {
810+
// Track symbols exported by `library()` or `require()` calls
811+
handle_package_attach_call(node, context)?;
812812
},
813813
_ => {},
814814
};
@@ -819,15 +819,15 @@ fn recurse_call(
819819
().ok()
820820
}
821821

822-
fn handle_library_call(node: Node, context: &mut DiagnosticContext) -> anyhow::Result<()> {
822+
fn handle_package_attach_call(node: Node, context: &mut DiagnosticContext) -> anyhow::Result<()> {
823823
// Find the first argument (package name). Positionally for now.
824824
let Some(value) = node.arguments_values().nth(0) else {
825-
return Err(anyhow::anyhow!("Can't unpack `library()` argument"));
825+
return Err(anyhow::anyhow!("Can't unpack attached package argument"));
826826
};
827827

828828
let package_name = value.get_identifier_or_string_text(context.contents)?;
829829

830-
// Insert exports globablly for now
830+
// Insert exports for the attached package
831831
if let Some(package) = context.library.get(&package_name) {
832832
for symbol in &package.namespace.exports {
833833
let pos = node.end_position();
@@ -1675,4 +1675,48 @@ foo
16751675
assert_eq!(messages.len(), 4);
16761676
});
16771677
}
1678+
1679+
#[test]
1680+
fn test_library_static_exports_require() {
1681+
r_task(|| {
1682+
// `pkg` exports `foo` and `bar`
1683+
let namespace = Namespace {
1684+
exports: vec!["foo".to_string(), "bar".to_string()],
1685+
imports: vec![],
1686+
bulk_imports: vec![],
1687+
};
1688+
let description = Description {
1689+
name: "pkg".to_string(),
1690+
version: "1.0.0".to_string(),
1691+
depends: vec![],
1692+
};
1693+
let package = Package {
1694+
path: PathBuf::from("/mock/path"),
1695+
description,
1696+
namespace,
1697+
};
1698+
1699+
let library = Library::new(vec![]).insert("pkg", package);
1700+
1701+
let console_scopes = vec![vec!["require".to_string()]];
1702+
let state = WorldState {
1703+
library,
1704+
console_scopes,
1705+
..Default::default()
1706+
};
1707+
1708+
let code = "
1709+
foo()
1710+
require(pkg)
1711+
bar
1712+
foo()
1713+
";
1714+
let document = Document::new(code, None);
1715+
let diagnostics = generate_diagnostics(document, state.clone());
1716+
assert!(diagnostics
1717+
.iter()
1718+
.any(|d| d.message.contains("No symbol named 'foo'")));
1719+
assert_eq!(diagnostics.len(), 1);
1720+
});
1721+
}
16781722
}

0 commit comments

Comments
 (0)