Js-beautify: Option to preserve existing var and object alignment (like preserve_array_indentation)

Created on 31 Jan 2014  ·  18Comments  ·  Source: beautify-web/js-beautify

Would it be possible/acceptable to add an option that converts

var short_var = "value";
var very_long_var = "value";

var obj = {
    "short_prop": "value",
    "very_long_property": "value" 
}

to

var short_var     = "value";
var very_long_var = "value";

var obj = {
    "short_prop"        : "value",
    "very_long_property": "value" 
}
javascript enhancement

Most helpful comment

+1. A solution that either handles the spacing or at least ignores the spacing inside objects. I don't even mind doing the spacing myself (my editor can do that) so long as JSB doesn't then revert my changes when I save.

All 18 comments

Theoretically — yes, it wouldn't be against the beautifier's spirit,
but the practical implementation would be quite horrible and hackish, the parser isn't really suited for observing the code in the ways needed for this.

See #200.

Thanks.
Instead, could we just add an option to prevent the beautifier from removing those extra whitespaces ?
We are already using that "alignment rule" in our project and I'd like to use the beautifier and keep the alignment.
Is it too specific to be implemented ?

Interesting.

Yes, that seems possible - we have an option to preserve array indentation the serves a similar purpose. The code path for this feature would be similar to that. (I've edited the title for this.)

However, it is lower priority for us than several other highly requested features and fixes. You are welcome to code it up and submit a PR, but I will warn you we're asking people to submit tests as well and we're trying to minimize api changes, especially on the command-line. So there might need to be some additional discussion.

Worst case if we don't accept your PR is still that you have a patch that you can use locally. :smile:

+1

I have no idea what bearing the features of PHP_Beautify has have on this project. :smile:

@bitwiseman It demonstrates a precedent for the feature

Sorry, I was mostly teasing you. :smile:
I already agreed this is a valid and potentially useful feature, just lower priority and high difficulty than other currently open issues.

I'll try to find some free time to make a PR then :)

A little unsolicited advice: I'd try implementing this for just the object literal case first. It's relatively cleanly recognized start and end, during which you can easily track the lines where fields are declared and the field lengths. Do a PR for that, we can discuss and work out any rough edges, and the go back and do that var case next. If you get stuck, drop me a line or an intermediate branch to look at.

Excuse my ignorance but is there any current way to preserve object formatting like:

var obj = { a: 123, b: "xyz" };

whoops looks like I should be looking at #315 and #370

eslint can do this for you with
~
Fix all auto-fixable problems
~

and the configuration:
~
"key-spacing": [2, {
"afterColon": true,
"align": "value",
"beforeColon": false
}]
~

This should link to #365 and #603

@clawconduce - Agreed, thanks.

+1. A solution that either handles the spacing or at least ignores the spacing inside objects. I don't even mind doing the spacing myself (my editor can do that) so long as JSB doesn't then revert my changes when I save.

See #1209 , it's not pretty, but it's at least a start, and could be mergeable with some love.

I gave this a quick go in #1209, but I don't think I'll have the time to properly wrap it up.

Among the harder things to figure out are when to ignore this logic - I maintain some files where the keys are function names and the values are the functions themselves - so the objects are hundreds of lines and I don't want those indented. Maybe if a function appears in the object, this formatting doesn't happen? Maybe it doesn't happen if the object covers a certain number of lines?

Anyways, hope someone can pick this up!

Was this page helpful?
0 / 5 - 0 ratings