@@ -806,9 +806,9 @@ fn recurse_call(
806
806
let fun = fun. as_str ( ) ;
807
807
808
808
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) ?;
812
812
} ,
813
813
_ => { } ,
814
814
} ;
@@ -819,15 +819,15 @@ fn recurse_call(
819
819
( ) . ok ( )
820
820
}
821
821
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 < ( ) > {
823
823
// Find the first argument (package name). Positionally for now.
824
824
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" ) ) ;
826
826
} ;
827
827
828
828
let package_name = value. get_identifier_or_string_text ( context. contents ) ?;
829
829
830
- // Insert exports globablly for now
830
+ // Insert exports for the attached package
831
831
if let Some ( package) = context. library . get ( & package_name) {
832
832
for symbol in & package. namespace . exports {
833
833
let pos = node. end_position ( ) ;
@@ -1675,4 +1675,48 @@ foo
1675
1675
assert_eq ! ( messages. len( ) , 4 ) ;
1676
1676
} ) ;
1677
1677
}
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
+ }
1678
1722
}
0 commit comments