From 702f543635d26ae9547e967c2ed43e998e5ff1eb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 18 Nov 2020 09:05:56 +0800 Subject: [PATCH] YAML: Apply `trailingComma` option (#9665) --- changelog_unreleased/yaml/pr-9665.md | 46 ++++ src/language-yaml/printer-yaml.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 184 +++++++++++++ .../trailing-comma/flow-mapping.yml | 19 ++ .../flow-mapping/trailing-comma/jsfmt.spec.js | 4 + .../__snapshots__/jsfmt.spec.js.snap | 192 +++++++++++++ .../trailing-comma/flow-sequence.yml | 19 ++ .../trailing-comma/jsfmt.spec.js | 4 + .../__snapshots__/jsfmt.spec.js.snap | 252 ++++++++++++++++++ tests/yaml/json/trailing-comma/jsfmt.spec.js | 4 + tests/yaml/json/trailing-comma/json.yml | 26 ++ 11 files changed, 753 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/yaml/pr-9665.md create mode 100644 tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml create mode 100644 tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js create mode 100644 tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml create mode 100644 tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js create mode 100644 tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/yaml/json/trailing-comma/jsfmt.spec.js create mode 100644 tests/yaml/json/trailing-comma/json.yml diff --git a/changelog_unreleased/yaml/pr-9665.md b/changelog_unreleased/yaml/pr-9665.md new file mode 100644 index 000000000..5e9990203 --- /dev/null +++ b/changelog_unreleased/yaml/pr-9665.md @@ -0,0 +1,46 @@ +#### Apply `trailingComma` option (#9665 by @fisker) + +When `--trailing-comma=none`, should not add trailing comma to flowMapping and flowSequence. + + +```yaml +# Input +flow-mapping: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } +flow-sequence: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +# Prettier stable +mapping: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + } +flow-sequence: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ] + +# Prettier master +flow-mapping: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } +flow-sequence: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here" + ] +``` diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index fa13a5081..4177c3b3e 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -499,6 +499,8 @@ function _print(node, parentNode, path, options, print) { lastItem.type === "flowMappingItem" && isEmptyNode(lastItem.key) && isEmptyNode(lastItem.value))(getLast(node.children)); + const trailingComma = + options.trailingComma === "none" ? "" : ifBreak(",", ""); return concat([ openMarker, indent( @@ -526,7 +528,7 @@ function _print(node, parentNode, path, options, print) { "children" ) ), - ifBreak(",", ""), + trailingComma, hasEndComments(node) ? concat([ hardline, diff --git a/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..2a5515d04 --- /dev/null +++ b/tests/yaml/flow-mapping/trailing-comma/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,184 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`flow-mapping.yml - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + } + +--- + +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +=====================================output===================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + } + +expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" } + +--- +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +================================================================================ +`; + +exports[`flow-mapping.yml - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + } + +--- + +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +=====================================output===================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + } + +expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" } + +--- +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +================================================================================ +`; + +exports[`flow-mapping.yml - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + } + +--- + +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +=====================================output===================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" } + +--- +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +================================================================================ +`; + +exports[`flow-mapping.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + } + +--- + +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +=====================================output===================================== +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + } + +expected: { "object-fits-within-print-width": "", "TEST": "comma NOT here" } + +--- +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } + +================================================================================ +`; diff --git a/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml b/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml new file mode 100644 index 000000000..103e9c803 --- /dev/null +++ b/tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml @@ -0,0 +1,19 @@ +failing: + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + } + +expected: + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + } + +--- + +does not suffice: > + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + } diff --git a/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js b/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js new file mode 100644 index 000000000..6bdc1ebe6 --- /dev/null +++ b/tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js @@ -0,0 +1,4 @@ +run_spec(__dirname, ["yaml"]); +run_spec(__dirname, ["yaml"], { trailingComma: "none" }); +run_spec(__dirname, ["yaml"], { trailingComma: "es5" }); +run_spec(__dirname, ["yaml"], { trailingComma: "all" }); diff --git a/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..a21a27c5b --- /dev/null +++ b/tests/yaml/flow-sequence/trailing-comma/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,192 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`flow-sequence.yml - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +failing: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +expected: + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ] + +--- + +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +=====================================output===================================== +failing: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ] + +expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"] + +--- +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +================================================================================ +`; + +exports[`flow-sequence.yml - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +failing: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +expected: + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ] + +--- + +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +=====================================output===================================== +failing: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ] + +expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"] + +--- +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +================================================================================ +`; + +exports[`flow-sequence.yml - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +failing: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +expected: + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ] + +--- + +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +=====================================output===================================== +failing: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here" + ] + +expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"] + +--- +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +================================================================================ +`; + +exports[`flow-sequence.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +failing: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +expected: + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ] + +--- + +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +=====================================output===================================== +failing: + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ] + +expected: ["object-fits-within-print-width", "", "TEST", "comma NOT here"] + +--- +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] + +================================================================================ +`; diff --git a/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml b/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml new file mode 100644 index 000000000..769ff94ba --- /dev/null +++ b/tests/yaml/flow-sequence/trailing-comma/flow-sequence.yml @@ -0,0 +1,19 @@ +failing: + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ] + +expected: + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ] + +--- + +does not suffice: > + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] diff --git a/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js b/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js new file mode 100644 index 000000000..6bdc1ebe6 --- /dev/null +++ b/tests/yaml/flow-sequence/trailing-comma/jsfmt.spec.js @@ -0,0 +1,4 @@ +run_spec(__dirname, ["yaml"]); +run_spec(__dirname, ["yaml"], { trailingComma: "none" }); +run_spec(__dirname, ["yaml"], { trailingComma: "es5" }); +run_spec(__dirname, ["yaml"], { trailingComma: "all" }); diff --git a/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap b/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..9580a7a6b --- /dev/null +++ b/tests/yaml/json/trailing-comma/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,252 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`json.yml - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ], + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ], + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] +] + +=====================================output===================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + }, + { "object-fits-within-print-width": "", "TEST": "comma NOT here" }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'", + }, + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ], + ["object-fits-within-print-width", "", "TEST", "comma NOT here"], + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma NOT here — but object's now a string due to '>'", + ], +] + +================================================================================ +`; + +exports[`json.yml - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ], + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ], + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] +] + +=====================================output===================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + }, + { "object-fits-within-print-width": "", "TEST": "comma NOT here" }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'", + }, + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ], + ["object-fits-within-print-width", "", "TEST", "comma NOT here"], + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma NOT here — but object's now a string due to '>'", + ], +] + +================================================================================ +`; + +exports[`json.yml - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ], + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ], + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] +] + +=====================================output===================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { "object-fits-within-print-width": "", "TEST": "comma NOT here" }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here" + ], + ["object-fits-within-print-width", "", "TEST", "comma NOT here"], + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma NOT here — but object's now a string due to '>'" + ] +] + +================================================================================ +`; + +exports[`json.yml format 1`] = ` +====================================options===================================== +parsers: ["yaml"] +printWidth: 80 + | printWidth +=====================================input====================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ], + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ], + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] +] + +=====================================output===================================== +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here", + }, + { "object-fits-within-print-width": "", "TEST": "comma NOT here" }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'", + }, + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma IS added here", + ], + ["object-fits-within-print-width", "", "TEST", "comma NOT here"], + [ + "object-does-not-fit-within-print-width", + "------", + "TEST", + "comma NOT here — but object's now a string due to '>'", + ], +] + +================================================================================ +`; diff --git a/tests/yaml/json/trailing-comma/jsfmt.spec.js b/tests/yaml/json/trailing-comma/jsfmt.spec.js new file mode 100644 index 000000000..6bdc1ebe6 --- /dev/null +++ b/tests/yaml/json/trailing-comma/jsfmt.spec.js @@ -0,0 +1,4 @@ +run_spec(__dirname, ["yaml"]); +run_spec(__dirname, ["yaml"], { trailingComma: "none" }); +run_spec(__dirname, ["yaml"], { trailingComma: "es5" }); +run_spec(__dirname, ["yaml"], { trailingComma: "all" }); diff --git a/tests/yaml/json/trailing-comma/json.yml b/tests/yaml/json/trailing-comma/json.yml new file mode 100644 index 000000000..c317d3820 --- /dev/null +++ b/tests/yaml/json/trailing-comma/json.yml @@ -0,0 +1,26 @@ +[ + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma IS added here" + }, + { + "object-fits-within-print-width": "", + "TEST": "comma NOT here" + }, + { + "object-does-not-fit-within-print-width": "------", + "TEST": "comma NOT here — but object's now a string due to '>'" + }, + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma IS added here" + ], + [ + "object-fits-within-print-width", "", + "TEST", "comma NOT here" + ], + [ + "object-does-not-fit-within-print-width", "------", + "TEST", "comma NOT here — but object's now a string due to '>'" + ] +]