jenkins-bot has submitted this change and it was merged.

Change subject: Only convert boolean true/false to !0/!1
......................................................................


Only convert boolean true/false to !0/!1

Fix-up for I5ab29b686b8. If we encounter stupid code like
`a.true = 1;` or `a = { true: 1 }`, we should not convert that to !0/!1.
Because JSMin barfs on such input, it is necessary to add another parameter to
the test method which specifies whether or not the minified JavaScript is
supposed to be valid JavaScript by the standards of JSMin.

Change-Id: Ib78c628147fdb95982d6e33e0ab298584fb63d0b
---
M includes/libs/JavaScriptMinifier.php
M tests/phpunit/includes/libs/JavaScriptMinifierTest.php
2 files changed, 21 insertions(+), 9 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  Jforrester: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/includes/libs/JavaScriptMinifier.php 
b/includes/libs/JavaScriptMinifier.php
index cb282eb..141a515 100644
--- a/includes/libs/JavaScriptMinifier.php
+++ b/includes/libs/JavaScriptMinifier.php
@@ -565,10 +565,13 @@
                                $out .= ' ';
                                $lineLength++;
                        }
-                       if( $token === 'true' ) {
-                               $token = '!0';
-                       } elseif( $token === 'false' ) {
-                               $token = '!1';
+                       if (
+                               $type === self::TYPE_LITERAL
+                               && ( $token === 'true' || $token === 'false' )
+                               && ( $state === self::EXPRESSION || $state === 
self::PROPERTY_EXPRESSION )
+                               && $last !== '.'
+                       ) {
+                               $token = ( $token === 'true' ) ? '!0' : '!1';
                        }
 
                        $out .= $token;
diff --git a/tests/phpunit/includes/libs/JavaScriptMinifierTest.php 
b/tests/phpunit/includes/libs/JavaScriptMinifierTest.php
index 13908b9..d23534e 100644
--- a/tests/phpunit/includes/libs/JavaScriptMinifierTest.php
+++ b/tests/phpunit/includes/libs/JavaScriptMinifierTest.php
@@ -140,6 +140,13 @@
                        array( "5..toString();", "5..toString();" ),
                        array( "5...toString();", false ),
                        array( "5.\n.toString();", '5..toString();' ),
+
+                       // Boolean minification (!0 / !1)
+                       array( "var a = { b: true };", "var a={b:!0};" ),
+                       array( "var a = { true: 12 };", "var a={true:12};", 
false ),
+                       array( "a.true = 12;", "a.true=12;", false ),
+                       array( "a.foo = true;", "a.foo=!0;" ),
+                       array( "a.foo = false;", "a.foo=!1;" ),
                );
        }
 
@@ -147,15 +154,17 @@
         * @dataProvider provideCases
         * @covers JavaScriptMinifier::minify
         */
-       public function testJavaScriptMinifierOutput( $code, $expectedOutput ) {
+       public function testJavaScriptMinifierOutput( $code, $expectedOutput, 
$expectedValid = true ) {
                $minified = JavaScriptMinifier::minify( $code );
 
                // JSMin+'s parser will throw an exception if output is not 
valid JS.
                // suppression of warnings needed for stupid crap
-               MediaWiki\suppressWarnings();
-               $parser = new JSParser();
-               MediaWiki\restoreWarnings();
-               $parser->parse( $minified, 'minify-test.js', 1 );
+               if ( $expectedValid ) {
+                       MediaWiki\suppressWarnings();
+                       $parser = new JSParser();
+                       MediaWiki\restoreWarnings();
+                       $parser->parse( $minified, 'minify-test.js', 1 );
+               }
 
                $this->assertEquals(
                        $expectedOutput,

-- 
To view, visit https://gerrit.wikimedia.org/r/231728
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib78c628147fdb95982d6e33e0ab298584fb63d0b
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Fomafix
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to