Closed Bug 906640 Opened 6 years ago Closed 6 years ago

Scratchpad does not have any script beautify/prettify function

Categories

(DevTools Graveyard :: Scratchpad, enhancement, P2)

enhancement

Tracking

(Not tracked)

RESOLVED FIXED
Firefox 26

People

(Reporter: eldmannen+mozilla, Assigned: fitzgen)

References

Details

Attachments

(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
Status: UNCONFIRMED → NEW
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

https://tbpl.mozilla.org/?tree=Try&rev=f6d379c287e3
Attachment #797854 - Flags: review?(anton)
Comment on attachment 797854 [details] [diff] [review]
bug-906640-scratchpad-pretty-print.patch

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.

https://tbpl.mozilla.org/?tree=Try&rev=ffe0e0b071cc
Attachment #797854 - Attachment is obsolete: true
https://hg.mozilla.org/mozilla-central/rev/cff37b5ea5fb
Status: NEW → RESOLVED
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 https://bugzilla.mozilla.org/js/yui/yahoo-dom-event/yahoo-dom-event.js

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() {
> }

Becomes:
> 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.