diff --git a/changelog_unreleased/typescript/18109.md b/changelog_unreleased/typescript/18109.md new file mode 100644 index 000000000..1029f9034 --- /dev/null +++ b/changelog_unreleased/typescript/18109.md @@ -0,0 +1,32 @@ +#### Remove unexpected blank line before union types (#18109 by @jspereiramoura, @fisker) + + +```tsx +// Input +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + Fooo1000 | Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; +type A = // comment + Fooo1000 | Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + +// Prettier stable +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + + | Fooo1000 + | Baz2000 + | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; +type A = // comment + + | Fooo1000 + | Baz2000 + | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + +// Prettier main +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + | Fooo1000 + | Baz2000 + | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; +type A = // comment + | Fooo1000 + | Baz2000 + | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; +``` diff --git a/src/language-js/print/assignment.js b/src/language-js/print/assignment.js index cbe8428a1..534cd44dd 100644 --- a/src/language-js/print/assignment.js +++ b/src/language-js/print/assignment.js @@ -142,6 +142,7 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) { node.type === "ImportAttribute" || (rightNode.type === "CallExpression" && rightNode.callee.name === "require") || + isUnionType(rightNode) || // do not put values on a separate line from the key in json options.parser === "json5" || options.parser === "jsonc" || diff --git a/tests/format/typescript/union/consistent-with-flow/81-characters.ts b/tests/format/typescript/union/consistent-with-flow/81-characters.ts new file mode 100644 index 000000000..ae68c1431 --- /dev/null +++ b/tests/format/typescript/union/consistent-with-flow/81-characters.ts @@ -0,0 +1,52 @@ +export type ButtonColor = + | "primary" + | "danger" + | "warning" + | "muted" + | "success"; +export type Button__80 = + | "primary" + | "danger" + | "warning" + | "muted" + | "success"; + +export type SupportOptionType = + | "int" + | "string" + | "boolean" + | "choice" + | "path"; +export type SupportOption_80 = + | "int" + | "string" + | "boolean" + | "choice" + | "path"; + +export type RenderContextType = + | 'data' + | 'header' + | 'footer' + | 'columnsSelector'; +export type RenderContext_80 = + | 'data' + | 'header' + | 'footer' + | 'columnsSelector'; + +export type PropertyNameNonComputed = + | Identifier + | NumberLiteral + | StringLiteral; +export type PropertyNameNon_____80 = + | Identifier + | NumberLiteral + | StringLiteral; + +type ClassLikeDeclaration = + | TSESTree.ClassDeclaration + | TSESTree.ClassExpression; +type ClassLike________80 = + | TSESTree.ClassDeclaration + | TSESTree.ClassExpression; diff --git a/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap index 343727d11..ce2c2b22e 100644 --- a/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/union/consistent-with-flow/__snapshots__/format.test.js.snap @@ -1,5 +1,102 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`81-characters.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +export type ButtonColor = + | "primary" + | "danger" + | "warning" + | "muted" + | "success"; +export type Button__80 = + | "primary" + | "danger" + | "warning" + | "muted" + | "success"; + +export type SupportOptionType = + | "int" + | "string" + | "boolean" + | "choice" + | "path"; +export type SupportOption_80 = + | "int" + | "string" + | "boolean" + | "choice" + | "path"; + +export type RenderContextType = + | 'data' + | 'header' + | 'footer' + | 'columnsSelector'; +export type RenderContext_80 = + | 'data' + | 'header' + | 'footer' + | 'columnsSelector'; + +export type PropertyNameNonComputed = + | Identifier + | NumberLiteral + | StringLiteral; +export type PropertyNameNon_____80 = + | Identifier + | NumberLiteral + | StringLiteral; + +type ClassLikeDeclaration = + | TSESTree.ClassDeclaration + | TSESTree.ClassExpression; +type ClassLike________80 = + | TSESTree.ClassDeclaration + | TSESTree.ClassExpression; + +=====================================output===================================== +export type ButtonColor = + | "primary" + | "danger" + | "warning" + | "muted" + | "success"; +export type Button__80 = "primary" | "danger" | "warning" | "muted" | "success"; + +export type SupportOptionType = + | "int" + | "string" + | "boolean" + | "choice" + | "path"; +export type SupportOption_80 = "int" | "string" | "boolean" | "choice" | "path"; + +export type RenderContextType = + | "data" + | "header" + | "footer" + | "columnsSelector"; +export type RenderContext_80 = "data" | "header" | "footer" | "columnsSelector"; + +export type PropertyNameNonComputed = + | Identifier + | NumberLiteral + | StringLiteral; +export type PropertyNameNon_____80 = Identifier | NumberLiteral | StringLiteral; + +type ClassLikeDeclaration = + | TSESTree.ClassDeclaration + | TSESTree.ClassExpression; +type ClassLike________80 = TSESTree.ClassDeclaration | TSESTree.ClassExpression; + +================================================================================ +`; + exports[`comment.ts format 1`] = ` ====================================options===================================== parsers: ["typescript", "flow"] @@ -11,9 +108,49 @@ type A1 = /* 4 */ ( | B )[] +type A2 = // dir, exp, arg, modifiers + | [string] + | [string, ExpressionNode] + | [string, ExpressionNode, ExpressionNode] + | [string, ExpressionNode, ExpressionNode, ObjectExpression] + +type A3 = // dir, exp, arg, modifiers + & [string] + & [string, ExpressionNode] + & [string, ExpressionNode, ExpressionNode] + & [string, ExpressionNode, ExpressionNode, ObjectExpression] + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + Fooo1000 | Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBl2 = + Fooo1000 & Baz2000 & BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + =====================================output===================================== type A1 = /* 4 */ (A | B)[]; +type A2 = // dir, exp, arg, modifiers + | [string] + | [string, ExpressionNode] + | [string, ExpressionNode, ExpressionNode] + | [string, ExpressionNode, ExpressionNode, ObjectExpression]; + +type A3 = // dir, exp, arg, modifiers + [string] & + [string, ExpressionNode] & + [string, ExpressionNode, ExpressionNode] & + [string, ExpressionNode, ExpressionNode, ObjectExpression]; + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + | Fooo1000 + | Baz2000 + | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBl2 = + Fooo1000 & + Baz2000 & + BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + ================================================================================ `; diff --git a/tests/format/typescript/union/consistent-with-flow/comment.ts b/tests/format/typescript/union/consistent-with-flow/comment.ts index 72ae39339..a30b43b8a 100644 --- a/tests/format/typescript/union/consistent-with-flow/comment.ts +++ b/tests/format/typescript/union/consistent-with-flow/comment.ts @@ -2,3 +2,21 @@ type A1 = /* 4 */ ( | A | B )[] + +type A2 = // dir, exp, arg, modifiers + | [string] + | [string, ExpressionNode] + | [string, ExpressionNode, ExpressionNode] + | [string, ExpressionNode, ExpressionNode, ObjectExpression] + +type A3 = // dir, exp, arg, modifiers + & [string] + & [string, ExpressionNode] + & [string, ExpressionNode, ExpressionNode] + & [string, ExpressionNode, ExpressionNode, ObjectExpression] + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla = + Fooo1000 | Baz2000 | BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong; + +type SuperLongTypeNameLoremIpsumLoremIpsumBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBl2 = + Fooo1000 & Baz2000 & BarLoooooooooooooooooooooooooooooooooooooooooooooooooLong;