Skip to content
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
22 changes: 22 additions & 0 deletions crates/oxc_minifier/src/peephole/replace_known_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ impl<'a> PeepholeOptimizations {
}

pub fn replace_known_property_access(node: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) {
// property access should be kept to keep `this` value
if matches!(
ctx.parent(),
Ancestor::CallExpressionCallee(_) | Ancestor::TaggedTemplateExpressionTag(_)
) {
return;
}

let (name, object, span) = match node {
Expression::StaticMemberExpression(member) if !member.optional => {
(member.property.name.as_str(), &member.object, member.span)
Expand Down Expand Up @@ -1612,6 +1620,20 @@ mod test {
test_same("v = [...a, 1][1]");
test_same("v = [1, ...a][0]");
test("v = [1, ...[1,2]][0]", "v = 1");

// property access should be kept to keep `this` value
test_same(
"
function f(){ console.log(this[0]) }
['PASS',f][1]()
",
);
test_same(
"
function f(){ console.log(this[0]) }
['PASS',f][1]``
",
);
}

#[test]
Expand Down
Loading