Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow formatting ranges of JSON #2298

Merged
merged 9 commits into from
Jun 27, 2017
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" });