Skip to content

Commit

Permalink
refactor: dependency location (#7635)
Browse files Browse the repository at this point in the history
* refactor: dependency location

* refactor: follow some suggestions
  • Loading branch information
shulaoda authored Aug 21, 2024
1 parent 057508a commit 0d40d9f
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 161 deletions.
111 changes: 111 additions & 0 deletions crates/rspack_core/src/dependency/dependency_location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::{fmt, sync::Arc};

use derivative::Derivative;

#[derive(Derivative)]
#[derivative(Debug, Clone)]
pub struct RealDependencyLocation {
pub end: u32,
pub start: u32,
#[derivative(Debug = "ignore")]
source: Option<Arc<dyn SourceLocation>>,
}

impl RealDependencyLocation {
pub fn new(start: u32, end: u32) -> Self {
RealDependencyLocation {
end,
start,
source: None,
}
}

pub fn with_source(mut self, source: Arc<dyn SourceLocation>) -> Self {
self.source = Some(source);
self
}
}

impl From<swc_core::common::Span> for RealDependencyLocation {
fn from(span: swc_core::common::Span) -> Self {
Self {
start: span.lo.0.saturating_sub(1),
end: span.hi.0.saturating_sub(1),
source: None,
}
}
}

impl fmt::Display for RealDependencyLocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let source = &self.source.clone().expect("missing sourcemap");
let (start, end) = source.look_up_range_pos(self.start, self.end);

if start.line == end.line {
if start.column == end.column {
return write!(f, "{}:{}", start.line, start.column);
}

return write!(f, "{}:{}-{}", start.line, start.column, end.column);
}

write!(
f,
"{}:{}-{}:{}",
start.line, start.column, end.line, end.column
)
}
}

#[derive(Debug, Clone)]
pub struct SyntheticDependencyName {
pub name: String,
}

impl SyntheticDependencyName {
pub fn new(name: &str) -> Self {
SyntheticDependencyName {
name: name.to_string(),
}
}
}

impl fmt::Display for SyntheticDependencyName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}

// #[derive(Debug, Clone)]
// pub enum DependencyLocation {
// Real(RealDependencyLocation),
// Synthetic(SyntheticDependencyName),
// }

#[derive(Debug, Clone, Copy)]
pub struct SourcePosition {
pub line: usize,
pub column: usize,
}

pub trait SourceLocation: Send + Sync {
fn look_up_range_pos(&self, start: u32, end: u32) -> (SourcePosition, SourcePosition);
}

impl SourceLocation for swc_core::common::SourceMap {
fn look_up_range_pos(&self, start: u32, end: u32) -> (SourcePosition, SourcePosition) {
let lo = self.lookup_char_pos(swc_core::common::BytePos(start + 1));
let hi = self.lookup_char_pos(swc_core::common::BytePos(end + 1));

(
SourcePosition {
line: lo.line,
column: lo.col_display,
},
SourcePosition {
line: hi.line,
column: hi.col_display,
},
)
}
}
97 changes: 0 additions & 97 deletions crates/rspack_core/src/dependency/dependency_range.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ mod context_dependency;
mod context_element_dependency;
mod dependency_category;
mod dependency_id;
mod dependency_location;
mod dependency_macro;
mod dependency_range;
mod dependency_template;
mod dependency_trait;
mod dependency_type;
Expand All @@ -25,7 +25,7 @@ pub use context_dependency::{AsContextDependency, ContextDependency};
pub use context_element_dependency::ContextElementDependency;
pub use dependency_category::DependencyCategory;
pub use dependency_id::*;
pub use dependency_range::*;
pub use dependency_location::*;
pub use dependency_template::*;
pub use dependency_trait::*;
pub use dependency_type::DependencyType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_core::{
module_id, property_access, to_normal_comment, Compilation, DependencyRange, ExportsType,
ExtendedReferencedExport, ModuleGraph, RuntimeGlobals, RuntimeSpec, UsedName,
module_id, property_access, to_normal_comment, Compilation, ExportsType,
ExtendedReferencedExport, ModuleGraph, RealDependencyLocation, RuntimeGlobals, RuntimeSpec,
UsedName,
};
use rspack_core::{AsContextDependency, Dependency, DependencyCategory};
use rspack_core::{DependencyId, DependencyTemplate};
Expand All @@ -13,7 +14,7 @@ pub struct CommonJsFullRequireDependency {
id: DependencyId,
request: String,
names: Vec<Atom>,
range: DependencyRange,
range: RealDependencyLocation,
is_call: bool,
optional: bool,
asi_safe: bool,
Expand All @@ -23,7 +24,7 @@ impl CommonJsFullRequireDependency {
pub fn new(
request: String,
names: Vec<Atom>,
range: DependencyRange,
range: RealDependencyLocation,
is_call: bool,
optional: bool,
asi_safe: bool,
Expand Down Expand Up @@ -54,7 +55,7 @@ impl Dependency for CommonJsFullRequireDependency {
}

fn loc(&self) -> Option<String> {
self.range.to_loc()
Some(self.range.to_string())
}

fn span(&self) -> Option<ErrorSpan> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use rspack_collections::{Identifier, IdentifierSet};
use rspack_core::rspack_sources::ReplacementEnforce;
use rspack_core::{
property_access, AsContextDependency, AsModuleDependency, Compilation, Dependency,
DependencyRange, DependencyType, ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec,
HarmonyExportInitFragment, ModuleGraph, RuntimeGlobals, RuntimeSpec, UsedName, DEFAULT_EXPORT,
DependencyType, ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, HarmonyExportInitFragment,
ModuleGraph, RealDependencyLocation, RuntimeGlobals, RuntimeSpec, UsedName, DEFAULT_EXPORT,
};
use rspack_core::{DependencyId, DependencyTemplate};
use rspack_core::{TemplateContext, TemplateReplaceSource};
Expand All @@ -20,13 +20,13 @@ pub enum DeclarationId {

#[derive(Debug, Clone)]
pub struct DeclarationInfo {
range: DependencyRange,
range: RealDependencyLocation,
prefix: String,
suffix: String,
}

impl DeclarationInfo {
pub fn new(range: DependencyRange, prefix: String, suffix: String) -> Self {
pub fn new(range: RealDependencyLocation, prefix: String, suffix: String) -> Self {
Self {
range,
prefix,
Expand All @@ -38,15 +38,15 @@ impl DeclarationInfo {
#[derive(Debug, Clone)]
pub struct HarmonyExportExpressionDependency {
id: DependencyId,
range: DependencyRange,
range_stmt: DependencyRange,
range: RealDependencyLocation,
range_stmt: RealDependencyLocation,
declaration: Option<DeclarationId>,
}

impl HarmonyExportExpressionDependency {
pub fn new(
range: DependencyRange,
range_stmt: DependencyRange,
range: RealDependencyLocation,
range_stmt: RealDependencyLocation,
declaration: Option<DeclarationId>,
) -> Self {
Self {
Expand All @@ -68,7 +68,7 @@ impl Dependency for HarmonyExportExpressionDependency {
}

fn loc(&self) -> Option<String> {
self.range.to_loc()
Some(self.range.to_string())
}

fn get_exports(&self, _mg: &ModuleGraph) -> Option<ExportsSpec> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rspack_core::{
AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyRange,
DependencyTemplate, DependencyType, RuntimeSpec, TemplateContext, TemplateReplaceSource,
AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId,
DependencyTemplate, DependencyType, RealDependencyLocation, RuntimeSpec, TemplateContext,
TemplateReplaceSource,
};

// Remove `export` label.
Expand All @@ -9,12 +10,12 @@ use rspack_core::{
#[derive(Debug, Clone)]
pub struct HarmonyExportHeaderDependency {
id: DependencyId,
range: DependencyRange,
range_decl: Option<DependencyRange>,
range: RealDependencyLocation,
range_decl: Option<RealDependencyLocation>,
}

impl HarmonyExportHeaderDependency {
pub fn new(range: DependencyRange, range_decl: Option<DependencyRange>) -> Self {
pub fn new(range: RealDependencyLocation, range_decl: Option<RealDependencyLocation>) -> Self {
Self {
range,
range_decl,
Expand All @@ -29,7 +30,7 @@ impl Dependency for HarmonyExportHeaderDependency {
}

fn loc(&self) -> Option<String> {
self.range.to_loc()
Some(self.range.to_string())
}

fn dependency_type(&self) -> &DependencyType {
Expand Down
Loading

2 comments on commit 0d40d9f

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-21 dd6e27a) Current Change
10000_development-mode + exec 2.36 s ± 30 ms 2.37 s ± 22 ms +0.67 %
10000_development-mode_hmr + exec 712 ms ± 10 ms 719 ms ± 5.4 ms +0.93 %
10000_production-mode + exec 3.02 s ± 24 ms 3.05 s ± 25 ms +1.11 %
arco-pro_development-mode + exec 1.91 s ± 60 ms 1.9 s ± 73 ms -0.32 %
arco-pro_development-mode_hmr + exec 438 ms ± 3.3 ms 438 ms ± 1.7 ms -0.01 %
arco-pro_production-mode + exec 3.47 s ± 86 ms 3.51 s ± 90 ms +1.06 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.52 s ± 83 ms 3.54 s ± 65 ms +0.80 %
threejs_development-mode_10x + exec 1.69 s ± 19 ms 1.7 s ± 15 ms +0.54 %
threejs_development-mode_10x_hmr + exec 797 ms ± 6 ms 799 ms ± 11 ms +0.29 %
threejs_production-mode_10x + exec 5.48 s ± 33 ms 5.55 s ± 41 ms +1.39 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ✅ success

Please sign in to comment.