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 4 commits
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
26 changes: 26 additions & 0 deletions changelog_unreleased/javascript/pr-8508.md
@@ -0,0 +1,26 @@
#### Quote and unquote number keys ([#8508](https://github.com/prettier/prettier/pull/8508) by [@lydell](https://github.com/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.

<!-- prettier-ignore -->
```jsx
// Input
x = {
"a": null,
"1": null,
};

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

// Prettier master
x = {
a: null,
1: null,
};
```
1 change: 1 addition & 0 deletions src/language-js/clean.js
Expand Up @@ -89,6 +89,7 @@ function clean(ast, newObj, parent) {
typeof ast.key === "object" &&
ast.key &&
(ast.key.type === "Literal" ||
ast.key.type === "NumericLiteral" ||
ast.key.type === "StringLiteral" ||
ast.key.type === "Identifier")
) {
Expand Down
29 changes: 22 additions & 7 deletions src/language-js/printer-estree.js
Expand Up @@ -68,12 +68,13 @@ const {
isMemberExpressionChain,
isMemberish,
isNgForOf,
isNumericLiteral,
isObjectType,
isObjectTypePropertyAFunction,
isSimpleFlowType,
isSimpleTemplateLiteral,
isStringLiteral,
isStringPropSafeToCoerceToIdentifier,
isStringPropSafeToUnquote,
isTemplateOnItsOwnLine,
isTestCall,
isTheOnlyJSXElementInMarkdown,
Expand Down Expand Up @@ -3733,33 +3734,47 @@ function printPropertyKey(path, options, print) {
(prop) =>
!prop.computed &&
prop.key &&
isStringLiteral(prop.key) &&
!isStringPropSafeToCoerceToIdentifier(prop, options)
((isStringLiteral(prop.key) &&
!isStringPropSafeToUnquote(prop, options)) ||
(isNumericLiteral(prop.key) &&
(options.parser === "flow" || options.parser === "babel-flow")))
);
needsQuoteProps.set(parent, objectHasStringProp);
}

if (
key.type === "Identifier" &&
(key.type === "Identifier" || isNumericLiteral(key)) &&
(options.parser === "json" ||
(options.quoteProps === "consistent" && needsQuoteProps.get(parent)))
) {
// a -> "a"
const prop = printString(JSON.stringify(key.name), options);
// 1e2 -> "100"
const prop = printString(
JSON.stringify(
key.type === "Identifier" ? key.name : key.value.toString()
),
options
);
lydell marked this conversation as resolved.
Show resolved Hide resolved
return path.call(
(keyPath) => comments.printComments(keyPath, () => prop, options),
"key"
);
}

if (
isStringPropSafeToCoerceToIdentifier(node, options) &&
isStringPropSafeToUnquote(node, options) &&
(options.quoteProps === "as-needed" ||
(options.quoteProps === "consistent" && !needsQuoteProps.get(parent)))
) {
// 'a' -> a
// '1e+100' -> 1e100
return path.call(
(keyPath) => comments.printComments(keyPath, () => key.value, options),
(keyPath) =>
comments.printComments(
keyPath,
() => (/^\d/.test(key.value) ? printNumber(key.value) : key.value),
options
),
"key"
);
}
Expand Down
22 changes: 12 additions & 10 deletions src/language-js/utils.js
Expand Up @@ -734,18 +734,20 @@ function returnArgumentHasLeadingComment(options, argument) {
return false;
}

function isStringPropSafeToCoerceToIdentifier(node, options) {
function isStringPropSafeToUnquote(node, options) {
return (
options.parser !== "json" &&
isStringLiteral(node.key) &&
isIdentifierName(node.key.value) &&
rawText(node.key).slice(1, -1) === node.key.value &&
options.parser !== "json" &&
// With `--strictPropertyInitialization`, TS treats properties with quoted names differently than unquoted ones.
// See https://github.com/microsoft/TypeScript/pull/20075
!(
(options.parser === "typescript" || options.parser === "babel-ts") &&
node.type === "ClassProperty"
)
((isIdentifierName(node.key.value) &&
// With `--strictPropertyInitialization`, TS treats properties with quoted names differently than unquoted ones.
// See https://github.com/microsoft/TypeScript/pull/20075
!(
(options.parser === "typescript" || options.parser === "babel-ts") &&
node.type === "ClassProperty"
)) ||
(String(Number(node.key.value)) === node.key.value &&
lydell marked this conversation as resolved.
Show resolved Hide resolved
!(options.parser === "flow" || options.parser === "babel-flow")))
);
}

Expand Down Expand Up @@ -1103,7 +1105,7 @@ module.exports = {
isSimpleFlowType,
isSimpleTemplateLiteral,
isStringLiteral,
isStringPropSafeToCoerceToIdentifier,
isStringPropSafeToUnquote,
isTemplateOnItsOwnLine,
isTestCall,
isTheOnlyJSXElementInMarkdown,
Expand Down