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

fix:amd-define-in-parameter (#9078) #9083

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,12 @@ impl CommonJsImportsParserPlugin {
}

impl JavascriptParserPlugin for CommonJsImportsParserPlugin {
fn can_rename(&self, parser: &mut JavascriptParser, str: &str) -> Option<bool> {
fn can_rename(
&self,
parser: &mut JavascriptParser,
str: &str,
_is_parameter: bool,
) -> Option<bool> {
if str == expr_name::REQUIRE && parser.is_unresolved_ident(str) {
Some(true)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub(super) struct DefineParserPlugin {
}

impl JavascriptParserPlugin for DefineParserPlugin {
fn can_rename(&self, _: &mut JavascriptParser, str: &str) -> Option<bool> {
fn can_rename(&self, _: &mut JavascriptParser, str: &str, _is_parameter: bool) -> Option<bool> {
self.walk_data.can_rename.contains(str).then_some(true)
}

Expand Down
9 changes: 7 additions & 2 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,14 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn can_rename(&self, parser: &mut JavascriptParser, str: &str) -> Option<bool> {
fn can_rename(
&self,
parser: &mut JavascriptParser,
str: &str,
is_parameter: bool,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.can_rename(parser, str);
let res = plugin.can_rename(parser, str, is_parameter);
// `SyncBailHook`
if res.is_some() {
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ impl ProvidePlugin {
}

impl JavascriptParserPlugin for ProvidePlugin {
fn can_rename(&self, _parser: &mut JavascriptParser, str: &str) -> Option<bool> {
fn can_rename(
&self,
_parser: &mut JavascriptParser,
str: &str,
_is_parameter: bool,
) -> Option<bool> {
let names = self.cached_names();
names.iter().any(|l| l.eq(str)).then_some(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,13 @@ impl JavascriptParserPlugin for RequireJsStuffPlugin {
None
}

fn can_rename(&self, _parser: &mut JavascriptParser, for_name: &str) -> Option<bool> {
if for_name == DEFINE {
fn can_rename(
&self,
_parser: &mut JavascriptParser,
for_name: &str,
is_parameter: bool,
) -> Option<bool> {
if for_name == DEFINE && !is_parameter {
return Some(true);
}
None
Expand Down
7 changes: 6 additions & 1 deletion crates/rspack_plugin_javascript/src/parser_plugin/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ pub trait JavascriptParserPlugin {
/// The return value will have no effect.
fn top_level_for_of_await_stmt(&self, _parser: &mut JavascriptParser, _stmt: &ForOfStmt) {}

fn can_rename(&self, _parser: &mut JavascriptParser, _str: &str) -> Option<bool> {
fn can_rename(
&self,
_parser: &mut JavascriptParser,
_str: &str,
_is_parameter: bool,
) -> Option<bool> {
None
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ pub struct URLPlugin {
}

impl JavascriptParserPlugin for URLPlugin {
fn can_rename(&self, _parser: &mut JavascriptParser, for_name: &str) -> Option<bool> {
fn can_rename(
&self,
_parser: &mut JavascriptParser,
for_name: &str,
_is_parameter: bool,
) -> Option<bool> {
(for_name == "URL").then_some(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl JavascriptParser<'_> {
{
let drive = self.plugin_drive.clone();
if drive
.can_rename(self, &renamed_identifier)
.can_rename(self, &renamed_identifier, false)
.unwrap_or_default()
{
if !drive
Expand Down Expand Up @@ -800,15 +800,15 @@ impl JavascriptParser<'_> {
for param in &expr.function.params {
let ident = param.pat.as_ident().expect("should be a `BindingIdent`");
fn_params.push(ident);
if get_variable_info(self, &Expr::Ident(ident.id.clone())).is_none() {
if get_variable_info(self, &Expr::Ident(ident.id.clone()), true).is_none() {
scope_params.push(Cow::Borrowed(&param.pat));
}
}
} else if let Some(expr) = expr.as_arrow() {
for param in &expr.params {
let ident = param.as_ident().expect("should be a `BindingIdent`");
fn_params.push(ident);
if get_variable_info(self, &Expr::Ident(ident.id.clone())).is_none() {
if get_variable_info(self, &Expr::Ident(ident.id.clone()), true).is_none() {
scope_params.push(Cow::Borrowed(param));
}
}
Expand Down Expand Up @@ -1080,7 +1080,9 @@ impl JavascriptParser<'_> {
if let Some(rename_identifier) = self.get_rename_identifier(&expr.right)
&& let drive = self.plugin_drive.clone()
&& rename_identifier
.call_hooks_name(self, |this, for_name| drive.can_rename(this, for_name))
.call_hooks_name(self, |this, for_name| {
drive.can_rename(this, for_name, false)
})
.unwrap_or_default()
{
if !rename_identifier
Expand Down Expand Up @@ -1485,11 +1487,14 @@ fn member_prop_len(member_prop: &MemberProp) -> Option<usize> {
fn get_variable_info<'p>(
parser: &'p mut JavascriptParser,
expr: &Expr,
is_parameter: bool,
) -> Option<&'p VariableInfo> {
if let Some(rename_identifier) = parser.get_rename_identifier(expr)
&& let drive = parser.plugin_drive.clone()
&& rename_identifier
.call_hooks_name(parser, |this, for_name| drive.can_rename(this, for_name))
.call_hooks_name(parser, |this, for_name| {
drive.can_rename(this, for_name, is_parameter)
})
.unwrap_or_default()
&& !rename_identifier
.call_hooks_name(parser, |this, for_name| drive.rename(this, expr, for_name))
Expand All @@ -1504,7 +1509,9 @@ fn get_variable_name(parser: &mut JavascriptParser, expr: &Expr) -> Option<Strin
if let Some(rename_identifier) = parser.get_rename_identifier(expr)
&& let drive = parser.plugin_drive.clone()
&& rename_identifier
.call_hooks_name(parser, |this, for_name| drive.can_rename(this, for_name))
.call_hooks_name(parser, |this, for_name| {
drive.can_rename(this, for_name, false)
})
.unwrap_or_default()
&& !rename_identifier
.call_hooks_name(parser, |this, for_name| drive.rename(this, expr, for_name))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

it("((function (define) {...}) (define)) should compile well", function () {
// Throws an exception message if it is a compilation exception:
// (function (__webpack_require__.amdD) {
// SyntaxError: Unexpected token '.'

expect(() => require('./lib')).toThrow('define cannot be used indirect');
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function (define) {
if (define) {
define(function () { return {} });
}
})(
define
);
Loading