Skip to content

Commit 0c77ac9

Browse files
committed
fix #393, add one more test for the fix of 385
1 parent ab5f4a7 commit 0c77ac9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/JS.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,18 @@ protected function stripComments()
209209
$placeholder = '/*'.$count.'*/';
210210
$minifier->extracted[$placeholder] = $match[0];
211211

212-
return $placeholder;
212+
return $placeholder . ($match[3] ? $match[2] . $match[3] : '');
213+
}
214+
// 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;
215+
if($match[3]) {
216+
return $match[2] . $match[3];
213217
}
214218

215219
return '';
216220
};
217221

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

221225
// single-line comments
222226
$this->registerPattern('/\/\/.*$/m', '');

tests/js/JSTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,11 @@ function someOtherFunction() {
13161316
'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;',
13171317
'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'
13181318
);
1319+
$tests[] = array(
1320+
'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;',
1321+
'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'
1322+
);
1323+
13191324
$tests[] = array(
13201325
'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;',
13211326
'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'
@@ -1331,6 +1336,15 @@ function someOtherFunction() {
13311336
'jQuery(document).ready(function(e){if(jQuery(document.body).on("updated_wc_div",o),jQuery(document.body).on("updated_cart_totals",o));})',
13321337
);
13331338

1339+
// https://github.com/matthiasmullie/minify/issues/393
1340+
$tests[] = array(
1341+
'var crypt=function() {}
1342+
/* some comment */
1343+
var Sbox = 2',
1344+
'var crypt=function(){}
1345+
var Sbox=2',
1346+
);
1347+
13341348
// known minified files to help doublecheck changes in places not yet
13351349
// anticipated in these tests
13361350
$files = glob(__DIR__.'/sample/minified/*.js');
@@ -1346,6 +1360,7 @@ function someOtherFunction() {
13461360
}
13471361

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

0 commit comments

Comments
 (0)