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

feat: rename during walk assign expr #5701

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
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";
Loading