YAML: Apply trailingComma option (#9665)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-11-18 09:05:56 +08:00
1 parent 85e5cfaf2f
commit 702f543635
11 files changed
+753 -1

No files matched your search

+46
View File
@@ -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"
]
```
+3 -1
View File
@@ -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,
@@ -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 '>'"
}
================================================================================
`;
@@ -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 '>'"
}
@@ -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,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 '>'"
]
================================================================================
`;
@@ -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 '>'"
]
@@ -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,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 '>'",
],
]
================================================================================
`;
@@ -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" });
+26
View File
@@ -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 '>'"
]
]