Skip to content

Commit 80600b4

Browse files
authored
refactor: use Atom for parser variable name (#11483)
1 parent 5676471 commit 80600b4

30 files changed

+264
-346
lines changed

crates/rspack_plugin_javascript/src/parser_plugin/amd/amd_define_dependency_parser_plugin.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl AMDDefineDependencyParserPlugin {
117117
parser: &mut JavascriptParser,
118118
call_expr: &CallExpr,
119119
param: &BasicEvaluatedExpression,
120-
identifiers: &mut FxHashMap<usize, &'static str>, // param index => "require" | "module" | "exports"
120+
identifiers: &mut FxHashMap<usize, Atom>, // param index => "require" | "module" | "exports"
121121
named_module: &Option<Atom>,
122122
) -> Option<bool> {
123123
if param.is_array() {
@@ -126,7 +126,7 @@ impl AMDDefineDependencyParserPlugin {
126126
if item.is_string() {
127127
let item = item.string();
128128
if let Some(i) = RESERVED_NAMES.iter().position(|s| s == item) {
129-
identifiers.insert(idx, RESERVED_NAMES[i]);
129+
identifiers.insert(idx, RESERVED_NAMES[i].into());
130130
}
131131
}
132132
let result = self.process_item(parser, call_expr, item, named_module);
@@ -140,15 +140,15 @@ impl AMDDefineDependencyParserPlugin {
140140
let array = param.array();
141141
for (i, request) in array.iter().enumerate() {
142142
if request == "require" {
143-
identifiers.insert(i, REQUIRE);
143+
identifiers.insert(i, REQUIRE.into());
144144
deps.push(AMDRequireArrayItem::String(
145145
RuntimeGlobals::REQUIRE.name().into(),
146146
));
147147
} else if request == "exports" {
148-
identifiers.insert(i, EXPORTS);
148+
identifiers.insert(i, EXPORTS.into());
149149
deps.push(AMDRequireArrayItem::String(request.into()));
150150
} else if request == "module" {
151-
identifiers.insert(i, MODULE);
151+
identifiers.insert(i, MODULE.into());
152152
deps.push(AMDRequireArrayItem::String(request.into()));
153153
} else if let Some(local_module) = parser.get_local_module_mut(request) {
154154
local_module.flag_used();
@@ -464,8 +464,8 @@ impl AMDDefineDependencyParserPlugin {
464464
}
465465
let idx = i - fn_params_offset;
466466
i += 1;
467-
if let Some(&name) = identifiers.get(&idx) {
468-
fn_renames.insert(get_ident_name(param), name);
467+
if let Some(name) = identifiers.get(&idx) {
468+
fn_renames.insert(get_ident_name(param), name.clone());
469469
return false;
470470
}
471471
true
@@ -480,7 +480,7 @@ impl AMDDefineDependencyParserPlugin {
480480
let idx = i - fn_params_offset;
481481
i += 1;
482482
if idx < RESERVED_NAMES.len() {
483-
fn_renames.insert(get_ident_name(param), RESERVED_NAMES[idx]);
483+
fn_renames.insert(get_ident_name(param), RESERVED_NAMES[idx].into());
484484
return false;
485485
}
486486
true
@@ -493,12 +493,12 @@ impl AMDDefineDependencyParserPlugin {
493493
true,
494494
fn_params.expect("fn_params should not be None").into_iter(),
495495
|parser| {
496-
for (name, &rename_identifier) in fn_renames.iter() {
496+
for (name, rename_identifier) in fn_renames.iter() {
497497
let variable = parser
498498
.get_variable_info(rename_identifier)
499499
.map(|info| ExportedVariableInfo::VariableInfo(info.id()))
500-
.unwrap_or(ExportedVariableInfo::Name(rename_identifier.to_string()));
501-
parser.set_variable(name.to_string(), variable);
500+
.unwrap_or(ExportedVariableInfo::Name(rename_identifier.clone()));
501+
parser.set_variable(name.clone(), variable);
502502
}
503503

504504
parser.in_try = in_try;
@@ -549,12 +549,12 @@ impl AMDDefineDependencyParserPlugin {
549549
.is_some_and(|ident| !RESERVED_NAMES.contains(&ident.sym.as_str()))
550550
}),
551551
|parser| {
552-
for (name, &rename_identifier) in fn_renames.iter() {
552+
for (name, rename_identifier) in fn_renames.iter() {
553553
let variable = parser
554554
.get_variable_info(rename_identifier)
555555
.map(|info| ExportedVariableInfo::VariableInfo(info.id()))
556-
.unwrap_or(ExportedVariableInfo::Name(rename_identifier.to_string()));
557-
parser.set_variable(name.to_string(), variable);
556+
.unwrap_or(ExportedVariableInfo::Name(rename_identifier.clone()));
557+
parser.set_variable(name.clone(), variable);
558558
}
559559

560560
parser.in_try = in_try;

crates/rspack_plugin_javascript/src/parser_plugin/amd/amd_plugin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ impl JavascriptParserPlugin for AMDParserPlugin {
167167
) -> Option<BasicEvaluatedExpression<'static>> {
168168
if for_name == DEFINE_AMD {
169169
return Some(evaluate_to_identifier(
170-
for_name.to_string(),
171-
"define".to_string(),
170+
for_name.into(),
171+
"define".into(),
172172
Some(true),
173173
start,
174174
end,
@@ -177,8 +177,8 @@ impl JavascriptParserPlugin for AMDParserPlugin {
177177

178178
if for_name == REQUIRE_AMD {
179179
return Some(evaluate_to_identifier(
180-
for_name.to_string(),
181-
"require".to_string(),
180+
for_name.into(),
181+
"require".into(),
182182
Some(true),
183183
start,
184184
end,

crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
use rspack_core::{ConstDependency, RuntimeGlobals, RuntimeRequirementsDependency, SpanExt};
2+
use rspack_error::{Severity, TraceableError};
23
use swc_core::{
3-
common::Spanned,
4-
ecma::ast::{CallExpr, Callee, Expr, Ident, UnaryExpr},
4+
common::{SourceFile, Span, Spanned},
5+
ecma::ast::{CallExpr, Ident, UnaryExpr},
56
};
67

78
use crate::{
89
dependency::ModuleArgumentDependency,
910
parser_plugin::JavascriptParserPlugin,
1011
utils::eval::{self, BasicEvaluatedExpression},
11-
visitors::{JavascriptParser, expr_matcher, expression_not_supported, extract_member_root},
12+
visitors::{JavascriptParser, create_traceable_error},
1213
};
1314

15+
fn expression_not_supported(
16+
file: &SourceFile,
17+
name: &str,
18+
is_call: bool,
19+
expr_span: Span,
20+
) -> (Box<TraceableError>, Box<ConstDependency>) {
21+
(
22+
Box::new(
23+
create_traceable_error(
24+
"Unsupported feature".into(),
25+
format!(
26+
"{name}{} is not supported by Rspack.",
27+
if is_call { "()" } else { "" }
28+
),
29+
file,
30+
expr_span.into(),
31+
)
32+
.with_severity(Severity::Warn)
33+
.with_hide_stack(Some(true)),
34+
),
35+
Box::new(ConstDependency::new(
36+
expr_span.into(),
37+
"(void 0)".into(),
38+
None,
39+
)),
40+
)
41+
}
42+
1443
const WEBPACK_HASH: &str = "__webpack_hash__";
1544
const WEBPACK_LAYER: &str = "__webpack_layer__";
1645
const WEBPACK_PUBLIC_PATH: &str = "__webpack_public_path__";
@@ -318,54 +347,42 @@ impl JavascriptParserPlugin for APIPlugin {
318347
&self,
319348
parser: &mut JavascriptParser,
320349
member_expr: &swc_core::ecma::ast::MemberExpr,
321-
_name: &str,
350+
for_name: &str,
322351
) -> Option<bool> {
323-
macro_rules! not_supported_expr {
324-
($check: ident, $expr: ident, $name: literal) => {
325-
if expr_matcher::$check($expr) {
326-
let (warning, dep) = expression_not_supported(&parser.source_file, $name, $expr);
327-
parser.warning_diagnostics.push(warning);
328-
parser.presentational_dependencies.push(dep);
329-
return Some(true);
330-
}
331-
};
332-
}
333-
334-
let expr = &swc_core::ecma::ast::Expr::Member(member_expr.to_owned());
335-
336-
if let Some(root) = extract_member_root(expr)
337-
&& let s = root.sym.as_str()
338-
&& parser.is_unresolved_ident(s)
352+
if for_name == "require.extensions"
353+
|| for_name == "require.config"
354+
|| for_name == "require.version"
355+
|| for_name == "require.include"
356+
|| for_name == "require.onError"
357+
|| for_name == "require.main.require"
358+
|| for_name == "module.parent.require"
339359
{
340-
if s == "require" {
341-
not_supported_expr!(is_require_extensions, expr, "require.extensions");
342-
not_supported_expr!(is_require_config, expr, "require.config");
343-
not_supported_expr!(is_require_version, expr, "require.version");
344-
not_supported_expr!(is_require_include, expr, "require.include");
345-
not_supported_expr!(is_require_onerror, expr, "require.onError");
346-
not_supported_expr!(is_require_main_require, expr, "require.main.require");
347-
} else if s == "module" {
348-
not_supported_expr!(is_module_parent_require, expr, "module.parent.require");
349-
}
360+
let (warning, dep) =
361+
expression_not_supported(parser.source_file, for_name, false, member_expr.span());
362+
parser.warning_diagnostics.push(warning);
363+
parser.presentational_dependencies.push(dep);
364+
return Some(true);
350365
}
351366

352-
if expr_matcher::is_require_cache(expr) {
367+
if for_name == "require.cache" {
353368
parser
354369
.presentational_dependencies
355370
.push(Box::new(ConstDependency::new(
356-
expr.span().into(),
371+
member_expr.span().into(),
357372
RuntimeGlobals::MODULE_CACHE.name().into(),
358373
Some(RuntimeGlobals::MODULE_CACHE),
359374
)));
360-
Some(true)
361-
} else if expr_matcher::is_require_main(expr) {
375+
return Some(true);
376+
}
377+
378+
if for_name == "require.main" {
362379
let mut runtime_requirements = RuntimeGlobals::default();
363380
runtime_requirements.insert(RuntimeGlobals::MODULE_CACHE);
364381
runtime_requirements.insert(RuntimeGlobals::ENTRY_MODULE_ID);
365382
parser
366383
.presentational_dependencies
367384
.push(Box::new(ConstDependency::new(
368-
expr.span().into(),
385+
member_expr.span().into(),
369386
format!(
370387
"{}[{}]",
371388
RuntimeGlobals::MODULE_CACHE,
@@ -374,8 +391,10 @@ impl JavascriptParserPlugin for APIPlugin {
374391
.into(),
375392
Some(runtime_requirements),
376393
)));
377-
Some(true)
378-
} else if expr_matcher::is_webpack_module_id(expr) {
394+
return Some(true);
395+
}
396+
397+
if for_name == "__webpack_module__.id" {
379398
parser
380399
.presentational_dependencies
381400
.push(Box::new(RuntimeRequirementsDependency::new(
@@ -385,51 +404,32 @@ impl JavascriptParserPlugin for APIPlugin {
385404
.presentational_dependencies
386405
.push(Box::new(ModuleArgumentDependency::new(
387406
Some("id".into()),
388-
expr.span().into(),
407+
member_expr.span().into(),
389408
Some(parser.source_map.clone()),
390409
)));
391-
Some(true)
392-
} else {
393-
None
394-
}
395-
}
396-
397-
fn call(&self, parser: &mut JavascriptParser, call_expr: &CallExpr, _name: &str) -> Option<bool> {
398-
macro_rules! not_supported_call {
399-
($check: ident, $name: literal) => {
400-
if let Callee::Expr(expr_box) = &call_expr.callee
401-
&& let Expr::Member(expr) = &**expr_box
402-
&& expr_matcher::$check(&Expr::Member(expr.to_owned()))
403-
{
404-
let (warning, dep) = expression_not_supported(
405-
&parser.source_file,
406-
$name,
407-
&Expr::Call(call_expr.to_owned()),
408-
);
409-
parser.warning_diagnostics.push(warning);
410-
parser.presentational_dependencies.push(dep);
411-
return Some(true);
412-
}
413-
};
410+
return Some(true);
414411
}
415412

416-
let root = call_expr
417-
.callee
418-
.as_expr()
419-
.and_then(|expr| extract_member_root(expr));
413+
None
414+
}
420415

421-
if let Some(root) = root
422-
&& let s = root.sym.as_str()
423-
&& parser.is_unresolved_ident(s)
416+
fn call(
417+
&self,
418+
parser: &mut JavascriptParser,
419+
call_expr: &CallExpr,
420+
for_name: &str,
421+
) -> Option<bool> {
422+
if for_name == "require.config"
423+
|| for_name == "require.include"
424+
|| for_name == "require.onError"
425+
|| for_name == "require.main.require"
426+
|| for_name == "module.parent.require"
424427
{
425-
if s == "require" {
426-
not_supported_call!(is_require_config, "require.config()");
427-
not_supported_call!(is_require_include, "require.include()");
428-
not_supported_call!(is_require_onerror, "require.onError()");
429-
not_supported_call!(is_require_main_require, "require.main.require()");
430-
} else if s == "module" {
431-
not_supported_call!(is_module_parent_require, "module.parent.require()");
432-
}
428+
let (warning, dep) =
429+
expression_not_supported(parser.source_file, for_name, true, call_expr.span());
430+
parser.warning_diagnostics.push(warning);
431+
parser.presentational_dependencies.push(dep);
432+
return Some(true);
433433
}
434434

435435
None

crates/rspack_plugin_javascript/src/parser_plugin/common_js_exports_parse_plugin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ impl JavascriptParser<'_> {
140140
}
141141

142142
fn is_module_ident(&mut self, ident: &Ident) -> bool {
143-
ident.sym == MODULE_NAME && self.is_unresolved_ident(MODULE_NAME)
143+
ident.sym == MODULE_NAME && !self.is_variable_defined(&MODULE_NAME.into())
144144
}
145145

146146
fn is_exports_ident<E: ExprLike>(&mut self, expr: &E) -> bool {
147-
expr
148-
.as_ident()
149-
.is_some_and(|ident| ident.sym == EXPORTS_NAME && self.is_unresolved_ident(EXPORTS_NAME))
147+
expr.as_ident().is_some_and(|ident| {
148+
ident.sym == EXPORTS_NAME && !self.is_variable_defined(&EXPORTS_NAME.into())
149+
})
150150
}
151151

152152
fn is_exports_expr<E: ExprLike>(&mut self, expr: &E) -> bool {
@@ -243,7 +243,7 @@ impl JavascriptParser<'_> {
243243
.map(|expr| {
244244
if matches!(
245245
&**expr,
246-
Expr::Ident(ident) if &ident.sym == "require" && self.is_unresolved_ident("require")
246+
Expr::Ident(ident) if &ident.sym == "require" && !self.is_variable_defined(&ident.sym)
247247
) {
248248
return true;
249249
}

0 commit comments

Comments
 (0)