Skip to content

Commit

Permalink
refactor(linter): read exported_bindings_from_star_export lazily (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 22, 2024
1 parent 547c102 commit 774babb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
37 changes: 35 additions & 2 deletions crates/oxc_linter/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
fmt,
path::{Path, PathBuf},
sync::{Arc, RwLock},
sync::{Arc, OnceLock, RwLock},
};

use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct ModuleRecord {

/// Reexported bindings from `export * from 'specifier'`
/// Keyed by resolved path
pub exported_bindings_from_star_export: RwLock<FxHashMap<PathBuf, Vec<CompactStr>>>,
exported_bindings_from_star_export: OnceLock<FxHashMap<PathBuf, Vec<CompactStr>>>,

/// `export default name`
/// ^^^^^^^ span
Expand Down Expand Up @@ -491,4 +491,37 @@ impl ModuleRecord {
..ModuleRecord::default()
}
}

pub(crate) fn exported_bindings_from_star_export(
&self,
) -> &FxHashMap<PathBuf, Vec<CompactStr>> {
self.exported_bindings_from_star_export.get_or_init(|| {
let mut exported_bindings_from_star_export: FxHashMap<PathBuf, Vec<CompactStr>> =
FxHashMap::default();
let loaded_modules = self.loaded_modules.read().unwrap();
for export_entry in &self.star_export_entries {
let Some(module_request) = &export_entry.module_request else {
continue;
};
let Some(remote_module_record) = loaded_modules.get(module_request.name()) else {
continue;
};
// Append both remote `bindings` and `exported_bindings_from_star_export`
let remote_exported_bindings_from_star_export = remote_module_record
.exported_bindings_from_star_export()
.iter()
.flat_map(|(_, value)| value.clone());
let remote_bindings = remote_module_record
.exported_bindings
.keys()
.cloned()
.chain(remote_exported_bindings_from_star_export);
exported_bindings_from_star_export
.entry(remote_module_record.resolved_absolute_path.clone())
.or_default()
.extend(remote_bindings);
}
exported_bindings_from_star_export
})
}
}
4 changes: 1 addition & 3 deletions crates/oxc_linter/src/rules/import/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ impl Rule for Named {
}
// check re-export
if remote_module_record
.exported_bindings_from_star_export
.read()
.unwrap()
.exported_bindings_from_star_export()
.iter()
.any(|(_, value)| value.contains(&import_name.name))
{
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_linter/src/rules/import/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,7 @@ fn check_binding_exported(
if module.exported_bindings.contains_key(name)
|| (name == "default" && module.export_default.is_some())
|| module
.exported_bindings_from_star_export
.read()
.unwrap()
.exported_bindings_from_star_export()
.iter()
.any(|(_, value)| value.iter().any(|s| s.as_str() == name))
{
Expand Down
31 changes: 0 additions & 31 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,37 +264,6 @@ impl Runtime {

// The thread is blocked here until all dependent modules are resolved.

// Resolve and append `star_export_bindings`
for export_entry in &module_record.star_export_entries {
let Some(module_request) = &export_entry.module_request else {
continue;
};
let loaded_modules = module_record.loaded_modules.read().unwrap();
let Some(remote_module_record) = loaded_modules.get(module_request.name()) else {
continue;
};
// Append both remote `bindings` and `exported_bindings_from_star_export`
let remote_exported_bindings_from_star_export =
remote_module_record.exported_bindings_from_star_export.read().unwrap();
let remote_exported_bindings_from_star_export =
remote_exported_bindings_from_star_export
.iter()
.flat_map(|(_, value)| value.clone());
let remote_bindings = remote_module_record
.exported_bindings
.keys()
.cloned()
.chain(remote_exported_bindings_from_star_export)
.collect::<Vec<_>>();
module_record
.exported_bindings_from_star_export
.write()
.unwrap()
.entry(remote_module_record.resolved_absolute_path.clone())
.or_default()
.extend(remote_bindings);
}

// Stop if the current module is not marked for lint.
if !self.paths.contains(path) {
return vec![];
Expand Down

0 comments on commit 774babb

Please sign in to comment.