From 75d928a20d54cbdb9c0345b3d823746fcca7eb43 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 7 Sep 2023 16:57:53 +0800 Subject: [PATCH] feat(syntax): add loaded_modules to ModuleRecord --- Cargo.lock | 2 + crates/oxc_linter/src/service.rs | 16 +++++--- .../oxc_semantic/src/module_record/builder.rs | 2 +- crates/oxc_syntax/Cargo.toml | 2 + crates/oxc_syntax/src/module_record.rs | 37 ++++++++++++++++--- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index daefed0eb..f859970ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1816,6 +1816,8 @@ name = "oxc_syntax" version = "0.1.3" dependencies = [ "bitflags 2.4.0", + "dashmap", + "indexmap 2.0.0", "oxc_index", "oxc_span", "rustc-hash", diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 6592dcd75..fad65df98 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -139,6 +139,10 @@ impl Runtime { fn process_path(&self, path: &Path, tx_error: &DiagnosticSender) { let Ok(source_type) = SourceType::from_path(path) else { return }; + if self.module_map.contains_key(path) { + return; + } + if self.init_cache_state(path) { return; } @@ -205,17 +209,19 @@ impl Runtime { // Retrieve all dependency modules from this module. module_record - .module_requests + .requested_modules .keys() .cloned() .par_bridge() .map_with(&self.resolver, |resolver, specifier| { - resolver.resolve(dir, &specifier).ok() + resolver.resolve(dir, &specifier).ok().map(|r| (specifier, r)) }) .flatten() - .filter(|r| !self.module_map.contains_key(r.path())) - .for_each_with(tx_error, |tx_error, resolution| { - self.process_path(resolution.path(), tx_error); + .for_each_with(tx_error, |tx_error, (specifier, resolution)| { + let path = resolution.path(); + self.process_path(path, tx_error); + let target_module_record = Arc::clone(&self.module_map.get(path).unwrap()); + module_record.loaded_modules.insert(specifier, target_module_record); }); } diff --git a/crates/oxc_semantic/src/module_record/builder.rs b/crates/oxc_semantic/src/module_record/builder.rs index 7caca9308..8e9f2024c 100644 --- a/crates/oxc_semantic/src/module_record/builder.rs +++ b/crates/oxc_semantic/src/module_record/builder.rs @@ -73,7 +73,7 @@ impl ModuleRecordBuilder { fn add_module_request(&mut self, name_span: &NameSpan) { self.module_record - .module_requests + .requested_modules .entry(name_span.name().clone()) .or_default() .push(name_span.span()); diff --git a/crates/oxc_syntax/Cargo.toml b/crates/oxc_syntax/Cargo.toml index 4205f3562..12207a90d 100644 --- a/crates/oxc_syntax/Cargo.toml +++ b/crates/oxc_syntax/Cargo.toml @@ -24,3 +24,5 @@ unicode-id-start = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } bitflags = { workspace = true } rustc-hash = { workspace = true } +indexmap = { workspace = true } +dashmap = { workspace = true } diff --git a/crates/oxc_syntax/src/module_record.rs b/crates/oxc_syntax/src/module_record.rs index 42351b7da..7ffe73d90 100644 --- a/crates/oxc_syntax/src/module_record.rs +++ b/crates/oxc_syntax/src/module_record.rs @@ -1,31 +1,56 @@ //! [ECMAScript Module Record](https://tc39.es/ecma262/#sec-abstract-module-records) -use oxc_span::{Atom, Span}; -use rustc_hash::FxHashMap; +use std::{hash::BuildHasherDefault, sync::Arc}; -/// [Source Text Module Record](https://tc39.es/ecma262/#table-additional-fields-of-source-text-module-records) +use dashmap::DashMap; +use indexmap::IndexMap; +use oxc_span::{Atom, Span}; +use rustc_hash::{FxHashMap, FxHasher}; + +/// Module Record +/// +/// See +/// * +/// * #[derive(Debug, Default)] pub struct ModuleRecord { - /// + /// `[[RequestedModules]]` + /// + /// A List of all the ModuleSpecifier strings used by the module represented by this record to request the importation of a module. The List is in source text occurrence order. + /// /// Module requests from: /// import ImportClause FromClause /// import ModuleSpecifier /// export ExportFromClause FromClause - /// Keyed by FromClause, valued by all node occurrences - pub module_requests: FxHashMap>, + /// Keyed by ModuleSpecifier, valued by all node occurrences + pub requested_modules: IndexMap, BuildHasherDefault>, + /// `[[LoadedModules]]` + /// + /// A map from the specifier strings used by the module represented by this record to request the importation of a module to the resolved Module Record. + /// The list does not contain two different Records with the same `[[Specifier]]`. + pub loaded_modules: DashMap, BuildHasherDefault>, + + /// `[[ImportEntries]]` + /// /// A List of ImportEntry records derived from the code of this module pub import_entries: Vec, + /// `[[LocalExportEntries]]` + /// /// A List of ExportEntry records derived from the code of this module /// that correspond to declarations that occur within the module pub local_export_entries: Vec, + /// `[[IndirectExportEntries]]` + /// /// A List of ExportEntry records derived from the code of this module /// that correspond to reexported imports that occur within the module /// or exports from export * as namespace declarations. pub indirect_export_entries: Vec, + /// `[[StarExportEntries]]` + /// /// A List of ExportEntry records derived from the code of this module /// that correspond to export * declarations that occur within the module, /// not including export * as namespace declarations.