Skip to content

Commit dc4c388

Browse files
committed
test(minifier): fail tests when parse fails (#8836)
1 parent 0928a19 commit dc4c388

File tree

2 files changed

+148
-50
lines changed

2 files changed

+148
-50
lines changed

crates/oxc_minifier/tests/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub(crate) fn run(
2929
) -> String {
3030
let allocator = Allocator::default();
3131
let ret = Parser::new(&allocator, source_text, source_type).parse();
32+
assert!(!ret.panicked, "{source_text}");
33+
assert!(ret.errors.is_empty(), "{source_text}");
3234
let mut program = ret.program;
3335
if let Some(options) = options {
3436
Compressor::new(&allocator, options).build(&mut program);

crates/oxc_minifier/tests/peephole/esbuild.rs

+146-50
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,13 @@ fn js_parser_test() {
277277
test("while(1) { function* x() {} }", "for (;;) { function* x() { }}");
278278
test("while(1) { async function x() {} }", "for (;;) { async function x() { }}");
279279
test("while(1) { async function* x() {} }", "for (;;) { async function* x() { }}");
280-
test("x(); switch (y) { case z: return w; }", "switch (x(), y) { case z: return w;}");
281280
test(
282-
"if (t) { x(); switch (y) { case z: return w; } }",
283-
"if (t) switch (x(), y) { case z: return w; }",
281+
"function _() { x(); switch (y) { case z: return w; } }",
282+
"function _() { switch (x(), y) { case z: return w; }}",
283+
);
284+
test(
285+
"function _() { if (t) { x(); switch (y) { case z: return w; } } }",
286+
"function _() { if (t) switch (x(), y) { case z: return w; } }",
284287
);
285288
test("a = '' + 0", "a = '0';");
286289
test("a = 0 + ''", "a = '0';");
@@ -369,7 +372,7 @@ fn js_parser_test() {
369372
test("a = !(b != c)", "a = b == c;");
370373
test("a = !(b === c)", "a = b !== c;");
371374
test("a = !(b !== c)", "a = b === c;");
372-
test("if (!(a, b)) return c", "if (a, !b) return c;");
375+
test("function _() { if (!(a, b)) return c }", "function _() { if (a, !b) return c; }");
373376
test("a = !(b < c)", "a = !(b < c);");
374377
test("a = !(b > c)", "a = !(b > c);");
375378
test("a = !(b <= c)", "a = !(b <= c);");
@@ -631,20 +634,41 @@ fn js_parser_test() {
631634
test("a ? b == c : b != c", "a ? b == c : b != c;");
632635
test("a ? b.c(d + e[f]) : b.c(d + e[g])", "a ? b.c(d + e[f]) : b.c(d + e[g]);");
633636
test("(a, b) ? c : d", "a, b ? c : d;");
634-
test("return a && ((b && c) && (d && e))", "return a && b && c && d && e;");
635-
test("return a || ((b || c) || (d || e))", "return a || b || c || d || e;");
636-
test("return a ?? ((b ?? c) ?? (d ?? e))", "return a ?? b ?? c ?? d ?? e;");
637+
test(
638+
"function _() { return a && ((b && c) && (d && e)) }",
639+
"function _() { return a && b && c && d && e; }",
640+
);
641+
test(
642+
"function _() { return a || ((b || c) || (d || e)) }",
643+
"function _() { return a || b || c || d || e; }",
644+
);
645+
test(
646+
"function _() { return a ?? ((b ?? c) ?? (d ?? e)) }",
647+
"function _() { return a ?? b ?? c ?? d ?? e; }",
648+
);
637649
test("if (a) if (b) if (c) d", "a && b && c && d;");
638650
test("if (!a) if (!b) if (!c) d", "a || b || c || d;");
639651
test(
640-
"let a, b, c; return a != null ? a : b != null ? b : c",
641-
"let a, b, c;return a ?? b ?? c;",
652+
"function _() { let a, b, c; return a != null ? a : b != null ? b : c }",
653+
"function _() { let a, b, c;return a ?? b ?? c; }",
654+
);
655+
test(
656+
"function _() { if (a) return c; if (b) return d; }",
657+
"function _() { if (a) return c;if (b) return d; }",
658+
);
659+
test(
660+
"function _() { if (a) return c; if (b) return c; }",
661+
"function _() { if (a || b) return c; }",
662+
);
663+
test(
664+
"function _() { if (a) return c; if (b) return; }",
665+
"function _() { if (a) return c;if (b) return; }",
666+
);
667+
test(
668+
"function _() { if (a) return; if (b) return c; }",
669+
"function _() { if (a) return;if (b) return c; }",
642670
);
643-
test("if (a) return c; if (b) return d;", "if (a) return c;if (b) return d;");
644-
test("if (a) return c; if (b) return c;", "if (a || b) return c;");
645-
test("if (a) return c; if (b) return;", "if (a) return c;if (b) return;");
646-
test("if (a) return; if (b) return c;", "if (a) return;if (b) return c;");
647-
test("if (a) return; if (b) return;", "if (a || b) return;");
671+
test("function _() { if (a) return; if (b) return; }", "function _() { if (a || b) return; }");
648672
test("if (a) throw c; if (b) throw d;", "if (a) throw c;if (b) throw d;");
649673
test("if (a) throw c; if (b) throw c;", "if (a || b) throw c;");
650674
// test("while (x) { if (a) break; if (b) break; }", "for (; x && !(a || b); ) ;");
@@ -736,54 +760,126 @@ fn js_parser_test() {
736760
test("if (0 == (a >>> b)) throw 0", "if (!(a >>> b)) throw 0;");
737761
// test("if (0 == +a) throw 0", "if (+a == 0) throw 0;");
738762
// test("if (0 == ~a) throw 0", "if (!~a) throw 0;");
739-
test("if (a) { if (b) return c } else return d", "if (a) { if (b) return c;} else return d;");
740763
test(
741-
"if (a) while (1) { if (b) return c } else return d",
742-
"if (a) { for (;;) if (b) return c;} else return d;",
764+
"function _() { if (a) { if (b) return c } else return d }",
765+
"function _() { if (a) { if (b) return c;} else return d; }",
766+
);
767+
test(
768+
"function _() { if (a) while (1) { if (b) return c } else return d }",
769+
"function _() { if (a) { for (;;) if (b) return c;} else return d; }",
770+
);
771+
test(
772+
"function _() { if (a) for (;;) { if (b) return c } else return d }",
773+
"function _() { if (a) { for (;;) if (b) return c;} else return d; }",
774+
);
775+
test(
776+
"function _() { if (a) for (x in y) { if (b) return c } else return d }",
777+
"function _() { if (a) { for (x in y) if (b) return c;} else return d; }",
778+
);
779+
test(
780+
"function _() { if (a) for (x of y) { if (b) return c } else return d }",
781+
"function _() { if (a) { for (x of y) if (b) return c;} else return d; }",
782+
);
783+
test(
784+
"function _() { if (a) with (x) { if (b) return c } else return d }",
785+
"function _() { if (a) { with (x) if (b) return c;} else return d; }",
786+
);
787+
test(
788+
"function _() { if (a) x: { if (b) break x } else return c }",
789+
"function _() { if (a) { x: if (b) break x;} else return c; }",
790+
);
791+
test(
792+
"function _() { let a; return a != null ? a.b : undefined }",
793+
"function _ () { let a;return a?.b; }",
794+
);
795+
test(
796+
"function _() { let a; return a != null ? a[b] : undefined }",
797+
"function _ () { let a;return a?.[b]; }",
798+
);
799+
test(
800+
"function _() { let a; return a != null ? a(b) : undefined }",
801+
"function _ () { let a;return a?.(b); }",
802+
);
803+
test(
804+
"function _() { let a; return a == null ? undefined : a.b }",
805+
"function _ () { let a;return a?.b; }",
806+
);
807+
test(
808+
"function _() { let a; return a == null ? undefined : a[b] }",
809+
"function _ () { let a;return a?.[b]; }",
810+
);
811+
test(
812+
"function _() { let a; return a == null ? undefined : a(b) }",
813+
"function _ () { let a;return a?.(b); }",
814+
);
815+
test(
816+
"function _() { let a; return null != a ? a.b : undefined }",
817+
"function _ () { let a;return a?.b; }",
818+
);
819+
test(
820+
"function _() { let a; return null != a ? a[b] : undefined }",
821+
"function _ () { let a;return a?.[b]; }",
822+
);
823+
test(
824+
"function _() { let a; return null != a ? a(b) : undefined }",
825+
"function _ () { let a;return a?.(b); }",
826+
);
827+
test(
828+
"function _() { let a; return null == a ? undefined : a.b }",
829+
"function _ () { let a;return a?.b; }",
830+
);
831+
test(
832+
"function _() { let a; return null == a ? undefined : a[b] }",
833+
"function _ () { let a;return a?.[b]; }",
834+
);
835+
test(
836+
"function _() { let a; return null == a ? undefined : a(b) }",
837+
"function _ () { let a;return a?.(b); }",
838+
);
839+
test(
840+
"function _() { return a != null ? a.b : undefined }",
841+
"function _ () { return a == null ? void 0 : a.b; }",
842+
);
843+
test(
844+
"function _() { let a; return a != null ? a.b : null }",
845+
"function _ () { let a;return a == null ? null : a.b; }",
846+
);
847+
test(
848+
"function _() { let a; return a != null ? b.a : undefined }",
849+
"function _ () { let a;return a == null ? void 0 : b.a; }",
850+
);
851+
test(
852+
"function _() { let a; return a != 0 ? a.b : undefined }",
853+
"function _ () { let a;return a == 0 ? void 0 : a.b; }",
854+
);
855+
test(
856+
"function _() { let a; return a !== null ? a.b : undefined }",
857+
"function _ () { let a;return a === null ? void 0 : a.b; }",
858+
);
859+
test(
860+
"function _() { let a; return a != undefined ? a.b : undefined }",
861+
"function _ () { let a;return a?.b; }",
743862
);
744863
test(
745-
"if (a) for (;;) { if (b) return c } else return d",
746-
"if (a) { for (;;) if (b) return c;} else return d;",
864+
"function _() { let a; return a != null ? a?.b : undefined }",
865+
"function _ () { let a;return a?.b; }",
747866
);
748867
test(
749-
"if (a) for (x in y) { if (b) return c } else return d",
750-
"if (a) { for (x in y) if (b) return c;} else return d;",
868+
"function _() { let a; return a != null ? a.b.c[d](e) : undefined }",
869+
"function _ () { let a;return a?.b.c[d](e); }",
751870
);
752871
test(
753-
"if (a) for (x of y) { if (b) return c } else return d",
754-
"if (a) { for (x of y) if (b) return c;} else return d;",
872+
"function _() { let a; return a != null ? a?.b.c[d](e) : undefined }",
873+
"function _ () { let a;return a?.b.c[d](e); }",
755874
);
756875
test(
757-
"if (a) with (x) { if (b) return c } else return d",
758-
"if (a) { with (x) if (b) return c;} else return d;",
876+
"function _() { let a; return a != null ? a.b.c?.[d](e) : undefined }",
877+
"function _ () { let a;return a?.b.c?.[d](e); }",
759878
);
760879
test(
761-
"if (a) x: { if (b) break x } else return c",
762-
"if (a) { x: if (b) break x;} else return c;",
880+
"function _() { let a; return a != null ? a?.b.c?.[d](e) : undefined }",
881+
"function _ () { let a;return a?.b.c?.[d](e); }",
763882
);
764-
test("let a; return a != null ? a.b : undefined", "let a;return a?.b;");
765-
test("let a; return a != null ? a[b] : undefined", "let a;return a?.[b];");
766-
test("let a; return a != null ? a(b) : undefined", "let a;return a?.(b);");
767-
test("let a; return a == null ? undefined : a.b", "let a;return a?.b;");
768-
test("let a; return a == null ? undefined : a[b]", "let a;return a?.[b];");
769-
test("let a; return a == null ? undefined : a(b)", "let a;return a?.(b);");
770-
test("let a; return null != a ? a.b : undefined", "let a;return a?.b;");
771-
test("let a; return null != a ? a[b] : undefined", "let a;return a?.[b];");
772-
test("let a; return null != a ? a(b) : undefined", "let a;return a?.(b);");
773-
test("let a; return null == a ? undefined : a.b", "let a;return a?.b;");
774-
test("let a; return null == a ? undefined : a[b]", "let a;return a?.[b];");
775-
test("let a; return null == a ? undefined : a(b)", "let a;return a?.(b);");
776-
test("return a != null ? a.b : undefined", "return a == null ? void 0 : a.b;");
777-
test("let a; return a != null ? a.b : null", "let a;return a == null ? null : a.b;");
778-
test("let a; return a != null ? b.a : undefined", "let a;return a == null ? void 0 : b.a;");
779-
test("let a; return a != 0 ? a.b : undefined", "let a;return a == 0 ? void 0 : a.b;");
780-
test("let a; return a !== null ? a.b : undefined", "let a;return a === null ? void 0 : a.b;");
781-
test("let a; return a != undefined ? a.b : undefined", "let a;return a?.b;");
782-
test("let a; return a != null ? a?.b : undefined", "let a;return a?.b;");
783-
test("let a; return a != null ? a.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
784-
test("let a; return a != null ? a?.b.c[d](e) : undefined", "let a;return a?.b.c[d](e);");
785-
test("let a; return a != null ? a.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
786-
test("let a; return a != null ? a?.b.c?.[d](e) : undefined", "let a;return a?.b.c?.[d](e);");
787883
}
788884

789885
#[test]

0 commit comments

Comments
 (0)