beautifier / js-beautify

Beautifier for javascript
https://beautifier.io
MIT License
8.61k stars 1.38k forks source link

break_chained_methods behaviour #809

Open marekkaczkowski opened 8 years ago

marekkaczkowski commented 8 years ago

There shoud be option to set max num of chained methods before breaking it:

Eg:

"break_chained_methods": 2

this.$("#fileName").val().addClass("disabled")
    .prop("disabled", true)

I see no sense in breaking when there is only 1 meth (it's not a chain in this case :)). So maybe default behaviour should be: if there is more than 1 chained meth then perform breaking.

joaobarcia commented 7 years ago

Definitely, this would be extremely useful. It is also the recommendation of AirBnB Javascript guidelines: https://github.com/airbnb/javascript#whitespace--chains

Use indentation when making long method chains (more than 2 method chains)

And makes all sense in terms of readibility

bitwiseman commented 7 years ago

@joaobarcia
This is an interesting guideline. In this "good" example:

$('#items')
  .find('.selected')
    .highlight()
    .end()
  .find('.open')
    .updateCount();

How are the indents for .hightlight() and .end() determined to be 4 instead of 2? If anything there have been more requests for unindenting - see #482.

joaobarcia commented 7 years ago

@bitwiseman I think they are increasing identation after each selector to make it easier to check what is being 'highlighted' and 'updated the count'.

But if that should be done or not is probably a whole other question by itself :P

Nevertheless, the part of "break chain if chain longer than x" I believe would be quite useful.