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: move context dependency walk_expression into create_context_dependency #6963

Merged
merged 11 commits into from
Jul 16, 2024
Next Next commit
Walk tpl expressions with odd index
  • Loading branch information
CPunisher authored and ahabhgk committed Jul 16, 2024
commit a855d42ae19f834f2f2d945850879576929b4125
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 19,13 @@ use crate::visitors::{extract_require_call_info, is_require_call_start};
fn create_commonjs_require_context_dependency(
parser: &mut JavascriptParser,
param: &BasicEvaluatedExpression,
expr: &Expr,
callee_start: u32,
callee_end: u32,
args_end: u32,
span: Option<ErrorSpan>,
) -> CommonJsRequireContextDependency {
let result = create_context_dependency(param, parser);
let result = create_context_dependency(param, expr, parser);
let options = ContextOptions {
mode: ContextMode::Sync,
recursive: true,
Expand Down Expand Up @@ -136,14 137,13 @@ impl CommonJsImportsParserPlugin {
let dep = create_commonjs_require_context_dependency(
parser,
param,
&argument_expr,
call_expr.callee.span().real_lo(),
call_expr.callee.span().real_hi(),
call_expr.span.real_hi(),
Some(call_expr.span.into()),
);
parser.dependencies.push(Box::new(dep));
// FIXME: align `parser.walk_expression` to webpack, which put into `context_dependency_helper`
parser.walk_expression(argument_expr);
Some(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 121,7 @@ impl JavascriptParserPlugin for ImportParserPlugin {
query,
fragment,
replaces,
} = create_context_dependency(&param, parser);
} = create_context_dependency(&param, &dyn_imported.expr, parser);
let reg_exp = context_reg_exp(&reg, "", Some(dyn_imported.span().into()), parser);
parser
.dependencies
Expand Down Expand Up @@ -156,8 156,6 @@ impl JavascriptParserPlugin for ImportParserPlugin {
Some(node.span.into()),
parser.in_try,
)));
// FIXME: align `parser.walk_expression` to webpack, which put into `context_dependency_helper`
parser.walk_expression(&dyn_imported.expr);
Some(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 4,13 @@ use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use rspack_core::parse_resource;
use rspack_core::SpanExt;
use rspack_error::Severity;
use rspack_util::json_stringify;
use swc_core::common::Spanned;
use swc_core::ecma::ast::Expr;
use swc_core::ecma::visit::Visit;
use swc_core::ecma::visit::VisitWith;

use super::create_traceable_error;
use crate::utils::eval::{BasicEvaluatedExpression, TemplateStringKind};
Expand All @@ -15,6 20,7 @@ const DEFAULT_WRAPPED_CONTEXT_REGEXP: &str = ".*";

pub fn create_context_dependency(
param: &BasicEvaluatedExpression,
expr: &Expr,
parser: &mut crate::visitors::JavascriptParser,
) -> ContextModuleScanResult {
if param.is_template_string() {
Expand Down Expand Up @@ -80,6 86,12 @@ pub fn create_context_dependency(
}
}

let mut walker = ExprSpanFinder {
targets: vec![&parts[1]],
on_visit: |n| parser.walk_expression(n),
};
expr.visit_with(&mut walker);

if parser.javascript_options.wrapped_context_critical {
let range = param.range();
parser.warning_diagnostics.push(Box::new(
Expand Down Expand Up @@ -215,3 227,21 @@ static META_REG: Lazy<Regex> = Lazy::new(|| {
pub fn quote_meta(str: &str) -> Cow<str> {
META_REG.replace_all(str, "\\$0")
}

struct ExprSpanFinder<'a, F: FnMut(&Expr) -> ()> {
targets: Vec<&'a BasicEvaluatedExpression>,
on_visit: F,
}

impl<'a, F: FnMut(&Expr) -> ()> Visit for ExprSpanFinder<'a, F> {
fn visit_expr(&mut self, n: &Expr) {
CPunisher marked this conversation as resolved.
Show resolved Hide resolved
for t in self.targets.iter() {
let span = n.span();
let (lo, hi) = t.range();
if span.real_lo() == lo && span.hi().0 == hi {
(self.on_visit)(n);
}
}
n.visit_children_with(self);
}
}