Open Bug 1686471 Opened 5 years ago Updated 1 year ago

Scratchpad "Pretty Print" doesn't understand template literals

Categories

(SeaMonkey :: General, defect)

SeaMonkey 2.53
defect
Not set
normal

Tracking

(Not tracked)

UNCONFIRMED

People

(Reporter: fieldhouse, Unassigned)

Details

The following JS statement prints Hello world to the console with a trivial use of a template literal:

console.log(`Hello ${"world"}`); 

With this statement in the Scratchpad (Shift+F4), select "Pretty Print":

Exception: Error while performing task "pretty-print": Error: Unexpected character '`' (1:12)
[4]</pp.raise@resource://devtools/shared/acorn/acorn.js:940:13
[13]</pp.getTokenFromCode@resource://devtools/shared/acorn/acorn.js:2785:3
[13]</pp.readToken@resource://devtools/shared/acorn/acorn.js:2490:10
[13]</pp.nextToken@resource://devtools/shared/acorn/acorn.js:2482:66
[13]</pp.next@resource://devtools/shared/acorn/acorn.js:2431:3
[13]</pp.getToken@resource://devtools/shared/acorn/acorn.js:2435:3
prettyFast@resource://devtools/shared/pretty-fast/pretty-fast.js:778:15
@resource://devtools/server/actors/pretty-print-worker.js:41:24
createHandler/<@resource://devtools/shared/worker/helper.js:85:24

@resource://devtools/server/actors/pretty-print-worker.js:51:12
createHandler/<@resource://devtools/shared/worker/helper.js:85:24

*/```
The test statement can however be run from the Scratchpad.

Obviously this isn't satisfactory.

I don't know whether it affects other code branches with Scratchpad (eg, incomplete implementation of bug 1317375), or it's specific to SeaMonkey, or it's just an issue from porting to SeaMonkey. It was probably present in 2.49.x. Apparently (bug 1565380) Scratchpad is no longer shipped in FireFox, and and I don't have a relevant old version to hand for testing.

The pretty-print code has a flag for selecting ES6 syntax, but it isn't set.

Scratchpad is in chrome/devtools/content/scratchpad/scratchpad.js.

Via chrome/devtools/modules/devtools/server/actors/pretty-print-worker.js, it calls the prettyFast(input, options) method from chrome/devtools/modules/devtools/shared/pretty-fast/pretty-fast.js to return the re-formatted code string.

This in turn calls this function from chrome/devtools/modules/devtools/shared/acorn/acorn.js:

function tokenizer(input, options) {
  return new _state.Parser(options, input);
}

The options object has a member ecmaversion. If this has value >= 6, ES6 parsing is enabled. From inspection it handles template syntax, let, const, etc, all of which confuse the released pretty-printer.

So there are various implementation options:

  • minimally, set ecmaversion to 6 in the call to prettyFast() , or
  • create a preference to be set as the ecmaversion value, or
  • add a UI for setting ecmaversion from the Scratchpad window (eg, Edit>JS Version), or
  • wrap the call to prettyFast() in a try block and retry as ES6 in case of error, or
  • some combination.

Since the Scratchpad supports evaluation of ES6 syntax (eg, evaluates `Hello ${"world"}` to "Hello world"), there's no reason not to have the first option:

--- a/chrome/devtools/modules/devtools/server/actors/pretty-print-worker.js
+++ b/chrome/devtools/modules/devtools/server/actors/pretty-print-worker.js
@@ -40,7 +40,8 @@
   try {
     const prettified = prettyFast(source, {
       url: url,
-      indent: " ".repeat(indent)
+      indent: " ".repeat(indent),
+      ecmaversion: 6
     });
 
     return {
Version: SeaMonkey 2.53 Branch → SeaMonkey 2.53

Above, ecmaversion should be ecmaVersion. Then the following is also needed, because the options object for prettyFast() is not passed directly through to tokenizer().

--- a/chrome/devtools/modules/devtools/shared/pretty-fast/pretty-fast.js
+++ b/chrome/devtools/modules/devtools/shared/pretty-fast/pretty-fast.js
@@ -761,6 +761,7 @@
     var tokenQueue = [];
 
     var tokens = acorn.tokenizer(input, {
+      ecmaVersion: options.ecmaVersion,
       locations: true,
       sourceFile: options.url,
       onComment: function (block, text, start, end, startLoc, endLoc) {
You need to log in before you can comment on or make changes to this bug.