From 31371a6a671ce798f04bbc27f64d10050d60090b Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Mon, 4 Jan 2016 14:59:16 -0800 Subject: [PATCH] Strip empty else-statements Fixes #91 --- src/JS.php | 10 ++++++++++ tests/js/JSTest.php | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/src/JS.php b/src/JS.php index b732abb..f0bd4fb 100644 --- a/src/JS.php +++ b/src/JS.php @@ -307,6 +307,16 @@ class JS extends Minify */ $content = preg_replace('/(for\([^;]*;[^;]*;[^;\{]*\));(\}|$)/s', '\\1;;\\2', $content); + /* + * We also can't strip empty else-statements. Even though they're + * useless and probably shouldn't be in the code in the first place, we + * shouldn't be stripping the `;` that follows it as it breaks the code. + * We can just remove those useless else-statements completely. + * + * @see https://github.com/matthiasmullie/minify/issues/91 + */ + $content = preg_replace('/else;/s', '', $content); + /* * We also don't really want to terminate statements followed by closing * curly braces (which we've ignored completely up until now) or end-of- diff --git a/tests/js/JSTest.php b/tests/js/JSTest.php index 0630701..b5e65d5 100644 --- a/tests/js/JSTest.php +++ b/tests/js/JSTest.php @@ -659,6 +659,12 @@ BUG "for(;;ja||(ja=!0)){}", ); + // https://github.com/matthiasmullie/minify/issues/91 + $tests[] = array( + 'if(true){if(true)console.log("test")else;}', + 'if(!0){if(!0)console.log("test")}', + ); + return $tests; } }