Skip to content

Commit

Permalink
Some more whitespace stripping
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
matthiasmullie committed Feb 2, 2018
1 parent ba5c229 commit 121cd3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,12 @@ protected function stripWhitespace($content)
/*
* Whitespace after `return` can be omitted in a few occasions
* (such as when followed by a string or regex)
* Same for whitespace in between `)` and `{`, or between `{` and some
* keywords.
*/
$content = preg_replace('/\breturn\s+(["\'\/\+\-])/', 'return$1', $content);
$content = preg_replace('/\)\s+\{/', '){', $content);
$content = preg_replace('/}\n(else|catch|finally)\b/', '}$1', $content);

/*
* Get rid of double semicolons, except where they can be used like:
Expand Down
17 changes: 5 additions & 12 deletions tests/js/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ public function dataProvider()
else
{
}',
'if(!0)
{}
else{}',
'if(!0){}else{}',
);
$tests[] = array(
'do
Expand Down Expand Up @@ -376,9 +374,7 @@ public function dataProvider()
// already a text element
else newElement = currentElement;
',
'if(currentElement.attr(\'type\')!=\'text\')
{currentElement.remove()}
else newElement=currentElement',
'if(currentElement.attr(\'type\')!=\'text\'){currentElement.remove()}else newElement=currentElement',
);
$tests[] = array(
'var jsBackend =
Expand Down Expand Up @@ -514,10 +510,8 @@ function foo (a, b)
{
return a / b;
}',
'function foo(a,b)
{return a/b}
function foo(a,b)
{return a/b}',
'function foo(a,b){return a/b}
function foo(a,b){return a/b}',
);

// https://github.com/matthiasmullie/minify/issues/15
Expand Down Expand Up @@ -803,8 +797,7 @@ function otherFuncName() {
func()
{ alert(\'hey\'); }',
'function func(){}
func()
{alert(\'hey\')}',
func(){alert(\'hey\')}',
);

// https://github.com/matthiasmullie/minify/issues/133
Expand Down

0 comments on commit 121cd3b

Please sign in to comment.