Nicer line wrapping for d3 (#4285)

* d3 counts as a constructor

* adds test

* Tweak wording

* docs: fix linting

* updates test

* no $ special case

* renames function

* shorter function name

* removes trailing white space
master
Adam Pearce 2018-04-24 06:54:45 -04:00 committed by Jed Fox
parent 80196b6553
commit 09ac476a32
5 changed files with 41 additions and 10 deletions

View File

@ -4141,11 +4141,11 @@ function printMemberChain(path, options, print) {
// .map(x => x) // .map(x => x)
// //
// In order to detect those cases, we use an heuristic: if the first // 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 // node is an identifier with the name starting with a capital letter,
// letter, just a sequence of _$ or this. The rationale is that they are // or shorter than tabWidth. The rationale is that they are likely
// likely to be factories. // to be factories.
function isFactory(name) { function isNoWrap(name) {
return name.match(/(^[A-Z])|^[_$]+$/); return name.match(/(^[A-Z])/) || name.length <= options.tabWidth;
} }
const shouldMerge = const shouldMerge =
groups.length >= 2 && groups.length >= 2 &&
@ -4153,12 +4153,12 @@ function printMemberChain(path, options, print) {
((groups[0].length === 1 && ((groups[0].length === 1 &&
(groups[0][0].node.type === "ThisExpression" || (groups[0][0].node.type === "ThisExpression" ||
(groups[0][0].node.type === "Identifier" && (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[1].length && groups[1][0].node.computed))))) ||
(groups[0].length > 1 && (groups[0].length > 1 &&
groups[0][groups[0].length - 1].node.type === "MemberExpression" && groups[0][groups[0].length - 1].node.type === "MemberExpression" &&
groups[0][groups[0].length - 1].node.property.type === "Identifier" && 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)))); (groups[1].length && groups[1][0].node.computed))));
function printGroup(printedGroup) { function printGroup(printedGroup) {

View File

@ -1341,7 +1341,7 @@ function memberOutside() {
function memberInAndOutWithCalls() { function memberInAndOutWithCalls() {
return ( return (
// Reason for a // Reason for a
a.b() aFunction.b()
).c.d() ).c.d()
} }
@ -1469,7 +1469,7 @@ function memberOutside() {
function memberInAndOutWithCalls() { function memberInAndOutWithCalls() {
return ( return (
a aFunction
.b// Reason for a .b// Reason for a
() ()
.c.d() .c.d()

View File

@ -82,7 +82,7 @@ function memberOutside() {
function memberInAndOutWithCalls() { function memberInAndOutWithCalls() {
return ( return (
// Reason for a // Reason for a
a.b() aFunction.b()
).c.d() ).c.d()
} }

View File

@ -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`] = ` exports[`first_long.js 1`] = `
export default function theFunction(action$, store) { export default function theFunction(action$, store) {
return action$.ofType(THE_ACTION).switchMap(action => Observable return action$.ofType(THE_ACTION).switchMap(action => Observable

9
tests/method-chain/d3.js vendored Normal file
View File

@ -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])