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

Quote and unquote number keys #8508

Merged
merged 19 commits into from Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog_unreleased/javascript/pr-8508.md
Expand Up @@ -24,3 +24,19 @@ x = {
1: null,
};
```

Note that 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" -> 1e+100
```

(As a side note, please don’t use confusing numbers as object keys!)
20 changes: 10 additions & 10 deletions src/language-js/printer-estree.js
Expand Up @@ -72,6 +72,7 @@ const {
isObjectType,
isObjectTypePropertyAFunction,
isSimpleFlowType,
isSimpleNumber,
isSimpleTemplateLiteral,
isStringLiteral,
isStringPropSafeToUnquote,
Expand Down Expand Up @@ -3742,22 +3743,20 @@ function printPropertyKey(path, options, print) {

if (
(key.type === "Identifier" ||
((isNumericLiteral(key) || key.type === "BigIntLiteral") &&
(isNumericLiteral(key) &&
isSimpleNumber(rawText(key)) &&
// Avoid converting 999999999999999999999 to 1e+21, 0.99999999999999999 to 1 and 1.0 to 1.
String(key.value) === rawText(key) &&
!(options.parser === "typescript" || options.parser === "babel-ts"))) &&
(options.parser === "json" ||
(options.quoteProps === "consistent" && needsQuoteProps.get(parent)))
) {
// a -> "a"
// 1e2 -> "100"
// 2n -> "2"
// 0b10n -> "2"
// 1 -> "1"
// 1.5 -> "1.5"
const prop = printString(
JSON.stringify(
key.type === "Identifier"
? key.name
: key.type === "BigIntLiteral"
? BigInt(key.value).toString()
: key.value.toString()
key.type === "Identifier" ? key.name : key.value.toString()
),
options
);
lydell marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -3773,7 +3772,8 @@ function printPropertyKey(path, options, print) {
(options.quoteProps === "consistent" && !needsQuoteProps.get(parent)))
) {
// 'a' -> a
// '1e+100' -> 1e100
// '1' -> 1
// '1.5' -> 1.5
return path.call(
(keyPath) =>
comments.printComments(
Expand Down
8 changes: 7 additions & 1 deletion src/language-js/utils.js
Expand Up @@ -765,7 +765,7 @@ function isStringPropSafeToUnquote(node, options) {
(options.parser === "typescript" || options.parser === "babel-ts") &&
node.type === "ClassProperty"
)) ||
(!node.key.value.startsWith("-") &&
(isSimpleNumber(node.key.value) &&
String(Number(node.key.value)) === node.key.value &&
!(
options.parser === "flow" ||
Expand All @@ -776,6 +776,11 @@ function isStringPropSafeToUnquote(node, options) {
);
}

// Matches “simple” numbers like `123` and `2.5` but not `1_000`, `1e+100` or `0b10`.
function isSimpleNumber(numberString) {
return /^(\d+|\d+\.\d+)$/.test(numberString);
}

function isJestEachTemplateLiteral(node, parentNode) {
/**
* describe.each`table`(name, fn)
Expand Down Expand Up @@ -1128,6 +1133,7 @@ module.exports = {
isObjectType,
isObjectTypePropertyAFunction,
isSimpleFlowType,
isSimpleNumber,
isSimpleTemplateLiteral,
isStringLiteral,
isStringPropSafeToUnquote,
Expand Down