-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add JSON support #2173
Add JSON support #2173
Conversation
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
bin/prettier.js
Outdated
@@ -231,7 +231,7 @@ if (argv["help"] || (!filepatterns.length && !stdin)) { | |||
" --jsx-bracket-same-line Put > on the last line instead of at a new line.\n" + | |||
" --trailing-comma <none|es5|all>\n" + | |||
" Print trailing commas wherever possible. Defaults to none.\n" + | |||
" --parser <flow|babylon|typescript|postcss>\n" + | |||
" --parser <flow|babylon|typescript|postcss|json-to-ast>\n" + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably just name it "json", json-to-ast is weird
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it's unlikely we are going to support two for json.
src/parser-json-to-ast.js
Outdated
function parse(text) { | ||
const jsonToAst = require("json-to-ast"); | ||
const ast = jsonToAst(text); | ||
toBabylon(ast); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you avoid doing the mutations and instead recreate those objects using Object.assign. It's way easier to debug when we don't mutate things.
src/parser-json-to-ast.js
Outdated
|
||
function parse(text) { | ||
const jsonToAst = require("json-to-ast"); | ||
const ast = jsonToAst(text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to try catch it and use createError to have a pretty error.
Awesome! |
Following up on commit a42db30 (prettier#2173), this change makes it so that literals in JSON files are formatted as they would be in an object literal. This normalizes trailing zeroes, exponential notation, unnecessarily escaped characters, etc. It works by using babylon to parse literals as expressions, and putting the result in the AST instead of a `json-literal` node. Since JSON string literals have to be double-quoted, there's a new `__prettier__isJson` flag on each node, which is used by the printer to force double-quotes.
* Format JSON literals like object literals Following up on commit a42db30 (#2173), this change makes it so that literals in JSON files are formatted as they would be in an object literal. This normalizes trailing zeroes, exponential notation, unnecessarily escaped characters, etc. It works by using babylon to parse literals as expressions, and putting the result in the AST instead of a `json-literal` node. Since JSON string literals have to be double-quoted, there's a new `__prettier__isJson` flag on each node, which is used by the printer to force double-quotes. * json: Check options.parser when formatting StringLiteral This is instead of having a custom AST property. See #2181 (comment) * multiparser: Use babylon.parseExpression See #2181 (comment) * json: Don't split result AST node into two See #2181 (comment) * json: Don't use babylon parser to build expression nodes See #2181 (comment) * Revert "multiparser: Use babylon.parseExpression" This reverts commit 3d7a436. * Remove unused parameter from babylon parser * json: Remove unused constructorTypes
This fixes #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).