Skip to content
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

refactor(linter): read exported_bindings_from_star_export lazily #8062

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading