2011-03-14 11:44:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-02-17 12:29:13 +00:00
|
|
|
class JavaScriptMinifierTest extends PHPUnit\Framework\TestCase {
|
2011-03-14 11:44:33 +00:00
|
|
|
|
2017-12-29 23:22:37 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2018-08-10 22:11:53 +00:00
|
|
|
protected function tearDown() {
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
// Reset
|
|
|
|
|
$this->setMaxLineLength( 1000 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function setMaxLineLength( $val ) {
|
|
|
|
|
$classReflect = new ReflectionClass( JavaScriptMinifier::class );
|
|
|
|
|
$propertyReflect = $classReflect->getProperty( 'maxLineLength' );
|
|
|
|
|
$propertyReflect->setAccessible( true );
|
|
|
|
|
$propertyReflect->setValue( JavaScriptMinifier::class, $val );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideCases() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2012-06-20 01:52:37 +00:00
|
|
|
|
|
|
|
|
// Basic whitespace and comments that should be stripped entirely
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "\r\t\f \v\n\r", "" ],
|
|
|
|
|
[ "/* Foo *\n*bar\n*/", "" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* Slashes used inside block comments (T28931).
|
2012-06-20 01:52:37 +00:00
|
|
|
* At some point there was a bug that caused this comment to be ended at '* /',
|
|
|
|
|
* causing /M... to be left as the beginning of a regex.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"/**\n * Foo\n * {\n * 'bar' : {\n * "
|
|
|
|
|
. "//Multiple rules with configurable operators\n * 'baz' : false\n * }\n */",
|
2016-02-17 09:09:32 +00:00
|
|
|
"" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-07-06 20:02:10 +00:00
|
|
|
/**
|
|
|
|
|
* ' Foo \' bar \
|
|
|
|
|
* baz \' quox ' .
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"' Foo \\' bar \\\n baz \\' quox ' .length",
|
|
|
|
|
"' Foo \\' bar \\\n baz \\' quox '.length"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"\" Foo \\\" bar \\\n baz \\\" quox \" .length",
|
|
|
|
|
"\" Foo \\\" bar \\\n baz \\\" quox \".length"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ "// Foo b/ar baz", "" ],
|
|
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"/ Foo \\/ bar [ / \\] / ] baz / .length",
|
|
|
|
|
"/ Foo \\/ bar [ / \\] / ] baz /.length"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 11:44:33 +00:00
|
|
|
// HTML comments
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "<!-- Foo bar", "" ],
|
|
|
|
|
[ "<!-- Foo --> bar", "" ],
|
|
|
|
|
[ "--> Foo", "" ],
|
|
|
|
|
[ "x --> y", "x-->y" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 11:44:33 +00:00
|
|
|
// Semicolon insertion
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "(function(){return\nx;})", "(function(){return\nx;})" ],
|
|
|
|
|
[ "throw\nx;", "throw\nx;" ],
|
|
|
|
|
[ "while(p){continue\nx;}", "while(p){continue\nx;}" ],
|
|
|
|
|
[ "while(p){break\nx;}", "while(p){break\nx;}" ],
|
|
|
|
|
[ "var\nx;", "var x;" ],
|
|
|
|
|
[ "x\ny;", "x\ny;" ],
|
|
|
|
|
[ "x\n++y;", "x\n++y;" ],
|
|
|
|
|
[ "x\n!y;", "x\n!y;" ],
|
|
|
|
|
[ "x\n{y}", "x\n{y}" ],
|
|
|
|
|
[ "x\n+y;", "x+y;" ],
|
|
|
|
|
[ "x\n(y);", "x(y);" ],
|
|
|
|
|
[ "5.\nx;", "5.\nx;" ],
|
|
|
|
|
[ "0xFF.\nx;", "0xFF.x;" ],
|
|
|
|
|
[ "5.3.\nx;", "5.3.x;" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2017-12-22 17:36:09 +00:00
|
|
|
// Cover failure case for incomplete hex literal
|
|
|
|
|
[ "0x;", false, false ],
|
|
|
|
|
|
|
|
|
|
// Cover failure case for number with no digits after E
|
|
|
|
|
[ "1.4E", false, false ],
|
|
|
|
|
|
|
|
|
|
// Cover failure case for number with several E
|
|
|
|
|
[ "1.4EE2", false, false ],
|
|
|
|
|
[ "1.4EE", false, false ],
|
|
|
|
|
|
|
|
|
|
// Cover failure case for number with several E (nonconsecutive)
|
|
|
|
|
// FIXME: This is invalid, but currently tolerated
|
|
|
|
|
[ "1.4E2E3", "1.4E2 E3", false ],
|
|
|
|
|
|
2012-06-20 01:52:37 +00:00
|
|
|
// Semicolon insertion between an expression having an inline
|
2017-02-20 23:45:58 +00:00
|
|
|
// comment after it, and a statement on the next line (T29046).
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"var a = this //foo bar \n for ( b = 0; c < d; b++ ) {}",
|
|
|
|
|
"var a=this\nfor(b=0;c<d;b++){}"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2018-01-10 02:02:27 +00:00
|
|
|
// Cover failure case of incomplete regexp at end of file (T75556)
|
|
|
|
|
// FIXME: This is invalid, but currently tolerated
|
|
|
|
|
[ "*/", "*/", false ],
|
|
|
|
|
|
2018-01-31 03:55:15 +00:00
|
|
|
// Cover failure case of incomplete char class in regexp (T75556)
|
|
|
|
|
// FIXME: This is invalid, but currently tolerated
|
|
|
|
|
[ "/a[b/.test", "/a[b/.test", false ],
|
|
|
|
|
|
2018-01-10 02:02:27 +00:00
|
|
|
// Cover failure case of incomplete string at end of file (T75556)
|
|
|
|
|
// FIXME: This is invalid, but currently tolerated
|
|
|
|
|
[ "'a", "'a", false ],
|
|
|
|
|
|
2011-03-14 11:44:33 +00:00
|
|
|
// Token separation
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "x in y", "x in y" ],
|
|
|
|
|
[ "/x/g in y", "/x/g in y" ],
|
|
|
|
|
[ "x in 30", "x in 30" ],
|
|
|
|
|
[ "x + ++ y", "x+ ++y" ],
|
|
|
|
|
[ "x ++ + y", "x++ +y" ],
|
|
|
|
|
[ "x / /y/.exec(z)", "x/ /y/.exec(z)" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 11:44:33 +00:00
|
|
|
// State machine
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "/ x/g", "/ x/g" ],
|
|
|
|
|
[ "(function(){return/ x/g})", "(function(){return/ x/g})" ],
|
|
|
|
|
[ "+/ x/g", "+/ x/g" ],
|
|
|
|
|
[ "++/ x/g", "++/ x/g" ],
|
|
|
|
|
[ "x/ x/g", "x/x/g" ],
|
|
|
|
|
[ "(/ x/g)", "(/ x/g)" ],
|
|
|
|
|
[ "if(/ x/g);", "if(/ x/g);" ],
|
|
|
|
|
[ "(x/ x/g)", "(x/x/g)" ],
|
|
|
|
|
[ "([/ x/g])", "([/ x/g])" ],
|
|
|
|
|
[ "+x/ x/g", "+x/x/g" ],
|
|
|
|
|
[ "{}/ x/g", "{}/ x/g" ],
|
|
|
|
|
[ "+{}/ x/g", "+{}/x/g" ],
|
|
|
|
|
[ "(x)/ x/g", "(x)/x/g" ],
|
|
|
|
|
[ "if(x)/ x/g", "if(x)/ x/g" ],
|
|
|
|
|
[ "for(x;x;{}/ x/g);", "for(x;x;{}/x/g);" ],
|
|
|
|
|
[ "x;x;{}/ x/g", "x;x;{}/ x/g" ],
|
|
|
|
|
[ "x:{}/ x/g", "x:{}/ x/g" ],
|
|
|
|
|
[ "switch(x){case y?z:{}/ x/g:{}/ x/g;}", "switch(x){case y?z:{}/x/g:{}/ x/g;}" ],
|
|
|
|
|
[ "function x(){}/ x/g", "function x(){}/ x/g" ],
|
|
|
|
|
[ "+function x(){}/ x/g", "+function x(){}/x/g" ],
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-03-14 13:55:42 +00:00
|
|
|
// Multiline quoted string
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "var foo=\"\\\nblah\\\n\";", "var foo=\"\\\nblah\\\n\";" ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 13:55:42 +00:00
|
|
|
// Multiline quoted string followed by string with spaces
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"var foo=\"\\\nblah\\\n\";\nvar baz = \" foo \";\n",
|
|
|
|
|
"var foo=\"\\\nblah\\\n\";var baz=\" foo \";"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 13:55:42 +00:00
|
|
|
// URL in quoted string ( // is not a comment)
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"aNode.setAttribute('href','http://foo.bar.org/baz');",
|
|
|
|
|
"aNode.setAttribute('href','http://foo.bar.org/baz');"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 13:55:42 +00:00
|
|
|
// URL in quoted string after multiline quoted string
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"var foo=\"\\\nblah\\\n\";\naNode.setAttribute('href','http://foo.bar.org/baz');",
|
|
|
|
|
"var foo=\"\\\nblah\\\n\";aNode.setAttribute('href','http://foo.bar.org/baz');"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
2011-03-14 13:55:42 +00:00
|
|
|
// Division vs. regex nastiness
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:46 +00:00
|
|
|
"alert( (10+10) / '/'.charCodeAt( 0 ) + '//' );",
|
|
|
|
|
"alert((10+10)/'/'.charCodeAt(0)+'//');"
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ "if(1)/a /g.exec('Pa ss');", "if(1)/a /g.exec('Pa ss');" ],
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-03-14 18:04:39 +00:00
|
|
|
// newline insertion after 1000 chars: break after the "++", not before
|
2016-02-17 09:09:32 +00:00
|
|
|
[ str_repeat( ';', 996 ) . "if(x++);", str_repeat( ';', 996 ) . "if(x++\n);" ],
|
2011-09-27 22:51:36 +00:00
|
|
|
|
2017-02-20 23:45:58 +00:00
|
|
|
// Unicode letter characters should pass through ok in identifiers (T33187)
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "var KaŝSkatolVal = {}", 'var KaŝSkatolVal={}' ],
|
2012-06-20 01:52:37 +00:00
|
|
|
|
|
|
|
|
// Per spec unicode char escape values should work in identifiers,
|
2011-09-27 22:51:36 +00:00
|
|
|
// as long as it's a valid char. In future it might get normalized.
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "var Ka\\u015dSkatolVal = {}", 'var Ka\\u015dSkatolVal={}' ],
|
2011-11-22 18:42:21 +00:00
|
|
|
|
2012-06-20 01:52:37 +00:00
|
|
|
// Some structures that might look invalid at first sight
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "var a = 5.;", "var a=5.;" ],
|
|
|
|
|
[ "5.0.toString();", "5.0.toString();" ],
|
|
|
|
|
[ "5..toString();", "5..toString();" ],
|
2017-12-22 17:36:09 +00:00
|
|
|
// Cover failure case for too many decimal points
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "5...toString();", false ],
|
|
|
|
|
[ "5.\n.toString();", '5..toString();' ],
|
2015-08-15 01:00:30 +00:00
|
|
|
|
|
|
|
|
// Boolean minification (!0 / !1)
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "var a = { b: true };", "var a={b:!0};" ],
|
|
|
|
|
[ "var a = { true: 12 };", "var a={true:12};", false ],
|
|
|
|
|
[ "a.true = 12;", "a.true=12;", false ],
|
|
|
|
|
[ "a.foo = true;", "a.foo=!0;" ],
|
|
|
|
|
[ "a.foo = false;", "a.foo=!1;" ],
|
|
|
|
|
];
|
2011-03-14 11:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCases
|
2013-10-24 16:45:52 +00:00
|
|
|
* @covers JavaScriptMinifier::minify
|
2017-12-22 17:36:09 +00:00
|
|
|
* @covers JavaScriptMinifier::parseError
|
2011-03-14 11:44:33 +00:00
|
|
|
*/
|
2017-12-22 17:36:09 +00:00
|
|
|
public function testMinifyOutput( $code, $expectedOutput, $expectedValid = true ) {
|
2011-07-06 20:02:10 +00:00
|
|
|
$minified = JavaScriptMinifier::minify( $code );
|
|
|
|
|
|
|
|
|
|
// JSMin+'s parser will throw an exception if output is not valid JS.
|
2011-11-23 00:22:46 +00:00
|
|
|
// suppression of warnings needed for stupid crap
|
2015-08-15 01:00:30 +00:00
|
|
|
if ( $expectedValid ) {
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2015-08-15 01:00:30 +00:00
|
|
|
$parser = new JSParser();
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2015-08-15 01:00:30 +00:00
|
|
|
$parser->parse( $minified, 'minify-test.js', 1 );
|
|
|
|
|
}
|
2011-07-06 20:02:10 +00:00
|
|
|
|
2014-04-24 16:06:46 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedOutput,
|
|
|
|
|
$minified,
|
|
|
|
|
"Minified output should be in the form expected."
|
|
|
|
|
);
|
2011-03-14 11:44:33 +00:00
|
|
|
}
|
2011-11-21 22:20:06 +00:00
|
|
|
|
2018-08-10 22:11:53 +00:00
|
|
|
public static function provideLineBreaker() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2018-08-10 22:11:53 +00:00
|
|
|
// Regression tests for T34548.
|
|
|
|
|
// Must not break between 'E' and '+'.
|
|
|
|
|
'var name = 1.23456789E55;',
|
|
|
|
|
[
|
|
|
|
|
'var',
|
|
|
|
|
'name',
|
|
|
|
|
'=',
|
|
|
|
|
'1.23456789E55',
|
|
|
|
|
';',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2018-08-10 22:11:53 +00:00
|
|
|
'var name = 1.23456789E+5;',
|
|
|
|
|
[
|
|
|
|
|
'var',
|
|
|
|
|
'name',
|
|
|
|
|
'=',
|
|
|
|
|
'1.23456789E+5',
|
|
|
|
|
';',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2018-08-10 22:11:53 +00:00
|
|
|
'var name = 1.23456789E-5;',
|
|
|
|
|
[
|
|
|
|
|
'var',
|
|
|
|
|
'name',
|
|
|
|
|
'=',
|
|
|
|
|
'1.23456789E-5',
|
|
|
|
|
';',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2018-08-10 22:11:53 +00:00
|
|
|
[
|
|
|
|
|
// Regression test for T201606.
|
|
|
|
|
// Must not break between 'return' and Expression.
|
|
|
|
|
<<<JAVASCRIPT
|
|
|
|
|
call( function () {
|
|
|
|
|
try {
|
|
|
|
|
} catch (e) {
|
JavaScriptMinifier: Fix bad state after '{}' in property value
Previously, $push contained:
self::PROPERTY_EXPRESSION_OP => [
self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
],
But $pop contained:
self::PROPERTY_EXPRESSION_OP => [ self::TYPE_BRACE_CLOSE => true ]
This meant that when a closing brace was found inside a property
expression, it would wrongly pop the stack, eventhough we are still
inside the property expression.
The impact is that everything after this is one level higher in
the stack than it should be, causing various other types to be
misinterpreted. Including in the following contrived example:
call( function () {
try {
} catch (e) {
obj = {
key: 1 ? 0 : {} // A
}; // B
} // C
return name === 'input';
} );
In the above, the closing brace at A would close the 'obj.key' assignment
(PROPERTY_EXPRESSION_OP), instead of waiting for the closing brace at B to
decide that.
Then the closing brace at B would wrongly close the 'catch' block (instead of
the 'obj' assignment). And lastly, the closing brace at C would close the
function body (STATEMENT).
This resulted in keyword 'return' being interpreted while in state
PAREN_EXPRESSION_OP instead of STATEMENT, where PAREN_EXPRESSION_OP is the
arguments list to `call()`. In an argument list, TYPE_RETURN is not valid,
which means we stay in that state, instead of progressing to EXPRESSION_NO_NL,
which then wrongly allows for a line break to be inserted.
Bug: T201606
Change-Id: I07b809a7ca56e282ecb48b5c89c217b4b8da6856
2018-08-10 22:23:08 +00:00
|
|
|
obj = {
|
|
|
|
|
key: 1 ? 0 : {}
|
2018-08-10 22:11:53 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return name === 'input';
|
|
|
|
|
} );
|
|
|
|
|
JAVASCRIPT
|
|
|
|
|
,
|
|
|
|
|
[
|
|
|
|
|
'call',
|
|
|
|
|
'(',
|
|
|
|
|
'function',
|
|
|
|
|
'(',
|
|
|
|
|
')',
|
|
|
|
|
'{',
|
|
|
|
|
'try',
|
|
|
|
|
'{',
|
|
|
|
|
'}',
|
|
|
|
|
'catch',
|
|
|
|
|
'(',
|
|
|
|
|
'e',
|
|
|
|
|
')',
|
|
|
|
|
'{',
|
JavaScriptMinifier: Fix bad state after '{}' in property value
Previously, $push contained:
self::PROPERTY_EXPRESSION_OP => [
self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
],
But $pop contained:
self::PROPERTY_EXPRESSION_OP => [ self::TYPE_BRACE_CLOSE => true ]
This meant that when a closing brace was found inside a property
expression, it would wrongly pop the stack, eventhough we are still
inside the property expression.
The impact is that everything after this is one level higher in
the stack than it should be, causing various other types to be
misinterpreted. Including in the following contrived example:
call( function () {
try {
} catch (e) {
obj = {
key: 1 ? 0 : {} // A
}; // B
} // C
return name === 'input';
} );
In the above, the closing brace at A would close the 'obj.key' assignment
(PROPERTY_EXPRESSION_OP), instead of waiting for the closing brace at B to
decide that.
Then the closing brace at B would wrongly close the 'catch' block (instead of
the 'obj' assignment). And lastly, the closing brace at C would close the
function body (STATEMENT).
This resulted in keyword 'return' being interpreted while in state
PAREN_EXPRESSION_OP instead of STATEMENT, where PAREN_EXPRESSION_OP is the
arguments list to `call()`. In an argument list, TYPE_RETURN is not valid,
which means we stay in that state, instead of progressing to EXPRESSION_NO_NL,
which then wrongly allows for a line break to be inserted.
Bug: T201606
Change-Id: I07b809a7ca56e282ecb48b5c89c217b4b8da6856
2018-08-10 22:23:08 +00:00
|
|
|
'obj',
|
2018-08-10 22:11:53 +00:00
|
|
|
'=',
|
|
|
|
|
'{',
|
JavaScriptMinifier: Fix bad state after '{}' in property value
Previously, $push contained:
self::PROPERTY_EXPRESSION_OP => [
self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
],
But $pop contained:
self::PROPERTY_EXPRESSION_OP => [ self::TYPE_BRACE_CLOSE => true ]
This meant that when a closing brace was found inside a property
expression, it would wrongly pop the stack, eventhough we are still
inside the property expression.
The impact is that everything after this is one level higher in
the stack than it should be, causing various other types to be
misinterpreted. Including in the following contrived example:
call( function () {
try {
} catch (e) {
obj = {
key: 1 ? 0 : {} // A
}; // B
} // C
return name === 'input';
} );
In the above, the closing brace at A would close the 'obj.key' assignment
(PROPERTY_EXPRESSION_OP), instead of waiting for the closing brace at B to
decide that.
Then the closing brace at B would wrongly close the 'catch' block (instead of
the 'obj' assignment). And lastly, the closing brace at C would close the
function body (STATEMENT).
This resulted in keyword 'return' being interpreted while in state
PAREN_EXPRESSION_OP instead of STATEMENT, where PAREN_EXPRESSION_OP is the
arguments list to `call()`. In an argument list, TYPE_RETURN is not valid,
which means we stay in that state, instead of progressing to EXPRESSION_NO_NL,
which then wrongly allows for a line break to be inserted.
Bug: T201606
Change-Id: I07b809a7ca56e282ecb48b5c89c217b4b8da6856
2018-08-10 22:23:08 +00:00
|
|
|
'key',
|
2018-08-10 22:11:53 +00:00
|
|
|
':',
|
|
|
|
|
'1',
|
|
|
|
|
'?',
|
|
|
|
|
'0',
|
|
|
|
|
':',
|
|
|
|
|
'{',
|
|
|
|
|
'}',
|
|
|
|
|
'}',
|
|
|
|
|
';',
|
|
|
|
|
'}',
|
JavaScriptMinifier: Fix bad state after '{}' in property value
Previously, $push contained:
self::PROPERTY_EXPRESSION_OP => [
self::TYPE_PAREN_OPEN => self::PROPERTY_EXPRESSION_OP
],
But $pop contained:
self::PROPERTY_EXPRESSION_OP => [ self::TYPE_BRACE_CLOSE => true ]
This meant that when a closing brace was found inside a property
expression, it would wrongly pop the stack, eventhough we are still
inside the property expression.
The impact is that everything after this is one level higher in
the stack than it should be, causing various other types to be
misinterpreted. Including in the following contrived example:
call( function () {
try {
} catch (e) {
obj = {
key: 1 ? 0 : {} // A
}; // B
} // C
return name === 'input';
} );
In the above, the closing brace at A would close the 'obj.key' assignment
(PROPERTY_EXPRESSION_OP), instead of waiting for the closing brace at B to
decide that.
Then the closing brace at B would wrongly close the 'catch' block (instead of
the 'obj' assignment). And lastly, the closing brace at C would close the
function body (STATEMENT).
This resulted in keyword 'return' being interpreted while in state
PAREN_EXPRESSION_OP instead of STATEMENT, where PAREN_EXPRESSION_OP is the
arguments list to `call()`. In an argument list, TYPE_RETURN is not valid,
which means we stay in that state, instead of progressing to EXPRESSION_NO_NL,
which then wrongly allows for a line break to be inserted.
Bug: T201606
Change-Id: I07b809a7ca56e282ecb48b5c89c217b4b8da6856
2018-08-10 22:23:08 +00:00
|
|
|
// The return Statement:
|
|
|
|
|
// return [no LineTerminator here] Expression
|
|
|
|
|
'return name',
|
2018-08-10 22:11:53 +00:00
|
|
|
'===',
|
|
|
|
|
"'input'",
|
|
|
|
|
';',
|
|
|
|
|
'}',
|
|
|
|
|
')',
|
|
|
|
|
';',
|
|
|
|
|
]
|
|
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-21 22:20:06 +00:00
|
|
|
}
|
2012-06-20 01:52:37 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-08-10 22:11:53 +00:00
|
|
|
* @dataProvider provideLineBreaker
|
2013-10-24 16:45:52 +00:00
|
|
|
* @covers JavaScriptMinifier::minify
|
2012-06-20 01:52:37 +00:00
|
|
|
*/
|
2018-08-10 22:11:53 +00:00
|
|
|
public function testLineBreaker( $code, array $expectedLines ) {
|
|
|
|
|
$this->setMaxLineLength( 1 );
|
|
|
|
|
$actual = JavaScriptMinifier::minify( $code );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
array_merge( [ '' ], $expectedLines ),
|
|
|
|
|
explode( "\n", $actual )
|
|
|
|
|
);
|
2012-06-20 01:52:37 +00:00
|
|
|
}
|
2011-03-14 11:44:33 +00:00
|
|
|
}
|