8000 feat(minifier): implement known method `Array.of` by 7086cmd · Pull Request #8805 · oxc-project/oxc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(minifier): implement known method Array.of #8805

8000
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

Merged
Merged
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
44 changes: 30 additions & 14 deletions crates/oxc_minifier/src/peephole/replace_known_methods.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::borrow::Cow;

use cow_utils::CowUtils;
use std::borrow::Cow;

use oxc_ast::ast::*;
use oxc_ecmascript::{
Expand All @@ -26,11 +25,11 @@ impl<'a> PeepholeOptimizations {
ctx: Ctx<'a, '_>,
) {
self.try_fold_concat_chain(node, ctx);
self.try_fold_known_string_methods(node, ctx);
self.try_fold_known_global_methods(node, ctx);
self.try_fold_known_property_access(node, ctx);
}

fn try_fold_known_string_methods(&mut self, node: &mut Expression<'a>, ctx: Ctx<'a, '_>) {
fn try_fold_known_global_methods(&mut self, node: &mut Expression<'a>, ctx: Ctx<'a, '_>) {
let Expression::CallExpression(ce) = node else { return };
let CallExpression { span, callee, arguments, .. } = ce.as_mut();
let (name, object) = match &callee {
Expand Down Expand Up @@ -69,6 +68,7 @@ impl<'a> PeepholeOptimizations {
Self::try_fold_math_unary(*span, arguments, name, object, ctx)
}
"min" | "max" => Self::try_fold_math_variadic(*span, arguments, name, object, ctx),
"of" => Self::try_fold_array_of(*span, arguments, name, object, ctx),
_ => None,
};
if let Some(replacement) = replacement {
Expand Down Expand Up @@ -775,6 +775,26 @@ impl<'a> PeepholeOptimizations {
_ => return None,
})
}

fn try_fold_array_of(
span: Span,
arguments: &mut Arguments<'a>,
name: &str,
object: &Expression<'a>,
ctx: Ctx<'a, '_>,
) -> Option<Expression<'a>> {
if !Self::validate_global_reference(object, "Array", ctx) {
return None;
}
if name != "of" {
return None;
}
Some(ctx.ast.expression_array(
span,
ctx.ast.vec_from_iter(arguments.drain(..).map(ArrayExpressionElement::from)),
None,
))
}
}

/// Port from: <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeReplaceKnownMethodsTest.java>
Expand Down Expand Up @@ -1575,39 +1595,35 @@ mod test {
}

#[test]
#[ignore]
fn test_array_of_spread() {
test("x = Array.of(...['a', 'b', 'c'])", "x = [...['a', 'b', 'c']]");
test("x = Array.of(...['a', 'b', 'c',])", "x = [...['a', 'b', 'c']]");
test("x = Array.of(...['a'], ...['b', 'c'])", "x = [...['a'], ...['b', 'c']]");
test("x = Array.of('a', ...['b', 'c'])", "x = ['a', ...['b', 'c']]");
test("x = Array.of('a', ...['b', 'c'])", "x = ['a', ...['b', 'c']]");
// Here, since our tests are fully opened, the dce may automatically optimize it into a simple array, instead of simply substitute the function call.
test("x = Array.of(...['a', 'b', 'c'])", "x = ['a', 'b', 'c']");
test("x = Array.of(...['a', 'b', 'c',])", "x = ['a', 'b', 'c']");
test("x = Array.of(...['a'], ...['b', 'c'])", "x = ['a', 'b', 'c']");
test("x = Array.of('a', ...['b', 'c'])", "x = ['a', 'b', 'c']");
test("x = Array.of('a', ...['b', 'c'])", "x = ['a', 'b', 'c']");
}

#[test]
#[ignore]
fn test_array_of_no_spread() {
test("x = Array.of('a', 'b', 'c')", "x = ['a', 'b', 'c']");
test("x = Array.of('a', ['b', 'c'])", "x = ['a', ['b', 'c']]");
test("x = Array.of('a', ['b', 'c'],)", "x = ['a', ['b', 'c']]");
}

#[test]
#[ignore]
fn test_array_of_no_args() {
test("x = Array.of()", "x = []");
}

#[test]
#[ignore]
fn test_array_of_no_change() {
test_same("x = Array.of.apply(window, ['a', 'b', 'c'])");
test_same("x = ['a', 'b', 'c']");
test_same("x = [Array.of, 'a', 'b', 'c']");
}

#[test]
#[ignore]
fn test_fold_array_bug() {
test_same("Array[123]()");
}
Expand Down
Loading
0