Skip to content

Commit

Permalink
feat: rename during walk assign expr (#5701)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored Feb 20, 2024
1 parent c45873e commit 6a1e9c2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
24 changes: 6 additions & 18 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use swc_core::common::Span;
use swc_core::ecma::ast::{BinExpr, CallExpr, IfStmt, UnaryOp, VarDecl, VarDeclarator};
use swc_core::ecma::ast::{BinExpr, CallExpr, Expr};
use swc_core::ecma::ast::{IfStmt, MemberExpr, Stmt, UnaryOp, VarDecl, VarDeclarator};

use super::{BoxJavascriptParserPlugin, JavascriptParserPlugin};
use crate::parser_plugin::r#const::is_logic_op;
Expand Down Expand Up @@ -323,12 +324,7 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn rename(
&self,
parser: &mut JavascriptParser,
expr: &swc_core::ecma::ast::Expr,
str: &str,
) -> Option<bool> {
fn rename(&self, parser: &mut JavascriptParser, expr: &Expr, str: &str) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.rename(parser, expr, str);
// `SyncBailHook`
Expand All @@ -339,11 +335,7 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn pre_statement(
&self,
parser: &mut JavascriptParser,
stmt: &swc_core::ecma::ast::Stmt,
) -> Option<bool> {
fn pre_statement(&self, parser: &mut JavascriptParser, stmt: &Stmt) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.pre_statement(parser, stmt);
// `SyncBailHook`
Expand All @@ -354,11 +346,7 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn import_call(
&self,
parser: &mut JavascriptParser,
expr: &swc_core::ecma::ast::CallExpr,
) -> Option<bool> {
fn import_call(&self, parser: &mut JavascriptParser, expr: &CallExpr) -> Option<bool> {
assert!(expr.callee.is_import());
for plugin in &self.plugins {
let res = plugin.import_call(parser, expr);
Expand Down Expand Up @@ -390,7 +378,7 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
&self,
parser: &mut JavascriptParser,
root_info: &ExportedVariableInfo,
expr: &swc_core::ecma::ast::MemberExpr,
expr: &MemberExpr,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.unhandled_expression_member_chain(parser, root_info, expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,17 @@ impl<'parser> JavascriptParser<'parser> {
{
// empty
} else if let Some(ident) = expr.left.as_ident() {
// if let Some(rename_identifier) = self.get_rename_identifier(&expr.right) {
// // TODO:
// }
if let Some(rename_identifier) = self.get_rename_identifier(&expr.right)
&& let Some(name) = rename_identifier.call_hooks_name(self)
&& let drive = self.plugin_drive.clone()
&& drive.can_rename(self, &name).unwrap_or_default()
{
if !drive.rename(self, &expr.right, &name).unwrap_or_default() {
self.set_variable(ident.sym.to_string(), name);
}
self.enter_assign = false;
return;
}
self.walk_expression(&expr.right);
self.enter_pattern(
Cow::Owned(warp_ident_to_pat(ident.clone())),
Expand Down
13 changes: 13 additions & 0 deletions packages/rspack/tests/cases/parsing/issue-3346/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function loadLocale(name) {
var local, aliasedRequire;
aliasedRequire = require;
local = aliasedRequire("./locale/" + name);
return local;
}

it("alias require should be works", () => {
expect(loadLocale("a")).toBe("a");
expect(loadLocale("a.js")).toBe("a");
expect(loadLocale("b")).toBe("b");
expect(loadLocale("b.js")).toBe("b");
});
1 change: 1 addition & 0 deletions packages/rspack/tests/cases/parsing/issue-3346/locale/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
1 change: 1 addition & 0 deletions packages/rspack/tests/cases/parsing/issue-3346/locale/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "b";

1 comment on commit 6a1e9c2

@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-02-20 f2130a0) Current Change
10000_development-mode + exec 1.7 s ± 25 ms 1.69 s ± 10 ms -0.83 %
10000_development-mode_hmr + exec 920 ms ± 9.9 ms 912 ms ± 13 ms -0.86 %
10000_production-mode + exec 2.66 s ± 38 ms 2.7 s ± 75 ms +1.37 %
arco-pro_development-mode + exec 2.48 s ± 27 ms 2.46 s ± 27 ms -0.66 %
arco-pro_development-mode_hmr + exec 869 ms ± 16 ms 867 ms ± 15 ms -0.17 %
arco-pro_production-mode + exec 4.07 s ± 53 ms 4.05 s ± 31 ms -0.64 %
threejs_development-mode_10x + exec 1.91 s ± 19 ms 1.89 s ± 24 ms -0.64 %
threejs_development-mode_10x_hmr + exec 1.14 s ± 12 ms 1.13 s ± 7.2 ms -0.75 %
threejs_production-mode_10x + exec 5.83 s ± 120 ms 5.7 s ± 136 ms -2.23 %

Please sign in to comment.