diff --git a/src/parser-json.js b/src/parser-json.js index d611e8f9..ff764ee3 100644 --- a/src/parser-json.js +++ b/src/parser-json.js @@ -29,8 +29,7 @@ function toBabylon(node) { object: "ObjectExpression", property: "ObjectProperty", identifier: "json-identifier", - array: "ArrayExpression", - literal: "json-literal" + array: "ArrayExpression" }; const result = { @@ -58,10 +57,24 @@ function toBabylon(node) { return Object.assign(result, { elements: node.children.map(toBabylon) }); - case "literal": + case "literal": { + const constructorTypes = { + String: "StringLiteral", + Number: "NumericLiteral", + Boolean: "BooleanLiteral" + }; + + const value = JSON.parse(node.rawValue); + const type = value === null + ? "NullLiteral" + : constructorTypes[value.constructor.name]; + return Object.assign(result, { - rawValue: node.rawValue + type: type, + value: value, + extra: { raw: node.rawValue } }); + } } } diff --git a/src/printer.js b/src/printer.js index 6f73416e..36c2f859 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2636,8 +2636,6 @@ function genericPrintNoParens(path, options, print, args) { }, "body"); case "json-identifier": return '"' + n.value + '"'; - case "json-literal": - return n.rawValue; default: throw new Error("unknown type: " + JSON.stringify(n.type)); @@ -4144,9 +4142,9 @@ function nodeStr(node, options, isFlowOrTypeScriptDirectiveLiteral) { canChangeDirectiveQuotes = true; } - const enclosingQuote = shouldUseAlternateQuote - ? alternate.quote - : preferred.quote; + const enclosingQuote = options.parser === "json" + ? double.quote + : shouldUseAlternateQuote ? alternate.quote : preferred.quote; // Directives are exact code unit sequences, which means that you can't // change the escape sequences they use. diff --git a/tests/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/__snapshots__/jsfmt.spec.js.snap index ff36d759..be767067 100644 --- a/tests/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/__snapshots__/jsfmt.spec.js.snap @@ -82,17 +82,17 @@ exports[`pass1.json 1`] = ` null, { "integer": 1234567890, - "real": -9876.543210, + "real": -9876.54321, "e": 0.123456789e-12, - "E": 1.234567890E+34, - "": 23456789012E66, + "E": 1.23456789e34, + "": 23456789012e66, "zero": 0, "one": 1, "space": " ", "quote": "\\"", "backslash": "\\\\", "controls": "\\b\\f\\n\\r\\t", - "slash": "/ & \\/", + "slash": "/ & /", "alpha": "abcdefghijklmnopqrstuvwyz", "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", "digit": "0123456789", @@ -121,9 +121,9 @@ exports[`pass1.json 1`] = ` 1e1, 0.1e1, 1e-1, - 1e00, - 2e+00, - 2e-00, + 1, + 2, + 2, "rosebud" ] `;