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

Add JSON support #2173

Merged
merged 4 commits into from
Jun 17, 2017
Merged

Add JSON support #2173

merged 4 commits into from
Jun 17, 2017

Conversation

josephfrazier
Copy link
Collaborator

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).

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" +
Copy link
Contributor

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

Copy link
Contributor

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.

function parse(text) {
const jsonToAst = require("json-to-ast");
const ast = jsonToAst(text);
toBabylon(ast);
Copy link
Contributor

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.


function parse(text) {
const jsonToAst = require("json-to-ast");
const ast = jsonToAst(text);
Copy link
Contributor

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.

@vjeux vjeux merged commit a42db30 into prettier:master Jun 17, 2017
@vjeux
Copy link
Contributor

vjeux commented Jun 17, 2017

Awesome!

@josephfrazier josephfrazier deleted the json branch June 17, 2017 21:02
josephfrazier added a commit to josephfrazier/prettier that referenced this pull request Jun 17, 2017
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.
josephfrazier added a commit that referenced this pull request Jun 18, 2017
* 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
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 20, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support autoformatting JSON too
2 participants