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

* Make JSON top comments be leading comments of root node

* Update snapshot
master
Lucas Duailibe 2017-11-07 20:01:09 -02:00 committed by Lucas Azzola
parent 68a99cf4b2
commit a35257bacf
5 changed files with 67 additions and 2 deletions

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;

View File

@ -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",

View File

@ -0,0 +1,3 @@
/* comment */{
"foo": "bar"
}

View File

@ -0,0 +1,5 @@
// comment 1
// comment 2
{
"foo": "bar"
}

View File

@ -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"
}