Page MenuHomePhabricator

ResourceLoader removes space in "1 .toString" and causes "SyntaxError: identifier starts immediately after numeric literal"
Closed, ResolvedPublic

Description

Valid (though, in this case, not exactly useful) input code:

var _ = 1 .toString;

Invalid output code after ResourceLoader minification:

var _=1.toString;

This is not valid syntax: an integer number literal can’t be followed directly by a member operator. Valid syntax would be 1 .toString (with a space, as in the input code) or 1.0.toString.

Firefox message:

SyntaxError: identifier starts immediately after numeric literal

Chromium and Node.js message:

SyntaxError: Invalid or unexpected token

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

The usual workaround for such issues is to disable ResourceLoader minification with a /*@nomin*/ comment; if the code is some kind of build result (and hopefully you weren’t writing 1 .toString manually), it’s probably minified already anyways. (Example implementation using vite-plugin-banner: https://github.com/wmde/new-lexeme-special-page/pull/82/commits/949e85f796c98a6dfcb1db6396be5afde0a73f71)

Krinkle triaged this task as High priority.Mar 21 2022, 6:13 PM

@Lucas_Werkmeister_WMDE Another option is to not minify it ahead of time. This might make debugging easier as well.

Confirmed:

nobody in Minify$ echo 'var _ = 1 .toString(); console.log(_);' | node
1
nobody in Minify$ echo 'var _ = 1 .toString(); console.log(_);' | bin/minify js | node
var _=1.toString();console.log(_);
      ^^
SyntaxError: Invalid or unexpected token

Change 776051 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/libs/Minify@master] JavaScriptMinifier: Fix handling of property on dotless number literal

https://gerrit.wikimedia.org/r/776051

Change 776051 merged by jenkins-bot:

[mediawiki/libs/Minify@master] JavaScriptMinifier: Fix handling of property on dotless number literal

https://gerrit.wikimedia.org/r/776051

Next: Release and pull through to core's composer vendor.

Change 785863 had a related patch set uploaded (by Krinkle; author: Krinkle):

[mediawiki/core@master] resourceloader: Update wikimedia/minify to 2.3.0

https://gerrit.wikimedia.org/r/785863

Change 785863 merged by jenkins-bot:

[mediawiki/core@master] resourceloader: Update wikimedia/minify to 2.3.0

https://gerrit.wikimedia.org/r/785863