From a35257bacfcbffb587dc0b2d65f1f325a4f44f91 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Tue, 7 Nov 2017 20:01:09 -0200 Subject: [PATCH] Print JSON top comments as leading comments of root node (#3187) * Make JSON top comments be leading comments of root node * Update snapshot --- src/comments.js | 7 ++- tests/json/__snapshots__/jsfmt.spec.js.snap | 52 +++++++++++++++++++ tests/json/top-block-comment.json | 3 ++ tests/json/top-line-comment.json | 5 ++ .../markdown/__snapshots__/jsfmt.spec.js.snap | 2 +- 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/json/top-block-comment.json create mode 100644 tests/json/top-line-comment.json diff --git a/src/comments.js b/src/comments.js index 2d1345468..a3da1b029 100644 --- a/src/comments.js +++ b/src/comments.js @@ -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; diff --git a/tests/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/__snapshots__/jsfmt.spec.js.snap index 93e1dc130..215517433 100644 --- a/tests/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/__snapshots__/jsfmt.spec.js.snap @@ -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", diff --git a/tests/json/top-block-comment.json b/tests/json/top-block-comment.json new file mode 100644 index 000000000..5b9ee9c27 --- /dev/null +++ b/tests/json/top-block-comment.json @@ -0,0 +1,3 @@ +/* comment */{ + "foo": "bar" +} diff --git a/tests/json/top-line-comment.json b/tests/json/top-line-comment.json new file mode 100644 index 000000000..3d3867f96 --- /dev/null +++ b/tests/json/top-line-comment.json @@ -0,0 +1,5 @@ +// comment 1 +// comment 2 +{ + "foo": "bar" +} diff --git a/tests/markdown/__snapshots__/jsfmt.spec.js.snap b/tests/markdown/__snapshots__/jsfmt.spec.js.snap index 708155020..1d3d00c8c 100644 --- a/tests/markdown/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown/__snapshots__/jsfmt.spec.js.snap @@ -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" }