From 964230dce2a74e930ef375530a3c61e560523080 Mon Sep 17 00:00:00 2001 From: Dmitry Gonchar Date: Wed, 1 Apr 2020 23:24:45 +1100 Subject: [PATCH] Fix #7920 - do not add comma for explicit inexact object with indexer property (#7923) * 7920 run tests for trailingComma:es5 option as well * 7920 example of wrong behavior - comma is always added at the end of the inexact object type * 7920 add test cases for trailingComma: 'all' * 7920 fix - do not force trailing comma on explicit inexact type if it has indexer or no properties * 7920 add changelog --- changelog_unreleased/flow/pr-7923.md | 52 ++ src/language-js/printer-estree.js | 10 +- .../__snapshots__/jsfmt.spec.js.snap | 518 ++++++++++++++++++ tests/flow_object_inexact/comments.js | 6 + tests/flow_object_inexact/jsfmt.spec.js | 2 + tests/flow_object_inexact/test.js | 5 + 6 files changed, 588 insertions(+), 5 deletions(-) create mode 100644 changelog_unreleased/flow/pr-7923.md diff --git a/changelog_unreleased/flow/pr-7923.md b/changelog_unreleased/flow/pr-7923.md new file mode 100644 index 000000000000..2fc11058597a --- /dev/null +++ b/changelog_unreleased/flow/pr-7923.md @@ -0,0 +1,52 @@ +#### Do not add comma for explicit inexact object with indexer property or no properties ([#7923](https://github.com/prettier/prettier/pull/7923) by [@DmitryGonchar](https://github.com/DmitryGonchar)) + + +```jsx +// Input +type T = { + a: number, + ..., +} + +type T = { + [string]: number, + ..., +} + +type T = { + // comment + ..., +} + +// Prettier stable +type T = { + a: number, + ... +} + +type T = { + [string]: number, + ..., +} + +type T = { + // comment + ..., +} + +// Prettier master +type T = { + a: number, + ... +} + +type T = { + [string]: number, + ... +} + +type T = { + // comment + ... +} +``` diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 2b136512a46f..2f2b6064adfe 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -1416,11 +1416,11 @@ function printPathNoParens(path, options, print, args) { const lastElem = getLast(n[propertiesField]); const canHaveTrailingSeparator = !( - lastElem && - (lastElem.type === "RestProperty" || - lastElem.type === "RestElement" || - hasNodeIgnoreComment(lastElem) || - n.inexact) + n.inexact || + (lastElem && + (lastElem.type === "RestProperty" || + lastElem.type === "RestElement" || + hasNodeIgnoreComment(lastElem))) ); let content; diff --git a/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap b/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap index fa0d6af61b93..f873374c1b27 100644 --- a/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow_object_inexact/__snapshots__/jsfmt.spec.js.snap @@ -4,6 +4,141 @@ exports[`comments.js 1`] = ` ====================================options===================================== parsers: ["flow", "babel"] printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +// @flow + +type Foo = { + // comment + ..., +}; + +type Foo = { + /* comment */ + ..., +}; + +type Foo = { /* comment */ ... }; + +type Foo = { /* comment */ + ...}; + +type Foo = { + // comment0 + // comment1 + ..., +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ..., +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment */ + [string]: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +=====================================output===================================== +// @flow + +type Foo = { + // comment + ... +}; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { /* comment */ ... }; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { + // comment0 + // comment1 + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ... +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment */ + [string]: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +================================================================================ +`; + +exports[`comments.js 2`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 trailingComma: "none" | printWidth =====================================input====================================== @@ -55,6 +190,147 @@ type Foo = { ... }; +type Foo = { + /* comment */ + [string]: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +=====================================output===================================== +// @flow + +type Foo = { + // comment + ... +}; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { /* comment */ ... }; + +type Foo = { + /* comment */ + ... +}; + +type Foo = { + // comment0 + // comment1 + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ... +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment */ + [string]: string, + ... +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + foo: string, + ... +}; + +================================================================================ +`; + +exports[`comments.js 3`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +// @flow + +type Foo = { + // comment + ..., +}; + +type Foo = { + /* comment */ + ..., +}; + +type Foo = { /* comment */ ... }; + +type Foo = { /* comment */ + ...}; + +type Foo = { + // comment0 + // comment1 + ..., +}; + +type Foo = { + /* comment0 */ + /* comment1 */ + ..., +}; + +type Foo = { + // comment + foo: string, + ... +}; + +type Foo = { + // comment0 + // comment1 + foo: string, + ... +}; + +type Foo = { + /* comment */ + foo: string, + ... +}; + +type Foo = { + /* comment */ + [string]: string, + ... +}; + type Foo = { /* comment0 */ /* comment1 */ @@ -113,6 +389,12 @@ type Foo = { ... }; +type Foo = { + /* comment */ + [string]: string, + ... +}; + type Foo = { /* comment0 */ /* comment1 */ @@ -127,6 +409,119 @@ exports[`test.js 1`] = ` ====================================options===================================== parsers: ["flow", "babel"] printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +//@flow +type T = { + a: number, + ..., +} + +type I = { + [string]: number, + ..., +} + +type U = { a: number, b: number, c: number, d: number, e: number, f: number, g: number, ...}; + +type V = {x: {...}, y: {x: {...}, a: number, b: number, c: number, d: number, e: number, f: number, ...}, z: {...}, foo: number, bar: {foo: number, ...}, ...}; + +function test(x: {foo: number, bar: number, baz: number, qux: nunber, a: number, b: number, c: {a: number, ...}, ...}) { return x; } +function test(x: {foo: number, bar: number, baz: number, qux: nunber, a: number, b: number, c: {a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, ...}, ...}) { return x; } + +type W = {...}; +type X = { + ..., +}; + +=====================================output===================================== +//@flow +type T = { + a: number, + ... +}; + +type I = { + [string]: number, + ... +}; + +type U = { + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + g: number, + ... +}; + +type V = { + x: { ... }, + y: { + x: { ... }, + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + ... + }, + z: { ... }, + foo: number, + bar: { foo: number, ... }, + ... +}; + +function test(x: { + foo: number, + bar: number, + baz: number, + qux: nunber, + a: number, + b: number, + c: { a: number, ... }, + ... +}) { + return x; +} +function test(x: { + foo: number, + bar: number, + baz: number, + qux: nunber, + a: number, + b: number, + c: { + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + g: number, + h: number, + i: number, + ... + }, + ... +}) { + return x; +} + +type W = { ... }; +type X = { ... }; + +================================================================================ +`; + +exports[`test.js 2`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 trailingComma: "none" | printWidth =====================================input====================================== @@ -136,6 +531,11 @@ type T = { ..., } +type I = { + [string]: number, + ..., +} + type U = { a: number, b: number, c: number, d: number, e: number, f: number, g: number, ...}; type V = {x: {...}, y: {x: {...}, a: number, b: number, c: number, d: number, e: number, f: number, ...}, z: {...}, foo: number, bar: {foo: number, ...}, ...}; @@ -155,6 +555,124 @@ type T = { ... }; +type I = { + [string]: number, + ... +}; + +type U = { + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + g: number, + ... +}; + +type V = { + x: { ... }, + y: { + x: { ... }, + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + ... + }, + z: { ... }, + foo: number, + bar: { foo: number, ... }, + ... +}; + +function test(x: { + foo: number, + bar: number, + baz: number, + qux: nunber, + a: number, + b: number, + c: { a: number, ... }, + ... +}) { + return x; +} +function test(x: { + foo: number, + bar: number, + baz: number, + qux: nunber, + a: number, + b: number, + c: { + a: number, + b: number, + c: number, + d: number, + e: number, + f: number, + g: number, + h: number, + i: number, + ... + }, + ... +}) { + return x; +} + +type W = { ... }; +type X = { ... }; + +================================================================================ +`; + +exports[`test.js 3`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +//@flow +type T = { + a: number, + ..., +} + +type I = { + [string]: number, + ..., +} + +type U = { a: number, b: number, c: number, d: number, e: number, f: number, g: number, ...}; + +type V = {x: {...}, y: {x: {...}, a: number, b: number, c: number, d: number, e: number, f: number, ...}, z: {...}, foo: number, bar: {foo: number, ...}, ...}; + +function test(x: {foo: number, bar: number, baz: number, qux: nunber, a: number, b: number, c: {a: number, ...}, ...}) { return x; } +function test(x: {foo: number, bar: number, baz: number, qux: nunber, a: number, b: number, c: {a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, ...}, ...}) { return x; } + +type W = {...}; +type X = { + ..., +}; + +=====================================output===================================== +//@flow +type T = { + a: number, + ... +}; + +type I = { + [string]: number, + ... +}; + type U = { a: number, b: number, diff --git a/tests/flow_object_inexact/comments.js b/tests/flow_object_inexact/comments.js index 17ebf492e322..a0f914103309 100644 --- a/tests/flow_object_inexact/comments.js +++ b/tests/flow_object_inexact/comments.js @@ -46,6 +46,12 @@ type Foo = { ... }; +type Foo = { + /* comment */ + [string]: string, + ... +}; + type Foo = { /* comment0 */ /* comment1 */ diff --git a/tests/flow_object_inexact/jsfmt.spec.js b/tests/flow_object_inexact/jsfmt.spec.js index c242cec590ff..693dcdb086eb 100644 --- a/tests/flow_object_inexact/jsfmt.spec.js +++ b/tests/flow_object_inexact/jsfmt.spec.js @@ -1 +1,3 @@ +run_spec(__dirname, ["flow", "babel"], { trailingComma: "es5" }); run_spec(__dirname, ["flow", "babel"], { trailingComma: "none" }); +run_spec(__dirname, ["flow", "babel"], { trailingComma: "all" }); diff --git a/tests/flow_object_inexact/test.js b/tests/flow_object_inexact/test.js index 62fde15dc890..d53841cda2c2 100644 --- a/tests/flow_object_inexact/test.js +++ b/tests/flow_object_inexact/test.js @@ -4,6 +4,11 @@ type T = { ..., } +type I = { + [string]: number, + ..., +} + type U = { a: number, b: number, c: number, d: number, e: number, f: number, g: number, ...}; type V = {x: {...}, y: {x: {...}, a: number, b: number, c: number, d: number, e: number, f: number, ...}, z: {...}, foo: number, bar: {foo: number, ...}, ...};