From db2bc3636dd27df62f80f7ec4ec04eb44640b29c Mon Sep 17 00:00:00 2001 From: Ika Date: Sat, 1 Sep 2018 20:04:37 +0800 Subject: [PATCH] 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) --- package.json | 4 +- .../replace-array-includes-with-indexof.js | 4 +- scripts/build/build.js | 2 +- src/language-yaml/parser-yaml.js | 27 +- src/language-yaml/printer-yaml.js | 366 +++++++++++------- src/language-yaml/utils.js | 86 ++-- .../__snapshots__/jsfmt.spec.js.snap | 6 +- tests/yaml_alias/common.yml | 3 +- .../__snapshots__/jsfmt.spec.js.snap | 3 +- .../__snapshots__/jsfmt.spec.js.snap | 45 ++- tests/yaml_flow_mapping/alias-key.yml | 2 +- tests/yaml_flow_mapping/very-long-value.yml | 3 + .../__snapshots__/jsfmt.spec.js.snap | 12 +- tests/yaml_flow_sequence/alias-key.yml | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 14 +- yarn.lock | 16 +- 17 files changed, 341 insertions(+), 256 deletions(-) create mode 100644 tests/yaml_flow_mapping/very-long-value.yml diff --git a/package.json b/package.json index 5b185800b55b..13845cb99548 100644 --- a/package.json +++ b/package.json @@ -62,8 +62,8 @@ "unicode-regex": "1.0.1", "unified": "6.1.6", "vnopts": "1.0.2", - "yaml": "ikatyang/yaml#a765c1ee16d6b8a5e715564645f2b85f7e04828b", - "yaml-unist-parser": "ikatyang/yaml-unist-parser#cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b" + "yaml": "1.0.0-rc.8", + "yaml-unist-parser": "1.0.0-rc.4" }, "devDependencies": { "@babel/cli": "7.0.0-beta.55", diff --git a/scripts/build/babel-plugins/replace-array-includes-with-indexof.js b/scripts/build/babel-plugins/replace-array-includes-with-indexof.js index c76ff88578be..18514344b78a 100644 --- a/scripts/build/babel-plugins/replace-array-includes-with-indexof.js +++ b/scripts/build/babel-plugins/replace-array-includes-with-indexof.js @@ -19,7 +19,9 @@ module.exports = ({ types: t }) => ({ t.isMemberExpression(callee, { computed: false }) && (t.isArrayExpression(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" }) ) { callee.property.name = "indexOf"; diff --git a/scripts/build/build.js b/scripts/build/build.js index f2e659842d43..3029f1b33ad8 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -98,7 +98,7 @@ async function run(params) { await execa("rm", ["-rf", ".cache"]); } - const bundleCache = new Cache(".cache/", "v5"); + const bundleCache = new Cache(".cache/", "v6"); await bundleCache.load(); console.log(chalk.inverse(" Building packages ")); diff --git a/src/language-yaml/parser-yaml.js b/src/language-yaml/parser-yaml.js index a3ba4f204105..1a5313e5065c 100644 --- a/src/language-yaml/parser-yaml.js +++ b/src/language-yaml/parser-yaml.js @@ -2,7 +2,7 @@ const createError = require("../common/parser-create-error"); const { hasPragma } = require("./pragma"); -const { createNull, defineShortcut, mapNode } = require("./utils"); +const { defineShortcut, mapNode } = require("./utils"); function defineShortcuts(node) { switch (node.type) { @@ -10,11 +10,12 @@ function defineShortcuts(node) { defineShortcut(node, "head", () => node.children[0]); defineShortcut(node, "body", () => node.children[1]); break; + case "documentBody": case "sequenceItem": case "flowSequenceItem": case "mappingKey": case "mappingValue": - defineShortcut(node, "node", () => node.children[0]); + defineShortcut(node, "content", () => node.children[0]); break; case "mappingItem": case "flowMappingItem": @@ -22,25 +23,15 @@ function defineShortcuts(node) { defineShortcut(node, "value", () => node.children[1]); break; } + return node; } function parse(text) { try { - const root = mapNode(require("yaml-unist-parser").parse(text), node => { - // replace explicit empty MappingKey/MappingValue with implicit one - if ( - (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; - }); + const root = mapNode( + require("yaml-unist-parser").parse(text), + defineShortcuts + ); /** * suppress `comment not printed` error @@ -53,7 +44,7 @@ function parse(text) { return root; } catch (error) { // istanbul ignore next - throw error && error.name === "YAMLSyntaxError" + throw error && error.position ? createError(error.message, error.position) : error; } diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index d2e403c1a7cb..420e69459d40 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -7,16 +7,16 @@ const { getFlowScalarLineContents, getLast, getLastDescendantNode, - hasExplicitDocumentEndMarker, hasLeadingComments, hasMiddleComments, - hasTrailingComments, + hasIndicatorComment, + hasTrailingComment, hasEndComments, hasPrettierIgnore, isLastDescendantNode, isNextLineEmpty, isNode, - isBlockValue + isEmptyNode } = require("./utils"); const docBuilders = require("../doc").builders; const { @@ -41,22 +41,18 @@ function genericPrint(path, options, print) { const node = path.getValue(); const parentNode = path.getParentNode(); - const tag = - "tag" in node && node.tag.type !== "null" ? path.call(print, "tag") : ""; - - const anchor = - "anchor" in node && node.anchor.type !== "null" - ? path.call(print, "anchor") - : ""; + const tag = !node.tag ? "" : path.call(print, "tag"); + const anchor = !node.anchor ? "" : path.call(print, "anchor"); const nextEmptyLine = - (node.type === "mapping" || - node.type === "sequence" || - node.type === "comment" || - node.type === "directive" || - node.type === "mappingItem" || - node.type === "sequenceItem") && - !isLastDescendantNode(path) + isNode(node, [ + "mapping", + "sequence", + "comment", + "directive", + "mappingItem", + "sequenceItem" + ]) && !isLastDescendantNode(path) ? printNextEmptyLine(path, options.originalText) : ""; @@ -67,14 +63,11 @@ function genericPrint(path, options, print) { tag, tag && anchor ? " " : "", anchor, - (node.type === "sequence" || node.type === "mapping") && - node.middleComments.length === 0 - ? tag || anchor + tag || anchor + ? isNode(node, ["sequence", "mapping"]) && !hasMiddleComments(node) ? hardline - : "" - : tag || anchor - ? " " - : "", + : " " + : "", hasMiddleComments(node) ? concat([ node.middleComments.length === 1 ? "" : hardline, @@ -88,23 +81,23 @@ function genericPrint(path, options, print) { node.position.end.offset ) : 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( concat([ - " ", + node.type === "mappingValue" && !node.content ? "" : " ", parentNode.type === "mappingKey" && path.getParentNode(2).type === "mapping" && isInlineNode(node) ? "" : breakParent, - join(hardline, path.map(print, "trailingComments")) + path.call(print, "trailingComment") ]) ) : "", nextEmptyLine, - hasEndComments(node) - ? (endComments => - node.type === "sequenceItem" ? align(2, endComments) : endComments)( + hasEndComments(node) && !isNode(node, ["documentHead", "documentBody"]) + ? align( + node.type === "sequenceItem" ? 2 : 0, concat([hardline, join(hardline, path.map(print, "endComments"))]) ) : "" @@ -115,67 +108,95 @@ function _print(node, parentNode, path, options, print) { switch (node.type) { case "root": return concat([ - concat( - path.map( - (childPath, index) => - index === node.children.length - 1 - ? print(childPath) - : concat([ - print(childPath), - hasTrailingComments(node.children[index]) || - (childPath.call(hasPrettierIgnore, "body") && - hasExplicitDocumentEndMarker( - node.children[index], - options.originalText - )) - ? "" - : concat([ - hardline, - node.children[index + 1].head.children.length === 0 - ? "---" - : "..." - ]), - hardline - ]), - "children" - ) + join( + hardline, + path.map((childPath, index) => { + const document = node.children[index]; + const nextDocument = node.children[index + 1]; + return concat([ + print(childPath), + shouldPrintDocumentEndMarker(document, nextDocument) + ? concat([ + hardline, + "...", + hasTrailingComment(document) + ? concat([" ", path.call(print, "trailingComment")]) + : "" + ]) + : !nextDocument || hasTrailingComment(nextDocument.head) + ? "" + : concat([hardline, "---"]) + ]); + }, "children") ), node.children.length === 0 || (lastDescendantNode => - isBlockValue(lastDescendantNode) && + isNode(lastDescendantNode, ["blockLiteral", "blockFolded"]) && lastDescendantNode.chomping === "keep")(getLastDescendantNode(node)) ? "" : hardline ]); - case "document": - return concat([ - node.head.children.length === 0 - ? path.call(print, "body") - : join( - hardline, - [path.call(print, "head"), "---"].concat( - node.body.children.length === 0 ? [] : path.call(print, "body") + case "document": { + const nextDocument = parentNode.children[path.getName() + 1]; + return join( + hardline, + [ + shouldPrintDocumentHeadEndMarker(node, nextDocument) === "head" + ? join( + 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 "documentBody": - return join(hardline, path.map(print, "children")); + return join( + 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": return concat(["%", join(" ", [node.name].concat(node.parameters))]); case "comment": return concat(["#", node.value]); case "alias": return concat(["*", node.value]); - case "null": - return ""; - case "verbatimTag": - return concat(["!<", node.value, ">"]); - case "shorthandTag": - return concat([node.handle, node.suffix]); - case "nonSpecificTag": - return "!"; + case "tag": + return options.originalText.slice( + node.position.start.offset, + node.position.end.offset + ); case "anchor": return concat(["&", node.value]); case "plain": @@ -249,18 +270,16 @@ function _print(node, parentNode, path, options, print) { } case "blockFolded": case "blockLiteral": { - const parentIndent = getAncestorCount( - path, - ancestorNode => - ancestorNode.type === "sequence" || ancestorNode.type === "mapping" + const parentIndent = getAncestorCount(path, ancestorNode => + isNode(ancestorNode, ["sequence", "mapping"]) ); const isLastDescendant = isLastDescendantNode(path); return concat([ node.type === "blockFolded" ? ">" : "|", node.indent === null ? "" : node.indent.toString(), node.chomping === "clip" ? "" : node.chomping === "keep" ? "+" : "-", - hasTrailingComments(node) - ? concat([" ", join(hardline, path.map(print, "trailingComments"))]) + hasIndicatorComment(node) + ? concat([" ", path.call(print, "indicatorComment")]) : "", (node.indent === null ? dedent : dedentToRoot)( align( @@ -275,23 +294,17 @@ function _print(node, parentNode, path, options, print) { }).reduce( (reduced, lineWords, index, lineContents) => reduced.concat( - index === 0 - ? hardline - : lineContents[index - 1].length === 0 - ? hardline - : index === lineContents.length - 1 && - lineWords.length === 0 - ? dedentToRoot(literalline) - : markAsRoot(literalline), + index === 0 ? hardline : "", fill(join(line, lineWords).parts), - index === lineContents.length - 1 && - node.chomping === "keep" && - isLastDescendant - ? lineWords.length === 0 || - !getLast(lineWords).endsWith(" ") - ? dedentToRoot(hardline) - : dedentToRoot(literalline) - : [] + index !== lineContents.length - 1 + ? lineWords.length === 0 + ? hardline + : markAsRoot(literalline) + : node.chomping === "keep" && isLastDescendant + ? lineWords.length === 0 + ? dedentToRoot(hardline) + : dedentToRoot(literalline) + : "" ), [] ) @@ -303,35 +316,37 @@ function _print(node, parentNode, path, options, print) { case "sequence": return join(hardline, path.map(print, "children")); case "sequenceItem": - return concat(["- ", align(2, path.call(print, "node"))]); + return concat([ + "- ", + align(2, !node.content ? "" : path.call(print, "content")) + ]); case "mappingKey": - return path.call(print, "node"); + return !node.content ? "" : path.call(print, "content"); case "mappingValue": - return path.call(print, "node"); + return !node.content ? "" : path.call(print, "content"); case "mapping": return join(hardline, path.map(print, "children")); case "mappingItem": case "flowMappingItem": { - if (node.key.type === "null" && node.value.type === "null") { - return concat([":", line]); + const isEmptyMappingKey = isEmptyNode(node.key); + const isEmptyMappingValue = isEmptyNode(node.value); + + if (isEmptyMappingKey && isEmptyMappingValue) { + return concat([": "]); } const key = path.call(print, "key"); const value = path.call(print, "value"); - if (node.value.type === "null") { + if (isEmptyMappingValue) { return node.type === "flowMappingItem" && - path.getParentNode().type !== "flowSequence" + parentNode.type === "flowMapping" ? key : node.type === "mappingItem" && - node.key.type !== "null" && - isAbsolutelyPrintedAsSingleLineNode(node.key.node, options) && - !hasTrailingComments(node.key.node) && - !( - parentNode.tag.type === "shorthandTag" && - parentNode.tag.handle === "!!" && - parentNode.tag.suffix === "set" - ) + isAbsolutelyPrintedAsSingleLineNode(node.key.content, options) && + !hasTrailingComment(node.key.content) && + (!parentNode.tag || + parentNode.tag.value !== "tag:yaml.org,2002:set") ? concat([ key, needsSpaceInFrontOfMappingValue(node) ? " " : "", @@ -340,19 +355,15 @@ function _print(node, parentNode, path, options, print) { : concat(["? ", align(2, key)]); } - if (node.key.type === "null") { - return concat([ - ":", - node.value.node.type === "null" ? "" : " ", - align(2, value) - ]); + if (isEmptyMappingKey) { + return concat([": ", align(2, value)]); } const groupId = Symbol("mappingKey"); const forceExplicitKey = - hasLeadingComments(node.value) || - (node.key.type !== "null" && !isInlineNode(node.key.node)); + hasLeadingComments(node.value) || !isInlineNode(node.key.content); + return forceExplicitKey ? concat([ "? ", @@ -368,15 +379,15 @@ function _print(node, parentNode, path, options, print) { align(2, value) ]) : // force singleline - isSingleLineNode(node.key.node) && - !hasLeadingComments(node.key.node) && - !hasMiddleComments(node.key.node) && - !hasTrailingComments(node.key.node) && + isSingleLineNode(node.key.content) && + !hasLeadingComments(node.key.content) && + !hasMiddleComments(node.key.content) && + !hasTrailingComment(node.key.content) && !hasEndComments(node.key) && - !hasLeadingComments(node.value.node) && - !hasMiddleComments(node.value.node) && + !hasLeadingComments(node.value.content) && + !hasMiddleComments(node.value.content) && !hasEndComments(node.value) && - isAbsolutelyPrintedAsSingleLineNode(node.value.node, options) + isAbsolutelyPrintedAsSingleLineNode(node.value.content, options) ? concat([ key, needsSpaceInFrontOfMappingValue(node) ? " " : "", @@ -394,18 +405,18 @@ function _print(node, parentNode, path, options, print) { concat([ needsSpaceInFrontOfMappingValue(node) ? " " : "", ":", - hasLeadingComments(node.value.node) || + hasLeadingComments(node.value.content) || (hasEndComments(node.value) && - node.value.node.type !== "null") || + node.value.content && + !isNode(node.value.content, ["mapping", "sequence"])) || (parentNode.type === "mapping" && - hasTrailingComments(node.key.node) && - isInlineNode(node.value.node)) || - ((node.value.node.type === "mapping" || - node.value.node.type === "sequence") && - node.value.node.tag.type === "null" && - node.value.node.anchor.type === "null") + hasTrailingComment(node.key.content) && + isInlineNode(node.value.content)) || + (isNode(node.value.content, ["mapping", "sequence"]) && + node.value.content.tag === null && + node.value.content.anchor === null) ? hardline - : node.value.node.type === "null" + : !node.value.content ? "" : line, value @@ -430,8 +441,8 @@ function _print(node, parentNode, path, options, print) { node.children.length !== 0 && (lastItem => lastItem.type === "flowMappingItem" && - lastItem.key.type === "null" && - lastItem.value.type === "null")(getLast(node.children)); + isEmptyNode(lastItem.key) && + isEmptyNode(lastItem.value))(getLast(node.children)); return concat([ openMarker, indent( @@ -467,7 +478,7 @@ function _print(node, parentNode, path, options, print) { ]); } case "flowSequenceItem": - return path.call(print, "node"); + return path.call(print, "content"); // istanbul ignore next default: throw new Error(`Unexpected node type ${node.type}`); @@ -485,6 +496,10 @@ function align(n, doc) { } function isInlineNode(node) { + if (!node) { + return true; + } + switch (node.type) { case "plain": case "quoteDouble": @@ -492,7 +507,6 @@ function isInlineNode(node) { case "alias": case "flowMapping": case "flowSequence": - case "null": return true; default: return false; @@ -500,6 +514,10 @@ function isInlineNode(node) { } function isSingleLineNode(node) { + if (!node) { + return true; + } + switch (node.type) { case "plain": 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) { + if (!node) { + return true; + } + switch (node.type) { case "plain": case "quoteSingle": @@ -552,14 +627,7 @@ function isAbsolutelyPrintedAsSingleLineNode(node, options) { } function needsSpaceInFrontOfMappingValue(node) { - // istanbul ignore else - if (node.key.type !== "null") { - switch (node.key.node.type) { - case "alias": - return true; - } - } - return false; + return node.key.content && node.key.content.type === "alias"; } function printNextEmptyLine(path, originalText) { diff --git a/src/language-yaml/utils.js b/src/language-yaml/utils.js index e0cf6c8cd33f..dbaca57dda04 100644 --- a/src/language-yaml/utils.js +++ b/src/language-yaml/utils.js @@ -16,8 +16,16 @@ function getAncestorCount(path, filter) { 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) { @@ -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) { let newlineCount = 0; const textLength = text.length; @@ -76,12 +74,12 @@ function isLastDescendantNode(path) { const node = path.getValue(); switch (node.type) { + case "tag": + case "anchor": case "comment": - case "verbatimTag": - case "shorthandTag": - case "nonSpecificTag": return false; } + const pathStackLength = path.stack.length; for (let i = 1; i < pathStackLength; i++) { @@ -116,53 +114,48 @@ function hasPrettierIgnore(path) { if (node.type === "documentBody") { const document = path.getParentNode(); return ( - document.head.children.length !== 0 && - (lastItem => lastItem.type === "comment" && isPrettierIgnore(lastItem))( - getLast(document.head.children) - ) + hasEndComments(document.head) && + isPrettierIgnore(getLast(document.head.endComments)) ); } return ( - "leadingComments" in node && - node.leadingComments.length !== 0 && - isPrettierIgnore(getLast(node.leadingComments)) + hasLeadingComments(node) && isPrettierIgnore(getLast(node.leadingComments)) ); } -function hasExplicitDocumentEndMarker(document, text) { - return ( - text.slice( - document.position.end.offset - 4, - document.position.end.offset - ) === "\n..." - ); +function isEmptyNode(node) { + return (!node.children || node.children.length === 0) && !hasComments(node); } -function isBlockValue(node) { - switch (node.type) { - case "blockFolded": - case "blockLiteral": - return true; - default: - return false; - } +function hasComments(node) { + return ( + hasLeadingComments(node) || + hasMiddleComments(node) || + hasIndicatorComment(node) || + hasTrailingComment(node) || + hasEndComments(node) + ); } function hasLeadingComments(node) { - return "leadingComments" in node && node.leadingComments.length !== 0; + return node && node.leadingComments && node.leadingComments.length !== 0; } function hasMiddleComments(node) { - return "middleComments" in node && node.middleComments.length !== 0; + return node && node.middleComments && node.middleComments.length !== 0; +} + +function hasIndicatorComment(node) { + return node && node.indicatorComment; } -function hasTrailingComments(node) { - return "trailingComments" in node && node.trailingComments.length !== 0; +function hasTrailingComment(node) { + return node && node.trailingComment; } 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, getAncestorCount, isNode, - isBlockValue, + isEmptyNode, mapNode, defineShortcut, - createNull, isNextLineEmpty, isLastDescendantNode, getBlockValueLineContents, @@ -349,7 +341,7 @@ module.exports = { hasPrettierIgnore, hasLeadingComments, hasMiddleComments, - hasTrailingComments, - hasEndComments, - hasExplicitDocumentEndMarker + hasIndicatorComment, + hasTrailingComment, + hasEndComments }; diff --git a/tests/yaml_alias/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_alias/__snapshots__/jsfmt.spec.js.snap index d36639ccd084..636491148eb1 100644 --- a/tests/yaml_alias/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_alias/__snapshots__/jsfmt.spec.js.snap @@ -1,8 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`common.yml - yaml-verify 1`] = ` -*abc +- &abc a +- *abc ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -*abc +- &abc a +- *abc `; diff --git a/tests/yaml_alias/common.yml b/tests/yaml_alias/common.yml index cd06d36f1807..c1687c2e837f 100644 --- a/tests/yaml_alias/common.yml +++ b/tests/yaml_alias/common.yml @@ -1 +1,2 @@ -*abc +- &abc a +- *abc diff --git a/tests/yaml_document/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_document/__snapshots__/jsfmt.spec.js.snap index 4f02bd829e6f..b6bb97a74d74 100644 --- a/tests/yaml_document/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_document/__snapshots__/jsfmt.spec.js.snap @@ -50,8 +50,7 @@ e # --- f ---- -# +--- # g `; diff --git a/tests/yaml_flow_mapping/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_flow_mapping/__snapshots__/jsfmt.spec.js.snap index d182a8615c18..2251c5a1862a 100644 --- a/tests/yaml_flow_mapping/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_flow_mapping/__snapshots__/jsfmt.spec.js.snap @@ -1,23 +1,23 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP 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`] = ` -{*123 : 456} +{&123 foo, *123 : 456} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -{ *123 : 456 } +{ &123 foo, *123 : 456 } `; 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} `; + +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, +} + +`; diff --git a/tests/yaml_flow_mapping/alias-key.yml b/tests/yaml_flow_mapping/alias-key.yml index ab4a8d77022a..fa510b20255e 100644 --- a/tests/yaml_flow_mapping/alias-key.yml +++ b/tests/yaml_flow_mapping/alias-key.yml @@ -1 +1 @@ -{*123 : 456} +{&123 foo, *123 : 456} diff --git a/tests/yaml_flow_mapping/very-long-value.yml b/tests/yaml_flow_mapping/very-long-value.yml new file mode 100644 index 000000000000..c93ac873696d --- /dev/null +++ b/tests/yaml_flow_mapping/very-long-value.yml @@ -0,0 +1,3 @@ +{ +x: 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 +} diff --git a/tests/yaml_flow_sequence/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_flow_sequence/__snapshots__/jsfmt.spec.js.snap index 9c4635c9bec4..fb98eea4f269 100644 --- a/tests/yaml_flow_sequence/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_flow_sequence/__snapshots__/jsfmt.spec.js.snap @@ -1,23 +1,23 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP 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`] = ` -[*123 : 456] +[&123 foo, *123 : 456] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[*123 : 456] +[&123 foo, *123 : 456] `; exports[`alias-key.yml - yaml-verify 3`] = ` -[*123 : 456] +[&123 foo, *123 : 456] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[*123 : 456] +[&123 foo, *123 : 456] `; diff --git a/tests/yaml_flow_sequence/alias-key.yml b/tests/yaml_flow_sequence/alias-key.yml index 9241a61bff95..c9c6174124b9 100644 --- a/tests/yaml_flow_sequence/alias-key.yml +++ b/tests/yaml_flow_sequence/alias-key.yml @@ -1 +1 @@ -[*123 : 456] +[&123 foo, *123 : 456] diff --git a/tests/yaml_prettier_ignore/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_prettier_ignore/__snapshots__/jsfmt.spec.js.snap index 1b42cf42541e..840655368560 100644 --- a/tests/yaml_prettier_ignore/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_prettier_ignore/__snapshots__/jsfmt.spec.js.snap @@ -13,7 +13,7 @@ aaaaa: --- aaaaa: bbbbb -... +--- aaaaa: bbbbb `; diff --git a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap index 816e9061ae07..6ada0df4e82f 100644 --- a/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap @@ -3445,13 +3445,11 @@ exports[`spec-example-6-21-local-tag-prefix.yml - yaml-verify 1`] = ` !m!light green ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %TAG !m! !my- ---- -# Bulb here +--- # Bulb here !m!light fluorescent ... %TAG !m! !my- ---- -# Color here +--- # Color here !m!light green `; @@ -3466,13 +3464,11 @@ exports[`spec-example-6-21-local-tag-prefix.yml - yaml-verify 2`] = ` !m!light green ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %TAG !m! !my- ---- -# Bulb here +--- # Bulb here !m!light fluorescent ... %TAG !m! !my- ---- -# Color here +--- # Color here !m!light green `; @@ -4334,7 +4330,6 @@ keep: |+ # Trail # comments. - `; exports[`spec-example-8-5-chomping-trailing-lines.yml - yaml-verify 2`] = ` @@ -4377,7 +4372,6 @@ keep: |+ # Trail # comments. - `; exports[`spec-example-8-6-empty-scalar-chomping.yml - yaml-verify 1`] = ` diff --git a/yarn.lock b/yarn.lock index 34052b7aa273..e6c4b67dc2d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5665,11 +5665,11 @@ tryit@^1.0.1: version "1.0.3" 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" 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" 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" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9" -yaml-unist-parser@ikatyang/yaml-unist-parser#cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b: - version "1.0.0-rc.3" - resolved "https://codeload.github.com/ikatyang/yaml-unist-parser/tar.gz/cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b" +yaml-unist-parser@1.0.0-rc.4: + version "1.0.0-rc.4" + resolved "https://registry.yarnpkg.com/yaml-unist-parser/-/yaml-unist-parser-1.0.0-rc.4.tgz#d8fb9c673d59a4f7d532840b120abd6400237014" dependencies: lines-and-columns "^1.1.6" tslib "^1.9.1" -yaml@ikatyang/yaml#a765c1ee16d6b8a5e715564645f2b85f7e04828b: - version "1.0.0-rc.7" - resolved "https://codeload.github.com/ikatyang/yaml/tar.gz/a765c1ee16d6b8a5e715564645f2b85f7e04828b" +yaml@1.0.0-rc.8: + version "1.0.0-rc.8" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.0.0-rc.8.tgz#e5604c52b7b07b16e469bcf875ab0dfe08c50d42" yargs-parser@^7.0.0: version "7.0.0"