Skip to content

Commit

Permalink
chore: 🤖 add rewrite usage span to record usage to rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Sep 26, 2023
1 parent afe2d3b commit bab35d5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
4 changes: 3 additions & 1 deletion crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use dyn_clone::{clone_trait_object, DynClone};
use crate::{
ChunkGroupOptionsKindRef, ConnectionState, Context, ContextMode, ContextOptions, ErrorSpan,
ExtendedReferencedExport, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, ReferencedExport,
RuntimeSpec,
RuntimeSpec, UsedByExports,
};

// Used to describe dependencies' types, see webpack's `type` getter in `Dependency`
Expand Down Expand Up @@ -191,6 +191,8 @@ pub trait Dependency:
None
}

fn set_used_by_exports(&mut self, used_by_exports: Option<UsedByExports>) {}

Check failure on line 194 in crates/rspack_core/src/dependency/mod.rs

View workflow job for this annotation

GitHub Actions / Rust check

unused variable: `used_by_exports`

fn get_module_evaluation_side_effects_state(
&self,
_module_graph: &ModuleGraph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ impl Dependency for HarmonyImportSpecifierDependency {
fn id(&self) -> &DependencyId {
&self.id
}

fn set_used_by_exports(&mut self, used_by_exports: Option<UsedByExports>) {
self.used_by_exports = used_by_exports;
}
fn category(&self) -> &DependencyCategory {
&DependencyCategory::Esm
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagno
use swc_core::common::SyntaxContext;

use crate::utils::syntax_by_module_type;
use crate::visitors::ScanDependenciesResult;
use crate::visitors::{run_before_pass, scan_dependencies, swc_visitor::resolver};
use crate::{SideEffectsFlagPluginVisitor, SyntaxContextInfo};
#[derive(Debug)]
Expand Down Expand Up @@ -127,7 +128,11 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
));
});

let (dependencies, presentational_dependencies) = ast.visit(|program, context| {
let ScanDependenciesResult {
dependencies,
presentational_dependencies,
rewrite_usage_span,
} = ast.visit(|program, context| {
scan_dependencies(
program,
context.unresolved_mark,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct HarmonyImportDependencyScanner<'a> {
pub import_map: &'a mut ImportMap,
pub imports: Imports,
pub build_info: &'a mut BuildInfo,
pub rewrite_usage_span: &'a mut HashSet<Span>,
}

impl<'a> HarmonyImportDependencyScanner<'a> {
Expand All @@ -74,13 +75,15 @@ impl<'a> HarmonyImportDependencyScanner<'a> {
presentational_dependencies: &'a mut Vec<BoxDependencyTemplate>,
import_map: &'a mut ImportMap,
build_info: &'a mut BuildInfo,
rewrite_usage_span: &'a mut HashSet<Span>,
) -> Self {
Self {
dependencies,
presentational_dependencies,
import_map,
imports: Default::default(),
build_info,
rewrite_usage_span,
}
}
}
Expand Down Expand Up @@ -158,6 +161,7 @@ impl Visit for HarmonyImportDependencyScanner<'_> {
program.visit_children_with(&mut HarmonyImportRefDependencyScanner::new(
self.import_map,
self.dependencies,
self.rewrite_usage_span,
));
}

Expand Down Expand Up @@ -300,15 +304,22 @@ pub struct HarmonyImportRefDependencyScanner<'a> {
pub import_map: &'a ImportMap,
pub dependencies: &'a mut Vec<BoxDependency>,
pub properties_in_destructuring: HashMap<JsWord, HashSet<JsWord>>,

pub rewrite_usage_span: &'a mut HashSet<Span>,
}

impl<'a> HarmonyImportRefDependencyScanner<'a> {
pub fn new(import_map: &'a ImportMap, dependencies: &'a mut Vec<BoxDependency>) -> Self {
pub fn new(
import_map: &'a ImportMap,
dependencies: &'a mut Vec<BoxDependency>,
rewrite_usage_span: &'a mut HashSet<Span>,
) -> Self {
Self {
import_map,
dependencies,
enter_callee: false,
properties_in_destructuring: HashMap::default(),
rewrite_usage_span,
}
}
}
Expand All @@ -331,6 +342,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> {
fn visit_prop(&mut self, n: &Prop) {
match n {
Prop::Shorthand(shorthand) => {
self.rewrite_usage_span.insert(shorthand.span);
if let Some(reference) = self.import_map.get(&shorthand.to_id()) {
self
.dependencies
Expand All @@ -353,6 +365,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> {

fn visit_ident(&mut self, ident: &Ident) {
if let Some(reference) = self.import_map.get(&ident.to_id()) {
self.rewrite_usage_span.insert(ident.span);
self
.dependencies
.push(Box::new(HarmonyImportSpecifierDependency::new(
Expand Down Expand Up @@ -386,6 +399,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> {
if let Some(prop) = prop {
let mut ids = reference.names.clone().map(|f| vec![f]).unwrap_or_default();
ids.push(prop);
self.rewrite_usage_span.insert(member_expr.span);
self
.dependencies
.push(Box::new(HarmonyImportSpecifierDependency::new(
Expand Down
18 changes: 16 additions & 2 deletions crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use rspack_core::{
ast::javascript::Program, BoxDependency, BoxDependencyTemplate, BuildInfo, BuildMeta,
CompilerOptions, ModuleIdentifier, ModuleType, ResourceData,
};
use rustc_hash::FxHashSet as HashSet;
use swc_core::common::Span;
use swc_core::common::{comments::Comments, Mark, SyntaxContext};
pub use util::*;

Expand All @@ -36,7 +38,12 @@ use self::{
node_stuff_scanner::NodeStuffScanner, require_context_scanner::RequireContextScanner,
url_scanner::UrlScanner, worker_scanner::WorkerScanner,
};
pub type ScanDependenciesResult = (Vec<BoxDependency>, Vec<BoxDependencyTemplate>);

pub struct ScanDependenciesResult {
pub dependencies: Vec<BoxDependency>,
pub presentational_dependencies: Vec<BoxDependencyTemplate>,
pub rewrite_usage_span: HashSet<Span>,
}

#[allow(clippy::too_many_arguments)]
pub fn scan_dependencies(
Expand All @@ -54,6 +61,8 @@ pub fn scan_dependencies(
let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark);
let comments = program.comments.clone();
let mut parser_exports_state = None;

let mut rewrite_usage_span = HashSet::default();
program.visit_with(&mut ApiScanner::new(
&unresolved_ctxt,
resource_data,
Expand Down Expand Up @@ -114,6 +123,7 @@ pub fn scan_dependencies(
&mut presentational_dependencies,
&mut import_map,
build_info,
&mut rewrite_usage_span,
));
program.visit_with(&mut HarmonyExportDependencyScanner::new(
&mut dependencies,
Expand Down Expand Up @@ -156,5 +166,9 @@ pub fn scan_dependencies(
));
}

(dependencies, presentational_dependencies)
ScanDependenciesResult {
dependencies,
presentational_dependencies,
rewrite_usage_span,
}
}

0 comments on commit bab35d5

Please sign in to comment.