Sublime JSFormat: configure to not auto-format JSON
Asked Answered
M

1

7

I have been looking around the web for a good Sublime (3) package to use to auto-format my source code in various languages, namely JavaScript. I came across this SOF post (Sublime Text 2: Auto fix indentation for javascript?) and thus decided to give JSFormat a try. So far, it seems to work pretty good...except for when it handles JSON objects in the JS code. For example, let's say that I have a function like this:

function foo() {
  return {name: 'Dave', score: 1000};
}

It returns a JavaScript object in JSON format, prettu much a hash object. I like writing such objects in one line because it is simple and easy to read, especially since it is just a small, ad-hoc object. But, if I were to format this with JSFormat, my function would now look like this:

function foo() {
  return {
    name: 'Dave',
    score: 1000
  };
}

Maybe this is just me, but I really don't like representing such simple JSON objects in multiple lines. Yes, normally JavaScript code that requires braces should have its contents on a separate lines from the braces, such as functions, if statements and loops. Maybe if the JSON object was a long object that contained functions inside of it, such as a jQuery Ajax class, then it makes sense to separate the attributes onto multiple lines.

Nonetheless, regardless whether my points about the braces makes sense, I know that JSFormat supports configuration and perhaps there is a way to configure JSFormat to not separate the attributes of a JSON object into multiple lines, if it is not desired. Any ideas?

Mohan answered 5/10, 2013 at 16:47 Comment(0)
S
6

Sorry for the bad news, but...

JSFormat uses js-beautify, which does not support single-line function definitions. Everything is broken into "beautified" lines, making it "more readable."

Look at the example given for js-beautify... the example itself is of a single-line function definition. There is no way to distinguish single-line function definitions you do want preserved from those you don't.

If you think about it, the ideal situation to use a beautifier is if you want to take minified code and make it readable... That's just one long line of code too.

I feel your pain, believe me.

Sjambok answered 13/1, 2014 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.