Skip to content

Commit

Permalink
avoid indenting body of arrow functions twice. fixes #357
Browse files Browse the repository at this point in the history
  • Loading branch information
millermedeiros committed Jan 29, 2016
1 parent 423f2da commit 1e50f33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/hooks/ArrowFunctionExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ exports.format = function ArrowFunctionExpression(node) {
};

exports.getIndentEdges = function(node, opts) {
var edges = [
node.body
];
var edges = [];
if (shouldIndentBody(node, opts)) {
edges.push(node.body);
}
if (shouldHandleParams(node)) {
edges.push(_params.getIndentEdges(node, opts));
}
Expand All @@ -35,3 +36,9 @@ function shouldHandleParams(node) {
// we don't check based on `node.params` because of `node.defaults`
return tk.findPrevNonEmpty(arrow).value === ')';
}

function shouldIndentBody(node, opts) {
// we don't want to indent the body twice if ObjectExpression or
// ArrayExpression or CallExpression
return node.body.type === 'BlockStatement' || !opts[node.body.type];
}
19 changes: 19 additions & 0 deletions test/compare/default/arrow_function_expression-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ a = () => {
return 1
})
}

// issue #357
const object = x => ({
x
});

const retObject = x => {
return {
x
};
}

const array = x => [
x
];

const callWithObject = x => f({
x
});
19 changes: 19 additions & 0 deletions test/compare/default/arrow_function_expression-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ a = () => {
return 1
})
}

// issue #357
const object = x => ({
x
});

const retObject = x => {
return {
x
};
}

const array = x => [
x
];

const callWithObject = x => f({
x
});

0 comments on commit 1e50f33

Please sign in to comment.