Print JSON top comments as leading comments of root node (#3187)

* Make JSON top comments be leading comments of root node

* Update snapshot
This commit is contained in:
Lucas Duailibe authored and Lucas Azzola committed 2017-11-08 09:01:09 +11:00
1 parent 68a99cf4b2
commit a35257bacf
5 files changed
+67 -2

No files matched your search

+6 -1
View File
@@ -159,7 +159,7 @@ function decorateComment(node, comment, text) {
}
}
function attach(comments, ast, text) {
function attach(comments, ast, text, options) {
if (!Array.isArray(comments)) {
return;
}
@@ -167,6 +167,11 @@ function attach(comments, ast, text) {
const tiesToBreak = [];
comments.forEach((comment, i) => {
if (options.parser === "json" && locStart(comment) - locStart(ast) <= 0) {
addLeadingComment(ast, comment);
return;
}
decorateComment(ast, comment, text);
const precedingNode = comment.precedingNode;
@@ -372,6 +372,58 @@ exports[`string.json 2`] = `
`;
exports[`top-block-comment.json 1`] = `
/* comment */{
"foo": "bar"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* comment */ {
"foo": "bar"
}
`;
exports[`top-block-comment.json 2`] = `
/* comment */{
"foo": "bar"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* comment */ {
"foo": "bar"
}
`;
exports[`top-line-comment.json 1`] = `
// comment 1
// comment 2
{
"foo": "bar"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// comment 1
// comment 2
{
"foo": "bar"
}
`;
exports[`top-line-comment.json 2`] = `
// comment 1
// comment 2
{
"foo": "bar"
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// comment 1
// comment 2
{
"foo": "bar"
}
`;
exports[`trailingComma.notjson 1`] = `
{
"k1": "v1",
+3
View File
@@ -0,0 +1,3 @@
/* comment */{
"foo": "bar"
}
+5
View File
@@ -0,0 +1,5 @@
// comment 1
// comment 2
{
"foo": "bar"
}
@@ -1763,8 +1763,8 @@ The options to the configuration file are the same the [API options](#options).
JSON:
\`\`\`json
// .prettierrc
{
// .prettierrc
"printWidth": 100,
"parser": "flow"
}