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
Quote and unquote number keys #8508
Conversation
446fa52
to
9be56ce
Compare
271 jsfmt.spec.js in flow-repo use only the flow parser. 18 use flow and babel. Now, all of them use only flow.
They also must not be quoted or unquoted in obect types: --parser typescript Input: var foo: { "2": boolean } Output: var foo: { 2: boolean }; |
Hm. I might be wrong about object types... |
Yeah, as far as I can tell TypeScript doesn’t care about quotes in object types |
Not always, but sometimes it does: So it's not safe to do in TS at all. Even in object literals. |
BigInts can be used as keys too: |
I wonder if the default "babel" parser is starting to become dangerous … it supports some type syntax, but is not entirely safe to run on Flow and TypeScript code 🤔 |
There is an issue about this #8204 |
Quoting and unquoting can be a little bit “destructive” (but still correct). x = {
0b10: null,
}; edit -> x = {
0b10: null,
"something-else": null,
}; prettier -> x = {
"2": null,
"something-else": null,
}; edit -> x = {
"2": null,
}; prettier -> x = {
2: null,
}; It’s like Prettier has turned Maybe we should only quote/unquote |
Quoting happens only if the user opts in for it using --parser babel Input: a={'1e+100':1,'2e-10':2} Output: a = { 1e100: 1, 2e-10: 2 }; |
But I think it’s important that Prettier is “round-trippable”. An addition followed by a deletion should end up with the input code. a = { 1e100: 1, 2e-10: 2 }; edit -> a = { 1e100: 1, 2e-10: 2, "a-a": 3 }; prettier -> a = { "1e+100": 1, "2e-10": 2, "a-a": 3 }; edit -> a = { "1e+100": 1, "2e-10": 2 }; prettier – here we need to unqoute to get back again! -> a = { 1e100: 1, 2e-10: 2 }; But that has the unfortunate side effect of That’s why I was thinking we should only do stuff with “digits-only” (maybe also floats?) keys – I’m thinking that’s the only likely case we’ll encounter in real code. |
Given everything we’ve learned in this PR, it could also make sense to leave things the way the are and close the issue, leaving it up to the user to decide, or to use the ESLint quote-props rule. |
All considered, I vote for "digits only, including floats". Let's play it safe and keep non-decimals, number separators, bigints, etc. as is. |
e717ec2
to
83bbfd3
Compare
Updated to only touch “simple” numbers. |
But, prettier/src/language-js/parser-babel.js Line 105 in 4009ecf
Related: #8204 (comment) |
That's fine. The |
In the latest release of Prettier, number keys are "unquoted" by default. This is all fine and well, but Flow reacts poorly to number literals as keys. The most diplomatic solution seems to be setting "quoteProps" to "preserve": https://prettier.io/docs/en/options.html#quote-props However, this precipitated an interesting error in jest, where it was unhappy about the reaction and review state props being unquoted, both in the flow type notation and the object literal itself. I recommend reading the PR that implemented this change for more discussion: prettier/prettier#8508 test plan: `yarn unit`, `yarn flow`, `yarn check-pretty`
* Bump prettier from 2.0.5 to 2.1.1 Bumps [prettier](https://github.com/prettier/prettier) from 2.0.5 to 2.1.1. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md) - [Commits](prettier/prettier@2.0.5...2.1.1) Signed-off-by: dependabot[bot] <support@github.com> * Prettier: preserve quotes in object props In the latest release of Prettier, number keys are "unquoted" by default. This is all fine and well, but Flow reacts poorly to number literals as keys. The most diplomatic solution seems to be setting "quoteProps" to "preserve": https://prettier.io/docs/en/options.html#quote-props However, this precipitated an interesting error in jest, where it was unhappy about the reaction and review state props being unquoted, both in the flow type notation and the object literal itself. I recommend reading the PR that implemented this change for more discussion: prettier/prettier#8508 test plan: `yarn unit`, `yarn flow`, `yarn check-pretty` Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin Siegler <17910833+topocount@users.noreply.github.com>
Quote & unquote with TypeScript seems to be considered "not safe" as well, as was noted in some comments added to the source code. |
Closes #6064
Closes #6132
Note: Flow parses number keys, but errors on them during type checking, so we don’t unquote number keys for the
flow
andbabel-flow
parsers.https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjOA7AzgFzAA8wBeMAb1TDAAYAuMARlQF8g
docs/
directory)changelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨