Prevent adding quotes if there's a numeric literal as key (#6138)

master
Lucas Duailibe 2019-06-04 13:45:29 -03:00 committed by GitHub
parent 64ab703d41
commit 2e6191fe77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 189 additions and 9 deletions

View File

@ -167,9 +167,9 @@ const v = /** @type{string} */ value;
const v = /** @type{string} */ (value);
```
### JavaScript: Prevent adding quotes when using `--quote-props=consistent` and one of the keys were a computed "complex" expression ([#6119] by [@duailibe])
### JavaScript: Prevent adding quotes when using `--quote-props=consistent` and objects had numbers or computed expressions as keys ([#6119] and [#6138] by [@duailibe])
Previously, Prettier added unnecessary quotes to keys of an object, or properties and methods of classes, if there was at least one computed key with a "complex" expression (e.g. a member expression).
Previously, Prettier added unnecessary quotes to keys of an object, or properties and methods of classes, if there was at least one computed key with a "complex" expression (e.g. a member expression) or a numeric literal.
<!-- prettier-ignore -->
```js
@ -459,6 +459,7 @@ export type Foo = [
```
[#5979]: https://github.com/prettier/prettier/pull/5979
[#6038]: https://github.com/prettier/prettier/pull/6038
[#6086]: https://github.com/prettier/prettier/pull/6086
[#6088]: https://github.com/prettier/prettier/pull/6088
[#6089]: https://github.com/prettier/prettier/pull/6089
@ -473,10 +474,10 @@ export type Foo = [
[#6131]: https://github.com/prettier/prettier/pull/6131
[#6133]: https://github.com/prettier/prettier/pull/6133
[#6136]: https://github.com/prettier/prettier/pull/6136
[#6138]: https://github.com/prettier/prettier/pull/6138
[#6140]: https://github.com/prettier/prettier/pull/6140
[#6038]: https://github.com/prettier/prettier/pull/6038
[#6148]: https://github.com/prettier/prettier/pull/6148
[#6146]: https://github.com/prettier/prettier/pull/6146
[#6148]: https://github.com/prettier/prettier/pull/6148
[#6152]: https://github.com/prettier/prettier/pull/6152
[#6159]: https://github.com/prettier/prettier/pull/6159
[#6172]: https://github.com/prettier/prettier/pull/6172

View File

@ -3620,7 +3620,7 @@ function printPropertyKey(path, options, print) {
prop =>
!prop.computed &&
prop.key &&
prop.key.type !== "Identifier" &&
isStringLiteral(prop.key) &&
!isStringPropSafeToCoerceToIdentifier(prop, options)
);
needsQuoteProps.set(parent, objectHasStringProp);

View File

@ -378,7 +378,7 @@ const d = {
================================================================================
`;
exports[`with-member-expressions.js 1`] = `
exports[`with_member_expressions.js 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
@ -409,7 +409,7 @@ class Foo {
================================================================================
`;
exports[`with-member-expressions.js 2`] = `
exports[`with_member_expressions.js 2`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
@ -440,7 +440,7 @@ class Foo {
================================================================================
`;
exports[`with-member-expressions.js 3`] = `
exports[`with_member_expressions.js 3`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
@ -471,7 +471,7 @@ class Foo {
================================================================================
`;
exports[`with-member-expressions.js 4`] = `
exports[`with_member_expressions.js 4`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
@ -502,3 +502,168 @@ class Foo {
================================================================================
`;
exports[`with_numbers.js 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
quoteProps: "as-needed"
| printWidth
=====================================input======================================
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
=====================================output=====================================
obj = {
foo: "",
1: ""
};
obj = {
bar: "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
================================================================================
`;
exports[`with_numbers.js 2`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
quoteProps: "preserve"
| printWidth
=====================================input======================================
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
=====================================output=====================================
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
================================================================================
`;
exports[`with_numbers.js 3`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
quoteProps: "consistent"
| printWidth
=====================================input======================================
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
=====================================output=====================================
obj = {
foo: "",
1: ""
};
obj = {
bar: "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
================================================================================
`;
exports[`with_numbers.js 4`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
quoteProps: "consistent"
singleQuote: true
| printWidth
=====================================input======================================
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};
=====================================output=====================================
obj = {
foo: '',
1: ''
};
obj = {
bar: '',
1: ''
};
obj = {
'foo-bar': '',
1: ''
};
================================================================================
`;

View File

@ -0,0 +1,14 @@
obj = {
foo: "",
1: ""
};
obj = {
"bar": "",
1: ""
};
obj = {
"foo-bar": "",
1: ""
};