beautifier / js-beautify

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

Indentation of function inside if #621

Open gabrielmaldi opened 9 years ago

gabrielmaldi commented 9 years ago

Beautifying the following chunk of code (which to me it is already beautified the right way):

if (data.every(function (a) {
    return a.value == 0;
})) {
    //Do something.
}

Produces:

if (data.every(function (a) {
        return a.value == 0;
    })) {
    //Do something.
}

Is this behavior by design or is it something that can be improved?

Thanks!

bitwiseman commented 9 years ago

Every time I think I've seen it all... I see the logic of your formatting, but getting that behavior is harder than you might think.
It's an interaction of #200 and #286 - we take out indents where they look redundant, but that led to under indenting of the conditional expressions inside if() in some cases. We fixed that, and by that logic the existing behavior makes sense. You're inside the conditional for the if, everything in there should be indented.

I'm open to discussing, but this seems like a low priority issue at best.

gabrielmaldi commented 9 years ago

I understand the complexity and that fixing this could cause other subtle issues. I say we wait and see if more people vote for this and maybe it becomes worth addressing.

Thanks

Icehunter commented 9 years ago

Late to comment but I really wish I could combine how we beautify the code and the rules in eslints "indent" rule.

Beautified:

var Count = function () {
    return new Promise(function (resolve, reject) {
        Client.count({
                index: 'something',
                type: 'data',
                body: {}
            })
            .then(resolve, reject);
    });
};

It will fail on the object being called in the function. So either I declare the object outside the function or I put .count on a new line to code around the linting.

Icehunter commented 9 years ago

As another option jsfiddle does the following:

var Count = function () {
    return new Promise(function (resolve, reject) {
        Client.count({
            index: 'something',
            type: 'data',
            body: {}
        })
            .then(resolve, reject);
    });
};
jonkri commented 4 years ago

I have a problem that I'm wondering if it's related to this.

Is there a way that I can get rid of the extra indentation in the Output section below?

Input/Expected (only one indentation before method, and no indentation before }) (which seem to conflict with ESLint's "indent": ["error", 2])):

fetch('https://example.com', {
  method: 'POST'
})
  .then(() => {})

Actual output:

fetch('https://example.com', {
    method: 'POST'
  })
  .then(() => {})

.jsbeautify:

{}

I have also tried with "unindent-chained-methods": true.

The version of js-beautify 1.10.2.

Thank you!

jonkri commented 4 years ago

There is an issue for my comment above in #1651.