Closed Bug 906640 Opened 6 years ago Closed 6 years ago

Scratchpad does not have any script beautify/prettify function


(DevTools Graveyard :: Scratchpad, enhancement, P2)



(Not tracked)

Firefox 26


(Reporter: eldmannen+mozilla, Assigned: fitzgen)




(1 file, 1 obsolete file)

User Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0 (Beta/Release)
Build ID: 20130818030219

Steps to reproduce:

Open Scratchpad. (Shift-F4)
Wrote ugly code.

Actual results:

Found no beautify/prettify button or menu entry.

Expected results:

There should have been a beautify/prettify button or menu entry that utilized the existing jsb (JavaScript beautifier) function in Firefox available in the Developer Toolbar (Shift-F2) under the 'jsb' command.
Component: Untriaged → Developer Tools: Scratchpad
Severity: normal → enhancement
Ever confirmed: true
OS: Windows 8 → All
Hardware: x86_64 → All
Version: 26 Branch → Trunk
Assignee: nobody → nfitzgerald
Depends on: 908913
hello sir

do you remember me?

I am contacting you by bugmail
Attachment #797854 - Flags: review?(anton)
Comment on attachment 797854 [details] [diff] [review]

Review of attachment 797854 [details] [diff] [review]:

Aside from one minor comment, looks good to me.

::: browser/devtools/scratchpad/scratchpad.js
@@ +533,5 @@
> +      const ast = Reflect.parse(uglyText);
> +      const prettyText = escodegen.generate(ast);
> +      this.setText(prettyText);
> +    } catch (e) {
> +      this.writeAsComment(DevToolsUtils.safeErrorString(e));

Why not this.writeAsErrorComment(e)?
Attachment #797854 - Flags: review?(anton) → review+
Priority: -- → P2
Using writeAsErrorComment. Pushing to try again to see if the "extra scratchpad window" thing has fixed itself.
Attachment #797854 - Attachment is obsolete: true
Closed: 6 years ago
Resolution: --- → FIXED
Whiteboard: [fixed-in-fx-team]
Target Milestone: --- → Firefox 26
I just tested the new "Pretty Print" button in the nightly build.

It works! :)
However, it strips all comments. :(
The "Pretty Print" button uses 2 space indention.

Open the Developer Toolbar (Shift+F2), type:
> jsb

The indention used by jsb defaults to 4 spaces.

This is inconsistent. It would be desirable to be consistent.

Douglas Crockford advocates four spaces.
Google and GitHub uses two spaces.
jQuery uses tabs.
Firefox built-in jsbeautifier defaults to two spaces.
The built-in prettyprinter in Chrome and Internet Explorer uses four spaces.
Given that Scratchpad itself uses four spaces by default, we should change Pretty Print to be consistent with it and use four spaces.
(In reply to Eldmannen from comment #9)
> However, it strips all comments. :(

Unfortunately Reflect.parse, which parses JS into AST, does not retain information about comments. In order to keep comments, we'd also have incorporate a separate parser, like Esprima or Acorn.
Filed bug 913060 regarding indentation
escodegen cannot handle some syntax (ForOfStatement, SpreadExpression, etc), when "Pretty Print", following lines will throw

  for (x of [1,2,3]) console.log(x);
  y = [0, ...[1,2,3]];
Depends on: 913301
It also strips newlines away from the code.

The code:
> function foo() {
> }
> function bar() {
> }

> function foo() {
> }
> function bar() {
> }

When using 'jsb' from the Developer Toolbar (Shift+F2) then this is not a problem. Only in Scratchpad.
Product: Firefox → DevTools
Product: DevTools → DevTools Graveyard
You need to log in before you can comment on or make changes to this bug.