Skip to content

Commit

Permalink
fix(vue): preserve custom block (#5458)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 23, 2018
1 parent 6ee2f46 commit b2eadd2
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 271 deletions.
89 changes: 49 additions & 40 deletions src/language-html/printer-html.js
Expand Up @@ -67,9 +67,9 @@ function embed(path, print, textToDoc, options) {
return builders.concat([
concat([
breakParent,
printOpeningTagPrefix(node),
printOpeningTagPrefix(node, options),
markAsRoot(stripTrailingHardline(textToDoc(value, { parser }))),
printClosingTagSuffix(node)
printClosingTagSuffix(node, options)
])
]);
}
Expand Down Expand Up @@ -245,17 +245,17 @@ function genericPrint(path, options, print) {
])
])
),
printClosingTag(node)
printClosingTag(node, options)
]);
}
case "ieConditionalStartComment":
case "ieConditionalEndComment":
return concat([printOpeningTagStart(node), printClosingTagEnd(node)]);
case "interpolation":
return concat([
printOpeningTagStart(node),
printOpeningTagStart(node, options),
concat(path.map(print, "children")),
printClosingTagEnd(node)
printClosingTagEnd(node, options)
]);
case "text": {
if (node.parent.type === "interpolation") {
Expand All @@ -273,9 +273,9 @@ function genericPrint(path, options, print) {
return fill(
normalizeParts(
[].concat(
printOpeningTagPrefix(node),
printOpeningTagPrefix(node, options),
getTextValueParts(node),
printClosingTagSuffix(node)
printClosingTagSuffix(node, options)
)
)
);
Expand All @@ -284,19 +284,19 @@ function genericPrint(path, options, print) {
return concat([
group(
concat([
printOpeningTagStart(node),
printOpeningTagStart(node, options),
" ",
node.value.replace(/^html\b/i, "html").replace(/\s+/g, " ")
])
),
printClosingTagEnd(node)
printClosingTagEnd(node, options)
]);
case "comment": {
const value = getCommentData(node);
return concat([
group(
concat([
printOpeningTagStart(node),
printOpeningTagStart(node, options),
value.trim().length === 0
? ""
: concat([
Expand All @@ -318,7 +318,7 @@ function genericPrint(path, options, print) {
])
])
),
printClosingTagEnd(node)
printClosingTagEnd(node, options)
]);
}
case "attribute":
Expand Down Expand Up @@ -445,7 +445,7 @@ function printChildren(path, options, print) {
if (hasPrettierIgnore(child)) {
return concat(
[].concat(
printOpeningTagPrefix(child),
printOpeningTagPrefix(child, options),
replaceNewlines(
options.originalText.slice(
options.locStart(child) +
Expand All @@ -455,20 +455,20 @@ function printChildren(path, options, print) {
: 0),
options.locEnd(child) -
(child.next && needsToBorrowPrevClosingTagEndMarker(child.next)
? printClosingTagEndMarker(child).length
? printClosingTagEndMarker(child, options).length
: 0)
),
literalline
),
printClosingTagSuffix(child)
printClosingTagSuffix(child, options)
)
);
}

if (shouldPreserveContent(child)) {
if (shouldPreserveContent(child, options)) {
return concat(
[].concat(
printOpeningTagPrefix(child),
printOpeningTagPrefix(child, options),
group(printOpeningTag(childPath, options, print)),
replaceNewlines(
options.originalText.slice(
Expand All @@ -480,15 +480,15 @@ function printChildren(path, options, print) {
child.endSourceSpan.start.offset +
(child.lastChild &&
needsToBorrowParentClosingTagStartMarker(child.lastChild)
? printClosingTagStartMarker(child).length
? printClosingTagStartMarker(child, options).length
: needsToBorrowLastChildClosingTagEndMarker(child)
? -printClosingTagEndMarker(child.lastChild).length
? -printClosingTagEndMarker(child.lastChild, options).length
: 0)
),
literalline
),
printClosingTag(child),
printClosingTagSuffix(child)
printClosingTag(child, options),
printClosingTagSuffix(child, options)
)
);
}
Expand Down Expand Up @@ -557,7 +557,7 @@ function printOpeningTag(path, options, print) {
node.attrs[0].fullName === "src" &&
node.children.length === 0;
return concat([
printOpeningTagStart(node),
printOpeningTagStart(node, options),
!node.attrs || node.attrs.length === 0
? node.isSelfClosing
? /**
Expand Down Expand Up @@ -630,10 +630,13 @@ function printOpeningTag(path, options, print) {
]);
}

function printOpeningTagStart(node) {
function printOpeningTagStart(node, options) {
return node.prev && needsToBorrowNextOpeningTagStartMarker(node.prev)
? ""
: concat([printOpeningTagPrefix(node), printOpeningTagStartMarker(node)]);
: concat([
printOpeningTagPrefix(node, options),
printOpeningTagStartMarker(node)
]);
}

function printOpeningTagEnd(node) {
Expand All @@ -643,26 +646,32 @@ function printOpeningTagEnd(node) {
: printOpeningTagEndMarker(node);
}

function printClosingTag(node) {
function printClosingTag(node, options) {
return concat([
node.isSelfClosing ? "" : printClosingTagStart(node),
printClosingTagEnd(node)
node.isSelfClosing ? "" : printClosingTagStart(node, options),
printClosingTagEnd(node, options)
]);
}

function printClosingTagStart(node) {
function printClosingTagStart(node, options) {
return node.lastChild &&
needsToBorrowParentClosingTagStartMarker(node.lastChild)
? ""
: concat([printClosingTagPrefix(node), printClosingTagStartMarker(node)]);
: concat([
printClosingTagPrefix(node, options),
printClosingTagStartMarker(node, options)
]);
}

function printClosingTagEnd(node) {
function printClosingTagEnd(node, options) {
return (node.next
? needsToBorrowPrevClosingTagEndMarker(node.next)
: needsToBorrowLastChildClosingTagEndMarker(node.parent))
? ""
: concat([printClosingTagEndMarker(node), printClosingTagSuffix(node)]);
: concat([
printClosingTagEndMarker(node, options),
printClosingTagSuffix(node, options)
]);
}

function needsToBorrowNextOpeningTagStartMarker(node) {
Expand Down Expand Up @@ -741,23 +750,23 @@ function needsToBorrowParentClosingTagStartMarker(node) {
);
}

function printOpeningTagPrefix(node) {
function printOpeningTagPrefix(node, options) {
return needsToBorrowParentOpeningTagEndMarker(node)
? printOpeningTagEndMarker(node.parent)
: needsToBorrowPrevClosingTagEndMarker(node)
? printClosingTagEndMarker(node.prev)
? printClosingTagEndMarker(node.prev, options)
: "";
}

function printClosingTagPrefix(node) {
function printClosingTagPrefix(node, options) {
return needsToBorrowLastChildClosingTagEndMarker(node)
? printClosingTagEndMarker(node.lastChild)
? printClosingTagEndMarker(node.lastChild, options)
: "";
}

function printClosingTagSuffix(node) {
function printClosingTagSuffix(node, options) {
return needsToBorrowParentClosingTagStartMarker(node)
? printClosingTagStartMarker(node.parent)
? printClosingTagStartMarker(node.parent, options)
: needsToBorrowNextOpeningTagStartMarker(node)
? printOpeningTagStartMarker(node.next)
: "";
Expand Down Expand Up @@ -801,9 +810,9 @@ function printOpeningTagEndMarker(node) {
}
}

function printClosingTagStartMarker(node) {
function printClosingTagStartMarker(node, options) {
assert(!node.isSelfClosing);
if (shouldNotPrintClosingTag(node)) {
if (shouldNotPrintClosingTag(node, options)) {
return "";
}
switch (node.type) {
Expand All @@ -814,8 +823,8 @@ function printClosingTagStartMarker(node) {
}
}

function printClosingTagEndMarker(node) {
if (shouldNotPrintClosingTag(node)) {
function printClosingTagEndMarker(node, options) {
if (shouldNotPrintClosingTag(node, options)) {
return "";
}
switch (node.type) {
Expand Down
23 changes: 20 additions & 3 deletions src/language-html/utils.js
Expand Up @@ -34,7 +34,7 @@ function mapObject(object, fn) {
return newObject;
}

function shouldPreserveContent(node) {
function shouldPreserveContent(node, options) {
if (
node.type === "element" &&
node.fullName === "template" &&
Expand All @@ -61,6 +61,23 @@ function shouldPreserveContent(node) {
return true;
}

// top-level elements (excluding <template>, <style> and <script>) in Vue SFC are considered custom block
// custom blocks can be written in other languages so we should preserve them to not break the code
if (
options.parser === "vue" &&
node.type === "element" &&
node.parent.type === "root" &&
[
"template",
"style",
"script",
// vue parser can be used for vue dom template as well, so we should still format top-level <html>
"html"
].indexOf(node.fullName) === -1
) {
return true;
}

// TODO: handle non-text children in <pre>
if (
isPreLikeNode(node) &&
Expand Down Expand Up @@ -636,11 +653,11 @@ function identity(x) {
return x;
}

function shouldNotPrintClosingTag(node) {
function shouldNotPrintClosingTag(node, options) {
return (
!node.isSelfClosing &&
!node.endSourceSpan &&
(hasPrettierIgnore(node) || shouldPreserveContent(node.parent))
(hasPrettierIgnore(node) || shouldPreserveContent(node.parent, options))
);
}

Expand Down

0 comments on commit b2eadd2

Please sign in to comment.