mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
part of https://github.com/oxc-project/backlog/issues/58 `monitor-oxc` run: https://github.com/oxc-project/monitor-oxc/actions/runs/10179047831 binary expression stack length tally using `counts` in top 100 npm packages from monitor-oxc: ``` 29772 counts ( 1) 17652 (59.3%, 59.3%): 0 ( 2) 5772 (19.4%, 78.7%): 1 ( 3) 3204 (10.8%, 89.4%): 2 ( 4) 1276 ( 4.3%, 93.7%): 3 ( 5) 616 ( 2.1%, 95.8%): 4 ( 6) 308 ( 1.0%, 96.8%): 5 ( 7) 202 ( 0.7%, 97.5%): 6 ( 8) 168 ( 0.6%, 98.1%): 7 ( 9) 114 ( 0.4%, 98.5%): 9 ( 10) 90 ( 0.3%, 98.8%): 8 ( 11) 84 ( 0.3%, 99.0%): 13 ( 12) 58 ( 0.2%, 99.2%): 10 ( 13) 48 ( 0.2%, 99.4%): 12 ( 14) 32 ( 0.1%, 99.5%): 11 ( 15) 20 ( 0.1%, 99.6%): 134 ( 16) 16 ( 0.1%, 99.6%): 18 ( 17) 16 ( 0.1%, 99.7%): 20 ( 18) 12 ( 0.0%, 99.7%): 19 ( 19) 12 ( 0.0%, 99.8%): 35 ( 20) 12 ( 0.0%, 99.8%): 51 ( 21) 10 ( 0.0%, 99.8%): 15 ( 22) 6 ( 0.0%, 99.9%): 17 ( 23) 6 ( 0.0%, 99.9%): 21 ( 24) 6 ( 0.0%, 99.9%): 45 ( 25) 4 ( 0.0%, 99.9%): 14 ( 26) 4 ( 0.0%, 99.9%): 26 ( 27) 4 ( 0.0%, 99.9%): 53 ( 28) 2 ( 0.0%, 99.9%): 172 ( 29) 2 ( 0.0%, 99.9%): 214 ( 30) 2 ( 0.0%,100.0%): 22 ( 31) 2 ( 0.0%,100.0%): 27 ( 32) 2 ( 0.0%,100.0%): 28 ( 33) 2 ( 0.0%,100.0%): 29 ( 34) 2 ( 0.0%,100.0%): 31 ( 35) 2 ( 0.0%,100.0%): 36 ( 36) 2 ( 0.0%,100.0%): 46 ( 37) 2 ( 0.0%,100.0%): 55 ```
213 lines
7.8 KiB
Rust
213 lines
7.8 KiB
Rust
use crate::tester::{test, test_minify};
|
|
|
|
#[test]
|
|
fn module_decl() {
|
|
test("export * as foo from 'foo'", "export * as foo from \"foo\";\n");
|
|
test("import x from './foo.js' with {}", "import x from \"./foo.js\" with {\n};\n");
|
|
test("import {} from './foo.js' with {}", "import {} from \"./foo.js\" with {\n};\n");
|
|
test("export * from './foo.js' with {}", "export * from \"./foo.js\" with {\n};\n");
|
|
}
|
|
|
|
#[test]
|
|
fn new_expr() {
|
|
test("new (foo()).bar();", "new (foo()).bar();\n");
|
|
}
|
|
|
|
#[test]
|
|
fn access_property() {
|
|
test(
|
|
"export default class Foo { @x @y accessor #aDef = 1 }",
|
|
"export default class Foo {\n\taccessor #aDef=1;\n}\n",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn for_stmt() {
|
|
test("for (let x = 0; x < 10; x++) {}", "for (let x = 0; x < 10; x++) {}\n");
|
|
test("for (;;) {}", "for (;;) {}\n");
|
|
test("for (let x = 1;;) {}", "for (let x = 1;;) {}\n");
|
|
test("for (;true;) {}", "for (; true;) {}\n");
|
|
test("for (;;i++) {}", "for (;; i++) {}\n");
|
|
|
|
test("for (using x = 1;;) {}", "for (using x = 1;;) {}\n");
|
|
// TODO
|
|
// test(
|
|
// "for (var a = 1 || (2 in {}) in { x: 1 }) count++;",
|
|
// "for (var a = 1 || (2 in {}) in {x: 1}) count++;\n",
|
|
// );
|
|
}
|
|
|
|
#[test]
|
|
fn shorthand() {
|
|
test("let _ = { x }", "let _ = { x };\n");
|
|
test("let { x } = y", "let { x } = y;\n");
|
|
test("({ x: (x) })", "({ x });\n");
|
|
test("({ x } = y)", "({x} = y);\n");
|
|
}
|
|
|
|
#[test]
|
|
fn unicode_escape() {
|
|
test("console.log('你好');", "console.log(\"你好\");\n");
|
|
test("console.log('こんにちは');", "console.log(\"こんにちは\");\n");
|
|
test("console.log('안녕하세요');", "console.log(\"안녕하세요\");\n");
|
|
test("console.log('🧑🤝🧑');", "console.log(\"🧑🤝🧑\");\n");
|
|
}
|
|
|
|
#[test]
|
|
fn comma() {
|
|
test_minify("1, 2, 3", "1,2,3;");
|
|
test_minify("1, a = b, 3", "1,a=b,3;");
|
|
test_minify("1, (2, 3), 4", "1,2,3,4;");
|
|
}
|
|
|
|
#[test]
|
|
fn assignment() {
|
|
test_minify("a = b ? c : d", "a=b?c:d;");
|
|
test_minify("[a,b] = (1, 2)", "[a,b]=(1,2);");
|
|
// `{a,b}` is a block, must wrap the whole expression to be an assignment expression
|
|
test_minify("({a,b} = (1, 2))", "({a,b}=(1,2));");
|
|
test_minify("a *= yield b", "a*=yield b;");
|
|
test_minify("a /= () => {}", "a/=()=>{};");
|
|
test_minify("a %= async () => {}", "a%=async ()=>{};");
|
|
test_minify("a -= (1, 2)", "a-=(1,2);");
|
|
test_minify("a >>= b >>= c", "a>>=b>>=c;");
|
|
}
|
|
|
|
#[test]
|
|
fn r#yield() {
|
|
test_minify("function *foo() { yield }", "function*foo(){yield}");
|
|
test_minify("function *foo() { yield * a ? b : c }", "function*foo(){yield*a?b:c}");
|
|
test_minify("function *foo() { yield * yield * a }", "function*foo(){yield*yield*a}");
|
|
test_minify("function *foo() { yield * () => {} }", "function*foo(){yield*()=>{}}");
|
|
test_minify("function *foo() { yield * async () => {} }", "function*foo(){yield*async ()=>{}}");
|
|
test_minify("function *foo() { yield a ? b : c }", "function*foo(){yield a?b:c}");
|
|
test_minify("function *foo() { yield yield a }", "function*foo(){yield yield a}");
|
|
test_minify("function *foo() { yield () => {} }", "function*foo(){yield ()=>{}}");
|
|
test_minify("function *foo() { yield async () => {} }", "function*foo(){yield async ()=>{}}");
|
|
test_minify(
|
|
"function *foo() { yield { a } = [ b ] = c ? b : d }",
|
|
"function*foo(){yield {a}=[b]=c?b:d}",
|
|
);
|
|
// TODO: remove the extra space in `yield (a,b)`
|
|
test_minify("function *foo() { yield (a, b) }", "function*foo(){yield (a,b)}");
|
|
test_minify("function *foo() { yield a, b }", "function*foo(){yield a,b}");
|
|
}
|
|
|
|
#[test]
|
|
fn arrow() {
|
|
test_minify("x => a, b", "(x)=>a,b;");
|
|
test_minify("x => (a, b)", "(x)=>(a,b);");
|
|
test_minify("x => (a => b)", "(x)=>(a)=>b;");
|
|
test_minify("x => y => a, b", "(x)=>(y)=>a,b;");
|
|
test_minify("x => y => (a = b)", "(x)=>(y)=>a=b;");
|
|
test_minify("x => y => z => a = b, c", "(x)=>(y)=>(z)=>a=b,c;");
|
|
test_minify("x => y => z => a = (b, c)", "(x)=>(y)=>(z)=>a=(b,c);");
|
|
test_minify("x => ({} + 0)", "(x)=>({})+0;");
|
|
}
|
|
|
|
#[test]
|
|
fn conditional() {
|
|
test_minify("a ? b : c", "a?b:c;");
|
|
test_minify("a ? (b, c) : (d, e)", "a?(b,c):(d,e);");
|
|
test_minify("a ? b : c ? b : c", "a?b:c?b:c;");
|
|
test_minify("(a ? b : c) ? b : c", "(a?b:c)?b:c;");
|
|
test_minify("a, b ? c : d", "a,b?c:d;");
|
|
test_minify("(a, b) ? c : d", "(a,b)?c:d;");
|
|
test_minify("a = b ? c : d", "a=b?c:d;");
|
|
test_minify("(a = b) ? c : d", "(a=b)?c:d;");
|
|
}
|
|
|
|
#[test]
|
|
fn coalesce() {
|
|
test_minify("a ?? b", "a??b;");
|
|
test_minify("a ?? b ?? c ?? d", "a??b??c??d;");
|
|
test_minify("a ?? (b ?? (c ?? d))", "a??(b??(c??d));");
|
|
test_minify("(a ?? (b ?? (c ?? d)))", "a??(b??(c??d));");
|
|
test_minify("a, b ?? c", "a,b??c;");
|
|
test_minify("(a, b) ?? c", "(a,b)??c;");
|
|
test_minify("a, b ?? c, d", "a,b??c,d;");
|
|
test_minify("a, b ?? (c, d)", "a,b??(c,d);");
|
|
test_minify("a = b ?? c", "a=b??c;");
|
|
test_minify("a ?? (b = c)", "a??(b=c);");
|
|
test_minify("(a | b) ?? (c | d)", "a|b??c|d;");
|
|
}
|
|
|
|
#[test]
|
|
fn logical_or() {
|
|
test_minify("a || b || c", "a||b||c;");
|
|
test_minify("(a || (b || c)) || d", "a||(b||c)||d;");
|
|
test_minify("a || (b || (c || d))", "a||(b||(c||d));");
|
|
test_minify("a || b && c", "a||b&&c;");
|
|
test_minify("(a || b) && c", "(a||b)&&c;");
|
|
test_minify("a, b || c, d", "a,b||c,d;");
|
|
test_minify("(a, b) || (c, d)", "(a,b)||(c,d);");
|
|
test_minify("(a && b) || (c && d)", "a&&b||c&&d;");
|
|
test_minify("a && b || c && d", "a&&b||c&&d;");
|
|
}
|
|
|
|
#[test]
|
|
fn logical_and() {
|
|
test_minify("a && b && c", "a&&b&&c;");
|
|
test_minify("a && ((b && c) && d)", "a&&(b&&c&&d);");
|
|
test_minify("((a && b) && c) && d", "a&&b&&c&&d;");
|
|
test_minify("(a || b) && (c || d)", "(a||b)&&(c||d);");
|
|
test_minify("a, b && c, d", "a,b&&c,d;");
|
|
test_minify("(a, b) && (c, d)", "(a,b)&&(c,d);");
|
|
test_minify("a || b && c || d", "a||b&&c||d;");
|
|
}
|
|
|
|
#[test]
|
|
fn bitwise_or() {
|
|
test_minify("a | b | c", "a|b|c;");
|
|
test_minify("(a | b) | c", "a|b|c;");
|
|
test_minify("a | (b | c)", "a|(b|c);");
|
|
test_minify("a | b ^ c", "a|b^c;");
|
|
test_minify("a | (b ^ c)", "a|b^c;");
|
|
test_minify("a | (b && c)", "a|(b&&c);");
|
|
test_minify("a | b && c", "a|b&&c;");
|
|
test_minify("(a ^ b) | (c ^ d)", "a^b|c^d;");
|
|
test_minify("(a, b) | (c, d)", "(a,b)|(c,d);");
|
|
test_minify("a, b | c, d", "a,b|c,d;");
|
|
}
|
|
|
|
#[test]
|
|
fn bitwise_xor() {
|
|
test_minify("a ^ b ^ c", "a^b^c;");
|
|
test_minify("(a ^ b) ^ c", "a^b^c;");
|
|
test_minify("a ^ (b ^ c)", "a^(b^c);");
|
|
test_minify("a | b & c", "a|b&c;");
|
|
test_minify("a | (b & c)", "a|b&c;");
|
|
test_minify("a | (b || c)", "a|(b||c);");
|
|
test_minify("a | b || c", "a|b||c;");
|
|
test_minify("(a, b) ^ (c, d)", "(a,b)^(c,d);");
|
|
test_minify("(a | b) ^ (c | d)", "(a|b)^(c|d);");
|
|
test_minify("a, b ^ c, d", "a,b^c,d;");
|
|
}
|
|
|
|
#[test]
|
|
fn bitwise_and() {
|
|
test_minify("a & b & c", "a&b&c;");
|
|
test_minify("((a & b) & c) & d", "a&b&c&d;");
|
|
test_minify("a & (b & (c & d))", "a&(b&(c&d));");
|
|
test_minify("a & b == c", "a&b==c;");
|
|
test_minify("a & (b == c)", "a&b==c;");
|
|
test_minify("a == b & c", "a==b&c;");
|
|
test_minify("(a == b) & c", "a==b&c;");
|
|
test_minify("a ^ b & c", "a^b&c;");
|
|
test_minify("(a ^ b) & c", "(a^b)&c;");
|
|
test_minify("(a, b) & (c, d)", "(a,b)&(c,d);");
|
|
test_minify("a, b & c, d", "a,b&c,d;");
|
|
}
|
|
|
|
#[test]
|
|
fn equality() {
|
|
test_minify("a == b != c === d !== e", "a==b!=c===d!==e;");
|
|
test_minify("a == (b != (c === (d !== e)))", "a==(b!=(c===(d!==e)));");
|
|
test_minify("(((a == b) != c) === d) !== e", "a==b!=c===d!==e;");
|
|
test_minify("a > b == c < d", "a>b==c<d;");
|
|
test_minify("(a > b) == (c < d)", "a>b==c<d;");
|
|
test_minify("a | b == c & d", "a|b==c&d;");
|
|
test_minify("(a | b) == (c & d)", "(a|b)==(c&d);");
|
|
test_minify("a, b == c , d", "a,b==c,d;");
|
|
test_minify("(a, b) == (c , d)", "(a,b)==(c,d);");
|
|
}
|