Skip to content

Commit

Permalink
YAML: Apply trailingComma option (#9665)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 18, 2020
1 parent 85e5cfa commit 702f543
Show file tree
Hide file tree
Showing 11 changed files with 753 additions and 1 deletion.
46 changes: 46 additions & 0 deletions changelog_unreleased/yaml/pr-9665.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#### Apply `trailingComma` option (#9665 by @fisker)

When `--trailing-comma=none`, should not add trailing comma to flowMapping and flowSequence.

<!-- prettier-ignore -->
```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"
]
```
4 changes: 3 additions & 1 deletion src/language-yaml/printer-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -526,7 +528,7 @@ function _print(node, parentNode, path, options, print) {
"children"
)
),
ifBreak(",", ""),
trailingComma,
hasEndComments(node)
? concat([
hardline,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 '>'"
}
================================================================================
`;
19 changes: 19 additions & 0 deletions tests/yaml/flow-mapping/trailing-comma/flow-mapping.yml
Original file line number Diff line number Diff line change
@@ -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 '>'"
}
4 changes: 4 additions & 0 deletions tests/yaml/flow-mapping/trailing-comma/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -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" });

0 comments on commit 702f543

Please sign in to comment.