fix(yaml): update parsers (#5027)

- upgrade to `yaml@1.0.0-rc.8` and `yaml-unist-parser@1.0.0-rc.4`
- refactor some logic since the AST has slightly changed (ikatyang/yaml-unist-parser#82)
- unmatched aliases are now errors since it may introduce invalid AST from `yaml`
- rewrite the document separator (`...`/`---`) logic, this fixes some cases where it can use `---` but we printed `...`
- removed some unnecessary duplicate trailing newline
- trailing comments on `document` (`... #comment`) and `documentHead` (`--- #comment`) are preserved (i.e. they won't be moved somewhere)
master
Ika 2018-09-01 20:04:37 +08:00 committed by GitHub
parent 165742014a
commit db2bc3636d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 341 additions and 256 deletions

View File

@ -62,8 +62,8 @@
"unicode-regex": "1.0.1", "unicode-regex": "1.0.1",
"unified": "6.1.6", "unified": "6.1.6",
"vnopts": "1.0.2", "vnopts": "1.0.2",
"yaml": "ikatyang/yaml#a765c1ee16d6b8a5e715564645f2b85f7e04828b", "yaml": "1.0.0-rc.8",
"yaml-unist-parser": "ikatyang/yaml-unist-parser#cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b" "yaml-unist-parser": "1.0.0-rc.4"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "7.0.0-beta.55", "@babel/cli": "7.0.0-beta.55",

View File

@ -19,7 +19,9 @@ module.exports = ({ types: t }) => ({
t.isMemberExpression(callee, { computed: false }) && t.isMemberExpression(callee, { computed: false }) &&
(t.isArrayExpression(callee.object) || (t.isArrayExpression(callee.object) ||
(t.isIdentifier(callee.object) && (t.isIdentifier(callee.object) &&
/^[A-Z_]+$/.test(callee.object.name))) && (/^[A-Z_]+$/.test(callee.object.name) ||
// https://github.com/eemeli/yaml/blob/1005d01/src/Anchors.js#L45
callee.object.name === "names"))) &&
t.isIdentifier(callee.property, { name: "includes" }) t.isIdentifier(callee.property, { name: "includes" })
) { ) {
callee.property.name = "indexOf"; callee.property.name = "indexOf";

View File

@ -98,7 +98,7 @@ async function run(params) {
await execa("rm", ["-rf", ".cache"]); await execa("rm", ["-rf", ".cache"]);
} }
const bundleCache = new Cache(".cache/", "v5"); const bundleCache = new Cache(".cache/", "v6");
await bundleCache.load(); await bundleCache.load();
console.log(chalk.inverse(" Building packages ")); console.log(chalk.inverse(" Building packages "));

View File

@ -2,7 +2,7 @@
const createError = require("../common/parser-create-error"); const createError = require("../common/parser-create-error");
const { hasPragma } = require("./pragma"); const { hasPragma } = require("./pragma");
const { createNull, defineShortcut, mapNode } = require("./utils"); const { defineShortcut, mapNode } = require("./utils");
function defineShortcuts(node) { function defineShortcuts(node) {
switch (node.type) { switch (node.type) {
@ -10,11 +10,12 @@ function defineShortcuts(node) {
defineShortcut(node, "head", () => node.children[0]); defineShortcut(node, "head", () => node.children[0]);
defineShortcut(node, "body", () => node.children[1]); defineShortcut(node, "body", () => node.children[1]);
break; break;
case "documentBody":
case "sequenceItem": case "sequenceItem":
case "flowSequenceItem": case "flowSequenceItem":
case "mappingKey": case "mappingKey":
case "mappingValue": case "mappingValue":
defineShortcut(node, "node", () => node.children[0]); defineShortcut(node, "content", () => node.children[0]);
break; break;
case "mappingItem": case "mappingItem":
case "flowMappingItem": case "flowMappingItem":
@ -22,25 +23,15 @@ function defineShortcuts(node) {
defineShortcut(node, "value", () => node.children[1]); defineShortcut(node, "value", () => node.children[1]);
break; break;
} }
return node;
} }
function parse(text) { function parse(text) {
try { try {
const root = mapNode(require("yaml-unist-parser").parse(text), node => { const root = mapNode(
// replace explicit empty MappingKey/MappingValue with implicit one require("yaml-unist-parser").parse(text),
if ( defineShortcuts
(node.type === "mappingKey" || node.type === "mappingValue") && );
node.children[0].type === "null" &&
node.leadingComments.length === 0 &&
node.trailingComments.length === 0 &&
node.endComments.length === 0
) {
return createNull();
}
defineShortcuts(node);
return node;
});
/** /**
* suppress `comment not printed` error * suppress `comment not printed` error
@ -53,7 +44,7 @@ function parse(text) {
return root; return root;
} catch (error) { } catch (error) {
// istanbul ignore next // istanbul ignore next
throw error && error.name === "YAMLSyntaxError" throw error && error.position
? createError(error.message, error.position) ? createError(error.message, error.position)
: error; : error;
} }

View File

@ -7,16 +7,16 @@ const {
getFlowScalarLineContents, getFlowScalarLineContents,
getLast, getLast,
getLastDescendantNode, getLastDescendantNode,
hasExplicitDocumentEndMarker,
hasLeadingComments, hasLeadingComments,
hasMiddleComments, hasMiddleComments,
hasTrailingComments, hasIndicatorComment,
hasTrailingComment,
hasEndComments, hasEndComments,
hasPrettierIgnore, hasPrettierIgnore,
isLastDescendantNode, isLastDescendantNode,
isNextLineEmpty, isNextLineEmpty,
isNode, isNode,
isBlockValue isEmptyNode
} = require("./utils"); } = require("./utils");
const docBuilders = require("../doc").builders; const docBuilders = require("../doc").builders;
const { const {
@ -41,22 +41,18 @@ function genericPrint(path, options, print) {
const node = path.getValue(); const node = path.getValue();
const parentNode = path.getParentNode(); const parentNode = path.getParentNode();
const tag = const tag = !node.tag ? "" : path.call(print, "tag");
"tag" in node && node.tag.type !== "null" ? path.call(print, "tag") : ""; const anchor = !node.anchor ? "" : path.call(print, "anchor");
const anchor =
"anchor" in node && node.anchor.type !== "null"
? path.call(print, "anchor")
: "";
const nextEmptyLine = const nextEmptyLine =
(node.type === "mapping" || isNode(node, [
node.type === "sequence" || "mapping",
node.type === "comment" || "sequence",
node.type === "directive" || "comment",
node.type === "mappingItem" || "directive",
node.type === "sequenceItem") && "mappingItem",
!isLastDescendantNode(path) "sequenceItem"
]) && !isLastDescendantNode(path)
? printNextEmptyLine(path, options.originalText) ? printNextEmptyLine(path, options.originalText)
: ""; : "";
@ -67,14 +63,11 @@ function genericPrint(path, options, print) {
tag, tag,
tag && anchor ? " " : "", tag && anchor ? " " : "",
anchor, anchor,
(node.type === "sequence" || node.type === "mapping") && tag || anchor
node.middleComments.length === 0 ? isNode(node, ["sequence", "mapping"]) && !hasMiddleComments(node)
? tag || anchor
? hardline ? hardline
: "" : " "
: tag || anchor : "",
? " "
: "",
hasMiddleComments(node) hasMiddleComments(node)
? concat([ ? concat([
node.middleComments.length === 1 ? "" : hardline, node.middleComments.length === 1 ? "" : hardline,
@ -88,23 +81,23 @@ function genericPrint(path, options, print) {
node.position.end.offset node.position.end.offset
) )
: group(_print(node, parentNode, path, options, print)), : group(_print(node, parentNode, path, options, print)),
!isBlockValue(node) && hasTrailingComments(node) // trailing comments for block value are handled themselves hasTrailingComment(node) && !isNode(node, ["document", "documentHead"])
? lineSuffix( ? lineSuffix(
concat([ concat([
" ", node.type === "mappingValue" && !node.content ? "" : " ",
parentNode.type === "mappingKey" && parentNode.type === "mappingKey" &&
path.getParentNode(2).type === "mapping" && path.getParentNode(2).type === "mapping" &&
isInlineNode(node) isInlineNode(node)
? "" ? ""
: breakParent, : breakParent,
join(hardline, path.map(print, "trailingComments")) path.call(print, "trailingComment")
]) ])
) )
: "", : "",
nextEmptyLine, nextEmptyLine,
hasEndComments(node) hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"])
? (endComments => ? align(
node.type === "sequenceItem" ? align(2, endComments) : endComments)( node.type === "sequenceItem" ? 2 : 0,
concat([hardline, join(hardline, path.map(print, "endComments"))]) concat([hardline, join(hardline, path.map(print, "endComments"))])
) )
: "" : ""
@ -115,67 +108,95 @@ function _print(node, parentNode, path, options, print) {
switch (node.type) { switch (node.type) {
case "root": case "root":
return concat([ return concat([
concat( join(
path.map( hardline,
(childPath, index) => path.map((childPath, index) => {
index === node.children.length - 1 const document = node.children[index];
? print(childPath) const nextDocument = node.children[index + 1];
: concat([ return concat([
print(childPath), print(childPath),
hasTrailingComments(node.children[index]) || shouldPrintDocumentEndMarker(document, nextDocument)
(childPath.call(hasPrettierIgnore, "body") && ? concat([
hasExplicitDocumentEndMarker( hardline,
node.children[index], "...",
options.originalText hasTrailingComment(document)
)) ? concat([" ", path.call(print, "trailingComment")])
? "" : ""
: concat([ ])
hardline, : !nextDocument || hasTrailingComment(nextDocument.head)
node.children[index + 1].head.children.length === 0 ? ""
? "---" : concat([hardline, "---"])
: "..." ]);
]), }, "children")
hardline
]),
"children"
)
), ),
node.children.length === 0 || node.children.length === 0 ||
(lastDescendantNode => (lastDescendantNode =>
isBlockValue(lastDescendantNode) && isNode(lastDescendantNode, ["blockLiteral", "blockFolded"]) &&
lastDescendantNode.chomping === "keep")(getLastDescendantNode(node)) lastDescendantNode.chomping === "keep")(getLastDescendantNode(node))
? "" ? ""
: hardline : hardline
]); ]);
case "document": case "document": {
return concat([ const nextDocument = parentNode.children[path.getName() + 1];
node.head.children.length === 0 return join(
? path.call(print, "body") hardline,
: join( [
hardline, shouldPrintDocumentHeadEndMarker(node, nextDocument) === "head"
[path.call(print, "head"), "---"].concat( ? join(
node.body.children.length === 0 ? [] : path.call(print, "body") hardline,
[
node.head.children.length === 0 &&
node.head.endComments.length === 0
? ""
: path.call(print, "head"),
concat([
"---",
hasTrailingComment(node.head)
? concat([
" ",
path.call(print, "head", "trailingComment")
])
: ""
])
].filter(Boolean)
) )
), : "",
hasTrailingComments(node) ? concat([hardline, "..."]) : "" shouldPrintDocumentBody(node) ? path.call(print, "body") : ""
]); ].filter(Boolean)
);
}
case "documentHead": case "documentHead":
case "documentBody": return join(
return join(hardline, path.map(print, "children")); hardline,
[].concat(path.map(print, "children"), path.map(print, "endComments"))
);
case "documentBody": {
const children = join(hardline, path.map(print, "children")).parts;
const endComments = join(hardline, path.map(print, "endComments")).parts;
const separator =
children.length === 0 || endComments.length === 0
? ""
: (lastDescendantNode =>
isNode(lastDescendantNode, ["blockFolded", "blockLiteral"])
? lastDescendantNode.chomping === "keep"
? // there's already a newline printed at the end of blockValue (chomping=keep, lastDescendant=true)
""
: // an extra newline for better readability
concat([hardline, hardline])
: hardline)(getLastDescendantNode(node));
return concat([].concat(children, separator, endComments));
}
case "directive": case "directive":
return concat(["%", join(" ", [node.name].concat(node.parameters))]); return concat(["%", join(" ", [node.name].concat(node.parameters))]);
case "comment": case "comment":
return concat(["#", node.value]); return concat(["#", node.value]);
case "alias": case "alias":
return concat(["*", node.value]); return concat(["*", node.value]);
case "null": case "tag":
return ""; return options.originalText.slice(
case "verbatimTag": node.position.start.offset,
return concat(["!<", node.value, ">"]); node.position.end.offset
case "shorthandTag": );
return concat([node.handle, node.suffix]);
case "nonSpecificTag":
return "!";
case "anchor": case "anchor":
return concat(["&", node.value]); return concat(["&", node.value]);
case "plain": case "plain":
@ -249,18 +270,16 @@ function _print(node, parentNode, path, options, print) {
} }
case "blockFolded": case "blockFolded":
case "blockLiteral": { case "blockLiteral": {
const parentIndent = getAncestorCount( const parentIndent = getAncestorCount(path, ancestorNode =>
path, isNode(ancestorNode, ["sequence", "mapping"])
ancestorNode =>
ancestorNode.type === "sequence" || ancestorNode.type === "mapping"
); );
const isLastDescendant = isLastDescendantNode(path); const isLastDescendant = isLastDescendantNode(path);
return concat([ return concat([
node.type === "blockFolded" ? ">" : "|", node.type === "blockFolded" ? ">" : "|",
node.indent === null ? "" : node.indent.toString(), node.indent === null ? "" : node.indent.toString(),
node.chomping === "clip" ? "" : node.chomping === "keep" ? "+" : "-", node.chomping === "clip" ? "" : node.chomping === "keep" ? "+" : "-",
hasTrailingComments(node) hasIndicatorComment(node)
? concat([" ", join(hardline, path.map(print, "trailingComments"))]) ? concat([" ", path.call(print, "indicatorComment")])
: "", : "",
(node.indent === null ? dedent : dedentToRoot)( (node.indent === null ? dedent : dedentToRoot)(
align( align(
@ -275,23 +294,17 @@ function _print(node, parentNode, path, options, print) {
}).reduce( }).reduce(
(reduced, lineWords, index, lineContents) => (reduced, lineWords, index, lineContents) =>
reduced.concat( reduced.concat(
index === 0 index === 0 ? hardline : "",
? hardline
: lineContents[index - 1].length === 0
? hardline
: index === lineContents.length - 1 &&
lineWords.length === 0
? dedentToRoot(literalline)
: markAsRoot(literalline),
fill(join(line, lineWords).parts), fill(join(line, lineWords).parts),
index === lineContents.length - 1 && index !== lineContents.length - 1
node.chomping === "keep" && ? lineWords.length === 0
isLastDescendant ? hardline
? lineWords.length === 0 || : markAsRoot(literalline)
!getLast(lineWords).endsWith(" ") : node.chomping === "keep" && isLastDescendant
? dedentToRoot(hardline) ? lineWords.length === 0
: dedentToRoot(literalline) ? dedentToRoot(hardline)
: [] : dedentToRoot(literalline)
: ""
), ),
[] []
) )
@ -303,35 +316,37 @@ function _print(node, parentNode, path, options, print) {
case "sequence": case "sequence":
return join(hardline, path.map(print, "children")); return join(hardline, path.map(print, "children"));
case "sequenceItem": case "sequenceItem":
return concat(["- ", align(2, path.call(print, "node"))]); return concat([
"- ",
align(2, !node.content ? "" : path.call(print, "content"))
]);
case "mappingKey": case "mappingKey":
return path.call(print, "node"); return !node.content ? "" : path.call(print, "content");
case "mappingValue": case "mappingValue":
return path.call(print, "node"); return !node.content ? "" : path.call(print, "content");
case "mapping": case "mapping":
return join(hardline, path.map(print, "children")); return join(hardline, path.map(print, "children"));
case "mappingItem": case "mappingItem":
case "flowMappingItem": { case "flowMappingItem": {
if (node.key.type === "null" && node.value.type === "null") { const isEmptyMappingKey = isEmptyNode(node.key);
return concat([":", line]); const isEmptyMappingValue = isEmptyNode(node.value);
if (isEmptyMappingKey && isEmptyMappingValue) {
return concat([": "]);
} }
const key = path.call(print, "key"); const key = path.call(print, "key");
const value = path.call(print, "value"); const value = path.call(print, "value");
if (node.value.type === "null") { if (isEmptyMappingValue) {
return node.type === "flowMappingItem" && return node.type === "flowMappingItem" &&
path.getParentNode().type !== "flowSequence" parentNode.type === "flowMapping"
? key ? key
: node.type === "mappingItem" && : node.type === "mappingItem" &&
node.key.type !== "null" && isAbsolutelyPrintedAsSingleLineNode(node.key.content, options) &&
isAbsolutelyPrintedAsSingleLineNode(node.key.node, options) && !hasTrailingComment(node.key.content) &&
!hasTrailingComments(node.key.node) && (!parentNode.tag ||
!( parentNode.tag.value !== "tag:yaml.org,2002:set")
parentNode.tag.type === "shorthandTag" &&
parentNode.tag.handle === "!!" &&
parentNode.tag.suffix === "set"
)
? concat([ ? concat([
key, key,
needsSpaceInFrontOfMappingValue(node) ? " " : "", needsSpaceInFrontOfMappingValue(node) ? " " : "",
@ -340,19 +355,15 @@ function _print(node, parentNode, path, options, print) {
: concat(["? ", align(2, key)]); : concat(["? ", align(2, key)]);
} }
if (node.key.type === "null") { if (isEmptyMappingKey) {
return concat([ return concat([": ", align(2, value)]);
":",
node.value.node.type === "null" ? "" : " ",
align(2, value)
]);
} }
const groupId = Symbol("mappingKey"); const groupId = Symbol("mappingKey");
const forceExplicitKey = const forceExplicitKey =
hasLeadingComments(node.value) || hasLeadingComments(node.value) || !isInlineNode(node.key.content);
(node.key.type !== "null" && !isInlineNode(node.key.node));
return forceExplicitKey return forceExplicitKey
? concat([ ? concat([
"? ", "? ",
@ -368,15 +379,15 @@ function _print(node, parentNode, path, options, print) {
align(2, value) align(2, value)
]) ])
: // force singleline : // force singleline
isSingleLineNode(node.key.node) && isSingleLineNode(node.key.content) &&
!hasLeadingComments(node.key.node) && !hasLeadingComments(node.key.content) &&
!hasMiddleComments(node.key.node) && !hasMiddleComments(node.key.content) &&
!hasTrailingComments(node.key.node) && !hasTrailingComment(node.key.content) &&
!hasEndComments(node.key) && !hasEndComments(node.key) &&
!hasLeadingComments(node.value.node) && !hasLeadingComments(node.value.content) &&
!hasMiddleComments(node.value.node) && !hasMiddleComments(node.value.content) &&
!hasEndComments(node.value) && !hasEndComments(node.value) &&
isAbsolutelyPrintedAsSingleLineNode(node.value.node, options) isAbsolutelyPrintedAsSingleLineNode(node.value.content, options)
? concat([ ? concat([
key, key,
needsSpaceInFrontOfMappingValue(node) ? " " : "", needsSpaceInFrontOfMappingValue(node) ? " " : "",
@ -394,18 +405,18 @@ function _print(node, parentNode, path, options, print) {
concat([ concat([
needsSpaceInFrontOfMappingValue(node) ? " " : "", needsSpaceInFrontOfMappingValue(node) ? " " : "",
":", ":",
hasLeadingComments(node.value.node) || hasLeadingComments(node.value.content) ||
(hasEndComments(node.value) && (hasEndComments(node.value) &&
node.value.node.type !== "null") || node.value.content &&
!isNode(node.value.content, ["mapping", "sequence"])) ||
(parentNode.type === "mapping" && (parentNode.type === "mapping" &&
hasTrailingComments(node.key.node) && hasTrailingComment(node.key.content) &&
isInlineNode(node.value.node)) || isInlineNode(node.value.content)) ||
((node.value.node.type === "mapping" || (isNode(node.value.content, ["mapping", "sequence"]) &&
node.value.node.type === "sequence") && node.value.content.tag === null &&
node.value.node.tag.type === "null" && node.value.content.anchor === null)
node.value.node.anchor.type === "null")
? hardline ? hardline
: node.value.node.type === "null" : !node.value.content
? "" ? ""
: line, : line,
value value
@ -430,8 +441,8 @@ function _print(node, parentNode, path, options, print) {
node.children.length !== 0 && node.children.length !== 0 &&
(lastItem => (lastItem =>
lastItem.type === "flowMappingItem" && lastItem.type === "flowMappingItem" &&
lastItem.key.type === "null" && isEmptyNode(lastItem.key) &&
lastItem.value.type === "null")(getLast(node.children)); isEmptyNode(lastItem.value))(getLast(node.children));
return concat([ return concat([
openMarker, openMarker,
indent( indent(
@ -467,7 +478,7 @@ function _print(node, parentNode, path, options, print) {
]); ]);
} }
case "flowSequenceItem": case "flowSequenceItem":
return path.call(print, "node"); return path.call(print, "content");
// istanbul ignore next // istanbul ignore next
default: default:
throw new Error(`Unexpected node type ${node.type}`); throw new Error(`Unexpected node type ${node.type}`);
@ -485,6 +496,10 @@ function align(n, doc) {
} }
function isInlineNode(node) { function isInlineNode(node) {
if (!node) {
return true;
}
switch (node.type) { switch (node.type) {
case "plain": case "plain":
case "quoteDouble": case "quoteDouble":
@ -492,7 +507,6 @@ function isInlineNode(node) {
case "alias": case "alias":
case "flowMapping": case "flowMapping":
case "flowSequence": case "flowSequence":
case "null":
return true; return true;
default: default:
return false; return false;
@ -500,6 +514,10 @@ function isInlineNode(node) {
} }
function isSingleLineNode(node) { function isSingleLineNode(node) {
if (!node) {
return true;
}
switch (node.type) { switch (node.type) {
case "plain": case "plain":
case "quoteDouble": case "quoteDouble":
@ -512,7 +530,64 @@ function isSingleLineNode(node) {
} }
} }
function shouldPrintDocumentBody(document) {
return document.body.children.length !== 0 || hasEndComments(document.body);
}
function shouldPrintDocumentEndMarker(document, nextDocument) {
return (
/**
*... # trailingComment
*/
hasTrailingComment(document) ||
(nextDocument &&
/**
* ...
* %DIRECTIVE
* ---
*/
(nextDocument.head.children.length !== 0 ||
/**
* ...
* # endComment
* ---
*/
hasEndComments(nextDocument.head)))
);
}
function shouldPrintDocumentHeadEndMarker(document, nextDocument) {
if (
/**
* %DIRECTIVE
* ---
*/
document.head.children.length !== 0 ||
/**
* # end comment
* ---
*/
hasEndComments(document.head) ||
/**
* --- # trailing comment
*/
hasTrailingComment(document.head)
) {
return "head";
}
if (shouldPrintDocumentEndMarker(document, nextDocument)) {
return false;
}
return nextDocument ? "root" : false;
}
function isAbsolutelyPrintedAsSingleLineNode(node, options) { function isAbsolutelyPrintedAsSingleLineNode(node, options) {
if (!node) {
return true;
}
switch (node.type) { switch (node.type) {
case "plain": case "plain":
case "quoteSingle": case "quoteSingle":
@ -552,14 +627,7 @@ function isAbsolutelyPrintedAsSingleLineNode(node, options) {
} }
function needsSpaceInFrontOfMappingValue(node) { function needsSpaceInFrontOfMappingValue(node) {
// istanbul ignore else return node.key.content && node.key.content.type === "alias";
if (node.key.type !== "null") {
switch (node.key.node.type) {
case "alias":
return true;
}
}
return false;
} }
function printNextEmptyLine(path, originalText) { function printNextEmptyLine(path, originalText) {

View File

@ -16,8 +16,16 @@ function getAncestorCount(path, filter) {
return counter; return counter;
} }
function isNode(value) { /**
return value && typeof value.type === "string"; * @param {any} value
* @param {string[]=} types
*/
function isNode(value, types) {
return (
value &&
typeof value.type === "string" &&
(!types || types.indexOf(value.type) !== -1)
);
} }
function mapNode(node, callback, parent) { function mapNode(node, callback, parent) {
@ -40,16 +48,6 @@ function defineShortcut(x, key, getter) {
}); });
} }
function createNull() {
return {
type: "null",
position: {
start: { line: -1, column: -1, offset: -1 },
end: { line: -1, column: -1, offset: -1 }
}
};
}
function isNextLineEmpty(node, text) { function isNextLineEmpty(node, text) {
let newlineCount = 0; let newlineCount = 0;
const textLength = text.length; const textLength = text.length;
@ -76,12 +74,12 @@ function isLastDescendantNode(path) {
const node = path.getValue(); const node = path.getValue();
switch (node.type) { switch (node.type) {
case "tag":
case "anchor":
case "comment": case "comment":
case "verbatimTag":
case "shorthandTag":
case "nonSpecificTag":
return false; return false;
} }
const pathStackLength = path.stack.length; const pathStackLength = path.stack.length;
for (let i = 1; i < pathStackLength; i++) { for (let i = 1; i < pathStackLength; i++) {
@ -116,53 +114,48 @@ function hasPrettierIgnore(path) {
if (node.type === "documentBody") { if (node.type === "documentBody") {
const document = path.getParentNode(); const document = path.getParentNode();
return ( return (
document.head.children.length !== 0 && hasEndComments(document.head) &&
(lastItem => lastItem.type === "comment" && isPrettierIgnore(lastItem))( isPrettierIgnore(getLast(document.head.endComments))
getLast(document.head.children)
)
); );
} }
return ( return (
"leadingComments" in node && hasLeadingComments(node) && isPrettierIgnore(getLast(node.leadingComments))
node.leadingComments.length !== 0 &&
isPrettierIgnore(getLast(node.leadingComments))
); );
} }
function hasExplicitDocumentEndMarker(document, text) { function isEmptyNode(node) {
return (!node.children || node.children.length === 0) && !hasComments(node);
}
function hasComments(node) {
return ( return (
text.slice( hasLeadingComments(node) ||
document.position.end.offset - 4, hasMiddleComments(node) ||
document.position.end.offset hasIndicatorComment(node) ||
) === "\n..." hasTrailingComment(node) ||
hasEndComments(node)
); );
} }
function isBlockValue(node) {
switch (node.type) {
case "blockFolded":
case "blockLiteral":
return true;
default:
return false;
}
}
function hasLeadingComments(node) { function hasLeadingComments(node) {
return "leadingComments" in node && node.leadingComments.length !== 0; return node && node.leadingComments && node.leadingComments.length !== 0;
} }
function hasMiddleComments(node) { function hasMiddleComments(node) {
return "middleComments" in node && node.middleComments.length !== 0; return node && node.middleComments && node.middleComments.length !== 0;
} }
function hasTrailingComments(node) { function hasIndicatorComment(node) {
return "trailingComments" in node && node.trailingComments.length !== 0; return node && node.indicatorComment;
}
function hasTrailingComment(node) {
return node && node.trailingComment;
} }
function hasEndComments(node) { function hasEndComments(node) {
return "endComments" in node && node.endComments.length !== 0; return node && node.endComments && node.endComments.length !== 0;
} }
/** /**
@ -337,10 +330,9 @@ module.exports = {
getLast, getLast,
getAncestorCount, getAncestorCount,
isNode, isNode,
isBlockValue, isEmptyNode,
mapNode, mapNode,
defineShortcut, defineShortcut,
createNull,
isNextLineEmpty, isNextLineEmpty,
isLastDescendantNode, isLastDescendantNode,
getBlockValueLineContents, getBlockValueLineContents,
@ -349,7 +341,7 @@ module.exports = {
hasPrettierIgnore, hasPrettierIgnore,
hasLeadingComments, hasLeadingComments,
hasMiddleComments, hasMiddleComments,
hasTrailingComments, hasIndicatorComment,
hasEndComments, hasTrailingComment,
hasExplicitDocumentEndMarker hasEndComments
}; };

View File

@ -1,8 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`common.yml - yaml-verify 1`] = ` exports[`common.yml - yaml-verify 1`] = `
*abc - &abc a
- *abc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*abc - &abc a
- *abc
`; `;

View File

@ -1 +1,2 @@
*abc - &abc a
- *abc

View File

@ -50,8 +50,7 @@ e
# #
--- ---
f f
--- --- #
#
g g
`; `;

View File

@ -1,23 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`alias-key.yml - yaml-verify 1`] = ` exports[`alias-key.yml - yaml-verify 1`] = `
{*123 : 456} {&123 foo, *123 : 456}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ *123 : 456 } { &123 foo, *123 : 456 }
`; `;
exports[`alias-key.yml - yaml-verify 2`] = ` exports[`alias-key.yml - yaml-verify 2`] = `
{*123 : 456} {&123 foo, *123 : 456}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{ *123 : 456 } { &123 foo, *123 : 456 }
`; `;
exports[`alias-key.yml - yaml-verify 3`] = ` exports[`alias-key.yml - yaml-verify 3`] = `
{*123 : 456} {&123 foo, *123 : 456}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{*123 : 456} {&123 foo, *123 : 456}
`; `;
@ -902,3 +902,36 @@ exports[`short-value.yml - yaml-verify 3`] = `
{1: 1, 2: 2, 3: 3} {1: 1, 2: 2, 3: 3}
`; `;
exports[`very-long-value.yml - yaml-verify 1`] = `
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
}
`;
exports[`very-long-value.yml - yaml-verify 2`] = `
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
}
`;
exports[`very-long-value.yml - yaml-verify 3`] = `
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
}
`;

View File

@ -1 +1 @@
{*123 : 456} {&123 foo, *123 : 456}

View File

@ -0,0 +1,3 @@
{
x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
}

View File

@ -1,23 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`alias-key.yml - yaml-verify 1`] = ` exports[`alias-key.yml - yaml-verify 1`] = `
[*123 : 456] [&123 foo, *123 : 456]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[*123 : 456] [&123 foo, *123 : 456]
`; `;
exports[`alias-key.yml - yaml-verify 2`] = ` exports[`alias-key.yml - yaml-verify 2`] = `
[*123 : 456] [&123 foo, *123 : 456]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[*123 : 456] [&123 foo, *123 : 456]
`; `;
exports[`alias-key.yml - yaml-verify 3`] = ` exports[`alias-key.yml - yaml-verify 3`] = `
[*123 : 456] [&123 foo, *123 : 456]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[*123 : 456] [&123 foo, *123 : 456]
`; `;

View File

@ -1 +1 @@
[*123 : 456] [&123 foo, *123 : 456]

View File

@ -13,7 +13,7 @@ aaaaa:
--- ---
aaaaa: aaaaa:
bbbbb bbbbb
... ---
aaaaa: bbbbb aaaaa: bbbbb
`; `;

View File

@ -3445,13 +3445,11 @@ exports[`spec-example-6-21-local-tag-prefix.yml - yaml-verify 1`] = `
!m!light green !m!light green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%TAG !m! !my- %TAG !m! !my-
--- --- # Bulb here
# Bulb here
!m!light fluorescent !m!light fluorescent
... ...
%TAG !m! !my- %TAG !m! !my-
--- --- # Color here
# Color here
!m!light green !m!light green
`; `;
@ -3466,13 +3464,11 @@ exports[`spec-example-6-21-local-tag-prefix.yml - yaml-verify 2`] = `
!m!light green !m!light green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%TAG !m! !my- %TAG !m! !my-
--- --- # Bulb here
# Bulb here
!m!light fluorescent !m!light fluorescent
... ...
%TAG !m! !my- %TAG !m! !my-
--- --- # Color here
# Color here
!m!light green !m!light green
`; `;
@ -4334,7 +4330,6 @@ keep: |+
# Trail # Trail
# comments. # comments.
`; `;
exports[`spec-example-8-5-chomping-trailing-lines.yml - yaml-verify 2`] = ` exports[`spec-example-8-5-chomping-trailing-lines.yml - yaml-verify 2`] = `
@ -4377,7 +4372,6 @@ keep: |+
# Trail # Trail
# comments. # comments.
`; `;
exports[`spec-example-8-6-empty-scalar-chomping.yml - yaml-verify 1`] = ` exports[`spec-example-8-6-empty-scalar-chomping.yml - yaml-verify 1`] = `

View File

@ -5665,11 +5665,11 @@ tryit@^1.0.1:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb" resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb"
tslib@^1.8.0, tslib@^1.9.1: tslib@^1.8.0:
version "1.9.1" version "1.9.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.1.tgz#a5d1f0532a49221c87755cfcc89ca37197242ba7" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.1.tgz#a5d1f0532a49221c87755cfcc89ca37197242ba7"
tslib@^1.9.3: tslib@^1.9.1, tslib@^1.9.3:
version "1.9.3" version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
@ -6119,16 +6119,16 @@ yallist@^3.0.0, yallist@^3.0.2:
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9"
yaml-unist-parser@ikatyang/yaml-unist-parser#cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b: yaml-unist-parser@1.0.0-rc.4:
version "1.0.0-rc.3" version "1.0.0-rc.4"
resolved "https://codeload.github.com/ikatyang/yaml-unist-parser/tar.gz/cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b" resolved "https://registry.yarnpkg.com/yaml-unist-parser/-/yaml-unist-parser-1.0.0-rc.4.tgz#d8fb9c673d59a4f7d532840b120abd6400237014"
dependencies: dependencies:
lines-and-columns "^1.1.6" lines-and-columns "^1.1.6"
tslib "^1.9.1" tslib "^1.9.1"
yaml@ikatyang/yaml#a765c1ee16d6b8a5e715564645f2b85f7e04828b: yaml@1.0.0-rc.8:
version "1.0.0-rc.7" version "1.0.0-rc.8"
resolved "https://codeload.github.com/ikatyang/yaml/tar.gz/a765c1ee16d6b8a5e715564645f2b85f7e04828b" resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.0.0-rc.8.tgz#e5604c52b7b07b16e469bcf875ab0dfe08c50d42"
yargs-parser@^7.0.0: yargs-parser@^7.0.0:
version "7.0.0" version "7.0.0"