Skip to content

Commit

Permalink
Allow formatting ranges of JSON (prettier#2298)
Browse files Browse the repository at this point in the history
* Add test for prettier#2297

* Add JSON types to isSourceElement()

See prettier#2297

* Avoid expanding format range when possible

This fixes prettier#2297

* Move JSON range test into tests/range_json

This is so that the AST_COMPARE=1 tests don't try to parse JSON with Flow.

* Check parser in isSourceElement()

Otherwise JSON formatting would format weird things inside of JS.
See prettier#2298 (comment)

* Use arrow functions instead of .bind()

See prettier#2298 (comment)

* Add test of range-formatting JSON identifier

See https://github.com/prettier/prettier/pull/2298/files#r124410750

* Allow range-formatting JSON identifier

See https://github.com/prettier/prettier/pull/2298/files#r124410750

* Fix lint
  • Loading branch information
josephfrazier authored and vjeux committed Jun 27, 2017
1 parent 5f063ab commit b47c0b4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ function findSiblingAncestors(startNodeAndParents, endNodeAndParents) {
let resultStartNode = startNodeAndParents.node;
let resultEndNode = endNodeAndParents.node;

if (resultStartNode === resultEndNode) {
return {
startNode: resultStartNode,
endNode: resultEndNode
};
}

for (const endParent of endNodeAndParents.parentNodes) {
if (
endParent.type !== "Program" &&
Expand Down Expand Up @@ -161,11 +168,19 @@ function findNodeAtOffset(node, offset, predicate, parentNodes) {
}

// See https://www.ecma-international.org/ecma-262/5.1/#sec-A.5
function isSourceElement(node) {
function isSourceElement(opts, node) {
if (node == null) {
return false;
}
switch (node.type) {
case "ObjectExpression": // JSON
case "ArrayExpression": // JSON
case "StringLiteral": // JSON
case "NumericLiteral": // JSON
case "BooleanLiteral": // JSON
case "NullLiteral": // JSON
case "json-identifier": // JSON
return opts.parser === "json";
case "FunctionDeclaration":
case "BlockStatement":
case "BreakStatement":
Expand Down Expand Up @@ -219,15 +234,11 @@ function calculateRange(text, opts, ast) {
}
}

const startNodeAndParents = findNodeAtOffset(
ast,
startNonWhitespace,
isSourceElement
const startNodeAndParents = findNodeAtOffset(ast, startNonWhitespace, node =>
isSourceElement(opts, node)
);
const endNodeAndParents = findNodeAtOffset(
ast,
endNonWhitespace,
isSourceElement
const endNodeAndParents = findNodeAtOffset(ast, endNonWhitespace, node =>
isSourceElement(opts, node)
);

if (!startNodeAndParents || !endNodeAndParents) {
Expand Down
15 changes: 15 additions & 0 deletions tests/range_json/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`identifier.json 1`] = `
{"a":1, "b":2}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{"a":1, "b":2}
`;

exports[`issue2297.json 1`] = `
{"a":1, "b":2}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{"a":1, "b":2}
`;
1 change: 1 addition & 0 deletions tests/range_json/identifier.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{<<<PRETTIER_RANGE_START>>>"a"<<<PRETTIER_RANGE_END>>>:1, "b":2}
1 change: 1 addition & 0 deletions tests/range_json/issue2297.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":<<<PRETTIER_RANGE_START>>>1<<<PRETTIER_RANGE_END>>>, "b":2}
1 change: 1 addition & 0 deletions tests/range_json/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, { parser: "json" });

0 comments on commit b47c0b4

Please sign in to comment.