Skip to content

Commit

Permalink
fix #393, add one more test for the fix of 385
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Apr 17, 2022
1 parent ab5f4a7 commit 0c77ac9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,18 @@ protected function stripComments()
$placeholder = '/*'.$count.'*/';
$minifier->extracted[$placeholder] = $match[0];

return $placeholder;
return $placeholder . ($match[3] ? $match[2] . $match[3] : '');
}
// should not remove the \n before a var|let etc. at this stage, because it could be a case like this: var a=1\n/*comment*/\nvar b=2;
if($match[3]) {
return $match[2] . $match[3];
}

return '';
};

// multi-line comments
$this->registerPattern('/\n?\/\*(.*?)\*\/\n?/s', $callback);
$this->registerPattern('/\n?\/\*(.*?)\*\/(\n?)(var|let|const|enum|function|class|)/s', $callback);

// single-line comments
$this->registerPattern('/\/\/.*$/m', '');
Expand Down
15 changes: 15 additions & 0 deletions tests/js/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,11 @@ function someOtherFunction() {
'if (l !== 3) { for (var V = w.map(function(e) { if(e > 5) { return e-5; } return e; }).length; l < V; V++); } else var C = 3;',
'if(l!==3){for(var V=w.map(function(e){if(e>5){return e-5}return e}).length;l<V;V++);}else var C=3'
);
$tests[] = array(
'if(l!==3){for(var V=w.length;V < w.map( function(e) { if(e>5){return e-5; }return e; }).length; l++ );}else var C=3;',
'if(l!==3){for(var V=w.length;V<w.map(function(e){if(e>5){return e-5}return e}).length;l++);}else var C=3'
);

$tests[] = array(
'if (l !== 3) { for (var V = w.length; l < V; V+=w.map(function(e) { if(e > 5) { return e-5; } return e; }).length); } else var C = 3;',
'if(l!==3){for(var V=w.length;l<V;V+=w.map(function(e){if(e>5){return e-5}return e}).length);}else var C=3'
Expand All @@ -1331,6 +1336,15 @@ function someOtherFunction() {
'jQuery(document).ready(function(e){if(jQuery(document.body).on("updated_wc_div",o),jQuery(document.body).on("updated_cart_totals",o));})',
);

// https://github.com/matthiasmullie/minify/issues/393
$tests[] = array(
'var crypt=function() {}
/* some comment */
var Sbox = 2',
'var crypt=function(){}
var Sbox=2',
);

// known minified files to help doublecheck changes in places not yet
// anticipated in these tests
$files = glob(__DIR__.'/sample/minified/*.js');
Expand All @@ -1346,6 +1360,7 @@ function someOtherFunction() {
}

//some other files that are minified correctly, ensure they stay like this
// https://github.com/matthiasmullie/minify/issues/393
$source = trim(file_get_contents(__DIR__.'/sample/source/Decrypt.js'));
$minified = trim(file_get_contents(__DIR__.'/sample/minified2/Decrypt.min.js'));
$tests[] = array($source, $minified);
Expand Down

0 comments on commit 0c77ac9

Please sign in to comment.