Skip to content

Commit

Permalink
Do not inline member expressions as part of assignments (#1256)
Browse files Browse the repository at this point in the history
This is a regression from #1036 and #1188.

Fixes #1241
Fixes #1236
  • Loading branch information
vjeux authored and jlongster committed Apr 14, 2017
1 parent 1802545 commit 97c662b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/printer.js
Expand Up @@ -185,6 +185,7 @@ function genericPrintNoParens(path, options, print, args) {
return concat(["(", path.call(print, "expression"), ")"]);
case "AssignmentExpression":
return printAssignment(
n.left,
path.call(print, "left"),
n.operator,
n.right,
Expand Down Expand Up @@ -232,7 +233,22 @@ function genericPrintNoParens(path, options, print, args) {
]);
case "MemberExpression": {
const parent = path.getParentNode();
let firstNonMemberParent;
let i = 0;
do {
firstNonMemberParent = path.getParentNode(i);
i++;
} while (
firstNonMemberParent &&
firstNonMemberParent.type === "MemberExpression"
);

const shouldInline =
firstNonMemberParent && (
(firstNonMemberParent.type === "VariableDeclarator" &&
firstNonMemberParent.id.type === "ObjectPattern") ||
(firstNonMemberParent.type === "AssignmentExpression" &&
firstNonMemberParent.left.type === "ObjectPattern")) ||
n.computed ||
(n.object.type === "Identifier" &&
n.property.type === "Identifier" &&
Expand Down Expand Up @@ -964,6 +980,7 @@ function genericPrintNoParens(path, options, print, args) {
return group(concat(parts));
case "VariableDeclarator":
return printAssignment(
n.id,
path.call(print, "id"),
"=",
n.init,
Expand Down Expand Up @@ -3161,6 +3178,7 @@ function printBinaryishExpressions(path, print, options, isNested) {
}

function printAssignment(
leftNode,
printedLeft,
operator,
rightNode,
Expand All @@ -3176,9 +3194,10 @@ function printAssignment(
printed = indent(concat([hardline, printedRight]));
} else if (
(isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) ||
rightNode.type === "StringLiteral" ||
(rightNode.type === "Literal" && typeof rightNode.value === "string") ||
isMemberExpressionChain(rightNode)
leftNode.type !== "ObjectPattern" && (
rightNode.type === "StringLiteral" ||
(rightNode.type === "Literal" && typeof rightNode.value === "string") ||
isMemberExpressionChain(rightNode))
) {
printed = indent(concat([line, printedRight]));
} else {
Expand Down
36 changes: 36 additions & 0 deletions tests/method-chain/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -129,6 +129,17 @@ superSupersuperSupersuperSupersuperSupersuperSuperLong.exampleOfOrderOfGetterAnd
expect(
findDOMNode(component.instance()).getElementsByClassName(styles.inner)[0].style.paddingRight
).toBe('1000px');
const { course, conflicts = [], index, scheduleId, studentId, something } = a.this.props;
const { course, conflicts = [], index, scheduleId, studentId, something } = this.props;
const {
updated,
author: { identifier: ownerId },
location,
category: categories,
} = rawAd.entry;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SomeVeryLongUpperCaseConstant.someVeryLongCallExpression()
.some_very_long_member_expression;
Expand All @@ -143,6 +154,31 @@ expect(
.style.paddingRight
).toBe("1000px");
const {
course,
conflicts = [],
index,
scheduleId,
studentId,
something
} = a.this.props;
const {
course,
conflicts = [],
index,
scheduleId,
studentId,
something
} = this.props;
const {
updated,
author: { identifier: ownerId },
location,
category: categories
} = rawAd.entry;
`;

exports[`comment.js 1`] = `
Expand Down
11 changes: 11 additions & 0 deletions tests/method-chain/break-last-member.js
Expand Up @@ -7,3 +7,14 @@ superSupersuperSupersuperSupersuperSupersuperSuperLong.exampleOfOrderOfGetterAnd
expect(
findDOMNode(component.instance()).getElementsByClassName(styles.inner)[0].style.paddingRight
).toBe('1000px');

const { course, conflicts = [], index, scheduleId, studentId, something } = a.this.props;

const { course, conflicts = [], index, scheduleId, studentId, something } = this.props;

const {
updated,
author: { identifier: ownerId },
location,
category: categories,
} = rawAd.entry;

0 comments on commit 97c662b

Please sign in to comment.