From b79cfcf345ce81b868a14bd14eb6e114fa953df2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 21 Nov 2025 23:09:13 +0800 Subject: [PATCH] Update `yaml-unist-parser` to v2.1 (#18295) --- package.json | 2 +- src/language-yaml/clean.js | 24 +++++ src/language-yaml/printer-yaml.js | 87 ++++++++----------- src/language-yaml/utils.js | 4 +- .../root/__snapshots__/format.test.js.snap | 12 +++ tests/format/yaml/root/format.test.js | 2 +- yarn.lock | 10 +-- 7 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 src/language-yaml/clean.js diff --git a/package.json b/package.json index 26618bedf..637a4f2b8 100644 --- a/package.json +++ b/package.json @@ -120,7 +120,7 @@ "vnopts": "2.0.3", "wcwidth.js": "2.0.0", "yaml": "1.10.2", - "yaml-unist-parser": "2.0.5" + "yaml-unist-parser": "2.1.1" }, "devDependencies": { "@babel/generator": "8.0.0-beta.3", diff --git a/src/language-yaml/clean.js b/src/language-yaml/clean.js new file mode 100644 index 000000000..9e6df5684 --- /dev/null +++ b/src/language-yaml/clean.js @@ -0,0 +1,24 @@ +import { isPragma } from "./pragma.js"; + +function clean(original, cloned /* , parent */) { + switch (original.type) { + case "comment": + // insert pragma + if (isPragma(original.value)) { + return null; + } + break; + case "quoteDouble": + case "quoteSingle": + cloned.type = "quote"; + break; + case "document": + // We may insert explicit start/end mark + delete cloned.directivesEndMarker; + delete cloned.documentEndMarker; + break; + } +} +clean.ignoredProperties = new Set(["position"]); + +export default clean; diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index a8646af1d..edc608133 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -12,10 +12,11 @@ import { } from "../document/index.js"; import isPreviousLineEmpty from "../utils/is-previous-line-empty.js"; import UnexpectedNodeError from "../utils/unexpected-node-error.js"; +import clean from "./clean.js"; import embed from "./embed.js"; import getVisitorKeys from "./get-visitor-keys.js"; import { locStart } from "./loc.js"; -import { insertPragma, isPragma } from "./pragma.js"; +import { insertPragma } from "./pragma.js"; import printBlock from "./print/block.js"; import { printFlowMapping, @@ -154,7 +155,7 @@ function printNode(path, options, print) { parts.push(hardline); } parts.push(print()); - if (shouldPrintDocumentEndMarker(document, nextDocument)) { + if (shouldPrintDocumentEndMarker(path)) { parts.push(hardline, "..."); if (hasTrailingComment(document)) { parts.push(" ", print("trailingComment")); @@ -175,7 +176,7 @@ function printNode(path, options, print) { } case "document": { const parts = []; - if (shouldPrintDocumentHeadEndMarker(path, options) === "head") { + if (shouldPrintDocumentHeadEndMarker(path)) { if (node.head.children.length > 0 || node.head.endComments.length > 0) { parts.push(print("head")); } @@ -341,39 +342,46 @@ function shouldPrintDocumentBody(document) { return document.body.children.length > 0 || hasEndComments(document.body); } -function shouldPrintDocumentEndMarker(document, nextDocument) { +function shouldPrintDocumentEndMarker(path) { + const document = path.node; + + /** + *... # trailingComment + */ + if (hasTrailingComment(document)) { + return true; + } + + if (path.isLast) { + return false; + } + + const nextDocument = path.next; + return ( /** - *... # trailingComment + * ... + * %DIRECTIVE + * --- */ - hasTrailingComment(document) || - (nextDocument && - /** - * ... - * %DIRECTIVE - * --- - */ - (nextDocument.head.children.length > 0 || - /** - * ... - * # endComment - * --- - */ - hasEndComments(nextDocument.head))) + nextDocument.head.children.length > 0 || + /** + * ... + * # endComment + * --- + */ + hasEndComments(nextDocument.head) ); } -function shouldPrintDocumentHeadEndMarker(path, options) { +function shouldPrintDocumentHeadEndMarker(path) { const document = path.node; - if ( + return ( /** * --- * preserve the first document head end marker */ - (path.isFirst && - /---(?:\s|$)/u.test( - options.originalText.slice(locStart(document), locStart(document) + 4), - )) || + (path.isFirst && document.directivesEndMarker) || /** * %DIRECTIVE * --- @@ -388,16 +396,7 @@ function shouldPrintDocumentHeadEndMarker(path, options) { * --- # trailing comment */ hasTrailingComment(document.head) - ) { - return "head"; - } - - const nextDocument = path.next; - if (shouldPrintDocumentEndMarker(document, nextDocument)) { - return false; - } - - return nextDocument ? "root" : false; + ); } function printFlowScalarContent(nodeType, content, options) { @@ -408,24 +407,6 @@ function printFlowScalarContent(nodeType, content, options) { ); } -function clean(original, cloned /* , parent */) { - if (isNode(original)) { - switch (original.type) { - case "comment": - // insert pragma - if (isPragma(original.value)) { - return null; - } - break; - case "quoteDouble": - case "quoteSingle": - cloned.type = "quote"; - break; - } - } -} -clean.ignoredProperties = new Set(["position"]); - const printer = { preprocess, embed, diff --git a/src/language-yaml/utils.js b/src/language-yaml/utils.js index edc3ebfc6..9632ff9ea 100644 --- a/src/language-yaml/utils.js +++ b/src/language-yaml/utils.js @@ -5,9 +5,7 @@ import isNonEmptyArray from "../utils/is-non-empty-array.js"; * @param {string[]=} types */ function isNode(value, types) { - return ( - typeof value?.type === "string" && (!types || types.includes(value.type)) - ); + return typeof value?.type === "string" && types.includes(value.type); } function mapNode(node, callback, parent) { diff --git a/tests/format/yaml/root/__snapshots__/format.test.js.snap b/tests/format/yaml/root/__snapshots__/format.test.js.snap index 994467076..3c0e3b7ec 100644 --- a/tests/format/yaml/root/__snapshots__/format.test.js.snap +++ b/tests/format/yaml/root/__snapshots__/format.test.js.snap @@ -482,3 +482,15 @@ foobar: !sexy ================================================================================ `; + +exports[`snippet: #0 format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== + +=====================================output===================================== + +================================================================================ +`; diff --git a/tests/format/yaml/root/format.test.js b/tests/format/yaml/root/format.test.js index 49e3f4bbf..066a91488 100644 --- a/tests/format/yaml/root/format.test.js +++ b/tests/format/yaml/root/format.test.js @@ -1 +1 @@ -runFormatTest(import.meta, ["yaml"]); +runFormatTest({ importMeta: import.meta, snippets: [""] }, ["yaml"]); diff --git a/yarn.lock b/yarn.lock index 88462bc20..134159b9f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7389,7 +7389,7 @@ __metadata: vnopts: "npm:2.0.3" wcwidth.js: "npm:2.0.0" yaml: "npm:1.10.2" - yaml-unist-parser: "npm:2.0.5" + yaml-unist-parser: "npm:2.1.1" bin: prettier: ./bin/prettier.cjs languageName: unknown @@ -8989,12 +8989,12 @@ __metadata: languageName: node linkType: hard -"yaml-unist-parser@npm:2.0.5": - version: 2.0.5 - resolution: "yaml-unist-parser@npm:2.0.5" +"yaml-unist-parser@npm:2.1.1": + version: 2.1.1 + resolution: "yaml-unist-parser@npm:2.1.1" dependencies: yaml: "npm:^1.10.2" - checksum: 10/e8b820e7ecc8157ba105b73c1c6d54dda49cfb7ef0dc234c38d1c5d0e57dc83ca04abe70f99171b42709c39cf157f6bbbaeffc5f5631615576390b97a73c9729 + checksum: 10/683513f5f7e69c2d42408d5316a5a50031c42cb3dbfd98809c8746c29350f7a6116c55532df4212146642d33b5abedfba0be5775304ce477c5d56dfc1204ee60 languageName: node linkType: hard