From eb0e28b860f98ef81f61cfd5628f5c3f720ec7ae Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 19:24:46 +0200 Subject: [PATCH 01/19] Quote and unquote number keys --- src/language-js/clean.js | 1 + src/language-js/printer-estree.js | 22 +- src/language-js/utils.js | 22 +- .../__snapshots__/jsfmt.spec.js.snap | 941 ++++++++++++++++++ tests/flow/quote-props/classes.js | 17 + tests/flow/quote-props/jsfmt.spec.js | 16 + tests/flow/quote-props/objects.js | 51 + .../quote-props/with_member_expressions.js | 9 + tests/flow/quote-props/with_numbers.js | 14 + .../__snapshots__/jsfmt.spec.js.snap | 308 +++++- tests/js/quote-props/jsfmt.spec.js | 9 +- tests/js/quote-props/objects.js | 34 + .../__snapshots__/jsfmt.spec.js.snap | 10 +- tests/typescript/quote-props/jsfmt.spec.js | 6 +- 14 files changed, 1414 insertions(+), 46 deletions(-) create mode 100644 tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/flow/quote-props/classes.js create mode 100644 tests/flow/quote-props/jsfmt.spec.js create mode 100644 tests/flow/quote-props/objects.js create mode 100644 tests/flow/quote-props/with_member_expressions.js create mode 100644 tests/flow/quote-props/with_numbers.js diff --git a/src/language-js/clean.js b/src/language-js/clean.js index 1dfc2174b9cc..55432b05ddd5 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -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") ) { diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 68875559ab90..444250aa6620 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -68,12 +68,13 @@ const { isMemberExpressionChain, isMemberish, isNgForOf, + isNumericLiteral, isObjectType, isObjectTypePropertyAFunction, isSimpleFlowType, isSimpleTemplateLiteral, isStringLiteral, - isStringPropSafeToCoerceToIdentifier, + isStringPropSafeToUnquote, isTemplateOnItsOwnLine, isTestCall, isTheOnlyJSXElementInMarkdown, @@ -3733,19 +3734,27 @@ 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 + ); return path.call( (keyPath) => comments.printComments(keyPath, () => prop, options), "key" @@ -3753,11 +3762,12 @@ function printPropertyKey(path, options, print) { } if ( - isStringPropSafeToCoerceToIdentifier(node, options) && + isStringPropSafeToUnquote(node, options) && (options.quoteProps === "as-needed" || (options.quoteProps === "consistent" && !needsQuoteProps.get(parent))) ) { // 'a' -> a + // '1e+100' -> 1e+100 return path.call( (keyPath) => comments.printComments(keyPath, () => key.value, options), "key" diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 3927017ef7ef..c6dc14a44db8 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -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 && + !(options.parser === "flow" || options.parser === "babel-flow"))) ); } @@ -1103,7 +1105,7 @@ module.exports = { isSimpleFlowType, isSimpleTemplateLiteral, isStringLiteral, - isStringPropSafeToCoerceToIdentifier, + isStringPropSafeToUnquote, isTemplateOnItsOwnLine, isTestCall, isTheOnlyJSXElementInMarkdown, diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..37bfa574177f --- /dev/null +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,941 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`classes.js - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "as-needed" + | printWidth +=====================================input====================================== +class A { + a = "a" +}; + +class B { + 'b' = "b" +}; + +class C { + c1 = "c1" + 'c2' = "c2" +}; + +class D { + d1 = "d1" + 'd-2' = "d2" +}; + +=====================================output===================================== +class A { + a = "a"; +} + +class B { + b = "b"; +} + +class C { + c1 = "c1"; + c2 = "c2"; +} + +class D { + d1 = "d1"; + "d-2" = "d2"; +} + +================================================================================ +`; + +exports[`classes.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" +singleQuote: true + | printWidth +=====================================input====================================== +class A { + a = "a" +}; + +class B { + 'b' = "b" +}; + +class C { + c1 = "c1" + 'c2' = "c2" +}; + +class D { + d1 = "d1" + 'd-2' = "d2" +}; + +=====================================output===================================== +class A { + a = 'a'; +} + +class B { + b = 'b'; +} + +class C { + c1 = 'c1'; + c2 = 'c2'; +} + +class D { + 'd1' = 'd1'; + 'd-2' = 'd2'; +} + +================================================================================ +`; + +exports[`classes.js - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" + | printWidth +=====================================input====================================== +class A { + a = "a" +}; + +class B { + 'b' = "b" +}; + +class C { + c1 = "c1" + 'c2' = "c2" +}; + +class D { + d1 = "d1" + 'd-2' = "d2" +}; + +=====================================output===================================== +class A { + a = "a"; +} + +class B { + b = "b"; +} + +class C { + c1 = "c1"; + c2 = "c2"; +} + +class D { + "d1" = "d1"; + "d-2" = "d2"; +} + +================================================================================ +`; + +exports[`classes.js - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "preserve" + | printWidth +=====================================input====================================== +class A { + a = "a" +}; + +class B { + 'b' = "b" +}; + +class C { + c1 = "c1" + 'c2' = "c2" +}; + +class D { + d1 = "d1" + 'd-2' = "d2" +}; + +=====================================output===================================== +class A { + a = "a"; +} + +class B { + "b" = "b"; +} + +class C { + c1 = "c1"; + "c2" = "c2"; +} + +class D { + d1 = "d1"; + "d-2" = "d2"; +} + +================================================================================ +`; + +exports[`objects.js - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "as-needed" + | printWidth +=====================================input====================================== +const a = { + a: "a" +}; + +const b = { + 'b': "b" +}; + +const c = { + c1: "c1", + 'c2': "c2" +}; + +const d = { + d1: "d1", + 'd-2': "d2" +}; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + +=====================================output===================================== +const a = { + a: "a", +}; + +const b = { + b: "b", +}; + +const c = { + c1: "c1", + c2: "c2", +}; + +const d = { + d1: "d1", + "d-2": "d2", +}; + +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + NaN: null, +}; + +const g = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + "a-a": null, +}; + +================================================================================ +`; + +exports[`objects.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" +singleQuote: true + | printWidth +=====================================input====================================== +const a = { + a: "a" +}; + +const b = { + 'b': "b" +}; + +const c = { + c1: "c1", + 'c2': "c2" +}; + +const d = { + d1: "d1", + 'd-2': "d2" +}; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + +=====================================output===================================== +const a = { + a: 'a', +}; + +const b = { + b: 'b', +}; + +const c = { + c1: 'c1', + c2: 'c2', +}; + +const d = { + 'd1': 'd1', + 'd-2': 'd2', +}; + +const e = { + '1': null, + '100': null, + '1000': null, + '1e+100': null, + '2': null, + '8': null, + '15': null, + 'NaN': null, +}; + +const f = { + '1': null, + '1E2': null, + '1e+3': null, + '1e+100': null, + '0b10': null, + '0o10': null, + '0xf': null, + 'NaN': null, +}; + +const g = { + '1': null, + '100': null, + '1000': null, + '1e+100': null, + '2': null, + '8': null, + '15': null, + 'NaN': null, + 'a-a': null, +}; + +================================================================================ +`; + +exports[`objects.js - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" + | printWidth +=====================================input====================================== +const a = { + a: "a" +}; + +const b = { + 'b': "b" +}; + +const c = { + c1: "c1", + 'c2': "c2" +}; + +const d = { + d1: "d1", + 'd-2': "d2" +}; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + +=====================================output===================================== +const a = { + a: "a", +}; + +const b = { + b: "b", +}; + +const c = { + c1: "c1", + c2: "c2", +}; + +const d = { + "d1": "d1", + "d-2": "d2", +}; + +const e = { + "1": null, + "100": null, + "1000": null, + "1e+100": null, + "2": null, + "8": null, + "15": null, + "NaN": null, +}; + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +}; + +const g = { + "1": null, + "100": null, + "1000": null, + "1e+100": null, + "2": null, + "8": null, + "15": null, + "NaN": null, + "a-a": null, +}; + +================================================================================ +`; + +exports[`objects.js - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "preserve" + | printWidth +=====================================input====================================== +const a = { + a: "a" +}; + +const b = { + 'b': "b" +}; + +const c = { + c1: "c1", + 'c2': "c2" +}; + +const d = { + d1: "d1", + 'd-2': "d2" +}; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + +=====================================output===================================== +const a = { + a: "a", +}; + +const b = { + "b": "b", +}; + +const c = { + c1: "c1", + "c2": "c2", +}; + +const d = { + d1: "d1", + "d-2": "d2", +}; + +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +}; + +const g = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + "a-a": null, +}; + +================================================================================ +`; + +exports[`with_member_expressions.js - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "as-needed" + | printWidth +=====================================input====================================== +const obj = { + foo: "", + [foo.bar]: "" +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +=====================================output===================================== +const obj = { + foo: "", + [foo.bar]: "", +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +================================================================================ +`; + +exports[`with_member_expressions.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" +singleQuote: true + | printWidth +=====================================input====================================== +const obj = { + foo: "", + [foo.bar]: "" +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +=====================================output===================================== +const obj = { + foo: '', + [foo.bar]: '', +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +================================================================================ +`; + +exports[`with_member_expressions.js - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "consistent" + | printWidth +=====================================input====================================== +const obj = { + foo: "", + [foo.bar]: "" +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +=====================================output===================================== +const obj = { + foo: "", + [foo.bar]: "", +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +================================================================================ +`; + +exports[`with_member_expressions.js - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +quoteProps: "preserve" + | printWidth +=====================================input====================================== +const obj = { + foo: "", + [foo.bar]: "" +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +=====================================output===================================== +const obj = { + foo: "", + [foo.bar]: "", +}; + +class Foo { + foo() {} + [foo.bar]() {} +} + +================================================================================ +`; + +exports[`with_numbers.js - {"quoteProps":"as-needed"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +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 - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +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': '', +}; + +================================================================================ +`; + +exports[`with_numbers.js - {"quoteProps":"consistent"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +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 - {"quoteProps":"preserve"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +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: "", +}; + +================================================================================ +`; diff --git a/tests/flow/quote-props/classes.js b/tests/flow/quote-props/classes.js new file mode 100644 index 000000000000..117447136f8f --- /dev/null +++ b/tests/flow/quote-props/classes.js @@ -0,0 +1,17 @@ +class A { + a = "a" +}; + +class B { + 'b' = "b" +}; + +class C { + c1 = "c1" + 'c2' = "c2" +}; + +class D { + d1 = "d1" + 'd-2' = "d2" +}; diff --git a/tests/flow/quote-props/jsfmt.spec.js b/tests/flow/quote-props/jsfmt.spec.js new file mode 100644 index 000000000000..b7b2029ab887 --- /dev/null +++ b/tests/flow/quote-props/jsfmt.spec.js @@ -0,0 +1,16 @@ +run_spec(__dirname, ["flow", "babel-flow"], { + quoteProps: "as-needed", +}); + +run_spec(__dirname, ["flow", "babel-flow"], { + quoteProps: "preserve", +}); + +run_spec(__dirname, ["flow", "babel-flow"], { + quoteProps: "consistent", +}); + +run_spec(__dirname, ["flow", "babel-flow"], { + quoteProps: "consistent", + singleQuote: true, +}); diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js new file mode 100644 index 000000000000..cf2fdad6d30b --- /dev/null +++ b/tests/flow/quote-props/objects.js @@ -0,0 +1,51 @@ +const a = { + a: "a" +}; + +const b = { + 'b': "b" +}; + +const c = { + c1: "c1", + 'c2': "c2" +}; + +const d = { + d1: "d1", + 'd-2': "d2" +}; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} diff --git a/tests/flow/quote-props/with_member_expressions.js b/tests/flow/quote-props/with_member_expressions.js new file mode 100644 index 000000000000..22c838e46219 --- /dev/null +++ b/tests/flow/quote-props/with_member_expressions.js @@ -0,0 +1,9 @@ +const obj = { + foo: "", + [foo.bar]: "" +}; + +class Foo { + foo() {} + [foo.bar]() {} +} diff --git a/tests/flow/quote-props/with_numbers.js b/tests/flow/quote-props/with_numbers.js new file mode 100644 index 000000000000..436f533284ae --- /dev/null +++ b/tests/flow/quote-props/with_numbers.js @@ -0,0 +1,14 @@ +obj = { + foo: "", + 1: "" +}; + +obj = { + "bar": "", + 1: "" +}; + +obj = { + "foo-bar": "", + 1: "" +}; diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index c76513d3cdba..73f68f988a11 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`classes.js - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -49,7 +49,7 @@ class D { exports[`classes.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" singleQuote: true @@ -97,7 +97,7 @@ class D { exports[`classes.js - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -144,7 +144,7 @@ class D { exports[`classes.js - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "preserve" | printWidth @@ -191,7 +191,7 @@ class D { exports[`objects.js - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -214,6 +214,40 @@ const d = { 'd-2': "d2" }; +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + =====================================output===================================== const a = { a: "a", @@ -233,12 +267,46 @@ const d = { "d-2": "d2", }; +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + 1: null, + "1E2": null, + "1e+3": null, + 1e+100: null, + "0b10": null, + "0o10": null, + "0xf": null, + NaN: null, +}; + +const g = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + "a-a": null, +}; + ================================================================================ `; exports[`objects.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" singleQuote: true @@ -262,6 +330,40 @@ const d = { 'd-2': "d2" }; +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + =====================================output===================================== const a = { a: 'a', @@ -281,12 +383,46 @@ const d = { 'd-2': 'd2', }; +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + '1': null, + '1E2': null, + '1e+3': null, + '1e+100': null, + '0b10': null, + '0o10': null, + '0xf': null, + 'NaN': null, +}; + +const g = { + '1': null, + '100': null, + '1000': null, + '1e+100': null, + '2': null, + '8': null, + '15': null, + 'NaN': null, + 'a-a': null, +}; + ================================================================================ `; exports[`objects.js - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -309,6 +445,40 @@ const d = { 'd-2': "d2" }; +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + =====================================output===================================== const a = { a: "a", @@ -328,12 +498,46 @@ const d = { "d-2": "d2", }; +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +}; + +const g = { + "1": null, + "100": null, + "1000": null, + "1e+100": null, + "2": null, + "8": null, + "15": null, + "NaN": null, + "a-a": null, +}; + ================================================================================ `; exports[`objects.js - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "preserve" | printWidth @@ -356,6 +560,40 @@ const d = { 'd-2': "d2" }; +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} + =====================================output===================================== const a = { a: "a", @@ -375,12 +613,46 @@ const d = { "d-2": "d2", }; +const e = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +}; + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +}; + +const g = { + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + "a-a": null, +}; + ================================================================================ `; exports[`with_member_expressions.js - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -411,7 +683,7 @@ class Foo { exports[`with_member_expressions.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" singleQuote: true @@ -443,7 +715,7 @@ class Foo { exports[`with_member_expressions.js - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -474,7 +746,7 @@ class Foo { exports[`with_member_expressions.js - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "preserve" | printWidth @@ -505,7 +777,7 @@ class Foo { exports[`with_numbers.js - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -546,7 +818,7 @@ obj = { exports[`with_numbers.js - {"quoteProps":"consistent","singleQuote":true} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" singleQuote: true @@ -580,7 +852,7 @@ obj = { obj = { 'foo-bar': '', - 1: '', + '1': '', }; ================================================================================ @@ -588,7 +860,7 @@ obj = { exports[`with_numbers.js - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -621,7 +893,7 @@ obj = { obj = { "foo-bar": "", - 1: "", + "1": "", }; ================================================================================ @@ -629,7 +901,7 @@ obj = { exports[`with_numbers.js - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["babel", "flow"] +parsers: ["babel"] printWidth: 80 quoteProps: "preserve" | printWidth diff --git a/tests/js/quote-props/jsfmt.spec.js b/tests/js/quote-props/jsfmt.spec.js index 2de1e7c2248f..9306f203b0e8 100644 --- a/tests/js/quote-props/jsfmt.spec.js +++ b/tests/js/quote-props/jsfmt.spec.js @@ -1,15 +1,16 @@ -run_spec(__dirname, ["babel", "flow"], { +run_spec(__dirname, ["babel"], { quoteProps: "as-needed", }); -run_spec(__dirname, ["babel", "flow"], { +run_spec(__dirname, ["babel"], { quoteProps: "preserve", }); -run_spec(__dirname, ["babel", "flow"], { +run_spec(__dirname, ["babel"], { quoteProps: "consistent", }); -run_spec(__dirname, ["babel", "flow"], { + +run_spec(__dirname, ["babel"], { quoteProps: "consistent", singleQuote: true, }); diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 9a3b9be0ad7a..cf2fdad6d30b 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -15,3 +15,37 @@ const d = { d1: "d1", 'd-2': "d2" }; + +const e = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, +} + +const f = { + "1": null, + "1E2": null, + "1e+3": null, + "1e+100": null, + "0b10": null, + "0o10": null, + "0xf": null, + "NaN": null, +} + +const g = { + 1: null, + 1E2: null, + 1e+3: null, + 1e+100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, + 'a-a': null, +} diff --git a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap index 5a1efc20b704..1847bbf04f52 100644 --- a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`types.ts - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["typescript"] +parsers: ["typescript", "babel-ts"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -25,7 +25,7 @@ type T = { type U = { 0: string; - "5": number; + 5: number; }; ================================================================================ @@ -33,7 +33,7 @@ type U = { exports[`types.ts - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["typescript"] +parsers: ["typescript", "babel-ts"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -56,7 +56,7 @@ type T = { type U = { 0: string; - "5": number; + 5: number; }; ================================================================================ @@ -64,7 +64,7 @@ type U = { exports[`types.ts - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["typescript"] +parsers: ["typescript", "babel-ts"] printWidth: 80 quoteProps: "preserve" | printWidth diff --git a/tests/typescript/quote-props/jsfmt.spec.js b/tests/typescript/quote-props/jsfmt.spec.js index c17eda737d71..d21a57629058 100644 --- a/tests/typescript/quote-props/jsfmt.spec.js +++ b/tests/typescript/quote-props/jsfmt.spec.js @@ -1,11 +1,11 @@ -run_spec(__dirname, ["typescript"], { +run_spec(__dirname, ["typescript", "babel-ts"], { quoteProps: "as-needed", }); -run_spec(__dirname, ["typescript"], { +run_spec(__dirname, ["typescript", "babel-ts"], { quoteProps: "preserve", }); -run_spec(__dirname, ["typescript"], { +run_spec(__dirname, ["typescript", "babel-ts"], { quoteProps: "consistent", }); From 8219665ba8cdd70402c50ac4ca5fe89149ce0dec Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 19:31:29 +0200 Subject: [PATCH 02/19] Add changelog entry --- changelog_unreleased/javascript/pr-8508.md | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 changelog_unreleased/javascript/pr-8508.md diff --git a/changelog_unreleased/javascript/pr-8508.md b/changelog_unreleased/javascript/pr-8508.md new file mode 100644 index 000000000000..18c507512450 --- /dev/null +++ b/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. + + +```jsx +// Input +x = { + "a": null, + "1": null, +}; + +// Prettier stable +x = { + a: null, + "1": null, +}; + +// Prettier master +x = { + a: null, + 1: null, +}; +``` From 3818eb6f3a035a23ad446495e00bc7ec069a7816 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 19:39:50 +0200 Subject: [PATCH 03/19] Add tests for escapes --- .../__snapshots__/jsfmt.spec.js.snap | 48 +++++++++++++++++++ tests/flow/quote-props/objects.js | 6 +++ .../__snapshots__/jsfmt.spec.js.snap | 48 +++++++++++++++++++ tests/js/quote-props/objects.js | 6 +++ 4 files changed, 108 insertions(+) diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index 37bfa574177f..fcb5c2af3398 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -204,6 +204,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -257,6 +263,12 @@ const b = { b: "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", c2: "c2", @@ -320,6 +332,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -373,6 +391,12 @@ const b = { b: 'b', }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': 'b', + '\\u0031': '1', +}; + const c = { c1: 'c1', c2: 'c2', @@ -435,6 +459,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -488,6 +518,12 @@ const b = { b: "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", c2: "c2", @@ -550,6 +586,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -603,6 +645,12 @@ const b = { "b": "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", "c2": "c2", diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js index cf2fdad6d30b..9834224c5d9b 100644 --- a/tests/flow/quote-props/objects.js +++ b/tests/flow/quote-props/objects.js @@ -6,6 +6,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\u0062': "b", + '\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 73f68f988a11..1ef03c19f320 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -204,6 +204,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -257,6 +263,12 @@ const b = { b: "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", c2: "c2", @@ -320,6 +332,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -373,6 +391,12 @@ const b = { b: 'b', }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': 'b', + '\\u0031': '1', +}; + const c = { c1: 'c1', c2: 'c2', @@ -435,6 +459,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -488,6 +518,12 @@ const b = { b: "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", c2: "c2", @@ -550,6 +586,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\\u0062': "b", + '\\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" @@ -603,6 +645,12 @@ const b = { "b": "b", }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + "\\u0062": "b", + "\\u0031": "1", +}; + const c = { c1: "c1", "c2": "c2", diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index cf2fdad6d30b..9834224c5d9b 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -6,6 +6,12 @@ const b = { 'b': "b" }; +const b2 = { + // Escapes should stay as escapes and not be unquoted. + '\u0062': "b", + '\u0031': "1" +}; + const c = { c1: "c1", 'c2': "c2" From 9be56ce9e75d15dee27946ac9e68433508747106 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 20:59:48 +0200 Subject: [PATCH 04/19] Fix idempotency --- src/language-js/printer-estree.js | 9 +++++++-- tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 444250aa6620..c81054cf4885 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3767,9 +3767,14 @@ function printPropertyKey(path, options, print) { (options.quoteProps === "consistent" && !needsQuoteProps.get(parent))) ) { // 'a' -> a - // '1e+100' -> 1e+100 + // '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" ); } diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 1ef03c19f320..23f28ed87b39 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -294,7 +294,7 @@ const f = { 1: null, "1E2": null, "1e+3": null, - 1e+100: null, + 1e100: null, "0b10": null, "0o10": null, "0xf": null, From 7c404b2d1ef1a2a2d4e09581845fd007a0a607bb Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 21:48:15 +0200 Subject: [PATCH 05/19] 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. --- tests/flow-repo/async_iteration/jsfmt.spec.js | 2 +- tests/flow-repo/declare_export/jsfmt.spec.js | 2 +- tests/flow-repo/def_site_variance/jsfmt.spec.js | 2 +- tests/flow-repo/dictionary/jsfmt.spec.js | 2 +- tests/flow-repo/es_declare_module/jsfmt.spec.js | 2 +- tests/flow-repo/esproposal_decorators.ignore/jsfmt.spec.js | 2 +- tests/flow-repo/esproposal_decorators.warn/jsfmt.spec.js | 2 +- tests/flow-repo/getters_and_setters_disabled/jsfmt.spec.js | 2 +- tests/flow-repo/getters_and_setters_enabled/jsfmt.spec.js | 2 +- tests/flow-repo/more_annot/jsfmt.spec.js | 2 +- tests/flow-repo/new_spread/jsfmt.spec.js | 2 +- tests/flow-repo/object-method/jsfmt.spec.js | 2 +- tests/flow-repo/predicates-abstract/jsfmt.spec.js | 2 +- tests/flow-repo/predicates-declared/jsfmt.spec.js | 2 +- tests/flow-repo/predicates-inferred/jsfmt.spec.js | 2 +- tests/flow-repo/predicates-parsing/jsfmt.spec.js | 2 +- tests/flow-repo/refinements/jsfmt.spec.js | 2 +- tests/flow-repo/typeapp_perf/jsfmt.spec.js | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/flow-repo/async_iteration/jsfmt.spec.js b/tests/flow-repo/async_iteration/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/async_iteration/jsfmt.spec.js +++ b/tests/flow-repo/async_iteration/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/declare_export/jsfmt.spec.js b/tests/flow-repo/declare_export/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/declare_export/jsfmt.spec.js +++ b/tests/flow-repo/declare_export/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/def_site_variance/jsfmt.spec.js b/tests/flow-repo/def_site_variance/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/def_site_variance/jsfmt.spec.js +++ b/tests/flow-repo/def_site_variance/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/dictionary/jsfmt.spec.js b/tests/flow-repo/dictionary/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/dictionary/jsfmt.spec.js +++ b/tests/flow-repo/dictionary/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/es_declare_module/jsfmt.spec.js b/tests/flow-repo/es_declare_module/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/es_declare_module/jsfmt.spec.js +++ b/tests/flow-repo/es_declare_module/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/esproposal_decorators.ignore/jsfmt.spec.js b/tests/flow-repo/esproposal_decorators.ignore/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/esproposal_decorators.ignore/jsfmt.spec.js +++ b/tests/flow-repo/esproposal_decorators.ignore/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/esproposal_decorators.warn/jsfmt.spec.js b/tests/flow-repo/esproposal_decorators.warn/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/esproposal_decorators.warn/jsfmt.spec.js +++ b/tests/flow-repo/esproposal_decorators.warn/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/getters_and_setters_disabled/jsfmt.spec.js b/tests/flow-repo/getters_and_setters_disabled/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/getters_and_setters_disabled/jsfmt.spec.js +++ b/tests/flow-repo/getters_and_setters_disabled/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/getters_and_setters_enabled/jsfmt.spec.js b/tests/flow-repo/getters_and_setters_enabled/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/getters_and_setters_enabled/jsfmt.spec.js +++ b/tests/flow-repo/getters_and_setters_enabled/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/more_annot/jsfmt.spec.js b/tests/flow-repo/more_annot/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/more_annot/jsfmt.spec.js +++ b/tests/flow-repo/more_annot/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/new_spread/jsfmt.spec.js b/tests/flow-repo/new_spread/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/new_spread/jsfmt.spec.js +++ b/tests/flow-repo/new_spread/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/object-method/jsfmt.spec.js b/tests/flow-repo/object-method/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/object-method/jsfmt.spec.js +++ b/tests/flow-repo/object-method/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/predicates-abstract/jsfmt.spec.js b/tests/flow-repo/predicates-abstract/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/predicates-abstract/jsfmt.spec.js +++ b/tests/flow-repo/predicates-abstract/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/predicates-declared/jsfmt.spec.js b/tests/flow-repo/predicates-declared/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/predicates-declared/jsfmt.spec.js +++ b/tests/flow-repo/predicates-declared/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/predicates-inferred/jsfmt.spec.js b/tests/flow-repo/predicates-inferred/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/predicates-inferred/jsfmt.spec.js +++ b/tests/flow-repo/predicates-inferred/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/predicates-parsing/jsfmt.spec.js b/tests/flow-repo/predicates-parsing/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/predicates-parsing/jsfmt.spec.js +++ b/tests/flow-repo/predicates-parsing/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/refinements/jsfmt.spec.js b/tests/flow-repo/refinements/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/refinements/jsfmt.spec.js +++ b/tests/flow-repo/refinements/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); diff --git a/tests/flow-repo/typeapp_perf/jsfmt.spec.js b/tests/flow-repo/typeapp_perf/jsfmt.spec.js index fbfa6501a049..b9a908981a50 100644 --- a/tests/flow-repo/typeapp_perf/jsfmt.spec.js +++ b/tests/flow-repo/typeapp_perf/jsfmt.spec.js @@ -1 +1 @@ -run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow"]); From 6e08c69ad25df525514a392da5f7336068273703 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 21:56:35 +0200 Subject: [PATCH 06/19] =?UTF-8?q?Don=E2=80=99t=20unquote=20negative=20numb?= =?UTF-8?q?ers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/language-js/utils.js | 3 +- .../__snapshots__/jsfmt.spec.js.snap | 40 +++++++++++++++++++ tests/flow/quote-props/objects.js | 5 +++ .../__snapshots__/jsfmt.spec.js.snap | 40 +++++++++++++++++++ tests/js/quote-props/objects.js | 5 +++ 5 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/language-js/utils.js b/src/language-js/utils.js index c6dc14a44db8..0662c4f7379f 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -746,7 +746,8 @@ function isStringPropSafeToUnquote(node, options) { (options.parser === "typescript" || options.parser === "babel-ts") && node.type === "ClassProperty" )) || - (String(Number(node.key.value)) === node.key.value && + (!node.key.value.startsWith("-") && + String(Number(node.key.value)) === node.key.value && !(options.parser === "flow" || options.parser === "babel-flow"))) ); } diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index fcb5c2af3398..f629cfb5c02d 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -254,6 +254,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -313,6 +318,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; @@ -382,6 +392,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: 'a', @@ -441,6 +456,11 @@ const g = { 'a-a': null, }; +const h = { + '-1': null, + '-0xa': null, +}; + ================================================================================ `; @@ -509,6 +529,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -568,6 +593,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; @@ -636,6 +666,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -695,6 +730,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js index 9834224c5d9b..434fa93da93a 100644 --- a/tests/flow/quote-props/objects.js +++ b/tests/flow/quote-props/objects.js @@ -55,3 +55,8 @@ const g = { NaN: null, 'a-a': null, } + +const h = { + "-1": null, + "-0xa": null, +} diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 23f28ed87b39..6ca7f4d0a7ab 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -254,6 +254,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -313,6 +318,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; @@ -382,6 +392,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: 'a', @@ -441,6 +456,11 @@ const g = { 'a-a': null, }; +const h = { + '-1': null, + '-0xa': null, +}; + ================================================================================ `; @@ -509,6 +529,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -568,6 +593,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; @@ -636,6 +666,11 @@ const g = { 'a-a': null, } +const h = { + "-1": null, + "-0xa": null, +} + =====================================output===================================== const a = { a: "a", @@ -695,6 +730,11 @@ const g = { "a-a": null, }; +const h = { + "-1": null, + "-0xa": null, +}; + ================================================================================ `; diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 9834224c5d9b..434fa93da93a 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -55,3 +55,8 @@ const g = { NaN: null, 'a-a': null, } + +const h = { + "-1": null, + "-0xa": null, +} From c2ac93e140b870c280f242c26cf161570739031a Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 22:11:01 +0200 Subject: [PATCH 07/19] =?UTF-8?q?Don=E2=80=99t=20quote/unquote=20numbers?= =?UTF-8?q?=20in=20TypeScript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/language-js/printer-estree.js | 5 +- src/language-js/utils.js | 26 ++++++++- .../__snapshots__/jsfmt.spec.js.snap | 8 +-- .../__snapshots__/jsfmt.spec.js.snap | 58 +++++++++---------- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 14 ++--- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 12 ++-- .../__snapshots__/jsfmt.spec.js.snap | 8 +-- .../__snapshots__/jsfmt.spec.js.snap | 20 +++---- .../__snapshots__/jsfmt.spec.js.snap | 10 ++-- .../__snapshots__/jsfmt.spec.js.snap | 12 ++-- .../__snapshots__/jsfmt.spec.js.snap | 24 ++++---- .../__snapshots__/jsfmt.spec.js.snap | 12 ++-- .../__snapshots__/jsfmt.spec.js.snap | 10 ++-- .../__snapshots__/jsfmt.spec.js.snap | 58 +++++++++---------- .../__snapshots__/jsfmt.spec.js.snap | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 10 ++-- 21 files changed, 164 insertions(+), 137 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index c81054cf4885..5c2810e35e02 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3737,7 +3737,10 @@ function printPropertyKey(path, options, print) { ((isStringLiteral(prop.key) && !isStringPropSafeToUnquote(prop, options)) || (isNumericLiteral(prop.key) && - (options.parser === "flow" || options.parser === "babel-flow"))) + (options.parser === "flow" || + options.parser === "babel-flow" || + options.parser === "typescript" || + options.parser === "babel-ts"))) ); needsQuoteProps.set(parent, objectHasStringProp); } diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 0662c4f7379f..084d84a2624f 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -734,6 +734,25 @@ function returnArgumentHasLeadingComment(options, argument) { return false; } +// Note: Quoting/unquoting numbers in TypeScript is not safe. +// +// let a = { 1: 1, 2: 2 } +// let b = { '1': 1, '2': 2 } +// +// declare let aa: keyof typeof a; +// declare let bb: keyof typeof b; +// +// aa = bb; +// ^^ +// Type '"1" | "2"' is not assignable to type '1 | 2'. +// Type '"1"' is not assignable to type '1 | 2'.(2322) +// +// And in Flow, you get: +// +// const x = { +// 0: 1 +// ^ Non-string literal property keys not supported. [unsupported-syntax] +// } function isStringPropSafeToUnquote(node, options) { return ( options.parser !== "json" && @@ -748,7 +767,12 @@ function isStringPropSafeToUnquote(node, options) { )) || (!node.key.value.startsWith("-") && String(Number(node.key.value)) === node.key.value && - !(options.parser === "flow" || options.parser === "babel-flow"))) + !( + options.parser === "flow" || + options.parser === "babel-flow" || + options.parser === "typescript" || + options.parser === "babel-ts" + ))) ); } diff --git a/tests/flow-repo/async_iteration/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/async_iteration/__snapshots__/jsfmt.spec.js.snap index f4d6c58e9b5d..38e7fa0ecf0b 100644 --- a/tests/flow-repo/async_iteration/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/async_iteration/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`delegate_yield.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -66,7 +66,7 @@ async function* delegate_return() { exports[`generator.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -128,7 +128,7 @@ async function f() { exports[`return.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -186,7 +186,7 @@ refuse_return() exports[`throw.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/declare_export/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/declare_export/__snapshots__/jsfmt.spec.js.snap index f639984646e7..49b7ccf7e94a 100644 --- a/tests/flow-repo/declare_export/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/declare_export/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`B.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -20,7 +20,7 @@ exports.numberValue = 42; exports[`C.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -34,7 +34,7 @@ printWidth: 80 exports[`CommonJS_Clobbering_Class.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -98,7 +98,7 @@ module.exports = Test; exports[`CommonJS_Clobbering_Lit.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -134,7 +134,7 @@ module.exports = { exports[`CommonJS_Named.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -166,7 +166,7 @@ exports.numberValue5 = 5; exports[`ES6_Default_AnonFunction1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -190,7 +190,7 @@ declare export default () => number; exports[`ES6_Default_AnonFunction2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -214,7 +214,7 @@ declare export default () => number; exports[`ES6_Default_NamedClass1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -252,7 +252,7 @@ declare export function getAFoo(): FooImpl; exports[`ES6_Default_NamedClass2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -278,7 +278,7 @@ declare export default class Foo { exports[`ES6_Default_NamedFunction1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -302,7 +302,7 @@ declare export default function foo(): number; exports[`ES6_Default_NamedFunction2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -326,7 +326,7 @@ declare export default function foo(): number; exports[`ES6_DefaultAndNamed.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -346,7 +346,7 @@ declare export var str: string; exports[`ES6_ExportAllFrom_Intermediary1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -370,7 +370,7 @@ declare export * from "ES6_ExportAllFrom_Source1" exports[`ES6_ExportAllFrom_Intermediary2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -394,7 +394,7 @@ declare export * from "ES6_ExportAllFrom_Source2" exports[`ES6_ExportAllFrom_Source1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -418,7 +418,7 @@ declare export var numberValue1: number; exports[`ES6_ExportAllFrom_Source2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -442,7 +442,7 @@ declare export var numberValue2: number; exports[`ES6_ExportAllFromMulti.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -462,7 +462,7 @@ declare export * from "./ES6_ExportAllFrom_Source2" exports[`ES6_ExportFrom_Intermediary1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -492,7 +492,7 @@ declare export { exports[`ES6_ExportFrom_Intermediary2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -522,7 +522,7 @@ declare export { exports[`ES6_ExportFrom_Source1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -548,7 +548,7 @@ declare export var numberValue2: number; exports[`ES6_ExportFrom_Source2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -574,7 +574,7 @@ declare export var numberValue2: number; exports[`ES6_Named1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -630,7 +630,7 @@ declare export var varDeclNumber2: number; exports[`ES6_Named2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -682,7 +682,7 @@ declare export var varDeclNumber4: number; exports[`ProvidesModuleA.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -714,7 +714,7 @@ exports.stringValue = "str"; exports[`ProvidesModuleCJSDefault.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -742,7 +742,7 @@ module.exports = { exports[`ProvidesModuleD.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -762,7 +762,7 @@ printWidth: 80 exports[`ProvidesModuleES6Default.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -794,7 +794,7 @@ export default { exports[`SideEffects.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -808,7 +808,7 @@ printWidth: 80 exports[`es6modules.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/def_site_variance/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/def_site_variance/__snapshots__/jsfmt.spec.js.snap index df0b791a501e..5257b2abf2ff 100644 --- a/tests/flow-repo/def_site_variance/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/def_site_variance/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`test.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/dictionary/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/dictionary/__snapshots__/jsfmt.spec.js.snap index f220f77220eb..4e23246fb417 100644 --- a/tests/flow-repo/dictionary/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/dictionary/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`any.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -24,7 +24,7 @@ const val: string = dict[k]; // error: number incompatible with string exports[`compatible.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -68,7 +68,7 @@ function foo2(x: { exports[`dictionary.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -676,7 +676,7 @@ function subtype_optional_c_to_dict(x: { p?: C }): { [k: string]: B } { exports[`incompatible.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -806,7 +806,7 @@ function foo8(x: { [key: string]: number }) { exports[`issue-1745.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -868,7 +868,7 @@ class B { exports[`test.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -900,7 +900,7 @@ module.exports = o; exports[`test_client.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/es_declare_module/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/es_declare_module/__snapshots__/jsfmt.spec.js.snap index 7f7dfc98ea77..59c9ab58ffa9 100644 --- a/tests/flow-repo/es_declare_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/es_declare_module/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`es_declare_module.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/esproposal_decorators.ignore/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/esproposal_decorators.ignore/__snapshots__/jsfmt.spec.js.snap index 4caf7e5e1442..f995a5198ba1 100644 --- a/tests/flow-repo/esproposal_decorators.ignore/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/esproposal_decorators.ignore/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`test.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/esproposal_decorators.warn/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/esproposal_decorators.warn/__snapshots__/jsfmt.spec.js.snap index 4caf7e5e1442..f995a5198ba1 100644 --- a/tests/flow-repo/esproposal_decorators.warn/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/esproposal_decorators.warn/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`test.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/getters_and_setters_disabled/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/getters_and_setters_disabled/__snapshots__/jsfmt.spec.js.snap index 40cbd33b2e08..1f28e5a98ab7 100644 --- a/tests/flow-repo/getters_and_setters_disabled/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/getters_and_setters_disabled/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`getters_and_setters.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/getters_and_setters_enabled/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/getters_and_setters_enabled/__snapshots__/jsfmt.spec.js.snap index e6d4f7dae78f..4f9f65456587 100644 --- a/tests/flow-repo/getters_and_setters_enabled/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/getters_and_setters_enabled/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`class.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -150,7 +150,7 @@ foo.propOverriddenWithSetter = 123; // Error number ~> string exports[`declare_class.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -256,7 +256,7 @@ foo.propOverriddenWithSetter = 123; // Error number ~> string exports[`object.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -412,7 +412,7 @@ var testExampleOrOrderOfGetterAndSetterReordered: number = exports[`object_type.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -531,7 +531,7 @@ function test(obj: T) { exports[`react.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -583,7 +583,7 @@ const Example = React.createClass({ exports[`variance.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/more_annot/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/more_annot/__snapshots__/jsfmt.spec.js.snap index 961350b22905..39e57a42df9b 100644 --- a/tests/flow-repo/more_annot/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/more_annot/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`client_object.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -20,7 +20,7 @@ var a: number = o.w.z.y; exports[`object.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -50,7 +50,7 @@ module.exports = o3; exports[`proto.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -76,7 +76,7 @@ var o2: Foo = new Foo(); exports[`super.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/new_spread/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/new_spread/__snapshots__/jsfmt.spec.js.snap index fe51266df7d7..ede5de668db0 100644 --- a/tests/flow-repo/new_spread/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/new_spread/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`type.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -208,7 +208,7 @@ declare var o13: O13; exports[`type_any.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -246,7 +246,7 @@ var o3: O3 = (0: mixed); // ok exports[`type_contra.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -284,7 +284,7 @@ declare var o2: O2; exports[`type_dict.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -372,7 +372,7 @@ declare var o12: { ...{| [string]: T |}, ...{| [string]: U |} }; exports[`type_generic.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -390,7 +390,7 @@ declare function spread(a: A, b: B): { ...A, ...B }; exports[`type_instance.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -430,7 +430,7 @@ declare var o2: O2; exports[`type_intersection.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -498,7 +498,7 @@ declare var o5: O5; exports[`type_intersection_optional.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -640,7 +640,7 @@ declare var o21: { ...{| p: T |} & { q: U } }; exports[`type_optional.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -712,7 +712,7 @@ declare var p: { ...{| p?: T |}, ...{| p?: U |} }; exports[`type_union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/object-method/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/object-method/__snapshots__/jsfmt.spec.js.snap index 9d91418b0cf5..b0743465ac67 100644 --- a/tests/flow-repo/object-method/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/object-method/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`id.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -20,7 +20,7 @@ module.exports = id; exports[`subtype.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -46,7 +46,7 @@ function subtypeCheck(x: Interface): ObjectType { exports[`test.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -82,7 +82,7 @@ module.exports = id(methodCaller); exports[`test2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -128,7 +128,7 @@ b.f(); // error, property \`p\` not found exports[`test3.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/predicates-abstract/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/predicates-abstract/__snapshots__/jsfmt.spec.js.snap index a5882c7cc8fa..60b9eca8af68 100644 --- a/tests/flow-repo/predicates-abstract/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/predicates-abstract/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`filter.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -43,7 +43,7 @@ function is_string(x): %checks { exports[`filter-union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -91,7 +91,7 @@ declare var ab: Array; exports[`refine.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -198,7 +198,7 @@ function is_string_and_number(x, y): %checks { exports[`sanity-filter.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -256,7 +256,7 @@ function is_string_regular(x): boolean { exports[`sanity-filter-union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -304,7 +304,7 @@ declare var ab: Array; exports[`sanity-refine.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/predicates-declared/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/predicates-declared/__snapshots__/jsfmt.spec.js.snap index 30d791faabee..8e2667b37b7e 100644 --- a/tests/flow-repo/predicates-declared/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/predicates-declared/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`function-bind.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -95,7 +95,7 @@ if (m.bind(this)(x)) { exports[`function-union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -142,7 +142,7 @@ function foo(x: number | string | Array): number { exports[`is-string-decl.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -190,7 +190,7 @@ function foo(x: string | Array): string { exports[`logical-or.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -248,7 +248,7 @@ Number(dollars) || 0; exports[`object-invariant.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -307,7 +307,7 @@ function f(_this: { m: ?Meeting }): string { exports[`orig-string-tag-check.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -342,7 +342,7 @@ function foo(x: string | Array): string { exports[`sanity-fall-through.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -380,7 +380,7 @@ function foo(s: Array): string { exports[`sanity-invalid-calls.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -429,7 +429,7 @@ function foo(s: Array): string { exports[`sanity-is-string-bug.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -471,7 +471,7 @@ function bar(x: string | Array): string { exports[`sanity-parameter-mismatch.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -503,7 +503,7 @@ foo(3, 3); exports[`sanity-pred-with-body.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -546,7 +546,7 @@ function foo(x: string | Array): string { exports[`sanity-return-type.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/predicates-inferred/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/predicates-inferred/__snapshots__/jsfmt.spec.js.snap index 1f6b4f5b7aee..5d809954dc0a 100644 --- a/tests/flow-repo/predicates-inferred/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/predicates-inferred/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`sanity.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -68,7 +68,7 @@ function bak(z: string | number): number { exports[`sanity-multi-params.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -108,7 +108,7 @@ function foo(x: string | Array): string { exports[`sanity-ordering.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -156,7 +156,7 @@ function dotAccess(head, create) { exports[`sanity-unbound-var.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -202,7 +202,7 @@ function foo(x: string | Array): string { exports[`simple-predicate-func.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -360,7 +360,7 @@ declare function from_two_strings(x: string, y: string): void; exports[`simple-predicate-func-post.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/predicates-parsing/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/predicates-parsing/__snapshots__/jsfmt.spec.js.snap index 5903e2befe61..4606c42d5755 100644 --- a/tests/flow-repo/predicates-parsing/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/predicates-parsing/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`fail-0.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -24,7 +24,7 @@ declare function f2(x: mixed): boolean %checks; exports[`fail-1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -46,7 +46,7 @@ function f6(x: mixed): %checks(x !== null) {} exports[`fail-2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -70,7 +70,7 @@ var a2 = (x: mixed): %checks(x !== null) => { exports[`fail-3.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -98,7 +98,7 @@ var a2 = (x: mixed): %checks(x !== null) => x !== null; exports[`pass.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/refinements/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/refinements/__snapshots__/jsfmt.spec.js.snap index 36f4985e63ac..26d568c5696f 100644 --- a/tests/flow-repo/refinements/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/refinements/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`assignment.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -80,7 +80,7 @@ function bar2(x: Bar) { exports[`ast_node.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -114,7 +114,7 @@ export type ASTNode = Node1 | Node2; exports[`bool.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -218,7 +218,7 @@ let tests = [ exports[`computed_string_literal.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -268,7 +268,7 @@ function testLiteralProperty(a: A) { exports[`cond_prop.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -382,7 +382,7 @@ let tests = [ exports[`constants.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -402,7 +402,7 @@ export const ERROR: "ERROR" = "ERROR"; exports[`eq.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -465,7 +465,7 @@ let tests = [ exports[`exists.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -507,7 +507,7 @@ function foo2(x: ?Class): string { exports[`func_call.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -543,7 +543,7 @@ let tests = [ exports[`hasOwnProperty.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -597,7 +597,7 @@ function bar(x: Object) { exports[`heap_defassign.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -777,7 +777,7 @@ function def_assign_setprop_nohavoc(obj: Obj, obj2: Obj2) { exports[`lib.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -795,7 +795,7 @@ declare var BAZ: { stuff?: (x: number) => void } | void; exports[`missing-property-cond.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -943,7 +943,7 @@ function foo9() { exports[`mixed.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1197,7 +1197,7 @@ function arr0(x: mixed) { exports[`node1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1211,7 +1211,7 @@ module.exports = "Node1"; exports[`not.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1327,7 +1327,7 @@ let tests = [ exports[`null.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1359,7 +1359,7 @@ function null_bogus_comparison() { exports[`number.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1613,7 +1613,7 @@ let tests = [ exports[`property.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1792,7 +1792,7 @@ function c3( exports[`refinements.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -1962,7 +1962,7 @@ function global_in_conditional2(x: number) { exports[`string.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -2242,7 +2242,7 @@ let tests = [ exports[`super_member.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -2307,7 +2307,7 @@ class C extends A { exports[`switch.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -2429,7 +2429,7 @@ function corge(text: string | number | Array): string { exports[`tagged_union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -3006,7 +3006,7 @@ let tests = [ exports[`tagged_union_import.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -3064,7 +3064,7 @@ function handleStatus(status: Success | Error) { exports[`typeof.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -3204,7 +3204,7 @@ function testInvalidTemplateLiteral(x: string | number) { exports[`undef.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -3376,7 +3376,7 @@ function undef_bogus_comparison() { exports[`union.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -3430,7 +3430,7 @@ function baz(x: ?thing) { exports[`void.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/flow-repo/typeapp_perf/__snapshots__/jsfmt.spec.js.snap b/tests/flow-repo/typeapp_perf/__snapshots__/jsfmt.spec.js.snap index 29c42ac95f94..e49af0223d86 100644 --- a/tests/flow-repo/typeapp_perf/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow-repo/typeapp_perf/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`test1.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== @@ -54,7 +54,7 @@ declare var b: B; exports[`test2.js format 1`] = ` ====================================options===================================== -parsers: ["flow", "babel"] +parsers: ["flow"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap index 1847bbf04f52..a984b52d4ec0 100644 --- a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -25,7 +25,7 @@ type T = { type U = { 0: string; - 5: number; + "5": number; }; ================================================================================ @@ -50,13 +50,13 @@ type U = { =====================================output===================================== type T = { - 0: string; - 5: number; + "0": string; + "5": number; }; type U = { - 0: string; - 5: number; + "0": string; + "5": number; }; ================================================================================ From cfe706de3c904dcabc3e6079ac9ed4207e45b8bc Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 22:18:28 +0200 Subject: [PATCH 08/19] Quote BigInt --- src/language-js/printer-estree.js | 5 +++- .../__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ tests/js/quote-props/objects.js | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 5c2810e35e02..a0ffbb42ee1c 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3746,12 +3746,15 @@ function printPropertyKey(path, options, print) { } if ( - (key.type === "Identifier" || isNumericLiteral(key)) && + (key.type === "Identifier" || + isNumericLiteral(key) || + key.type === "BigIntLiteral") && (options.parser === "json" || (options.quoteProps === "consistent" && needsQuoteProps.get(parent))) ) { // a -> "a" // 1e2 -> "100" + // 2n -> "2" const prop = printString( JSON.stringify( key.type === "Identifier" ? key.name : key.value.toString() diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 6ca7f4d0a7ab..ebe6801f5689 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -228,6 +228,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, } @@ -239,6 +240,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, } @@ -251,6 +253,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, 'a-a': null, } @@ -292,6 +295,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, }; @@ -303,6 +307,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, NaN: null, }; @@ -315,6 +320,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, "a-a": null, }; @@ -366,6 +372,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, } @@ -377,6 +384,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, } @@ -389,6 +397,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, 'a-a': null, } @@ -430,6 +439,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, }; @@ -441,6 +451,7 @@ const f = { '0b10': null, '0o10': null, '0xf': null, + '2n': null, 'NaN': null, }; @@ -453,6 +464,7 @@ const g = { '8': null, '15': null, 'NaN': null, + '2': null, 'a-a': null, }; @@ -503,6 +515,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, } @@ -514,6 +527,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, } @@ -526,6 +540,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, 'a-a': null, } @@ -567,6 +582,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, }; @@ -578,6 +594,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, }; @@ -590,6 +607,7 @@ const g = { "8": null, "15": null, "NaN": null, + "2": null, "a-a": null, }; @@ -640,6 +658,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, } @@ -651,6 +670,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, } @@ -663,6 +683,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, 'a-a': null, } @@ -704,6 +725,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, }; @@ -715,6 +737,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, }; @@ -727,6 +750,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, "a-a": null, }; diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 434fa93da93a..4a7cd4854315 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -30,6 +30,7 @@ const e = { 0b10: null, 0o10: null, 0xf: null, + 2n: null, NaN: null, } @@ -41,6 +42,7 @@ const f = { "0b10": null, "0o10": null, "0xf": null, + "2n": null, "NaN": null, } @@ -53,6 +55,7 @@ const g = { 0o10: null, 0xf: null, NaN: null, + 2n: null, 'a-a': null, } From cd55145b3b96ea3d1bb7df913fbff220e93dfd35 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 22:29:26 +0200 Subject: [PATCH 09/19] Fix AST check --- src/language-js/clean.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/language-js/clean.js b/src/language-js/clean.js index 55432b05ddd5..c955f9dc435d 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -78,7 +78,9 @@ function clean(ast, newObj, parent) { delete newObj.closingElement; } - // We change {'key': value} into {key: value} + // We change {'key': value} into {key: value}. + // And {key: value} into {'key': value}. + // Also for number keys. if ( (ast.type === "Property" || ast.type === "ObjectProperty" || @@ -90,6 +92,7 @@ function clean(ast, newObj, parent) { ast.key && (ast.key.type === "Literal" || ast.key.type === "NumericLiteral" || + ast.key.type === "BigIntLiteral" || ast.key.type === "StringLiteral" || ast.key.type === "Identifier") ) { From 9544cfa99864ec4b1a2c7e4ceef86196ae9f291e Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 6 Jun 2020 23:58:59 +0200 Subject: [PATCH 10/19] Fix condition --- src/language-js/printer-estree.js | 13 ++--- .../__snapshots__/jsfmt.spec.js.snap | 48 +++++++++---------- .../__snapshots__/jsfmt.spec.js.snap | 6 +-- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index a0ffbb42ee1c..0628724beb0e 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3734,21 +3734,16 @@ function printPropertyKey(path, options, print) { (prop) => !prop.computed && prop.key && - ((isStringLiteral(prop.key) && - !isStringPropSafeToUnquote(prop, options)) || - (isNumericLiteral(prop.key) && - (options.parser === "flow" || - options.parser === "babel-flow" || - options.parser === "typescript" || - options.parser === "babel-ts"))) + isStringLiteral(prop.key) && + !isStringPropSafeToUnquote(prop, options) ); needsQuoteProps.set(parent, objectHasStringProp); } if ( (key.type === "Identifier" || - isNumericLiteral(key) || - key.type === "BigIntLiteral") && + ((isNumericLiteral(key) || key.type === "BigIntLiteral") && + !(options.parser === "typescript" || options.parser === "babel-ts"))) && (options.parser === "json" || (options.quoteProps === "consistent" && needsQuoteProps.get(parent))) ) { diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index f629cfb5c02d..81235755f715 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -423,14 +423,14 @@ const d = { }; const e = { - '1': null, - '100': null, - '1000': null, - '1e+100': null, - '2': null, - '8': null, - '15': null, - 'NaN': null, + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, }; const f = { @@ -560,14 +560,14 @@ const d = { }; const e = { - "1": null, - "100": null, - "1000": null, - "1e+100": null, - "2": null, - "8": null, - "15": null, - "NaN": null, + 1: null, + 1e2: null, + 1e3: null, + 1e100: null, + 0b10: null, + 0o10: null, + 0xf: null, + NaN: null, }; const f = { @@ -929,13 +929,13 @@ obj = { =====================================output===================================== obj = { - 'foo': '', - '1': '', + foo: '', + 1: '', }; obj = { - 'bar': '', - '1': '', + bar: '', + 1: '', }; obj = { @@ -970,13 +970,13 @@ obj = { =====================================output===================================== obj = { - "foo": "", - "1": "", + foo: "", + 1: "", }; obj = { - "bar": "", - "1": "", + bar: "", + 1: "", }; obj = { diff --git a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap index a984b52d4ec0..a50916094150 100644 --- a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -50,12 +50,12 @@ type U = { =====================================output===================================== type T = { - "0": string; - "5": number; + 0: string; + 5: number; }; type U = { - "0": string; + 0: string; "5": number; }; From 63c7dcf6203311a686ef22a5efd312aa5e932050 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 7 Jun 2020 11:04:38 +0200 Subject: [PATCH 11/19] Fix BigInt to string --- src/language-js/printer-estree.js | 7 +++- .../__snapshots__/jsfmt.spec.js.snap | 36 +++++++++++++++++++ tests/js/quote-props/objects.js | 4 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 0628724beb0e..65b2fe66d7b1 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3750,9 +3750,14 @@ function printPropertyKey(path, options, print) { // a -> "a" // 1e2 -> "100" // 2n -> "2" + // 0b10n -> "2" const prop = printString( JSON.stringify( - key.type === "Identifier" ? key.name : key.value.toString() + key.type === "Identifier" + ? key.name + : key.type === "BigIntLiteral" + ? BigInt(key.value).toString() + : key.value.toString() ), options ); diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index ebe6801f5689..1cddfbf4c0c1 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -262,6 +262,10 @@ const h = { "-0xa": null, } +Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, + 0xan:3, + 0b100000000000_000000000000000011n:4}) + =====================================output===================================== const a = { a: "a", @@ -329,6 +333,14 @@ const h = { "-0xa": null, }; +Object.keys({ + "g-": 4, + 0xb_b: 88, + 0xb_b_bn: 333, + 0xan: 3, + 0b100000000000_000000000000000011n: 4, +}); + ================================================================================ `; @@ -406,6 +418,10 @@ const h = { "-0xa": null, } +Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, + 0xan:3, + 0b100000000000_000000000000000011n:4}) + =====================================output===================================== const a = { a: 'a', @@ -473,6 +489,8 @@ const h = { '-0xa': null, }; +Object.keys({ 'g-': 4, '187': 88, '3003': 333, '10': 3, '536870915': 4 }); + ================================================================================ `; @@ -549,6 +567,10 @@ const h = { "-0xa": null, } +Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, + 0xan:3, + 0b100000000000_000000000000000011n:4}) + =====================================output===================================== const a = { a: "a", @@ -616,6 +638,8 @@ const h = { "-0xa": null, }; +Object.keys({ "g-": 4, "187": 88, "3003": 333, "10": 3, "536870915": 4 }); + ================================================================================ `; @@ -692,6 +716,10 @@ const h = { "-0xa": null, } +Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, + 0xan:3, + 0b100000000000_000000000000000011n:4}) + =====================================output===================================== const a = { a: "a", @@ -759,6 +787,14 @@ const h = { "-0xa": null, }; +Object.keys({ + "g-": 4, + 0xb_b: 88, + 0xb_b_bn: 333, + 0xan: 3, + 0b100000000000_000000000000000011n: 4, +}); + ================================================================================ `; diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 4a7cd4854315..565ca3959fd6 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -63,3 +63,7 @@ const h = { "-1": null, "-0xa": null, } + +Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, + 0xan:3, + 0b100000000000_000000000000000011n:4}) From 9ced300b5f70f6efd928740ae5d1045bcf3d8c34 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 7 Jun 2020 11:18:27 +0200 Subject: [PATCH 12/19] Fix ESLint not recognizing BigInt --- .eslintrc.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index d69d0478a653..7c07d6a454e0 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,12 +1,10 @@ root: true env: - es6: true + es2020: true node: true extends: - eslint:recommended - plugin:prettier/recommended -parserOptions: - ecmaVersion: 2018 plugins: - import - unicorn From faa644ce80c1be6f8be5a09e623f93d825842604 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 13 Jun 2020 12:05:11 +0200 Subject: [PATCH 13/19] =?UTF-8?q?Only=20quote/unquote=20=E2=80=9Csimple?= =?UTF-8?q?=E2=80=9D=20numbers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog_unreleased/javascript/pr-8508.md | 16 + src/language-js/printer-estree.js | 20 +- src/language-js/utils.js | 8 +- .../__snapshots__/jsfmt.spec.js.snap | 552 +++++++++++++----- tests/flow/quote-props/objects.js | 65 ++- .../__snapshots__/jsfmt.spec.js.snap | 526 +++++++++++------ tests/js/quote-props/objects.js | 64 +- 7 files changed, 900 insertions(+), 351 deletions(-) diff --git a/changelog_unreleased/javascript/pr-8508.md b/changelog_unreleased/javascript/pr-8508.md index 18c507512450..b4a449b25266 100644 --- a/changelog_unreleased/javascript/pr-8508.md +++ b/changelog_unreleased/javascript/pr-8508.md @@ -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!) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 65b2fe66d7b1..4a5e41810d9e 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -72,6 +72,7 @@ const { isObjectType, isObjectTypePropertyAFunction, isSimpleFlowType, + isSimpleNumber, isSimpleTemplateLiteral, isStringLiteral, isStringPropSafeToUnquote, @@ -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 ); @@ -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( diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 084d84a2624f..8362437c69e6 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -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" || @@ -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) @@ -1128,6 +1133,7 @@ module.exports = { isObjectType, isObjectTypePropertyAFunction, isSimpleFlowType, + isSimpleNumber, isSimpleTemplateLiteral, isStringLiteral, isStringPropSafeToUnquote, diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index 81235755f715..bf06f864f5a0 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -220,43 +220,78 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, } const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 'a-a': null, + "2n": null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } =====================================output===================================== @@ -284,43 +319,78 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, }; const f = { + // This should be unquoted for quoteProps=as-needed. + NaN: null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - NaN: null, -}; - -const g = { - 1: null, - 1e2: null, - 1e3: null, - 1e100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - "a-a": null, -}; - -const h = { + "2n": null, +}; + +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + NaN: "NaN", + 1: "1", + 1.5: "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: "0xb_b", // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}); + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; ================================================================================ @@ -358,43 +428,78 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, } const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, + "2n": null, } -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 'a-a': null, -} - -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } =====================================output===================================== @@ -422,43 +527,78 @@ const d = { 'd-2': 'd2', }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, }; const f = { + // This should be unquoted for quoteProps=as-needed. + 'NaN': null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: '1': null, + '1.5': null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + '1.0': null, + '999999999999999999999': null, + '0.99999999999999999': null, '1E2': null, '1e+3': null, '1e+100': null, '0b10': null, '0o10': null, '0xf': null, - 'NaN': null, -}; - -const g = { - '1': null, - '100': null, - '1000': null, - '1e+100': null, - '2': null, - '8': null, - '15': null, - 'NaN': null, - 'a-a': null, -}; - -const h = { + '2n': null, +}; + +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + 'NaN': 'NaN', + '1': '1', + '1.5': '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1e2: '1E2', // 100 + 1e3: '1e+3', // 1000 + 1e100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}); + +// Negative numbers cannot be unquoted. +!{ '-1': null, - '-0xa': null, + '-1.5': null, }; ================================================================================ @@ -495,43 +635,78 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, } const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 'a-a': null, + "2n": null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } =====================================output===================================== @@ -559,43 +734,78 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, }; const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, -}; - -const g = { - "1": null, - "100": null, - "1000": null, - "1e+100": null, - "2": null, - "8": null, - "15": null, - "NaN": null, - "a-a": null, -}; - -const h = { + "2n": null, +}; + +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + "NaN": "NaN", + "1": "1", + "1.5": "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: "0xb_b", // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}); + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; ================================================================================ @@ -632,43 +842,78 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, } const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 'a-a': null, + "2n": null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } =====================================output===================================== @@ -696,43 +941,78 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, }; const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, -}; - -const g = { - 1: null, - 1e2: null, - 1e3: null, - 1e100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - "a-a": null, -}; - -const h = { + "2n": null, +}; + +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + NaN: "NaN", + 1: "1", + 1.5: "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: "0xb_b", // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}); + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; ================================================================================ diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js index 434fa93da93a..fb9b080d5905 100644 --- a/tests/flow/quote-props/objects.js +++ b/tests/flow/quote-props/objects.js @@ -22,41 +22,76 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, 0b10: null, 0o10: null, 0xf: null, - NaN: null, + // Commented out because Flow does not parse BigInt as object keys. + // 2n: null, } const f = { + // This should be unquoted for quoteProps=as-needed. + "NaN": null, + // Flow does parses number keys, but errors on them during type checking so + // don’t unquote them: "1": null, + "1.5": null, + // These should never be unquoted. `1e+100` technically could (it’s the only + // one where `String(Number(key)) === key`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, "0b10": null, "0o10": null, "0xf": null, - "NaN": null, + "2n": null, } -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 'a-a': null, -} +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + // Commented out because Flow does not parse BigInt as object keys. + // 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + // 0xb_b_bn: '0xb_b_bn', // 3003 + // 0xan: '0xan', // 10 + // 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) -const h = { +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 1cddfbf4c0c1..f4a8eb8ae96f 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -220,8 +220,14 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, @@ -229,11 +235,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, } const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -241,31 +255,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - 'a-a': null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } -Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, - 0xan:3, - 0b100000000000_000000000000000011n:4}) - =====================================output===================================== const a = { a: "a", @@ -291,8 +315,14 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, @@ -300,47 +330,61 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, }; const f = { + // These should be unquoted for quoteProps=as-needed. + NaN: null, 1: null, + 1.5: null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, - 1e100: null, + "1e+100": null, "0b10": null, "0o10": null, "0xf": null, "2n": null, - NaN: null, }; -const g = { - 1: null, - 1e2: null, - 1e3: null, - 1e100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - "a-a": null, -}; +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + NaN: "NaN", + 1: "1", + 1.5: "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + 2n: "2n", // 2 + 0xb_b: "0xb_b", // 187 + 0xb_b_bn: "0xb_b_bn", // 3003 + 0xan: "0xan", // 10 + 0b100000000000_000000000000000011n: "0b100000000000_000000000000000011n", // 536870915 +}); -const h = { +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; -Object.keys({ - "g-": 4, - 0xb_b: 88, - 0xb_b_bn: 333, - 0xan: 3, - 0b100000000000_000000000000000011n: 4, -}); - ================================================================================ `; @@ -376,8 +420,14 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, @@ -385,11 +435,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, } const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -397,31 +455,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - 'a-a': null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } -Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, - 0xan:3, - 0b100000000000_000000000000000011n:4}) - =====================================output===================================== const a = { a: 'a', @@ -447,8 +515,14 @@ const d = { 'd-2': 'd2', }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, @@ -456,11 +530,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, }; const f = { + // These should be unquoted for quoteProps=as-needed. + 'NaN': null, '1': null, + '1.5': null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + '1.0': null, + '999999999999999999999': null, + '0.99999999999999999': null, '1E2': null, '1e+3': null, '1e+100': null, @@ -468,29 +550,41 @@ const f = { '0o10': null, '0xf': null, '2n': null, - 'NaN': null, }; -const g = { - '1': null, - '100': null, - '1000': null, - '1e+100': null, - '2': null, - '8': null, - '15': null, - 'NaN': null, - '2': null, - 'a-a': null, -}; +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + 'NaN': 'NaN', + '1': '1', + '1.5': '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1e2: '1E2', // 100 + 1e3: '1e+3', // 1000 + 1e100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n', // 536870915 +}); -const h = { +// Negative numbers cannot be unquoted. +!{ '-1': null, - '-0xa': null, + '-1.5': null, }; -Object.keys({ 'g-': 4, '187': 88, '3003': 333, '10': 3, '536870915': 4 }); - ================================================================================ `; @@ -525,8 +619,14 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, @@ -534,11 +634,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, } const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -546,31 +654,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, -} - -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - 'a-a': null, } -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } -Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, - 0xan:3, - 0b100000000000_000000000000000011n:4}) - =====================================output===================================== const a = { a: "a", @@ -596,8 +714,14 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, @@ -605,11 +729,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, }; const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -617,29 +749,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, }; -const g = { - "1": null, - "100": null, - "1000": null, - "1e+100": null, - "2": null, - "8": null, - "15": null, - "NaN": null, - "2": null, - "a-a": null, -}; +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + "NaN": "NaN", + "1": "1", + "1.5": "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + 2n: "2n", // 2 + 0xb_b: "0xb_b", // 187 + 0xb_b_bn: "0xb_b_bn", // 3003 + 0xan: "0xan", // 10 + 0b100000000000_000000000000000011n: "0b100000000000_000000000000000011n", // 536870915 +}); -const h = { +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; -Object.keys({ "g-": 4, "187": 88, "3003": 333, "10": 3, "536870915": 4 }); - ================================================================================ `; @@ -674,8 +818,14 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, @@ -683,11 +833,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, } const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -695,31 +853,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, } -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - 'a-a': null, -} - -const h = { +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) + +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } -Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, - 0xan:3, - 0b100000000000_000000000000000011n:4}) - =====================================output===================================== const a = { a: "a", @@ -745,8 +913,14 @@ const d = { "d-2": "d2", }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1e2: null, 1e3: null, 1e100: null, @@ -754,11 +928,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, }; const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. \`1e+100\` technically could (it’s the only + // one where \`String(Number(key)) === key\`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -766,35 +948,41 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, }; -const g = { - 1: null, - 1e2: null, - 1e3: null, - 1e100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - "a-a": null, -}; +Object.entries({ + // To force quotes for quoteProps=consistent. + "a-": "a-", + // These can be quoted: + NaN: "NaN", + 1: "1", + 1.5: "1.5", + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: "1.0", // 1 + 999999999999999999999: "999999999999999999999", // 1e+21 + 0.99999999999999999: "0.99999999999999999", // 1 + 1e2: "1E2", // 100 + 1e3: "1e+3", // 1000 + 1e100: "1e+100", // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: "0b10", // 2 + 0o10: "0o10", // 8 + 0xf: "0xf", // 15 + 2n: "2n", // 2 + 0xb_b: "0xb_b", // 187 + 0xb_b_bn: "0xb_b_bn", // 3003 + 0xan: "0xan", // 10 + 0b100000000000_000000000000000011n: "0b100000000000_000000000000000011n", // 536870915 +}); -const h = { +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, }; -Object.keys({ - "g-": 4, - 0xb_b: 88, - 0xb_b_bn: 333, - 0xan: 3, - 0b100000000000_000000000000000011n: 4, -}); - ================================================================================ `; diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 565ca3959fd6..6bfc99dd21a0 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -22,8 +22,14 @@ const d = { 'd-2': "d2" }; +// None of these should become quoted, regardless of the quoteProps value. const e = { + NaN: null, 1: null, + 1.5: null, + 1.0: null, + 999999999999999999999: null, + 0.99999999999999999: null, 1E2: null, 1e+3: null, 1e+100: null, @@ -31,11 +37,19 @@ const e = { 0o10: null, 0xf: null, 2n: null, - NaN: null, } const f = { + // These should be unquoted for quoteProps=as-needed. + "NaN": null, "1": null, + "1.5": null, + // These should never be unquoted. `1e+100` technically could (it’s the only + // one where `String(Number(key)) === key`), but we came to the conclusion + // that it is unexpected. + "1.0": null, + "999999999999999999999": null, + "0.99999999999999999": null, "1E2": null, "1e+3": null, "1e+100": null, @@ -43,27 +57,37 @@ const f = { "0o10": null, "0xf": null, "2n": null, - "NaN": null, } -const g = { - 1: null, - 1E2: null, - 1e+3: null, - 1e+100: null, - 0b10: null, - 0o10: null, - 0xf: null, - NaN: null, - 2n: null, - 'a-a': null, -} +Object.entries({ + // To force quotes for quoteProps=consistent. + 'a-': 'a-', + // These can be quoted: + NaN: 'NaN', + 1: '1', + 1.5: '1.5', + // These should never be quoted. The _actual_ keys are shown as comments. + // Copy-paste this into the console to verify. If we were to convert these + // numbers into decimal (which completely valid), “information/intent” is + // lost. Either way, writing code like this is super confusing. + 1.0: '1.0', // 1 + 999999999999999999999: '999999999999999999999', // 1e+21 + 0.99999999999999999: '0.99999999999999999', // 1 + 1E2: '1E2', // 100 + 1e+3: '1e+3', // 1000 + 1e+100: '1e+100', // 1e+100 – this one is identical, but would be inconsistent to quote. + 0b10: '0b10', // 2 + 0o10: '0o10', // 8 + 0xf: '0xf', // 15 + 2n: '2n', // 2 + 0xb_b: '0xb_b', // 187 + 0xb_b_bn: '0xb_b_bn', // 3003 + 0xan: '0xan', // 10 + 0b100000000000_000000000000000011n: '0b100000000000_000000000000000011n' // 536870915 +}) -const h = { +// Negative numbers cannot be unquoted. +!{ "-1": null, - "-0xa": null, + "-1.5": null, } - -Object.keys({'g-':4,0xb_b:88,0xb_b_bn:333, - 0xan:3, - 0b100000000000_000000000000000011n:4}) From 83bbfd384a8b2b6a4ee395f1141412715caf8140 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 13 Jun 2020 12:07:19 +0200 Subject: [PATCH 14/19] No need to clean BigIntLiteral anymore --- src/language-js/clean.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/language-js/clean.js b/src/language-js/clean.js index c955f9dc435d..53f54e567533 100644 --- a/src/language-js/clean.js +++ b/src/language-js/clean.js @@ -80,7 +80,7 @@ function clean(ast, newObj, parent) { // We change {'key': value} into {key: value}. // And {key: value} into {'key': value}. - // Also for number keys. + // Also for (some) number keys. if ( (ast.type === "Property" || ast.type === "ObjectProperty" || @@ -92,7 +92,6 @@ function clean(ast, newObj, parent) { ast.key && (ast.key.type === "Literal" || ast.key.type === "NumericLiteral" || - ast.key.type === "BigIntLiteral" || ast.key.type === "StringLiteral" || ast.key.type === "Identifier") ) { From b47caec20dda7e24ffba4e53fba8d9affe5e611b Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 13 Jun 2020 13:57:58 +0200 Subject: [PATCH 15/19] Add tests for 1. and .1 --- .../__snapshots__/jsfmt.spec.js.snap | 48 +++++++++++++++++++ tests/flow/quote-props/objects.js | 6 +++ .../__snapshots__/jsfmt.spec.js.snap | 48 +++++++++++++++++++ tests/js/quote-props/objects.js | 6 +++ 4 files changed, 108 insertions(+) diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index bf06f864f5a0..90d57f3ef892 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -226,6 +226,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -249,6 +251,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -272,6 +276,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -325,6 +331,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -348,6 +356,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -371,6 +381,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -434,6 +446,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -457,6 +471,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -480,6 +496,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -533,6 +551,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -556,6 +576,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. '1.0': null, + '.1': null, + '1.': null, '999999999999999999999': null, '0.99999999999999999': null, '1E2': null, @@ -579,6 +601,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + 0.1: '.1', // 0.1 + 1: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1e2: '1E2', // 100 @@ -641,6 +665,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -664,6 +690,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -687,6 +715,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -740,6 +770,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -763,6 +795,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -786,6 +820,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -848,6 +884,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -871,6 +909,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -894,6 +934,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -947,6 +989,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -970,6 +1014,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -993,6 +1039,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js index fb9b080d5905..1415f1c3b9aa 100644 --- a/tests/flow/quote-props/objects.js +++ b/tests/flow/quote-props/objects.js @@ -28,6 +28,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -51,6 +53,8 @@ const f = { // one where `String(Number(key)) === key`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -74,6 +78,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index f4a8eb8ae96f..01df860898d7 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -226,6 +226,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -246,6 +248,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -269,6 +273,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -321,6 +327,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -341,6 +349,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -364,6 +374,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -426,6 +438,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -446,6 +460,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -469,6 +485,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -521,6 +539,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -541,6 +561,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. '1.0': null, + '.1': null, + '1.': null, '999999999999999999999': null, '0.99999999999999999': null, '1E2': null, @@ -564,6 +586,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + 0.1: '.1', // 0.1 + 1: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1e2: '1E2', // 100 @@ -625,6 +649,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -645,6 +671,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -668,6 +696,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -720,6 +750,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -740,6 +772,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -763,6 +797,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -824,6 +860,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -844,6 +882,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -867,6 +907,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -919,6 +961,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + 0.1: null, + 1: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -939,6 +983,8 @@ const f = { // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -962,6 +1008,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 + 0.1: ".1", // 0.1 + 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index 6bfc99dd21a0..e3a8eb175b5b 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -28,6 +28,8 @@ const e = { 1: null, 1.5: null, 1.0: null, + .1: null, + 1.: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -48,6 +50,8 @@ const f = { // one where `String(Number(key)) === key`), but we came to the conclusion // that it is unexpected. "1.0": null, + ".1": null, + "1.": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -71,6 +75,8 @@ Object.entries({ // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 + .1: '.1', // 0.1 + 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 From d8a6db9ec269ffcbb9768050b1ddf93e74590c29 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sat, 13 Jun 2020 14:45:41 +0200 Subject: [PATCH 16/19] Fix 1. and .1 --- src/language-js/printer-estree.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 72 ++++++++++--------- tests/flow/quote-props/objects.js | 9 +-- .../__snapshots__/jsfmt.spec.js.snap | 72 ++++++++++--------- tests/js/quote-props/objects.js | 9 +-- 5 files changed, 92 insertions(+), 74 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 4a5e41810d9e..d44b1bd746f3 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3744,9 +3744,9 @@ function printPropertyKey(path, options, print) { if ( (key.type === "Identifier" || (isNumericLiteral(key) && - isSimpleNumber(rawText(key)) && + isSimpleNumber(printNumber(rawText(key))) && // Avoid converting 999999999999999999999 to 1e+21, 0.99999999999999999 to 1 and 1.0 to 1. - String(key.value) === rawText(key) && + String(key.value) === printNumber(rawText(key)) && !(options.parser === "typescript" || options.parser === "babel-ts"))) && (options.parser === "json" || (options.quoteProps === "consistent" && needsQuoteProps.get(parent))) diff --git a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap index 90d57f3ef892..05055176de80 100644 --- a/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -225,9 +225,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -250,9 +250,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -271,13 +271,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -330,9 +331,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -355,9 +356,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -376,13 +377,14 @@ Object.entries({ NaN: "NaN", 1: "1", 1.5: "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + 0.1: ".1", + 1: "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -445,9 +447,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -470,9 +472,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -491,13 +493,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -550,9 +553,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -575,9 +578,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - '1.0': null, '.1': null, '1.': null, + '1.0': null, '999999999999999999999': null, '0.99999999999999999': null, '1E2': null, @@ -596,13 +599,14 @@ Object.entries({ 'NaN': 'NaN', '1': '1', '1.5': '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + '0.1': '.1', + '1': '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - 0.1: '.1', // 0.1 - 1: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1e2: '1E2', // 100 @@ -664,9 +668,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -689,9 +693,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -710,13 +714,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -769,9 +774,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -794,9 +799,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -815,13 +820,14 @@ Object.entries({ "NaN": "NaN", "1": "1", "1.5": "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + "0.1": ".1", + "1": "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -883,9 +889,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -908,9 +914,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -929,13 +935,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -988,9 +995,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -1013,9 +1020,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -1034,13 +1041,14 @@ Object.entries({ NaN: "NaN", 1: "1", 1.5: "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + 0.1: ".1", + 1: "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 diff --git a/tests/flow/quote-props/objects.js b/tests/flow/quote-props/objects.js index 1415f1c3b9aa..2f29b8b74a23 100644 --- a/tests/flow/quote-props/objects.js +++ b/tests/flow/quote-props/objects.js @@ -27,9 +27,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -52,9 +52,9 @@ const f = { // These should never be unquoted. `1e+100` technically could (it’s the only // one where `String(Number(key)) === key`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -73,13 +73,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to `0.1` and `1` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 diff --git a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap index 01df860898d7..7368ed457639 100644 --- a/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -225,9 +225,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -247,9 +247,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -268,13 +268,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -326,9 +327,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -348,9 +349,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -369,13 +370,14 @@ Object.entries({ NaN: "NaN", 1: "1", 1.5: "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + 0.1: ".1", + 1: "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -437,9 +439,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -459,9 +461,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -480,13 +482,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -538,9 +541,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -560,9 +563,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - '1.0': null, '.1': null, '1.': null, + '1.0': null, '999999999999999999999': null, '0.99999999999999999': null, '1E2': null, @@ -581,13 +584,14 @@ Object.entries({ 'NaN': 'NaN', '1': '1', '1.5': '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + '0.1': '.1', + '1': '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - 0.1: '.1', // 0.1 - 1: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1e2: '1E2', // 100 @@ -648,9 +652,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -670,9 +674,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -691,13 +695,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -749,9 +754,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -771,9 +776,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -792,13 +797,14 @@ Object.entries({ "NaN": "NaN", "1": "1", "1.5": "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + "0.1": ".1", + "1": "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 @@ -859,9 +865,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -881,9 +887,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -902,13 +908,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 @@ -960,9 +967,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, 0.1: null, 1: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1e2: null, @@ -982,9 +989,9 @@ const f = { // These should never be unquoted. \`1e+100\` technically could (it’s the only // one where \`String(Number(key)) === key\`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -1003,13 +1010,14 @@ Object.entries({ NaN: "NaN", 1: "1", 1.5: "1.5", + // Prettier will normalize these to \`0.1\` and \`1\` – then they can be quoted. + 0.1: ".1", + 1: "1.", // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: "1.0", // 1 - 0.1: ".1", // 0.1 - 1: "1.", // 1 999999999999999999999: "999999999999999999999", // 1e+21 0.99999999999999999: "0.99999999999999999", // 1 1e2: "1E2", // 100 diff --git a/tests/js/quote-props/objects.js b/tests/js/quote-props/objects.js index e3a8eb175b5b..1c23be6fe2e2 100644 --- a/tests/js/quote-props/objects.js +++ b/tests/js/quote-props/objects.js @@ -27,9 +27,9 @@ const e = { NaN: null, 1: null, 1.5: null, - 1.0: null, .1: null, 1.: null, + 1.0: null, 999999999999999999999: null, 0.99999999999999999: null, 1E2: null, @@ -49,9 +49,9 @@ const f = { // These should never be unquoted. `1e+100` technically could (it’s the only // one where `String(Number(key)) === key`), but we came to the conclusion // that it is unexpected. - "1.0": null, ".1": null, "1.": null, + "1.0": null, "999999999999999999999": null, "0.99999999999999999": null, "1E2": null, @@ -70,13 +70,14 @@ Object.entries({ NaN: 'NaN', 1: '1', 1.5: '1.5', + // Prettier will normalize these to `0.1` and `1` – then they can be quoted. + .1: '.1', + 1.: '1.', // These should never be quoted. The _actual_ keys are shown as comments. // Copy-paste this into the console to verify. If we were to convert these // numbers into decimal (which completely valid), “information/intent” is // lost. Either way, writing code like this is super confusing. 1.0: '1.0', // 1 - .1: '.1', // 0.1 - 1.: '1.', // 1 999999999999999999999: '999999999999999999999', // 1e+21 0.99999999999999999: '0.99999999999999999', // 1 1E2: '1E2', // 100 From 1bdb7e3adc1bee3def472e222ba5f3b4fd9f953c Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Thu, 18 Jun 2020 06:35:40 +0200 Subject: [PATCH 17/19] Remove unnecessary babel-ts from tests --- .../typescript/quote-props/__snapshots__/jsfmt.spec.js.snap | 6 +++--- tests/typescript/quote-props/jsfmt.spec.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap index a50916094150..5a1efc20b704 100644 --- a/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/quote-props/__snapshots__/jsfmt.spec.js.snap @@ -2,7 +2,7 @@ exports[`types.ts - {"quoteProps":"as-needed"} format 1`] = ` ====================================options===================================== -parsers: ["typescript", "babel-ts"] +parsers: ["typescript"] printWidth: 80 quoteProps: "as-needed" | printWidth @@ -33,7 +33,7 @@ type U = { exports[`types.ts - {"quoteProps":"consistent"} format 1`] = ` ====================================options===================================== -parsers: ["typescript", "babel-ts"] +parsers: ["typescript"] printWidth: 80 quoteProps: "consistent" | printWidth @@ -64,7 +64,7 @@ type U = { exports[`types.ts - {"quoteProps":"preserve"} format 1`] = ` ====================================options===================================== -parsers: ["typescript", "babel-ts"] +parsers: ["typescript"] printWidth: 80 quoteProps: "preserve" | printWidth diff --git a/tests/typescript/quote-props/jsfmt.spec.js b/tests/typescript/quote-props/jsfmt.spec.js index d21a57629058..c17eda737d71 100644 --- a/tests/typescript/quote-props/jsfmt.spec.js +++ b/tests/typescript/quote-props/jsfmt.spec.js @@ -1,11 +1,11 @@ -run_spec(__dirname, ["typescript", "babel-ts"], { +run_spec(__dirname, ["typescript"], { quoteProps: "as-needed", }); -run_spec(__dirname, ["typescript", "babel-ts"], { +run_spec(__dirname, ["typescript"], { quoteProps: "preserve", }); -run_spec(__dirname, ["typescript", "babel-ts"], { +run_spec(__dirname, ["typescript"], { quoteProps: "consistent", }); From d08df7dbc1864d74f936a1eeb925ffe33b2fae63 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Thu, 18 Jun 2020 06:42:00 +0200 Subject: [PATCH 18/19] Only unquote numbers for babel --- src/language-js/printer-estree.js | 2 ++ src/language-js/utils.js | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index d44b1bd746f3..1631109dd794 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3747,6 +3747,8 @@ function printPropertyKey(path, options, print) { isSimpleNumber(printNumber(rawText(key))) && // Avoid converting 999999999999999999999 to 1e+21, 0.99999999999999999 to 1 and 1.0 to 1. String(key.value) === printNumber(rawText(key)) && + // Quoting number keys is safe in JS and Flow, but not in TypeScript (as + // mentioned in `isStringPropSafeToUnquote`). !(options.parser === "typescript" || options.parser === "babel-ts"))) && (options.parser === "json" || (options.quoteProps === "consistent" && needsQuoteProps.get(parent))) diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 8362437c69e6..df4068ccbec8 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -753,6 +753,13 @@ function returnArgumentHasLeadingComment(options, argument) { // 0: 1 // ^ Non-string literal property keys not supported. [unsupported-syntax] // } +// +// Angular does not support unquoted numbers in expressions. +// +// So we play it safe and only unquote numbers for the "babel" parser. +// (Vue supports unquoted numbers in expressions, but let’s keep it simple.) +// +// Identifiers can be unquoted in more circumstances, though. function isStringPropSafeToUnquote(node, options) { return ( options.parser !== "json" && @@ -767,12 +774,7 @@ function isStringPropSafeToUnquote(node, options) { )) || (isSimpleNumber(node.key.value) && String(Number(node.key.value)) === node.key.value && - !( - options.parser === "flow" || - options.parser === "babel-flow" || - options.parser === "typescript" || - options.parser === "babel-ts" - ))) + options.parser === "babel")) ); } From 3105f4776109533f2698a2b7bb199ea197439ef7 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Thu, 18 Jun 2020 07:27:43 +0200 Subject: [PATCH 19/19] Update changelog --- changelog_unreleased/javascript/pr-8508.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/changelog_unreleased/javascript/pr-8508.md b/changelog_unreleased/javascript/pr-8508.md index b4a449b25266..9f252faf62c2 100644 --- a/changelog_unreleased/javascript/pr-8508.md +++ b/changelog_unreleased/javascript/pr-8508.md @@ -25,7 +25,7 @@ x = { }; ``` -Note that Prettier only touches “simple” numbers such as `1` and `123.5`. It _won’t_ make the following transformations, as they feel unexpected: +Prettier only touches “simple” numbers such as `1` and `123.5`. It _won’t_ make the following transformations, as they feel unexpected: ``` 1e2 -> "100" @@ -36,7 +36,9 @@ Note that Prettier only touches “simple” numbers such as `1` and `123.5`. It 999999999999999999999 -> "1e+21" 2n -> "2" -"1e+100" -> 1e+100 +"1e+100" -> 1e100 ``` -(As a side note, please don’t use confusing numbers as object keys!) +(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.