Print JSON top comments as leading comments of root node (#3187)
* Make JSON top comments be leading comments of root node * Update snapshotmaster
parent
68a99cf4b2
commit
a35257bacf
|
@ -159,7 +159,7 @@ function decorateComment(node, comment, text) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function attach(comments, ast, text) {
|
function attach(comments, ast, text, options) {
|
||||||
if (!Array.isArray(comments)) {
|
if (!Array.isArray(comments)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -167,6 +167,11 @@ function attach(comments, ast, text) {
|
||||||
const tiesToBreak = [];
|
const tiesToBreak = [];
|
||||||
|
|
||||||
comments.forEach((comment, i) => {
|
comments.forEach((comment, i) => {
|
||||||
|
if (options.parser === "json" && locStart(comment) - locStart(ast) <= 0) {
|
||||||
|
addLeadingComment(ast, comment);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
decorateComment(ast, comment, text);
|
decorateComment(ast, comment, text);
|
||||||
|
|
||||||
const precedingNode = comment.precedingNode;
|
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`] = `
|
exports[`trailingComma.notjson 1`] = `
|
||||||
{
|
{
|
||||||
"k1": "v1",
|
"k1": "v1",
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
/* comment */{
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
|
@ -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:
|
||||||
|
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
|
// .prettierrc
|
||||||
{
|
{
|
||||||
// .prettierrc
|
|
||||||
"printWidth": 100,
|
"printWidth": 100,
|
||||||
"parser": "flow"
|
"parser": "flow"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue