2018-09-19 23:07:27 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
2021-03-29 08:00:50 -04:00
|
|
|
/**
|
|
|
|
|
* @typedef {import("../common/ast-path")} AstPath
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-26 20:30:21 +08:00
|
|
|
const htmlTagNames = require("html-tag-names");
|
|
|
|
|
const htmlElementAttributes = require("html-element-attributes");
|
2021-07-21 09:10:34 +08:00
|
|
|
const {
|
|
|
|
|
inferParserByLanguage,
|
|
|
|
|
isFrontMatterNode,
|
|
|
|
|
} = require("../common/util.js");
|
2018-10-13 13:55:38 +08:00
|
|
|
const {
|
|
|
|
|
CSS_DISPLAY_TAGS,
|
|
|
|
|
CSS_DISPLAY_DEFAULT,
|
|
|
|
|
CSS_WHITE_SPACE_TAGS,
|
|
|
|
|
CSS_WHITE_SPACE_DEFAULT,
|
2021-07-21 09:10:34 +08:00
|
|
|
} = require("./constants.evaluate.js");
|
2018-09-19 23:07:27 +08:00
|
|
|
|
2018-09-22 21:53:38 +08:00
|
|
|
const HTML_TAGS = arrayToMap(htmlTagNames);
|
2018-09-27 20:18:00 +08:00
|
|
|
const HTML_ELEMENT_ATTRIBUTES = mapObject(htmlElementAttributes, arrayToMap);
|
2018-09-22 21:53:38 +08:00
|
|
|
|
2020-04-25 21:56:28 +08:00
|
|
|
// https://infra.spec.whatwg.org/#ascii-whitespace
|
|
|
|
|
const HTML_WHITESPACE = new Set(["\t", "\n", "\f", "\r", " "]);
|
|
|
|
|
const htmlTrimStart = (string) => string.replace(/^[\t\n\f\r ]+/, "");
|
|
|
|
|
const htmlTrimEnd = (string) => string.replace(/[\t\n\f\r ]+$/, "");
|
|
|
|
|
const htmlTrim = (string) => htmlTrimStart(htmlTrimEnd(string));
|
|
|
|
|
const htmlTrimLeadingBlankLines = (string) =>
|
|
|
|
|
string.replace(/^[\t\f\r ]*?\n/g, "");
|
|
|
|
|
const htmlTrimPreserveIndentation = (string) =>
|
|
|
|
|
htmlTrimLeadingBlankLines(htmlTrimEnd(string));
|
|
|
|
|
const splitByHtmlWhitespace = (string) => string.split(/[\t\n\f\r ]+/);
|
|
|
|
|
const getLeadingHtmlWhitespace = (string) => string.match(/^[\t\n\f\r ]*/)[0];
|
|
|
|
|
const getLeadingAndTrailingHtmlWhitespace = (string) => {
|
|
|
|
|
const [, leadingWhitespace, text, trailingWhitespace] = string.match(
|
2021-05-18 02:44:21 +08:00
|
|
|
/^([\t\n\f\r ]*)(.*?)([\t\n\f\r ]*)$/s
|
2020-04-25 21:56:28 +08:00
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
leadingWhitespace,
|
|
|
|
|
trailingWhitespace,
|
|
|
|
|
text,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
const hasHtmlWhitespace = (string) => /[\t\n\f\r ]/.test(string);
|
|
|
|
|
|
2018-09-22 21:53:38 +08:00
|
|
|
function arrayToMap(array) {
|
|
|
|
|
const map = Object.create(null);
|
|
|
|
|
for (const value of array) {
|
|
|
|
|
map[value] = true;
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2018-09-19 23:07:27 +08:00
|
|
|
|
2018-09-27 20:18:00 +08:00
|
|
|
function mapObject(object, fn) {
|
|
|
|
|
const newObject = Object.create(null);
|
2021-01-26 09:48:31 +08:00
|
|
|
for (const [key, value] of Object.entries(object)) {
|
|
|
|
|
newObject[key] = fn(value, key);
|
2018-09-27 20:18:00 +08:00
|
|
|
}
|
|
|
|
|
return newObject;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 12:33:48 +08:00
|
|
|
function shouldPreserveContent(node, options) {
|
2018-11-09 23:26:59 +08:00
|
|
|
// unterminated node in ie conditional comment
|
|
|
|
|
// e.g. <!--[if lt IE 9]><html><![endif]-->
|
|
|
|
|
if (
|
|
|
|
|
node.type === "ieConditionalComment" &&
|
|
|
|
|
node.lastChild &&
|
|
|
|
|
!node.lastChild.isSelfClosing &&
|
|
|
|
|
!node.lastChild.endSourceSpan
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 16:44:44 +08:00
|
|
|
// incomplete html in ie conditional comment
|
|
|
|
|
// e.g. <!--[if lt IE 9]></div><![endif]-->
|
|
|
|
|
if (node.type === "ieConditionalComment" && !node.complete) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
// TODO: handle non-text children in <pre>
|
|
|
|
|
if (
|
|
|
|
|
isPreLikeNode(node) &&
|
2018-11-09 09:50:57 +08:00
|
|
|
node.children.some(
|
|
|
|
|
(child) => child.type !== "text" && child.type !== "interpolation"
|
|
|
|
|
)
|
2018-11-04 23:03:07 +08:00
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
if (
|
2020-06-17 19:18:29 +08:00
|
|
|
isVueNonHtmlBlock(node, options) &&
|
|
|
|
|
!isScriptLikeTag(node) &&
|
|
|
|
|
node.type !== "interpolation"
|
2020-04-17 07:54:24 +09:00
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 19:19:03 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 23:26:59 +08:00
|
|
|
function hasPrettierIgnore(node) {
|
2020-08-21 09:43:24 +08:00
|
|
|
/* istanbul ignore next */
|
2020-02-25 03:11:08 -08:00
|
|
|
if (node.type === "attribute") {
|
2018-11-09 19:19:03 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 09:43:24 +08:00
|
|
|
/* istanbul ignore next */
|
2018-11-09 23:26:59 +08:00
|
|
|
if (!node.parent) {
|
2018-09-19 23:07:27 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 23:26:59 +08:00
|
|
|
if (typeof node.index !== "number" || node.index === 0) {
|
2018-09-19 23:07:27 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 23:26:59 +08:00
|
|
|
const prevNode = node.parent.children[node.index - 1];
|
2018-10-13 13:55:38 +08:00
|
|
|
return isPrettierIgnore(prevNode);
|
2018-09-19 23:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPrettierIgnore(node) {
|
2018-11-04 23:03:07 +08:00
|
|
|
return node.type === "comment" && node.value.trim() === "prettier-ignore";
|
2018-09-19 23:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
function getPrettierIgnoreAttributeCommentData(value) {
|
2021-05-18 02:44:21 +08:00
|
|
|
const match = value.trim().match(/^prettier-ignore-attribute(?:\s+(.+))?$/s);
|
2018-11-04 23:03:07 +08:00
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!match[1]) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match[1].split(/\s+/);
|
2018-09-19 23:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-23 21:25:58 +08:00
|
|
|
/** there's no opening/closing tag or it's considered not breakable */
|
|
|
|
|
function isTextLikeNode(node) {
|
|
|
|
|
return node.type === "text" || node.type === "comment";
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 13:55:38 +08:00
|
|
|
function isScriptLikeTag(node) {
|
2018-11-04 23:03:07 +08:00
|
|
|
return (
|
|
|
|
|
node.type === "element" &&
|
|
|
|
|
(node.fullName === "script" ||
|
|
|
|
|
node.fullName === "style" ||
|
2020-04-02 01:35:58 +08:00
|
|
|
node.fullName === "svg:style" ||
|
|
|
|
|
(isUnknownNamespace(node) &&
|
|
|
|
|
(node.name === "script" || node.name === "style")))
|
2018-11-04 23:03:07 +08:00
|
|
|
);
|
2018-09-19 23:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
function canHaveInterpolation(node) {
|
|
|
|
|
return node.children && !isScriptLikeTag(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isWhitespaceSensitiveNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
isScriptLikeTag(node) ||
|
|
|
|
|
node.type === "interpolation" ||
|
|
|
|
|
isIndentationSensitiveNode(node)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIndentationSensitiveNode(node) {
|
|
|
|
|
return getNodeCssStyleWhiteSpace(node).startsWith("pre");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
function isLeadingSpaceSensitiveNode(node, options) {
|
2018-11-15 10:34:42 +08:00
|
|
|
const isLeadingSpaceSensitive = _isLeadingSpaceSensitiveNode();
|
2018-10-13 13:55:38 +08:00
|
|
|
|
|
|
|
|
if (
|
2018-11-15 10:34:42 +08:00
|
|
|
isLeadingSpaceSensitive &&
|
2018-11-04 23:03:07 +08:00
|
|
|
!node.prev &&
|
2018-11-15 10:34:42 +08:00
|
|
|
node.parent &&
|
|
|
|
|
node.parent.tagDefinition &&
|
2018-11-04 23:03:07 +08:00
|
|
|
node.parent.tagDefinition.ignoreFirstLf
|
2018-10-13 13:55:38 +08:00
|
|
|
) {
|
2018-11-15 10:34:42 +08:00
|
|
|
return node.type === "interpolation";
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 10:34:42 +08:00
|
|
|
return isLeadingSpaceSensitive;
|
|
|
|
|
|
|
|
|
|
function _isLeadingSpaceSensitiveNode() {
|
|
|
|
|
if (isFrontMatterNode(node)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(node.type === "text" || node.type === "interpolation") &&
|
|
|
|
|
node.prev &&
|
|
|
|
|
(node.prev.type === "text" || node.prev.type === "interpolation")
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!node.parent || node.parent.cssDisplay === "none") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isPreLikeNode(node.parent)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!node.prev &&
|
|
|
|
|
(node.parent.type === "root" ||
|
2020-01-06 11:00:09 -08:00
|
|
|
(isPreLikeNode(node) && node.parent) ||
|
2018-11-15 10:34:42 +08:00
|
|
|
isScriptLikeTag(node.parent) ||
|
2020-04-17 07:54:24 +09:00
|
|
|
isVueCustomBlock(node.parent, options) ||
|
2018-11-15 10:34:42 +08:00
|
|
|
!isFirstChildLeadingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
node.prev &&
|
|
|
|
|
!isNextLeadingSpaceSensitiveCssDisplay(node.prev.cssDisplay)
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
function isTrailingSpaceSensitiveNode(node, options) {
|
2018-10-13 13:55:38 +08:00
|
|
|
if (isFrontMatterNode(node)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 01:00:26 +08:00
|
|
|
if (
|
|
|
|
|
(node.type === "text" || node.type === "interpolation") &&
|
|
|
|
|
node.next &&
|
|
|
|
|
(node.next.type === "text" || node.next.type === "interpolation")
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
if (!node.parent || node.parent.cssDisplay === "none") {
|
2018-10-13 13:55:38 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
if (isPreLikeNode(node.parent)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 13:55:38 +08:00
|
|
|
if (
|
2018-11-04 23:03:07 +08:00
|
|
|
!node.next &&
|
|
|
|
|
(node.parent.type === "root" ||
|
2020-01-06 11:00:09 -08:00
|
|
|
(isPreLikeNode(node) && node.parent) ||
|
2018-11-04 23:03:07 +08:00
|
|
|
isScriptLikeTag(node.parent) ||
|
2020-04-17 07:54:24 +09:00
|
|
|
isVueCustomBlock(node.parent, options) ||
|
2018-11-04 23:03:07 +08:00
|
|
|
!isLastChildTrailingSpaceSensitiveCssDisplay(node.parent.cssDisplay))
|
2018-10-13 13:55:38 +08:00
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
if (
|
|
|
|
|
node.next &&
|
|
|
|
|
!isPrevTrailingSpaceSensitiveCssDisplay(node.next.cssDisplay)
|
|
|
|
|
) {
|
2018-10-13 13:55:38 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isDanglingSpaceSensitiveNode(node) {
|
2018-11-04 23:03:07 +08:00
|
|
|
return (
|
|
|
|
|
isDanglingSpaceSensitiveCssDisplay(node.cssDisplay) &&
|
|
|
|
|
!isScriptLikeTag(node)
|
|
|
|
|
);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function forceNextEmptyLine(node) {
|
2018-11-04 23:03:07 +08:00
|
|
|
return (
|
|
|
|
|
isFrontMatterNode(node) ||
|
|
|
|
|
(node.next &&
|
2020-07-14 22:02:32 +08:00
|
|
|
node.sourceSpan.end &&
|
2018-11-04 23:03:07 +08:00
|
|
|
node.sourceSpan.end.line + 1 < node.next.sourceSpan.start.line)
|
|
|
|
|
);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** firstChild leadingSpaces and lastChild trailingSpaces */
|
|
|
|
|
function forceBreakContent(node) {
|
|
|
|
|
return (
|
|
|
|
|
forceBreakChildren(node) ||
|
2018-11-04 23:03:07 +08:00
|
|
|
(node.type === "element" &&
|
2021-01-04 10:11:57 +08:00
|
|
|
node.children.length > 0 &&
|
2019-11-17 18:06:14 +08:00
|
|
|
(["body", "script", "style"].includes(node.name) ||
|
2018-12-11 12:43:53 +08:00
|
|
|
node.children.some((child) => hasNonTextChild(child)))) ||
|
|
|
|
|
(node.firstChild &&
|
|
|
|
|
node.firstChild === node.lastChild &&
|
2020-07-06 14:18:22 +08:00
|
|
|
node.firstChild.type !== "text" &&
|
2019-11-09 01:14:31 +01:00
|
|
|
hasLeadingLineBreak(node.firstChild) &&
|
|
|
|
|
(!node.lastChild.isTrailingSpaceSensitive ||
|
|
|
|
|
hasTrailingLineBreak(node.lastChild)))
|
2018-10-13 13:55:38 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** spaces between children */
|
|
|
|
|
function forceBreakChildren(node) {
|
|
|
|
|
return (
|
2018-11-04 23:03:07 +08:00
|
|
|
node.type === "element" &&
|
2021-01-04 10:11:57 +08:00
|
|
|
node.children.length > 0 &&
|
2019-11-17 18:06:14 +08:00
|
|
|
(["html", "head", "ul", "ol", "select"].includes(node.name) ||
|
2018-10-13 13:55:38 +08:00
|
|
|
(node.cssDisplay.startsWith("table") && node.cssDisplay !== "table-cell"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preferHardlineAsLeadingSpaces(node) {
|
|
|
|
|
return (
|
|
|
|
|
preferHardlineAsSurroundingSpaces(node) ||
|
2018-11-04 23:03:07 +08:00
|
|
|
(node.prev && preferHardlineAsTrailingSpaces(node.prev)) ||
|
2018-12-11 12:43:53 +08:00
|
|
|
hasSurroundingLineBreak(node)
|
2018-10-13 13:55:38 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preferHardlineAsTrailingSpaces(node) {
|
|
|
|
|
return (
|
|
|
|
|
preferHardlineAsSurroundingSpaces(node) ||
|
2018-11-04 23:03:07 +08:00
|
|
|
(node.type === "element" && node.fullName === "br") ||
|
2018-12-11 12:43:53 +08:00
|
|
|
hasSurroundingLineBreak(node)
|
2018-11-09 09:51:31 +08:00
|
|
|
);
|
2018-11-04 23:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasSurroundingLineBreak(node) {
|
|
|
|
|
return hasLeadingLineBreak(node) && hasTrailingLineBreak(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasLeadingLineBreak(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.hasLeadingSpaces &&
|
|
|
|
|
(node.prev
|
|
|
|
|
? node.prev.sourceSpan.end.line < node.sourceSpan.start.line
|
|
|
|
|
: node.parent.type === "root" ||
|
|
|
|
|
node.parent.startSourceSpan.end.line < node.sourceSpan.start.line)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasTrailingLineBreak(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.hasTrailingSpaces &&
|
|
|
|
|
(node.next
|
|
|
|
|
? node.next.sourceSpan.start.line > node.sourceSpan.end.line
|
|
|
|
|
: node.parent.type === "root" ||
|
2020-01-13 22:31:03 +08:00
|
|
|
(node.parent.endSourceSpan &&
|
|
|
|
|
node.parent.endSourceSpan.start.line > node.sourceSpan.end.line))
|
2018-10-13 13:55:38 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preferHardlineAsSurroundingSpaces(node) {
|
|
|
|
|
switch (node.type) {
|
|
|
|
|
case "ieConditionalComment":
|
|
|
|
|
case "comment":
|
|
|
|
|
case "directive":
|
|
|
|
|
return true;
|
2018-11-04 23:03:07 +08:00
|
|
|
case "element":
|
2019-11-17 18:06:14 +08:00
|
|
|
return ["script", "select"].includes(node.name);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLastDescendant(node) {
|
|
|
|
|
return node.lastChild ? getLastDescendant(node.lastChild) : node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasNonTextChild(node) {
|
|
|
|
|
return node.children && node.children.some((child) => child.type !== "text");
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
function _inferScriptParser(node) {
|
|
|
|
|
const { type, lang } = node.attrMap;
|
|
|
|
|
if (
|
|
|
|
|
type === "module" ||
|
|
|
|
|
type === "text/javascript" ||
|
|
|
|
|
type === "text/babel" ||
|
|
|
|
|
type === "application/javascript" ||
|
|
|
|
|
lang === "jsx"
|
|
|
|
|
) {
|
|
|
|
|
return "babel";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "application/x-typescript" || lang === "ts" || lang === "tsx") {
|
|
|
|
|
return "typescript";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "text/markdown") {
|
|
|
|
|
return "markdown";
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 20:29:10 +09:00
|
|
|
if (type === "text/html") {
|
|
|
|
|
return "html";
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
if (type && (type.endsWith("json") || type.endsWith("importmap"))) {
|
|
|
|
|
return "json";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "text/x-handlebars-template") {
|
|
|
|
|
return "glimmer";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function inferStyleParser(node) {
|
|
|
|
|
const { lang } = node.attrMap;
|
2020-08-26 00:26:03 +08:00
|
|
|
if (!lang || lang === "postcss" || lang === "css") {
|
2020-04-17 07:54:24 +09:00
|
|
|
return "css";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lang === "scss") {
|
|
|
|
|
return "scss";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lang === "less") {
|
|
|
|
|
return "less";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function inferScriptParser(node, options) {
|
2018-11-04 23:03:07 +08:00
|
|
|
if (node.name === "script" && !node.attrMap.src) {
|
2020-04-17 07:54:24 +09:00
|
|
|
if (!node.attrMap.lang && !node.attrMap.type) {
|
2018-12-27 08:05:19 -05:00
|
|
|
return "babel";
|
2018-11-04 23:03:07 +08:00
|
|
|
}
|
2020-04-17 07:54:24 +09:00
|
|
|
return _inferScriptParser(node);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node.name === "style") {
|
2020-08-26 00:26:03 +08:00
|
|
|
return inferStyleParser(node);
|
2020-04-17 07:54:24 +09:00
|
|
|
}
|
2018-11-04 23:03:07 +08:00
|
|
|
|
2020-06-17 19:18:29 +08:00
|
|
|
if (options && isVueNonHtmlBlock(node, options)) {
|
2020-04-17 07:54:24 +09:00
|
|
|
return (
|
|
|
|
|
_inferScriptParser(node) ||
|
2020-11-02 10:41:03 +08:00
|
|
|
(!("src" in node.attrMap) &&
|
|
|
|
|
inferParserByLanguage(node.attrMap.lang, options))
|
2020-04-17 07:54:24 +09:00
|
|
|
);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isBlockLikeCssDisplay(cssDisplay) {
|
2018-11-04 23:03:07 +08:00
|
|
|
return (
|
|
|
|
|
cssDisplay === "block" ||
|
|
|
|
|
cssDisplay === "list-item" ||
|
|
|
|
|
cssDisplay.startsWith("table")
|
|
|
|
|
);
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
function isFirstChildLeadingSpaceSensitiveCssDisplay(cssDisplay) {
|
|
|
|
|
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isLastChildTrailingSpaceSensitiveCssDisplay(cssDisplay) {
|
|
|
|
|
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPrevTrailingSpaceSensitiveCssDisplay(cssDisplay) {
|
|
|
|
|
return !isBlockLikeCssDisplay(cssDisplay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isNextLeadingSpaceSensitiveCssDisplay(cssDisplay) {
|
|
|
|
|
return !isBlockLikeCssDisplay(cssDisplay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isDanglingSpaceSensitiveCssDisplay(cssDisplay) {
|
|
|
|
|
return !isBlockLikeCssDisplay(cssDisplay) && cssDisplay !== "inline-block";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPreLikeNode(node) {
|
|
|
|
|
return getNodeCssStyleWhiteSpace(node).startsWith("pre");
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 08:00:50 -04:00
|
|
|
/**
|
|
|
|
|
* @param {AstPath} path
|
|
|
|
|
* @param {(any) => boolean} predicate
|
|
|
|
|
*/
|
2020-08-21 09:43:24 +08:00
|
|
|
function countParents(path, predicate) {
|
2018-11-04 23:03:07 +08:00
|
|
|
let counter = 0;
|
|
|
|
|
for (let i = path.stack.length - 1; i >= 0; i--) {
|
|
|
|
|
const value = path.stack[i];
|
|
|
|
|
if (
|
|
|
|
|
value &&
|
|
|
|
|
typeof value === "object" &&
|
|
|
|
|
!Array.isArray(value) &&
|
|
|
|
|
predicate(value)
|
|
|
|
|
) {
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return counter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasParent(node, fn) {
|
|
|
|
|
let current = node;
|
|
|
|
|
|
|
|
|
|
while (current) {
|
|
|
|
|
if (fn(current)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current = current.parent;
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNodeCssStyleDisplay(node, options) {
|
|
|
|
|
if (node.prev && node.prev.type === "comment") {
|
2018-10-13 13:55:38 +08:00
|
|
|
// <!-- display: block -->
|
2018-11-04 23:03:07 +08:00
|
|
|
const match = node.prev.value.match(/^\s*display:\s*([a-z]+)\s*$/);
|
2018-10-13 13:55:38 +08:00
|
|
|
if (match) {
|
|
|
|
|
return match[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 23:03:07 +08:00
|
|
|
let isInSvgForeignObject = false;
|
|
|
|
|
if (node.type === "element" && node.namespace === "svg") {
|
|
|
|
|
if (hasParent(node, (parent) => parent.fullName === "svg:foreignObject")) {
|
|
|
|
|
isInSvgForeignObject = true;
|
|
|
|
|
} else {
|
|
|
|
|
return node.name === "svg" ? "inline-block" : "block";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 13:55:38 +08:00
|
|
|
switch (options.htmlWhitespaceSensitivity) {
|
|
|
|
|
case "strict":
|
|
|
|
|
return "inline";
|
|
|
|
|
case "ignore":
|
|
|
|
|
return "block";
|
2020-05-18 18:50:15 +09:00
|
|
|
default: {
|
|
|
|
|
// See https://github.com/prettier/prettier/issues/8151
|
|
|
|
|
if (
|
|
|
|
|
options.parser === "vue" &&
|
|
|
|
|
node.parent &&
|
|
|
|
|
node.parent.type === "root"
|
|
|
|
|
) {
|
|
|
|
|
return "block";
|
|
|
|
|
}
|
2018-10-13 13:55:38 +08:00
|
|
|
return (
|
2018-11-04 23:03:07 +08:00
|
|
|
(node.type === "element" &&
|
2020-01-20 22:39:19 +08:00
|
|
|
(!node.namespace ||
|
|
|
|
|
isInSvgForeignObject ||
|
|
|
|
|
isUnknownNamespace(node)) &&
|
2018-11-04 23:03:07 +08:00
|
|
|
CSS_DISPLAY_TAGS[node.name]) ||
|
|
|
|
|
CSS_DISPLAY_DEFAULT
|
2018-10-13 13:55:38 +08:00
|
|
|
);
|
2020-05-18 18:50:15 +09:00
|
|
|
}
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 22:39:19 +08:00
|
|
|
function isUnknownNamespace(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type === "element" &&
|
|
|
|
|
!node.hasExplicitNamespace &&
|
|
|
|
|
!["html", "svg"].includes(node.namespace)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-13 13:55:38 +08:00
|
|
|
function getNodeCssStyleWhiteSpace(node) {
|
|
|
|
|
return (
|
2018-11-04 23:03:07 +08:00
|
|
|
(node.type === "element" &&
|
2020-01-20 22:39:19 +08:00
|
|
|
(!node.namespace || isUnknownNamespace(node)) &&
|
2018-11-04 23:03:07 +08:00
|
|
|
CSS_WHITE_SPACE_TAGS[node.name]) ||
|
|
|
|
|
CSS_WHITE_SPACE_DEFAULT
|
2018-10-13 13:55:38 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getMinIndentation(text) {
|
2020-12-21 17:09:20 +08:00
|
|
|
let minIndentation = Number.POSITIVE_INFINITY;
|
2018-10-13 13:55:38 +08:00
|
|
|
|
|
|
|
|
for (const lineText of text.split("\n")) {
|
2018-11-04 23:03:07 +08:00
|
|
|
if (lineText.length === 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 21:56:28 +08:00
|
|
|
if (!HTML_WHITESPACE.has(lineText[0])) {
|
2018-10-13 13:55:38 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 21:56:28 +08:00
|
|
|
const indentation = getLeadingHtmlWhitespace(lineText).length;
|
2018-10-13 13:55:38 +08:00
|
|
|
|
|
|
|
|
if (lineText.length === indentation) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (indentation < minIndentation) {
|
|
|
|
|
minIndentation = indentation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 17:09:20 +08:00
|
|
|
return minIndentation === Number.POSITIVE_INFINITY ? 0 : minIndentation;
|
2018-10-13 13:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dedentString(text, minIndent = getMinIndentation(text)) {
|
|
|
|
|
return minIndent === 0
|
|
|
|
|
? text
|
|
|
|
|
: text
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((lineText) => lineText.slice(minIndent))
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 12:33:48 +08:00
|
|
|
function shouldNotPrintClosingTag(node, options) {
|
2018-11-09 23:26:59 +08:00
|
|
|
return (
|
|
|
|
|
!node.isSelfClosing &&
|
|
|
|
|
!node.endSourceSpan &&
|
2018-11-23 12:33:48 +08:00
|
|
|
(hasPrettierIgnore(node) || shouldPreserveContent(node.parent, options))
|
2018-11-09 23:26:59 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 11:15:09 +08:00
|
|
|
function countChars(text, char) {
|
|
|
|
|
let counter = 0;
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
|
if (text[i] === char) {
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return counter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unescapeQuoteEntities(text) {
|
|
|
|
|
return text.replace(/'/g, "'").replace(/"/g, '"');
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 07:54:24 +09:00
|
|
|
// top-level elements (excluding <template>, <style> and <script>) in Vue SFC are considered custom block
|
2020-05-14 23:08:40 +09:00
|
|
|
// See https://vue-loader.vuejs.org/spec.html for detail
|
2020-05-19 10:30:03 +09:00
|
|
|
const vueRootElementsSet = new Set(["template", "style", "script"]);
|
2020-04-17 07:54:24 +09:00
|
|
|
function isVueCustomBlock(node, options) {
|
2020-06-17 19:18:29 +08:00
|
|
|
return isVueSfcBlock(node, options) && !vueRootElementsSet.has(node.fullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isVueSfcBlock(node, options) {
|
2020-04-17 07:54:24 +09:00
|
|
|
return (
|
|
|
|
|
options.parser === "vue" &&
|
|
|
|
|
node.type === "element" &&
|
|
|
|
|
node.parent.type === "root" &&
|
2020-05-19 10:30:03 +09:00
|
|
|
node.fullName.toLowerCase() !== "html"
|
2020-04-17 07:54:24 +09:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 19:18:29 +08:00
|
|
|
function isVueNonHtmlBlock(node, options) {
|
|
|
|
|
return (
|
|
|
|
|
isVueSfcBlock(node, options) &&
|
|
|
|
|
(isVueCustomBlock(node, options) ||
|
|
|
|
|
(node.attrMap.lang && node.attrMap.lang !== "html"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 09:05:30 +08:00
|
|
|
function isVueSlotAttribute(attribute) {
|
|
|
|
|
const attributeName = attribute.fullName;
|
|
|
|
|
return (
|
|
|
|
|
attributeName.charAt(0) === "#" ||
|
|
|
|
|
attributeName === "slot-scope" ||
|
|
|
|
|
attributeName === "v-slot" ||
|
|
|
|
|
attributeName.startsWith("v-slot:")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isVueSfcBindingsAttribute(attribute, options) {
|
|
|
|
|
const element = attribute.parent;
|
|
|
|
|
if (!isVueSfcBlock(element, options)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const tagName = element.fullName;
|
|
|
|
|
const attributeName = attribute.fullName;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
// https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-script-setup.md
|
|
|
|
|
(tagName === "script" && attributeName === "setup") ||
|
|
|
|
|
// https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-style-variables.md
|
|
|
|
|
(tagName === "style" && attributeName === "vars")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 23:07:27 +08:00
|
|
|
module.exports = {
|
2018-09-27 20:18:00 +08:00
|
|
|
HTML_ELEMENT_ATTRIBUTES,
|
2018-09-22 21:53:38 +08:00
|
|
|
HTML_TAGS,
|
2020-04-25 21:56:28 +08:00
|
|
|
htmlTrim,
|
|
|
|
|
htmlTrimPreserveIndentation,
|
|
|
|
|
splitByHtmlWhitespace,
|
|
|
|
|
hasHtmlWhitespace,
|
|
|
|
|
getLeadingAndTrailingHtmlWhitespace,
|
2018-11-04 23:03:07 +08:00
|
|
|
canHaveInterpolation,
|
2018-12-05 11:15:09 +08:00
|
|
|
countChars,
|
2018-11-04 23:03:07 +08:00
|
|
|
countParents,
|
2018-10-13 13:55:38 +08:00
|
|
|
dedentString,
|
|
|
|
|
forceBreakChildren,
|
|
|
|
|
forceBreakContent,
|
|
|
|
|
forceNextEmptyLine,
|
|
|
|
|
getLastDescendant,
|
|
|
|
|
getNodeCssStyleDisplay,
|
|
|
|
|
getNodeCssStyleWhiteSpace,
|
2018-11-04 23:03:07 +08:00
|
|
|
getPrettierIgnoreAttributeCommentData,
|
2018-09-19 23:07:27 +08:00
|
|
|
hasPrettierIgnore,
|
2018-10-13 13:55:38 +08:00
|
|
|
inferScriptParser,
|
2020-04-17 07:54:24 +09:00
|
|
|
isVueCustomBlock,
|
2020-06-17 19:18:29 +08:00
|
|
|
isVueNonHtmlBlock,
|
2020-11-13 09:05:30 +08:00
|
|
|
isVueSlotAttribute,
|
|
|
|
|
isVueSfcBindingsAttribute,
|
2018-10-13 13:55:38 +08:00
|
|
|
isDanglingSpaceSensitiveNode,
|
2018-11-04 23:03:07 +08:00
|
|
|
isIndentationSensitiveNode,
|
2018-10-13 13:55:38 +08:00
|
|
|
isLeadingSpaceSensitiveNode,
|
2018-11-04 23:03:07 +08:00
|
|
|
isPreLikeNode,
|
2018-10-13 13:55:38 +08:00
|
|
|
isScriptLikeTag,
|
2018-11-23 21:25:58 +08:00
|
|
|
isTextLikeNode,
|
2018-10-13 13:55:38 +08:00
|
|
|
isTrailingSpaceSensitiveNode,
|
2018-11-04 23:03:07 +08:00
|
|
|
isWhitespaceSensitiveNode,
|
2020-01-20 22:39:19 +08:00
|
|
|
isUnknownNamespace,
|
2018-10-13 13:55:38 +08:00
|
|
|
preferHardlineAsLeadingSpaces,
|
|
|
|
|
preferHardlineAsTrailingSpaces,
|
2018-11-09 23:26:59 +08:00
|
|
|
shouldNotPrintClosingTag,
|
2018-12-05 11:15:09 +08:00
|
|
|
shouldPreserveContent,
|
|
|
|
|
unescapeQuoteEntities,
|
2018-09-19 23:07:27 +08:00
|
|
|
};
|