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

Format JSON.parse string literals #7043

Closed
nickmccurdy opened this issue Nov 24, 2019 · 4 comments
Closed

Format JSON.parse string literals #7043

nickmccurdy opened this issue Nov 24, 2019 · 4 comments
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:needs discussion Issues needing discussion and a decision to be made before action can be taken

Comments

@nickmccurdy
Copy link
Member

nickmccurdy commented Nov 24, 2019

Prettier 1.19.1
Playground link

--parser babel

Input:

JSON.parse('{"property":"value"}');
JSON.parse`{"property":"value"}`;

Output:

JSON.parse('{"property":"value"}');
JSON.parse`{"property":"value"}`;

Expected behavior:

JSON.parse('{ "property": "value" }');
JSON.parse`{ "property": "value" }`;

For more complex JavaScript objects, it can be faster and more reliable to use JSON.parse with an inline string rather than just using an object literal (explanation). It would be useful if Prettier could format strings passed directly to JSON.parse as embedded JSON, so they look like what would happen if you loaded and parsed a separate JSON file formatted with Prettier. Note that template strings work with JSON.parse in JavaScript, but we'd have to handle edge cases when formatting template variables.

@thorn0
Copy link
Member

thorn0 commented Nov 24, 2019

I suppose this issue can be considered a duplicate of #5908.

@nickmccurdy
Copy link
Member Author

Would that also cover automatic support for JSON when JSON.parse is used?

@thorn0
Copy link
Member

thorn0 commented Nov 24, 2019

It definitely wouldn't be automatic. A special comment, an option or something else would be needed to trigger it. See #5588, #6626.

@thorn0 thorn0 added area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:javascript Issues affecting JS status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Nov 24, 2019
@thorn0
Copy link
Member

thorn0 commented Feb 9, 2021

Closing in favor of #5588

@thorn0 thorn0 closed this as completed Feb 9, 2021
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label May 11, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area:multiparser Issues with printing one language inside another, like CSS-in-JS lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. status:needs discussion Issues needing discussion and a decision to be made before action can be taken
Projects
None yet
Development

No branches or pull requests

2 participants