From 2e6191fe77eaa10a999e21b3f78bc274b5096b3b Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Tue, 4 Jun 2019 13:45:29 -0300 Subject: [PATCH] Prevent adding quotes if there's a numeric literal as key (#6138) --- CHANGELOG.unreleased.md | 9 +- src/language-js/printer-estree.js | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 173 +++++++++++++++++- ...ressions.js => with_member_expressions.js} | 0 tests/quote_props/with_numbers.js | 14 ++ 5 files changed, 189 insertions(+), 9 deletions(-) rename tests/quote_props/{with-member-expressions.js => with_member_expressions.js} (100%) create mode 100644 tests/quote_props/with_numbers.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 974484f7..77f0e41d 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -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. ```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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index dfb1981f..0805ba32 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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); diff --git a/tests/quote_props/__snapshots__/jsfmt.spec.js.snap b/tests/quote_props/__snapshots__/jsfmt.spec.js.snap index 13712b0a..e9a95fd6 100644 --- a/tests/quote_props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/quote_props/__snapshots__/jsfmt.spec.js.snap @@ -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: '' +}; + +================================================================================ +`; diff --git a/tests/quote_props/with-member-expressions.js b/tests/quote_props/with_member_expressions.js similarity index 100% rename from tests/quote_props/with-member-expressions.js rename to tests/quote_props/with_member_expressions.js diff --git a/tests/quote_props/with_numbers.js b/tests/quote_props/with_numbers.js new file mode 100644 index 00000000..436f5332 --- /dev/null +++ b/tests/quote_props/with_numbers.js @@ -0,0 +1,14 @@ +obj = { + foo: "", + 1: "" +}; + +obj = { + "bar": "", + 1: "" +}; + +obj = { + "foo-bar": "", + 1: "" +};