beautifier / js-beautify

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

Allow single chained methods to be placed on same line #293

Open Qantas94Heavy opened 11 years ago

Qantas94Heavy commented 11 years ago

I'd like an option to allow single chained methods to be placed on same line, as in if there is only two calls, then those two are placed on the same line. Here are some examples of code that doesn't really look good with the current "break lines on chained methods" option:

this._foo = new Date()
    .getTime();

$(document)
    .ready(function () {
        // function code here
    });

Would this be feasible or is this just too much work for little gain?

bitwiseman commented 11 years ago

You can turn off "break lines on chained methods", and then turn on "preserve newlines". This won't enforce the structure, but it will preserve what you have:

// input - expected to be unchanged after beautify
this._foo = new Date().getTime().milliseconds();

// other input - also expected to be unchanged
this._foo = new Date().getTime()
    .milliseconds();

Combine this with line wraps and you should get reasonable chains. If what I'm describing doesn't sound sufficient, could you give some more extensive examples? Include input, current output, and what you'd like to see.

gilazo commented 9 years ago

I believe I'm experiencing something similar.

I have the following code:

angular
    .module("app", []);

When I run beautify I get the following:

angular.module("app", []);

I want my first example to be unaffected by beautifying.

Any tips or hints?

bitwiseman commented 9 years ago

@gilazo - set break_chained_methods option to true.

Bram-Zijp commented 8 years ago

I have the same issue and turning off "break lines on chained methods" is not possible for in my case. Input:

daisy.chain().me().please();

undefined._foo = new Date().getTime();

$(document).ready(function () {
    // function code here
});

$('foo').on('focus blur', function () {
    var foo = bar;
}).on('click', function () {
    var bar = foo;
});

output:

daisy.chain()
    .me()
    .please();

undefined._foo = new Date()
    .getTime();

$(document)
    .ready(function() {
        // function code here
    });

$('foo')
    .on('focus blur', function() {
        var foo = bar;
    })
    .on('click', function() {
        var bar = foo;
    });

desired output:

// i can live with this:
daisy.chain()
    .me()
    .please();
// i believe this is the latest convention:
daisy
    .chain()
    .me()
    .please();

undefined._foo = new Date().getTime();

$(document).ready(function() {
    // function code here
});

$('foo')
    .on('focus blur', function() {
        var foo = bar;
    })
    .on('click', function() {
        var bar = foo;
    });
joaobarcia commented 7 years ago

Related to #809 ?

bitwiseman commented 7 years ago

@joaobarcia - Sure. #809 would be the more flexible setting.