prettier/src/doc/doc-utils.js

195 lines
4.4 KiB
JavaScript
Raw Normal View History

"use strict";
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
function traverseDocRec(doc) {
let shouldRecurse = true;
if (onEnter) {
if (onEnter(doc) === false) {
shouldRecurse = false;
}
}
if (shouldRecurse) {
if (doc.type === "concat" || doc.type === "fill") {
for (let i = 0; i < doc.parts.length; i++) {
traverseDocRec(doc.parts[i]);
}
} else if (doc.type === "if-break") {
if (doc.breakContents) {
traverseDocRec(doc.breakContents);
}
if (doc.flatContents) {
traverseDocRec(doc.flatContents);
}
} else if (doc.type === "group" && doc.expandedStates) {
if (shouldTraverseConditionalGroups) {
doc.expandedStates.forEach(traverseDocRec);
} else {
traverseDocRec(doc.contents);
}
} else if (doc.contents) {
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
traverseDocRec(doc.contents);
}
}
if (onExit) {
onExit(doc);
}
}
traverseDocRec(doc);
}
2017-02-23 20:00:29 +03:00
function mapDoc(doc, func) {
doc = func(doc);
if (doc.type === "concat" || doc.type === "fill") {
2017-02-23 20:57:51 +03:00
return Object.assign({}, doc, {
parts: doc.parts.map(d => mapDoc(d, func))
});
2017-02-23 20:00:29 +03:00
} else if (doc.type === "if-break") {
return Object.assign({}, doc, {
breakContents: doc.breakContents && mapDoc(doc.breakContents, func),
flatContents: doc.flatContents && mapDoc(doc.flatContents, func)
});
2017-02-23 20:57:51 +03:00
} else if (doc.contents) {
2017-02-23 20:00:29 +03:00
return Object.assign({}, doc, { contents: mapDoc(doc.contents, func) });
}
return doc;
2017-02-23 20:00:29 +03:00
}
function findInDoc(doc, fn, defaultValue) {
let result = defaultValue;
let hasStopped = false;
traverseDoc(doc, doc => {
const maybeResult = fn(doc);
if (maybeResult !== undefined) {
hasStopped = true;
result = maybeResult;
}
if (hasStopped) {
return false;
}
});
return result;
}
function isEmpty(n) {
return typeof n === "string" && n.length === 0;
}
function isLineNext(doc) {
2017-01-28 18:50:22 +03:00
return findInDoc(
doc,
doc => {
if (typeof doc === "string") {
return false;
}
if (doc.type === "line") {
return true;
}
},
false
);
}
function willBreak(doc) {
2017-01-28 18:50:22 +03:00
return findInDoc(
doc,
doc => {
if (doc.type === "group" && doc.break) {
return true;
}
if (doc.type === "line" && doc.hard) {
return true;
}
if (doc.type === "break-parent") {
return true;
}
2017-01-28 18:50:22 +03:00
},
false
);
}
function breakParentGroup(groupStack) {
2017-01-28 18:50:22 +03:00
if (groupStack.length > 0) {
const parentGroup = groupStack[groupStack.length - 1];
// Breaks are not propagated through conditional groups because
// the user is expected to manually handle what breaks.
2017-01-28 18:50:22 +03:00
if (!parentGroup.expandedStates) {
parentGroup.break = true;
}
}
return null;
}
function propagateBreaks(doc) {
const alreadyVisited = new Map();
const groupStack = [];
traverseDoc(
doc,
doc => {
if (doc.type === "break-parent") {
breakParentGroup(groupStack);
}
if (doc.type === "group") {
groupStack.push(doc);
if (alreadyVisited.has(doc)) {
return false;
}
alreadyVisited.set(doc, true);
}
},
doc => {
if (doc.type === "group") {
const group = groupStack.pop();
if (group.break) {
breakParentGroup(groupStack);
}
}
Fix edge cases triggered by newlines in arrow functions (#1217) This one is pretty crazy. In #927, I changed ```js concat(["(", join(concat([", "]), printed), ")"]), ``` into ```js concat(["(", join(concat([", "]), printedLastArgExpanded), ")"]), ``` which makes the example in #1203 look ugly. The crazy thing is that `JSON.stringify(printed) === JSON.stringify(printedLastArgExpanded)`. So they are deep equal but not the same object. In a non-mutative world, this should cause any problem, but we actually mutate those to propagate breaks. In the break propagation, we only looked at the first one in case of a conditional group. But, because the two were the same object then it also applied to the second one and happened to be the correct behavior! When I changed that piece of code to be two distinct objects, it no longer worked by accident and caused a bunch of weird issues where breaks didn't propagate correctly. The solution for this case is to propagate the breaks on all the conditions. I'm pretty sure that this is the expected behavior, we want to deeply recurse in all of them, we don't propagate it up the boundary anyway. The other use case for `traverseInDoc()` is `findInDoc()`, right now it searches for the first conditional group but it seems very arbitrary. I changed it to not search on any and none of the tests are failing, so I think it's safe(tm). If it triggers weird behavior, then it'll at least be expected and not randomly explode at us if we move groups around. I tried to go through all the conditions for `findInDoc()` but it triggers a few failures (the output look bad). I'm not really sure why. https://gist.github.com/vjeux/5fb7566cc3d65974817d512d1ef6abe1 Fix #1203
2017-04-13 19:21:18 +03:00
},
/* shouldTraverseConditionalGroups */ true
);
}
function removeLines(doc) {
// Force this doc into flat mode by statically converting all
// lines into spaces (or soft lines into nothing). Hard lines
// should still output because there's too great of a chance
// of breaking existing assumptions otherwise.
return mapDoc(doc, d => {
if (d.type === "line" && !d.hard) {
return d.soft ? "" : " ";
} else if (d.type === "if-break") {
return d.flatContents || "";
}
return d;
});
}
function stripTrailingHardline(doc) {
// HACK remove ending hardline, original PR: #1984
if (
doc.type === "concat" &&
doc.parts.length === 2 &&
doc.parts[1].type === "concat" &&
doc.parts[1].parts.length === 2 &&
doc.parts[1].parts[0].hard &&
doc.parts[1].parts[1].type === "break-parent"
) {
return doc.parts[0];
}
return doc;
}
module.exports = {
isEmpty,
willBreak,
isLineNext,
traverseDoc,
2017-02-23 20:00:29 +03:00
mapDoc,
propagateBreaks,
removeLines,
stripTrailingHardline
};