diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index f973e426..63eae5ed 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -4141,11 +4141,11 @@ function printMemberChain(path, options, print) { // .map(x => x) // // In order to detect those cases, we use an heuristic: if the first - // node is just an identifier with the name starting with a capital - // letter, just a sequence of _$ or this. The rationale is that they are - // likely to be factories. - function isFactory(name) { - return name.match(/(^[A-Z])|^[_$]+$/); + // node is an identifier with the name starting with a capital letter, + // or shorter than tabWidth. The rationale is that they are likely + // to be factories. + function isNoWrap(name) { + return name.match(/(^[A-Z])/) || name.length <= options.tabWidth; } const shouldMerge = groups.length >= 2 && @@ -4153,12 +4153,12 @@ function printMemberChain(path, options, print) { ((groups[0].length === 1 && (groups[0][0].node.type === "ThisExpression" || (groups[0][0].node.type === "Identifier" && - (isFactory(groups[0][0].node.name) || + (isNoWrap(groups[0][0].node.name) || (groups[1].length && groups[1][0].node.computed))))) || (groups[0].length > 1 && groups[0][groups[0].length - 1].node.type === "MemberExpression" && groups[0][groups[0].length - 1].node.property.type === "Identifier" && - (isFactory(groups[0][groups[0].length - 1].node.property.name) || + (isNoWrap(groups[0][groups[0].length - 1].node.property.name) || (groups[1].length && groups[1][0].node.computed)))); function printGroup(printedGroup) { diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 0757ebef..ccf9db93 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -1341,7 +1341,7 @@ function memberOutside() { function memberInAndOutWithCalls() { return ( // Reason for a - a.b() + aFunction.b() ).c.d() } @@ -1469,7 +1469,7 @@ function memberOutside() { function memberInAndOutWithCalls() { return ( - a + aFunction .b// Reason for a () .c.d() diff --git a/tests/comments/return-statement.js b/tests/comments/return-statement.js index 838e079e..4953d6b7 100644 --- a/tests/comments/return-statement.js +++ b/tests/comments/return-statement.js @@ -82,7 +82,7 @@ function memberOutside() { function memberInAndOutWithCalls() { return ( // Reason for a - a.b() + aFunction.b() ).c.d() } diff --git a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap index 29f54015..f9d26340 100644 --- a/tests/method-chain/__snapshots__/jsfmt.spec.js.snap +++ b/tests/method-chain/__snapshots__/jsfmt.spec.js.snap @@ -485,6 +485,28 @@ object[ `; +exports[`d3.js 1`] = ` +d3.select('body') + .append('circle') + .at({ width: 30, fill: '#f0f' }) + .st({ fontWeight: 600 }) + +d3.scaleLinear() + .domain([1950, 1980]) + .range([0, width]) + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +d3.select("body") + .append("circle") + .at({ width: 30, fill: "#f0f" }) + .st({ fontWeight: 600 }); + +d3.scaleLinear() + .domain([1950, 1980]) + .range([0, width]); + +`; + exports[`first_long.js 1`] = ` export default function theFunction(action$, store) { return action$.ofType(THE_ACTION).switchMap(action => Observable diff --git a/tests/method-chain/d3.js b/tests/method-chain/d3.js new file mode 100644 index 00000000..5912bb74 --- /dev/null +++ b/tests/method-chain/d3.js @@ -0,0 +1,9 @@ +d3.select('body') + .append('circle') + .at({ width: 30, fill: '#f0f' }) + .st({ fontWeight: 600 }) + +d3.scaleLinear() + .domain([1950, 1980]) + .range([0, width]) +