views:

60

answers:

2

I am looking for a tool that formats my javascript for me. There are many times when I wish I could just let a tool format my source code because of restructuring some code or because I want to make sure the code structure really is like I believe it to be. My problem is that all formatters I have found produce really retarded results.

If I format the source myself I would format it like this:

var structure = {
  key1: {
    subkey1: 3,
    subkey: 6,
    sublist: [
      {
         deep: "yes"
      },
      {
        deep: "yes",
        somekey: 4
      },
      5,
      10          
    ]
  },
  key2: [
    {
      key: 5,
      key2: 10
    },
    [1, 2, 3],
    [
      {
        nestedObj: "hello",
        siblingProp: 5
      },
      {}
    ]
  ]
}

Whatever tool I find doesn't have to do it exactly the same way but look at what eclipse's built in formatter does:

var structure = {
    key1 : {
        subkey1 : 3,
        subkey : 6,
        sublist : [{
                    deep : "yes"
                }, {
                    deep : "yes",
                    somekey : 4
                }, 5, 10]
    },
    key2 : [{
                key : 5,
                key2 : 10
            }, [1, 2, 3], [{
                        nestedObj : "hello",
                        siblingProp : 5
                    }, {}]]
}

Or the one I get with Aptana's plugin (3.0 beta)

var structure = {
    key1: {
        subkey1: 3,
        subkey: 6,
        sublist: [
        {
            deep: "yes"
        },
        {
            deep: "yes",
            somekey: 4
        },
        5,
        10
        ]
    },
    key2: [
    {
        key: 5,
        key2: 10
    },
    [1, 2, 3],
    [
    {
        nestedObj: "hello",
        siblingProp: 5
    },
    {}
    ]
    ]
}

They provide zero help in visualizing the structure of my datastructure. Does anyone know of a decent formatter? Please.

A: 

I think the JS Beautifier with "braces on own line" enabled comes at least close.

Pekka
it is mostly how the tools handle arrays that I cannot get over.
ormuriauga
@ormuri I see. ` `
Pekka
+1  A: 

WebStorm / PHPStorm / Intellij IDEA

Just made a quick check in my PHPStorm: i've pasted your code from "Eclipse" example, ran Code -> Reformat code and it gave me the code, like you would do it manually (the only difference is that it wraps colons with spaces from both sides)

Anton N
Can you show the result? Might be interesting to see
Pekka
here is my screenshot: http://lh4.ggpht.com/_rUMy9XR_FLU/TIiwBy08uNI/AAAAAAAAAGE/DDdtYHIQSVc/phpstorm-js-test.PNG
Anton N
Looks good. The big downside is that webstorm is about 60 euros, and it is rather shy on inserting linebreaks which leaves a lot of manual work helpiong it fix what eclipse has done to my code.
ormuriauga
if no-one comes up with something almost as good and free I will accept this as the answer. Thank you!
ormuriauga