forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add JSON support This fixes prettier#1973 by using [json-to-ast] to parse JSON, then converting the AST into a mostly-Babylon AST, so as to piggyback on the existing printer logic. Identifiers and literals are currently printed verbatim from the input, but this could be improved upon later (at least for literals, not sure about identifiers). [json-to-ast]: https://github.com/vtrushin/json-to-ast * Rename `json-to-ast` parser to `json` See prettier#2173 (comment) * json: Use createError to have a pretty error See prettier#2173 (comment) * json: Transform AST without mutations See prettier#2173 (comment)
- Loading branch information
1 parent
59a294f
commit a42db30
Showing
13 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"use strict"; | ||
|
||
const createError = require("./parser-create-error"); | ||
|
||
function parse(text) { | ||
const jsonToAst = require("json-to-ast"); | ||
try { | ||
const ast = jsonToAst(text); | ||
return toBabylon(ast); | ||
} catch (err) { | ||
const firstNewlineIndex = err.message.indexOf("\n"); | ||
const firstLine = err.message.slice(0, firstNewlineIndex); | ||
const lastSpaceIndex = firstLine.lastIndexOf(" "); | ||
const message = firstLine.slice(0, lastSpaceIndex); | ||
const locString = firstLine.slice(lastSpaceIndex + 1); | ||
const lineCol = locString.split(":").map(Number); | ||
|
||
throw createError("(json-to-ast) " + message, { | ||
start: { | ||
line: lineCol[0], | ||
column: lineCol[1] | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function toBabylon(node) { | ||
const typeMap = { | ||
object: "ObjectExpression", | ||
property: "ObjectProperty", | ||
identifier: "json-identifier", | ||
array: "ArrayExpression", | ||
literal: "json-literal" | ||
}; | ||
|
||
const result = { | ||
type: typeMap[node.type], | ||
start: node.loc.start.offset, | ||
end: node.loc.end.offset, | ||
loc: node.loc | ||
}; | ||
|
||
switch (node.type) { | ||
case "object": | ||
return Object.assign(result, { | ||
properties: node.children.map(toBabylon) | ||
}); | ||
case "property": | ||
return Object.assign(result, { | ||
key: toBabylon(node.key), | ||
value: toBabylon(node.value) | ||
}); | ||
case "identifier": | ||
return Object.assign(result, { | ||
value: node.value | ||
}); | ||
case "array": | ||
return Object.assign(result, { | ||
elements: node.children.map(toBabylon) | ||
}); | ||
case "literal": | ||
return Object.assign(result, { | ||
rawValue: node.rawValue | ||
}); | ||
} | ||
} | ||
|
||
module.exports = parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`multi-line.json 1`] = ` | ||
{"key1":[true,false,null],"key2":{"key3":[1,2,"3", | ||
1e10,1e-3]}} | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
{ | ||
"key1": [true, false, null], | ||
"key2": { | ||
"key3": [1, 2, "3", 1e10, 1e-3] | ||
} | ||
} | ||
`; | ||
|
||
exports[`pass1.json 1`] = ` | ||
[ | ||
"JSON Test Pattern pass1", | ||
{"object with 1 member":["array with 1 element"]}, | ||
{}, | ||
[], | ||
-42, | ||
true, | ||
false, | ||
null, | ||
{ | ||
"integer": 1234567890, | ||
"real": -9876.543210, | ||
"e": 0.123456789e-12, | ||
"E": 1.234567890E+34, | ||
"": 23456789012E66, | ||
"zero": 0, | ||
"one": 1, | ||
"space": " ", | ||
"quote": "\\"", | ||
"backslash": "\\\\", | ||
"controls": "\\b\\f\\n\\r\\t", | ||
"slash": "/ & \\/", | ||
"alpha": "abcdefghijklmnopqrstuvwyz", | ||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", | ||
"digit": "0123456789", | ||
"0123456789": "digit", | ||
"special": "\`1~!@#$%^&*()_+-={':[,]}|;.</>?", | ||
"hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A", | ||
"true": true, | ||
"false": false, | ||
"null": null, | ||
"array":[ ], | ||
"object":{ }, | ||
"address": "50 St. James Street", | ||
"url": "http://www.JSON.org/", | ||
"comment": "// /* <!-- --", | ||
"# -- --> */": " ", | ||
" s p a c e d " :[1,2 , 3 | ||
, | ||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], | ||
"jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", | ||
"quotes": "" \\u0022 %22 0x22 034 "", | ||
"\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?" | ||
: "A key can be any string" | ||
}, | ||
0.5 ,98.6 | ||
, | ||
99.44 | ||
, | ||
1066, | ||
1e1, | ||
0.1e1, | ||
1e-1, | ||
1e00,2e+00,2e-00 | ||
,"rosebud"]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
[ | ||
"JSON Test Pattern pass1", | ||
{ "object with 1 member": ["array with 1 element"] }, | ||
{}, | ||
[], | ||
-42, | ||
true, | ||
false, | ||
null, | ||
{ | ||
"integer": 1234567890, | ||
"real": -9876.543210, | ||
"e": 0.123456789e-12, | ||
"E": 1.234567890E+34, | ||
"": 23456789012E66, | ||
"zero": 0, | ||
"one": 1, | ||
"space": " ", | ||
"quote": "\\"", | ||
"backslash": "\\\\", | ||
"controls": "\\b\\f\\n\\r\\t", | ||
"slash": "/ & \\/", | ||
"alpha": "abcdefghijklmnopqrstuvwyz", | ||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", | ||
"digit": "0123456789", | ||
"0123456789": "digit", | ||
"special": "\`1~!@#$%^&*()_+-={':[,]}|;.</>?", | ||
"hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A", | ||
"true": true, | ||
"false": false, | ||
"null": null, | ||
"array": [], | ||
"object": {}, | ||
"address": "50 St. James Street", | ||
"url": "http://www.JSON.org/", | ||
"comment": "// /* <!-- --", | ||
"# -- --> */": " ", | ||
" s p a c e d ": [1, 2, 3, 4, 5, 6, 7], | ||
"compact": [1, 2, 3, 4, 5, 6, 7], | ||
"jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", | ||
"quotes": "" \\u0022 %22 0x22 034 "", | ||
"\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t\`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string" | ||
}, | ||
0.5, | ||
98.6, | ||
99.44, | ||
1066, | ||
1e1, | ||
0.1e1, | ||
1e-1, | ||
1e00, | ||
2e+00, | ||
2e-00, | ||
"rosebud" | ||
] | ||
`; | ||
exports[`single-line.json 1`] = ` | ||
{"key1":[true,false,null],"key2":{"key3":[1,2,"3",1e10,1e-3]}} | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
{ "key1": [true, false, null], "key2": { "key3": [1, 2, "3", 1e10, 1e-3] } } | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
run_spec(__dirname, { parser: "json" }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{"key1":[true,false,null],"key2":{"key3":[1,2,"3", | ||
1e10,1e-3]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
[ | ||
"JSON Test Pattern pass1", | ||
{"object with 1 member":["array with 1 element"]}, | ||
{}, | ||
[], | ||
-42, | ||
true, | ||
false, | ||
null, | ||
{ | ||
"integer": 1234567890, | ||
"real": -9876.543210, | ||
"e": 0.123456789e-12, | ||
"E": 1.234567890E+34, | ||
"": 23456789012E66, | ||
"zero": 0, | ||
"one": 1, | ||
"space": " ", | ||
"quote": "\"", | ||
"backslash": "\\", | ||
"controls": "\b\f\n\r\t", | ||
"slash": "/ & \/", | ||
"alpha": "abcdefghijklmnopqrstuvwyz", | ||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", | ||
"digit": "0123456789", | ||
"0123456789": "digit", | ||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?", | ||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", | ||
"true": true, | ||
"false": false, | ||
"null": null, | ||
"array":[ ], | ||
"object":{ }, | ||
"address": "50 St. James Street", | ||
"url": "http://www.JSON.org/", | ||
"comment": "// /* <!-- --", | ||
"# -- --> */": " ", | ||
" s p a c e d " :[1,2 , 3 | ||
|
||
, | ||
|
||
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], | ||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", | ||
"quotes": "" \u0022 %22 0x22 034 "", | ||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" | ||
: "A key can be any string" | ||
}, | ||
0.5 ,98.6 | ||
, | ||
99.44 | ||
, | ||
|
||
1066, | ||
1e1, | ||
0.1e1, | ||
1e-1, | ||
1e00,2e+00,2e-00 | ||
,"rosebud"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"key1":[true,false,null],"key2":{"key3":[1,2,"3",1e10,1e-3]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2316,6 +2316,10 @@ json-stringify-safe@~5.0.1: | |
version "5.0.1" | ||
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" | ||
|
||
[email protected]: | ||
version "2.0.0-alpha1.2" | ||
resolved "https://registry.yarnpkg.com/json-to-ast/-/json-to-ast-2.0.0-alpha1.2.tgz#fe27fd89eb639eca1e01f578e79d46ee36e238e8" | ||
|
||
json5@^0.5.0, json5@^0.5.1: | ||
version "0.5.1" | ||
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" | ||
|