From 1e50f33091589fd981d75a07da937f5956a6a6f7 Mon Sep 17 00:00:00 2001 From: Miller Medeiros Date: Thu, 28 Jan 2016 17:50:06 -0800 Subject: [PATCH] avoid indenting body of arrow functions twice. fixes #357 --- lib/hooks/ArrowFunctionExpression.js | 13 ++++++++++--- .../default/arrow_function_expression-in.js | 19 +++++++++++++++++++ .../default/arrow_function_expression-out.js | 19 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/hooks/ArrowFunctionExpression.js b/lib/hooks/ArrowFunctionExpression.js index f89a6ac..88d5d50 100644 --- a/lib/hooks/ArrowFunctionExpression.js +++ b/lib/hooks/ArrowFunctionExpression.js @@ -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)); } @@ -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]; +} diff --git a/test/compare/default/arrow_function_expression-in.js b/test/compare/default/arrow_function_expression-in.js index 49445f1..a94df28 100644 --- a/test/compare/default/arrow_function_expression-in.js +++ b/test/compare/default/arrow_function_expression-in.js @@ -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 +}); diff --git a/test/compare/default/arrow_function_expression-out.js b/test/compare/default/arrow_function_expression-out.js index ab944bc..1b7dd38 100644 --- a/test/compare/default/arrow_function_expression-out.js +++ b/test/compare/default/arrow_function_expression-out.js @@ -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 +});