Files
test-repo/changelog_unreleased/javascript/pr-8508.md
T
Simon Lydell 9184743081 Quote and unquote number keys (#8508)
* Quote and unquote number keys

* Add changelog entry

* Add tests for escapes

* Fix idempotency

* Only run flow-repo/ tests with flow

271 jsfmt.spec.js in flow-repo use only the flow parser.
18 use flow and babel.
Now, all of them use only flow.

* Don’t unquote negative numbers

* Don’t quote/unquote numbers in TypeScript

* Quote BigInt

* Fix AST check

* Fix condition

* Fix BigInt to string

* Fix ESLint not recognizing BigInt

* Only quote/unquote “simple” numbers

* No need to clean BigIntLiteral anymore

* Add tests for 1. and .1

* Fix 1. and .1

* Remove unnecessary babel-ts from tests

* Only unquote numbers for babel

* Update changelog
2020-06-18 18:13:25 +03:00

1.0 KiB
Raw Blame History

Quote and unquote number keys (#8508 by @lydell)

Prettier removes quotes from object keys if they are identifiers. Now, Prettier also removes quotes from object keys that are numbers.

If you use quoteProps: "consistent", Prettier can also add quotes to number keys so that all properties end up with quotes.

// Input
x = {
  "a": null,
  "1": null,
};

// Prettier stable
x = {
  a: null,
  "1": null,
};

// Prettier master
x = {
  a: null,
  1: null,
};

Prettier only touches “simple” numbers such as 1 and 123.5. It won’t make the following transformations, as they feel unexpected:

1e2 -> "100"
0b10 -> "10"
1_000 -> "1000"
1.0 -> "1"
0.99999999999999999 -> "1"
999999999999999999999 -> "1e+21"
2n -> "2"

"1e+100" -> 1e100

(Please don’t use confusing numbers as object keys!)

Note that Prettier only unquotes numbers using the "babel" parser. It’s not completely safe to do so in TypeScript.