millermedeiros / esformatter

ECMAScript code beautifier/formatter
MIT License
970 stars 91 forks source link

Unary and Binary Operators combined with Functions produce incorrect indentation #445

Open Faleij opened 8 years ago

Faleij commented 8 years ago

Thanks for fixing #443, however I have found some more indentation problems with Unary and Binary operators combined with Functions. Return statement is irrelevant in all cases, could as well be an variable assignment.

'use strict';

// Unary Operators produce incorrect indentation with functions (return statement is irrelevant)
function aFn() {
    return [].reduce((p, c) => {
            return p + c;
        }, 0) / 1;
}

function aFn() {
    return 1 + (function() {
            return arguments;
        })();
}

function aFn() {
    return typeof function() {
            return arguments;
        } === 'function';
}

function aFn() {
    const b = [].reduce((p, c) => {
            return p + c;
        }, 0) / 1;
    return b;
}

// Binary Operators |, &&, instanceof and in, (possibly more?) produce incorrect indentation combined with functions  (return statement is irrelevant)
function aFn() {
    return 1 | (function() {
            return arguments;
        })();
}

function aFn(a) {
    return (function() {
            return arguments;
        })() && (function() {
            return arguments;
        })(); // same results if functions are not wrapped
}

function aFn(a) {
    return [function() {
            return;
        }] instanceof Array;
}

function aFn(a) {
    const b = [function() {
            return;
        }] instanceof Array;
    return b;
}

function aFn(a) {
    return a in [function() {
            return;
        }];
}

function aFn(a) {
    return function() {
            return;
        } instanceof Function;
}

aFn();
$ esformatter test.js > test_out.js
$ eslint test_out.js

D:\github\test\test.js
   6:13  error  Expected indentation of 8 space characters but found 12  indent
   7:9   error  Expected indentation of 4 space characters but found 8   indent
  12:13  error  Expected indentation of 8 space characters but found 12  indent
  13:9   error  Expected indentation of 4 space characters but found 8   indent
  18:13  error  Expected indentation of 8 space characters but found 12  indent
  19:9   error  Expected indentation of 4 space characters but found 8   indent
  24:13  error  Expected indentation of 8 space characters but found 12  indent
  25:9   error  Expected indentation of 4 space characters but found 8   indent
  38:13  error  Expected indentation of 8 space characters but found 12  indent
  39:9   error  Expected indentation of 4 space characters but found 8   indent
  46:13  error  Expected indentation of 8 space characters but found 12  indent
  47:9   error  Expected indentation of 4 space characters but found 8   indent
  52:13  error  Expected indentation of 8 space characters but found 12  indent
  53:9   error  Expected indentation of 4 space characters but found 8   indent
  60:13  error  Expected indentation of 8 space characters but found 12  indent
  61:9   error  Expected indentation of 4 space characters but found 8   indent
  66:13  error  Expected indentation of 8 space characters but found 12  indent
  67:9   error  Expected indentation of 4 space characters but found 8   indent
  73:13  error  Expected indentation of 8 space characters but found 12  indent
  74:9   error  Expected indentation of 4 space characters but found 8   indent
  79:13  error  Expected indentation of 8 space characters but found 12  indent
  80:9   error  Expected indentation of 4 space characters but found 8   indent

✖ 22 problems (22 errors, 0 warnings)

esformatter v0.9.5

millermedeiros commented 8 years ago

@Faleij thanks a lot for the bug reports! please keep them coming!

it is a conflict with the ReturnStatement indentation, similar to what happened on #443 - will probably need to improve logic to actually check if nodes at left/right of the operator will add indentation or not (for BinaryExpression, UnaryExpression and LogicalExpression)

maybe we can abstract the logic somehow, so we don't need to implement it also for AssignmentExpression, VariableDeclaration and ObjectExpression... - I wish the whole indentation logic was simpler...