Skip to content

Commit 2c06eda

Browse files
CopilotBrooooooklynsapphi-redautofix-ci[bot]
authored
test(minifier): add comprehensive test coverage for unary plus removal (#12860)
This PR adds comprehensive test coverage for the existing unary plus removal functionality in `substitute_alternate_syntax.rs`, which safely removes redundant unary plus operators (`+`) in mathematical operations while avoiding BigInt compatibility issues. ## Problem The existing implementation correctly handles safe removal of unary plus operators, but the test coverage was minimal and didn't demonstrate the full capability of the feature. This made it unclear what scenarios were supported. ## Solution Added comprehensive test cases that demonstrate the implementation correctly handles: - **Mathematical operations**: Division, multiplication, bitwise operations (`|`, `&`, `^`) - **Complex expressions**: Method calls, member access, nested expressions - **Safety checks**: Properly preserves unary plus when operand types are unknown to avoid BigInt runtime errors ## Examples ```javascript // ✅ Safe optimizations (other operand is number literal) +d / 1000 → d / 1000 1000 * +d → 1000 * d 5 | +b → 5 | b 2 - +x.call() → 2 - x.call() // ✅ Correctly preserved (unknown operand types) a | +b → a | +b (unchanged) +a * b → +a * b (unchanged) ``` The expanded test suite confirms the implementation works correctly across all supported mathematical operations while maintaining BigInt safety. <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/oxc-project/oxc/issues/new?title=✨Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Brooooooklyn <3468483+Brooooooklyn@users.noreply.github.com> Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent d253ad7 commit 2c06eda

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,21 @@ mod test {
16891689
test_same("v = +foo - bar");
16901690
test_same("v = foo - +bar");
16911691
test_same("v = 1 + +foo"); // cannot compress into `1 + foo` because `foo` can be a string
1692+
1693+
test("v = +d / 1000", "v = d / 1000");
1694+
test("v = 1000 * +d", "v = 1000 * d");
1695+
test("v = +d * 1000", "v = d * 1000");
1696+
test("v = 2 - +this._x.call(null, node.data)", "v = 2 - this._x.call(null, node.data)");
1697+
1698+
test("v = 5 | +b", "v = 5 | b");
1699+
test("v = +b | 5", "v = b | 5");
1700+
test("v = 7 & +c", "v = 7 & c");
1701+
test("v = 3 ^ +d", "v = 3 ^ d");
1702+
// Don't remove - unsafe for BigInt operations
1703+
test_same("v = a - +b");
1704+
test_same("v = +a - b");
1705+
test_same("v = a | +b");
1706+
test_same("v = +a | b");
16921707
}
16931708

16941709
#[test]

0 commit comments

Comments
 (0)