Commit graph

36 commits

Author SHA1 Message Date
Timo Tijhof
15110b1ca9 JavaScriptMinifier: Fix bad state after ternary in object literal
The following pattern of input (found in jquery.js) triggered
this bug:

    call( {
        key: 1 ? 0 : function () {
            return this;
        }
    } );

The open brace changes state to PROPERTY_ASSIGNMENT (for object literals).
The colon after 'key' sets state to PROPERTY_EXPRESSION.

Each individual parts of an expression (identifiers and literal values)
is recognised with state *_EXPRESSION_OP, such as PROPERTY_EXPRESSION_OP.

The '1' after 'key:' correctly sets the state to PROPERTY_EXPRESSION_OP.
Upto there it goes well, but after that it goes wrong.

The question mark (TYPE_HOOK) in this context was wrongly switching
back to PROPERTY_EXPRESSION. That is a problem because that does not
handle TYPE_COLON, which meant '0: function' was seen together as a
sequence of continuous PROPERTY_EXPRESSION_OP where TYPE_FUNC may
not be handled.

Fixed by changing handling of TYPE_HOOK in PROPERTY_EXPRESSION to
switch states to EXPRESSION_TERNARY, and also performing a push
so that ternary handling can pop back to the property expression.

This mirrors the handling that already exists for ternaries in
the regular handling of EXPRESSION/EXPRESSION_OP (as opposed to
the variant for object literal properties).

Bug: T201606
Change-Id: I6104c839cfc3416257543b54a91b74cb4aa4193b
2018-08-16 17:02:07 +00:00
Timo Tijhof
92ce940ec3 JavaScriptMinifier: Add test case for another line-break bug
Discovered by adding a test case that uses file_get_contents()
to pass jquery.js, and then scan the PHPUnit output for an entry
containing `return` by itself on a line where the next line
is isn't `;` - then reducing it to a test case as small as possible.

This was reduced from the definition of jQuery.event.addProp.

Bug: T201606
Change-Id: I1f907436c32630102e60e3ded7092dbeb9669fe8
2018-08-12 00:24:56 +01:00
Timo Tijhof
da3aa28cea JavaScriptMinifier: Move test case for 'x++' to provideLineBreaker()
Easier to read.

Bug: T201606
Change-Id: Ie12910edf5ab6a9d7246000ca78a3c9208aeb152
2018-08-12 00:24:10 +01:00
Timo Tijhof
1f5f6fc204 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-11 00:16:47 +01:00
Timo Tijhof
8cfe4abf8d JavaScriptMinifier: Add better line-breaker tests
Set maxLineLength to '1' instead of messing with filler content.
This makes it easy to see all potential line-breaks, instead of only
at the 999th offset.

Bug: T201606
Change-Id: I220b145c5bc8e7d1a41efacd2a6cea738545f006
2018-08-10 23:22:49 +01:00
Timo Tijhof
53e05f7b9c JavaScriptMinifier: Add test case for T201606
Bug: T201606
Change-Id: I2058765c7f1cfb9e2d644f19e780926d01b9b68c
2018-08-10 21:04:58 +01:00
Umherirrender
63d96c15fd build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
Change-Id: I59b59f79bbf3ce4feff3b3a20c1c31bc16370531
2018-02-17 13:29:13 +01:00
Reedy
39f0f919c5 Update suppressWarning()/restoreWarning() calls
Bug: T182273
Change-Id: I9e1b628fe5949ca54258424c2e45b2fb6d491d0f
2018-02-10 08:50:12 +00:00
Timo Tijhof
3316a00c26 JavaScriptMinifier: Fix "Uninitialized offset" in regexp char class parsing
Bug: T75556
Change-Id: I0bb63212dd44aec3c6b40477553dbf6a471bc7b3
2018-01-30 19:55:15 -08:00
Timo Tijhof
f2ef535e39 JavaScriptMinifier: Fix "Uninitialized offset" in string and regexp parsing
When parsing an incomplete string literal or regex literal at the end of a file,
$end would be set to an offset higher than $length, because the code
speculatively increases $end without correcting for this scenario.

This is due to the assumption that the strcspn() search will end because
an end character was seen, instead of simply ending because the string
doesn't have any further characters.

Bug: T75556
Change-Id: I2325c9aff33293c13ff414699c2d47306182aaa6
2018-01-11 17:47:59 +00:00
Kunal Mehta
75160bdd3b Use MediaWikiCoversValidator for tests that don't use MediaWikiTestCase
Change-Id: I8c4de7e9c72c9969088666007b54c6fd23f6cc13
2018-01-01 08:28:02 +00:00
Timo Tijhof
201c1d226c resourceloader: Improve JavaScriptMinifier code coverage
Change-Id: I234b996cfec8ef48ce3dc48aabbdf88bc9439c15
2017-12-22 18:36:09 +01:00
James D. Forrester
1e9c361960 tests: Replace implicit Bugzilla bug numbers with Phab ones
It's unreasonable to expect newbies to know that "bug 12345" means "Task T14345"
except where it doesn't, so let's just standardise on the real numbers.

Change-Id: I46261416f7603558dceb76ebe695a5cac274e417
2017-02-21 02:14:34 +00:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Ori Livneh
3ecec5434e 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
2015-08-15 14:13:59 -07:00
Kunal Mehta
f6e5079a69 Use mediawiki/at-ease library for suppressing warnings
wfSuppressWarnings() and wfRestoreWarnings() were split out into a
separate library. All usages in core were replaced with the new
functions, and the wf* global functions are marked as deprecated.

Additionally, some uses of @ were replaced due to composer's autoloader
being loaded even earlier.

Ie1234f8c12693408de9b94bf6f84480a90bd4f8e adds the library to
mediawiki/vendor.

Bug: T100923
Change-Id: I5c35079a0a656180852be0ae6b1262d40f6534c4
2015-06-11 18:49:29 +00:00
gladoscc
2967766734 Rename testBug32548Exponent to testExponentLineBreaking
Fix for @todo

Change-Id: I0dcef36fe9cee95ae320b535bac883d5e26ad140
2015-01-09 12:06:31 +00:00
Chad Horohoe
59afd687ce Convert most libs/ tests to use PHPUnit_Framework_TestCase
They don't actually need any of the mess that MediaWikiTestCase provides

Change-Id: Ibd067480fc294096d1249132cb800e09889efb18
2014-12-29 20:53:24 -08:00
Siebrand Mazeland
896bda7c85 Pass phpcs-strict on some test files (7/11)
Change-Id: Ia0ceea60e7ef43959f868378f3fcd463a3adf782
2014-04-24 18:27:12 +00:00
addshore
9e85aabe52 Cleanup a bunch of tests and add todos
Change-Id: Iae44427edee3ed2a62abdb5a8f5d9f1ed2e7d660
2014-03-09 15:12:45 +01:00
addshore
749599fc3c @covers tags for unit tests
Change-Id: I2b486b38326bf23bd379efba2142034e24c0745f
2013-10-24 18:22:57 +01:00
addshore
de7af7ac2c Fix scope on all /phpunit test methods
Change-Id: I3ce92463d485a0fb23e464e9a8059330f32d79af
2013-10-24 10:31:32 +02:00
Timo Tijhof
b36d883017 Tests: Make phpunit providers "public static".
Follows-up I9d2b148e57 (including phpunit/languages this time).

Bug: 46434
Change-Id: I30e5efcd88c516121c454676bd7a18f9b7c8fca6
2013-03-22 03:12:37 +01:00
Siebrand Mazeland
de04f37ef1 Update formatting
5 of n.

Change-Id: I811ca957b6588085d67606ebc0cd4033a1e53839
2013-02-15 12:35:50 +00:00
Timo Tijhof
c33087beaa JavaScriptMinifierTest: Increase test coverage
* Per https://bugzilla.wikimedia.org/show_bug.cgi?id=27395#c1
  adding test cases for things that broke in the past:
  - bug 26931
  - bug 27046

* Added various other small tests that seem fitting in their context

* Moved testBug32548Exponent to below the data provider (like most
  other tests iirc).

Change-Id: I7a4df097108ec05bc58d5a110329e13ff9587c8a
2012-06-20 05:18:39 +02:00
Brion Vibber
e97346ec18 Revert r103978, r103979 -- screwed something up, breaks jQuery minification.
Incremented ResourceLoader::filterCacheVersion rather than decrementing to avoid potential confusion, especially since we already needed the incr.
2011-11-23 00:22:46 +00:00
Brion Vibber
6b06770b6d Followup r103865 and r103915
r103915 added a parse error for 'more than 2 decimal points' in a number; this is the wrong place to check for that. Should only check whether there's more digits or identifier chars -- identifier chars would be illegal.

Added test cases for the exponent missing fails, tweaked it to be more consistent (only need to check for one e; if we have more we can lump them in with 'not digits' :)


Also cleaned up no-longer-needed suppress/restore warnings around JS parser invocation
2011-11-22 23:10:22 +00:00
Platonides
a82a7940f6 Final fixup to r103910 and follow-ups.
Reverts r103931 test change.
2011-11-22 18:56:55 +00:00
Platonides
6cac97e88c Two decimal points may be valid, as 5..toString() == (5.).toString()
Added some tests
2011-11-22 18:42:21 +00:00
Platonides
b23dc2f758 Follow-up r103915: We need to increment $end before the strcspn.
Adjusting the tests, since it is now adding the newline after the number.
2011-11-22 18:10:25 +00:00
Brion Vibber
8bf802734c Add PHPUnit tests for the minification failure case in bug 32548.
This will trigger 2 test failures, where an exponent in a JS numeric literal gets split over line breaks at the '-' or '+', causing a parse error in the resulting output.
A number with the same string length but without using + or - in the exponent passes through fine, indicating that it's the -/+ that's getting misinterpreted.
2011-11-21 22:20:06 +00:00
Brion Vibber
72ba305da4 * (bug 31187) Fix for user JavaScript validation to allow identifiers with valid Unicode letter characters
Followup r91591, r93020: patch to jsminplus to support Unicode chars and char escapes in identifiers

Fast-path check keeps runtime about the same on most scripts (eg jquery.js parsing was abround 4100ms both before and after on my test machine)
Slow-path code kicks in if plain ASCII word chars don't extend all the way to the next whitespace or punctuation char.
Using PCRE's Unicode properties magic to ensure that we're catching everything, following ECMA-262 edition 5.1 spec.

Note that identifiers using escapes don't get normalized to their UTF-8 form; this might be a nice thing to do as it saves a couple bytes, but currently there's no change made to output.


Added QUnit tests to verify that unicode letter & escapes work in identifiers in all supported browsers (ok back to IE 6, yay)
2011-09-27 22:51:36 +00:00
Brion Vibber
12ccca0349 Followup r83885: add JSMin+ 1.3 to use its parser to verify output of JavaScriptMinifierTest unit test cases. Had to change some of the test cases because they were not valid JavaScript programs -- one of the quoting tests was incorrectly quoted in the PHP sources, and several tests around return, continue, and break keywords failed due to not using them in the contexts that they require.
http://crisp.tweakblogs.net/blog/1856/jsmin+-version-13.html
JSMin+ under MPL 1.1/ GPL 2.0 / LGPL 2.1 license.
2011-07-06 20:02:10 +00:00
Roan Kattouw
27508f06af Followup r83891: don't insert a newline before ++ or -- . Patch by Paul Copperman 2011-03-14 18:04:39 +00:00
Roan Kattouw
5ce4ecfced Per Nikerabbit, add some minifier tests for historic failure cases that I had lying around 2011-03-14 13:55:42 +00:00
Roan Kattouw
867fc1cba8 (bug 27528) Incorporate Paul Copperman's minifier 2011-03-14 11:44:33 +00:00