Rewrite union type print (#18827)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-02-02 03:46:49 +08:00
1 parent 2b8a924a13
commit 60cf4bd86e
32 files changed
+583 -140

No files matched your search

@@ -0,0 +1,19 @@
#### Don't break union type when it can fit (#18827 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
type Browser = "chromium" | "webkit" | "firefox" | "chromium-msedge" | "chromium-chrome";
// Prettier stable
type Browser =
| "chromium"
| "webkit"
| "firefox"
| "chromium-msedge"
| "chromium-chrome";
// Prettier main
type Browser =
"chromium" | "webkit" | "firefox" | "chromium-msedge" | "chromium-chrome";
```
+22
View File
@@ -0,0 +1,22 @@
#### Remove extra indention for union type in conditional type (#18827 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
type A = T extends "arrow"
? ExcalidrawArrowElement["startBinding"] | ExcalidrawElbowArrowElement["startBinding"]
: never;
// Prettier stable
type A = T extends "arrow"
?
| ExcalidrawArrowElement["startBinding"]
| ExcalidrawElbowArrowElement["startBinding"]
: never;
// Prettier main
type A = T extends "arrow"
? | ExcalidrawArrowElement["startBinding"]
| ExcalidrawElbowArrowElement["startBinding"]
: never;
```
+2
View File
@@ -28,6 +28,7 @@ import {
} from "../utilities/node-types.js";
import { shouldInlineLogicalExpression } from "./binaryish.js";
import { printCallExpression } from "./call-expression.js";
import { shouldHugUnionType } from "./union-type.js";
/**
* @import AstPath from "../../common/ast-path.js"
@@ -136,6 +137,7 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) {
if (
isHeadOfLongChain ||
(isUnionType(rightNode) && !shouldHugUnionType(rightNode)) ||
hasLeadingOwnLineComment(options.originalText, rightNode)
) {
return "break-after-operator";
+2 -2
View File
@@ -53,7 +53,7 @@ import { printTypePredicate } from "./type-predicate.js";
import { printTypeQuery } from "./type-query.js";
import { printUnionType } from "./union-type.js";
function printFlow(path, options, print) {
function printFlow(path, options, print, args) {
const { node } = path;
if (isFlowKeywordType(node)) {
@@ -117,7 +117,7 @@ function printFlow(path, options, print) {
case "IntersectionTypeAnnotation":
return printIntersectionType(path, options, print);
case "UnionTypeAnnotation":
return printUnionType(path, options, print);
return printUnionType(path, options, print, args);
case "ConditionalTypeAnnotation":
return printTernary(path, options, print);
case "InferTypeAnnotation":
+2 -2
View File
@@ -45,7 +45,7 @@ import { printTypePredicate } from "./type-predicate.js";
import { printTypeQuery } from "./type-query.js";
import { printUnionType } from "./union-type.js";
function printTypescript(path, options, print) {
function printTypescript(path, options, print, args) {
const { node } = path;
// TypeScript nodes always starts with `TS`
@@ -186,7 +186,7 @@ function printTypescript(path, options, print) {
case "TSIntersectionType":
return printIntersectionType(path, options, print);
case "TSUnionType":
return printUnionType(path, options, print);
return printUnionType(path, options, print, args);
case "TSFunctionType":
case "TSCallSignatureDeclaration":
case "TSConstructorType":
+48 -42
View File
@@ -12,13 +12,13 @@ import {
printCommentsSeparately,
} from "../../main/comments/print.js";
import needsParentheses from "../parentheses/needs-parentheses.js";
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
import { hasComment } from "../utilities/comments.js";
import { createTypeCheckFunction } from "../utilities/create-type-check-function.js";
import { hasLeadingOwnLineComment } from "../utilities/has-leading-own-line-comment.js";
import { isFlowObjectTypePropertyAFunction } from "../utilities/is-flow-object-type-property-a-function.js";
import {
isConditionalType,
isTupleType,
isTypeAlias,
isTypeParameterInstantiation,
} from "../utilities/node-types.js";
import { shouldUnionTypePrintOwnComments } from "../utilities/should-union-type-print-own-comments.js";
@@ -27,7 +27,7 @@ import { shouldUnionTypePrintOwnComments } from "../utilities/should-union-type-
*/
// `TSUnionType` and `UnionTypeAnnotation`
function printUnionType(path, options, print) {
function printUnionType(path, options, print, args) {
const { node } = path;
// single-line variation
// A | B | C
@@ -39,33 +39,6 @@ function printUnionType(path, options, print) {
const { parent } = path;
// If there's a leading comment, the parent is doing the indentation
const shouldIndent =
parent.type !== "TypeParameterInstantiation" &&
parent.type !== "TSTypeParameterInstantiation" &&
parent.type !== "GenericTypeAnnotation" &&
parent.type !== "TSTypeReference" &&
parent.type !== "TSTypeAssertion" &&
!isTupleType(parent) &&
!(
parent.type === "FunctionTypeParam" &&
!parent.name &&
path.grandparent.this !== parent
) &&
!(
isConditionalType(parent) &&
!options.experimentalTernaries &&
hasLeadingOwnLineComment(options.originalText, node)
) &&
!(
(isTypeAlias(parent) || parent.type === "VariableDeclarator") &&
hasLeadingOwnLineComment(options.originalText, node)
) &&
!(
isTypeAlias(parent) &&
hasComment(parent.id, CommentCheckFlags.Trailing | CommentCheckFlags.Line)
);
// {
// a: string
// } | null | void
@@ -97,32 +70,65 @@ function printUnionType(path, options, print) {
return [leading, join(" | ", printed), trailing];
}
const shouldAddStartLine =
shouldIndent && !hasLeadingOwnLineComment(options.originalText, node);
const mainParts = [
ifBreak([shouldAddStartLine ? line : "", "| "]),
join([line, "| "], printed),
leading,
group([ifBreak("| "), join([line, "| "], printed)]),
trailing,
];
if (needsParentheses(path, options)) {
return [leading, group([indent(mainParts), softline]), trailing];
return group([indent([softline, mainParts]), softline]);
}
const parts = [leading, group(mainParts)];
if (isTupleType(parent) && parent.elementTypes.length > 1) {
return [
group([
indent([ifBreak(["(", softline]), parts]),
indent([ifBreak(["(", softline]), mainParts]),
softline,
ifBreak(")"),
]),
trailing,
];
}
return [group(shouldIndent ? indent(parts) : parts), trailing];
// Already indent in parent
if (
args?.assignmentLayout === "break-after-operator" ||
!shouldIndentUnionType(path)
) {
return mainParts;
}
return group(indent([softline, mainParts]));
}
function shouldIndentUnionType(path) {
const { key, parent } = path;
if (
(key === "typeAnnotation" && parent.type === "TSTypeAssertion") ||
(key === "elementTypes" && isTupleType(parent)) ||
((key === "trueType" || key === "falseType") &&
isConditionalType(parent)) ||
(key === "params" && isTypeParameterInstantiation(parent))
) {
return false;
}
if (
path.match(
undefined,
(node, key) =>
key === "typeAnnotation" && node.type === "FunctionTypeParam",
(node, key) => key === "params" && node.type === "FunctionTypeAnnotation",
(node, key) =>
key === "value" &&
node.type === "ObjectTypeProperty" &&
isFlowObjectTypePropertyAFunction(node),
)
) {
return false;
}
return true;
}
const isVoidType = createTypeCheckFunction([
+5
View File
@@ -137,4 +137,9 @@ export const isArrayType = createTypeCheckFunction([
"ArrayTypeAnnotation",
]);
export const isTypeParameterInstantiation = createTypeCheckFunction([
"TSTypeParameterInstantiation",
"TypeParameterInstantiation",
]);
export * from "./literal.js";
@@ -25,7 +25,6 @@ const unstableTests = new Map(
"js/sequence-expression/parenthesized.js",
"typescript/satisfies-operators/comments-unstable.ts",
"jsx/comments/in-attributes.js",
"typescript/union/consistent-with-flow/single-type.ts",
"typescript/import-type/long-module-name/long-module-name4.ts",
// Unstable due to lack of indent information
"js/multiparser-comments/comment-inside.js",
@@ -87,8 +87,7 @@ const value1 =
const value2 =
thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value3 = thisIsAReallyLongIdentifier as
| SomeInterface
| SomeOtherInterface;
SomeInterface | SomeOtherInterface;
const value4 = thisIsAReallyLongIdentifier as {
prop1: string,
prop2: number,
@@ -13,7 +13,8 @@ type Foo = {
=====================================output=====================================
type Foo = {
bar: // test
bar:
// test
| { loooooooooooooooooongPropName1: string }
| { loooooooooooooooooongPropName2: string },
};
@@ -41,8 +41,7 @@ declare class Array<T> {
}
type IteratorResult<Yield, Return> =
| { done: true, value?: Return }
| { done: false, value: Yield };
{ done: true, value?: Return } | { done: false, value: Yield };
interface $Iterator<+Yield, +Return, -Next> {
@@iterator(): $Iterator<Yield, Return, Next>;
@@ -1015,13 +1015,7 @@ export type BinaryOperator =
| "..";
export type UnaryOperator =
| "-"
| "+"
| "!"
| "~"
| "typeof"
| "void"
| "delete";
"-" | "+" | "!" | "~" | "typeof" | "void" | "delete";
export type AssignmentOperator =
| "="
@@ -12,7 +12,8 @@ const myValue = (callcallcallcallcallcall(87689769876876897698768768976987687689
=====================================output=====================================
const myValue = (callcallcallcallcallcall(
87689769876876897698768768976987687689769876,
): // Comment
):
// Comment
| one
| two
| thre
@@ -104,8 +104,7 @@ const value1 =
const value2 =
thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value3 = thisIsAReallyLongIdentifier as
| SomeInterface
| SomeOtherInterface;
SomeInterface | SomeOtherInterface;
const value4 = thisIsAReallyLongIdentifier as {
prop1: string;
prop2: number;
@@ -44,8 +44,7 @@ const firstItem_1 = ______________.items.find(
// 79 width until \`as\` or \`satisfies\` keyword
const firstItem2 = ______________.items.find((item) => item.type === "item") as
| string
| number;
string | number;
// 80 width until \`as\` or \`satisfies\` keyword
const firstItem_2 = ______________.items.find(
(item) => item.type === "item",
@@ -53,8 +52,7 @@ const firstItem_2 = ______________.items.find(
// 79 width until \`as\` or \`satisfies\` keyword
const firstItem3 = _____________.find((item) => item.type === "item") satisfies
| string
| number;
string | number;
// 80 width until \`as\` or \`satisfies\` keyword
const firstItem_3 = _____________.find(
(item) => item.type === "item",
@@ -88,6 +88,11 @@ T = any instanceof B
// Multiline comment
? B | C
: D;
T = any instanceof B
? D
// Comment
// Multiline comment
: B | C;
type T = any extends B
// Comment
@@ -201,8 +206,8 @@ c */
type T =
any extends B ?
// Comment
// Multiline comment
B | C
// Multiline comment
B | C
: D;
T =
any instanceof B ?
@@ -210,11 +215,16 @@ T =
// Multiline comment
B | C
: D;
T =
any instanceof B ? D
// Comment
// Multiline comment
: B | C;
type T =
any extends B ?
// Comment
B | C
B | C
: D;
T =
any instanceof B ?
@@ -225,9 +235,9 @@ T =
type T =
any extends B ?
/**
* Comment
*/
B | C
* Comment
*/
B | C
: D;
T = any instanceof B ?
/**
@@ -328,6 +338,11 @@ T = any instanceof B
// Multiline comment
? B | C
: D;
T = any instanceof B
? D
// Comment
// Multiline comment
: B | C;
type T = any extends B
// Comment
@@ -441,6 +456,12 @@ T =
// Multiline comment
B | C
: D;
T =
any instanceof B
? D
: // Comment
// Multiline comment
B | C;
type T = any extends B
? // Comment
@@ -80,6 +80,11 @@ T = any instanceof B
// Multiline comment
? B | C
: D;
T = any instanceof B
? D
// Comment
// Multiline comment
: B | C;
type T = any extends B
// Comment
@@ -101,45 +101,39 @@ unionOfDifferentReturnType1(true); // error in type of parameter
unionOfDifferentReturnType1(); // error missing parameter
var unionOfDifferentParameterTypes:
| { (a: number): number }
| { (a: string): Date };
{ (a: number): number } | { (a: string): Date };
unionOfDifferentParameterTypes(10); // error - no call signatures
unionOfDifferentParameterTypes("hello"); // error - no call signatures
unionOfDifferentParameterTypes(); // error - no call signatures
var unionOfDifferentNumberOfSignatures:
| { (a: number): number }
| { (a: number): Date; (a: string): boolean };
{ (a: number): number } | { (a: number): Date; (a: string): boolean };
unionOfDifferentNumberOfSignatures(); // error - no call signatures
unionOfDifferentNumberOfSignatures(10); // error - no call signatures
unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures
var unionWithDifferentParameterCount:
| { (a: string): string }
| { (a: string, b: number): number };
{ (a: string): string } | { (a: string, b: number): number };
unionWithDifferentParameterCount(); // no call signature
unionWithDifferentParameterCount("hello"); // no call signature
unionWithDifferentParameterCount("hello", 10); // no call signature
var unionWithOptionalParameter1:
| { (a: string, b?: number): string }
| { (a: string, b?: number): number };
{ (a: string, b?: number): string } | { (a: string, b?: number): number };
strOrNum = unionWithOptionalParameter1("hello");
strOrNum = unionWithOptionalParameter1("hello", 10);
strOrNum = unionWithOptionalParameter1("hello", "hello"); // error in parameter type
strOrNum = unionWithOptionalParameter1(); // error
var unionWithOptionalParameter2:
| { (a: string, b?: number): string }
| { (a: string, b: number): number };
{ (a: string, b?: number): string } | { (a: string, b: number): number };
strOrNum = unionWithOptionalParameter2("hello"); // error no call signature
strOrNum = unionWithOptionalParameter2("hello", 10); // error no call signature
strOrNum = unionWithOptionalParameter2("hello", "hello"); // error no call signature
strOrNum = unionWithOptionalParameter2(); // error no call signature
var unionWithOptionalParameter3:
| { (a: string, b?: number): string }
| { (a: string): number };
{ (a: string, b?: number): string } | { (a: string): number };
strOrNum = unionWithOptionalParameter3("hello");
strOrNum = unionWithOptionalParameter3("hello", 10); // error no call signature
strOrNum = unionWithOptionalParameter3("hello", "hello"); // error no call signature
@@ -155,8 +149,7 @@ strOrNum = unionWithRestParameter1("hello", "hello"); // error in parameter type
strOrNum = unionWithRestParameter1(); // error
var unionWithRestParameter2:
| { (a: string, ...b: number[]): string }
| { (a: string, b: number): number };
{ (a: string, ...b: number[]): string } | { (a: string, b: number): number };
strOrNum = unionWithRestParameter2("hello"); // error no call signature
strOrNum = unionWithRestParameter2("hello", 10); // error no call signature
strOrNum = unionWithRestParameter2("hello", 10, 11); // error no call signature
@@ -164,8 +157,7 @@ strOrNum = unionWithRestParameter2("hello", "hello"); // error no call signature
strOrNum = unionWithRestParameter2(); // error no call signature
var unionWithRestParameter3:
| { (a: string, ...b: number[]): string }
| { (a: string): number };
{ (a: string, ...b: number[]): string } | { (a: string): number };
strOrNum = unionWithRestParameter3("hello");
strOrNum = unionWithRestParameter3("hello", 10); // error no call signature
strOrNum = unionWithRestParameter3("hello", 10, 11); // error no call signature
@@ -173,8 +165,7 @@ strOrNum = unionWithRestParameter3("hello", "hello"); // error no call signature
strOrNum = unionWithRestParameter3(); // error no call signature
var unionWithRestParameter4:
| { (...a: string[]): string }
| { (a: string, b: string): number };
{ (...a: string[]): string } | { (a: string, b: string): number };
strOrNum = unionWithRestParameter4("hello"); // error supplied parameters do not match any call signature
strOrNum = unionWithRestParameter4("hello", "world");
@@ -366,8 +357,7 @@ var strOrNum: string | number;
// If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types,
// U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U.
var unionOfDifferentReturnType:
| { new (a: number): number }
| { new (a: number): Date };
{ new (a: number): number } | { new (a: number): Date };
numOrDate = new unionOfDifferentReturnType(10);
strOrBoolean = new unionOfDifferentReturnType("hello"); // error
new unionOfDifferentReturnType1(true); // error in type of parameter
@@ -381,8 +371,7 @@ new unionOfDifferentReturnType1(true); // error in type of parameter
new unionOfDifferentReturnType1(); // error missing parameter
var unionOfDifferentParameterTypes:
| { new (a: number): number }
| { new (a: string): Date };
{ new (a: number): number } | { new (a: string): Date };
new unionOfDifferentParameterTypes(10); // error - no call signatures
new unionOfDifferentParameterTypes("hello"); // error - no call signatures
new unionOfDifferentParameterTypes(); // error - no call signatures
@@ -395,8 +384,7 @@ new unionOfDifferentNumberOfSignatures(10); // error - no call signatures
new unionOfDifferentNumberOfSignatures("hello"); // error - no call signatures
var unionWithDifferentParameterCount:
| { new (a: string): string }
| { new (a: string, b: number): number };
{ new (a: string): string } | { new (a: string, b: number): number };
new unionWithDifferentParameterCount(); // no call signature
new unionWithDifferentParameterCount("hello"); // no call signature
new unionWithDifferentParameterCount("hello", 10); // no call signature
@@ -418,8 +406,7 @@ strOrNum = new unionWithOptionalParameter2("hello", "hello"); // error no call s
strOrNum = new unionWithOptionalParameter2(); // error no call signature
var unionWithOptionalParameter3:
| { new (a: string, b?: number): string }
| { new (a: string): number };
{ new (a: string, b?: number): string } | { new (a: string): number };
strOrNum = new unionWithOptionalParameter3("hello"); // error no call signature
strOrNum = new unionWithOptionalParameter3("hello", 10); // error no call signature
strOrNum = new unionWithOptionalParameter3("hello", "hello"); // error no call signature
@@ -444,8 +431,7 @@ strOrNum = new unionWithRestParameter2("hello", "hello"); // error no call signa
strOrNum = new unionWithRestParameter2(); // error no call signature
var unionWithRestParameter3:
| { new (a: string, ...b: number[]): string }
| { new (a: string): number };
{ new (a: string, ...b: number[]): string } | { new (a: string): number };
strOrNum = new unionWithRestParameter3("hello"); // error no call signature
strOrNum = new unionWithRestParameter3("hello", 10); // error no call signature
strOrNum = new unionWithRestParameter3("hello", 10, 11); // error no call signature
@@ -610,22 +596,19 @@ numOrDate = unionOfDifferentReturnType["hello"]; // number | Date
numOrDate = unionOfDifferentReturnType[10]; // number | Date
var unionOfTypesWithAndWithoutStringSignature:
| { [a: string]: number }
| boolean;
{ [a: string]: number } | boolean;
anyVar = unionOfTypesWithAndWithoutStringSignature["hello"]; // any
anyVar = unionOfTypesWithAndWithoutStringSignature[10]; // any
// If each type in U has a numeric index signature,
// U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
var unionOfDifferentReturnType1:
| { [a: number]: number }
| { [a: number]: Date };
{ [a: number]: number } | { [a: number]: Date };
numOrDate = unionOfDifferentReturnType1["hello"]; // any
numOrDate = unionOfDifferentReturnType1[10]; // number | Date
var unionOfTypesWithAndWithoutStringSignature1:
| { [a: number]: number }
| boolean;
{ [a: number]: number } | boolean;
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any
@@ -131,7 +131,8 @@ type c = {
};
type d = {
[A in B]: // prettier-ignore
[A in B]:
// prettier-ignore
C | D;
};
@@ -150,7 +151,8 @@ type f = {
};
type g = {
[A in B]: /* prettier-ignore */
[A in B]:
/* prettier-ignore */
C | D;
};
@@ -66,7 +66,8 @@ export interface DirectiveArgumentNode extends ArrayExpression {
=====================================output=====================================
export interface DirectiveArgumentNode extends ArrayExpression {
elements: // dir, exp, arg, modifiers
elements:
// dir, exp, arg, modifiers
| [string]
| [string, ExpressionNode]
| [string, ExpressionNode, ExpressionNode]
@@ -62,7 +62,8 @@ type A<
type BufferStreamOrVoid<
C extends undefined | Buffer | Stream | boolean,
P extends undefined | Buffer,
R extends Buffer | Stream | void = // Inline comment about void.
R extends
Buffer | Stream | void = // Inline comment about void.
// Above line comment about exclue.
Exclude<C, false> extends never
? void
@@ -556,13 +557,11 @@ export type OuterType32<
export type OuterType4<
LongerLongerLongerLongerInnerType extends
| LongerLongerLongerLongerLongerLo
| ngerLongerLongerOtherType,
LongerLongerLongerLongerLongerLo | ngerLongerLongerOtherType,
> = { a: 1 };
export type OuterType42<
LongerLongerLongerLongerInnerType =
| LongerLongerLongerLongerLongerLo
| ngerLongerLongerOtherType,
LongerLongerLongerLongerLongerLo | ngerLongerLongerOtherType,
> = { a: 1 };
================================================================================
+16
View File
@@ -0,0 +1,16 @@
export interface ProductStatus {
type:
// eslint-disable-next-line sonar/max-union-size
| 'NotDeployed'
| 'InDeployment'
| 'Running'
| 'DeploymentFailure'
| 'RunningAbnormal'
| 'Deleting'
| 'DeleteFailure'
| 'DeploymentSucceed'
| 'SystemError'
| 'Upgrading';
icon: string;
text: string;
}
+32
View File
@@ -0,0 +1,32 @@
export type TransformedSource = {
code: string;
map?:
// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
}
| string
| null;
};
export type TransformedSource2 = {
code: string,
map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number,
sources: string[],
names: string[],
sourceRoot?: string,
sourcesContent?: string[],
mappings: string,
file: string
}
| string
| null
};
+16
View File
@@ -0,0 +1,16 @@
type T = {
prop:
// comment
| T1
// comment
| T2
// comment
}
type T2 = (arg:
// comment
| T1
// comment
| T2
// comment
) => void
@@ -1,5 +1,168 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`5849.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
export type TransformedSource = {
code: string;
map?:
// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
}
| string
| null;
};
export type TransformedSource2 = {
code: string,
map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number,
sources: string[],
names: string[],
sourceRoot?: string,
sourcesContent?: string[],
mappings: string,
file: string
}
| string
| null
};
=====================================output=====================================
export type TransformedSource = {
code: string;
map?:
// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
}
| string
| null;
};
export type TransformedSource2 = {
code: string;
map?:
// copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
| {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
}
| string
| null;
};
================================================================================
`;
exports[`7725.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
type T = {
prop:
// comment
| T1
// comment
| T2
// comment
}
type T2 = (arg:
// comment
| T1
// comment
| T2
// comment
) => void
=====================================output=====================================
type T = {
prop:
// comment
| T1
// comment
| T2;
// comment
};
type T2 = (
arg:
// comment
| T1
// comment
| T2,
// comment
) => void;
================================================================================
`;
exports[`10599.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
export interface ProductStatus {
type:
// eslint-disable-next-line sonar/max-union-size
| 'NotDeployed'
| 'InDeployment'
| 'Running'
| 'DeploymentFailure'
| 'RunningAbnormal'
| 'Deleting'
| 'DeleteFailure'
| 'DeploymentSucceed'
| 'SystemError'
| 'Upgrading';
icon: string;
text: string;
}
=====================================output=====================================
export interface ProductStatus {
type:
// eslint-disable-next-line sonar/max-union-size
| "NotDeployed"
| "InDeployment"
| "Running"
| "DeploymentFailure"
| "RunningAbnormal"
| "Deleting"
| "DeleteFailure"
| "DeploymentSucceed"
| "SystemError"
| "Upgrading";
icon: string;
text: string;
}
================================================================================
`;
exports[`inlining.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
@@ -149,6 +312,32 @@ type T8 = number | ((arg: any) => void);
================================================================================
`;
exports[`trailing-comment.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
export const CONDITIONAL_AXIS_PROP_INDEX: Record<
ConditionalAxisProp,
{
part: keyof AxisEncode;
vgProp: VgEncodeChannel;
} | null // null if we need to convert condition to signal
> = {
}
=====================================output=====================================
export const CONDITIONAL_AXIS_PROP_INDEX: Record<
ConditionalAxisProp,
{
part: keyof AxisEncode;
vgProp: VgEncodeChannel;
} | null // null if we need to convert condition to signal
> = {};
================================================================================
`;
exports[`union-parens.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
@@ -306,8 +495,7 @@ type State = {
);
const foo1 = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
| string
| undefined
string | undefined
)[];
const foo2: (
@@ -50,7 +50,8 @@ type A3 = /* block comment
=====================================output=====================================
export interface DirectiveArgumentNode1 extends ArrayExpression {
elements: // dir, exp, arg, modifiers
elements:
// dir, exp, arg, modifiers
| [string]
| [string, ExpressionNode]
| [string, ExpressionNode, ExpressionNode]
@@ -58,7 +59,8 @@ export interface DirectiveArgumentNode1 extends ArrayExpression {
}
export class DirectiveArgumentNode12 extends ArrayExpression {
elements: // dir, exp, arg, modifiers
elements:
// dir, exp, arg, modifiers
| [string]
| [string, ExpressionNode]
| [string, ExpressionNode, ExpressionNode]
@@ -71,7 +73,8 @@ type A = // dir, exp, arg, modifiers
| [string, ExpressionNode, ExpressionNode]
| [string, ExpressionNode, ExpressionNode, ObjectExpression];
const elements: // dir, exp, arg, modifiers
const elements:
// dir, exp, arg, modifiers
| [string]
| [string, ExpressionNode]
| [string, ExpressionNode, ExpressionNode]
@@ -214,3 +217,24 @@ type A4 =
================================================================================
`;
exports[`mutiple.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
printWidth: 80 (default) |
=====================================input======================================
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
TakesString<AllKeys> | void;
=====================================output=====================================
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
TakesString<AllKeys> | void;
================================================================================
`;
@@ -0,0 +1,5 @@
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
TakesString<AllKeys> | void;
@@ -0,0 +1,18 @@
type T = any extends B
/**
* Comment
*/
? B | C
: D;
type T2 = any extends B
? B | C
/**
* Comment
*/
: D;
T = any instanceof B
/**
* Comment
*/
? B | C
: D;
@@ -0,0 +1,3 @@
functionArg = a as
// comment
TSESTree.ArrowFunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression | undefined;
@@ -60,42 +60,96 @@ type ClassLike________80 =
=====================================output=====================================
export type ButtonColor =
| "primary"
| "danger"
| "warning"
| "muted"
| "success";
"primary" | "danger" | "warning" | "muted" | "success";
export type Button__80 = "primary" | "danger" | "warning" | "muted" | "success";
export type SupportOptionType =
| "int"
| "string"
| "boolean"
| "choice"
| "path";
"int" | "string" | "boolean" | "choice" | "path";
export type SupportOption_80 = "int" | "string" | "boolean" | "choice" | "path";
export type RenderContextType =
| "data"
| "header"
| "footer"
| "columnsSelector";
"data" | "header" | "footer" | "columnsSelector";
export type RenderContext_80 = "data" | "header" | "footer" | "columnsSelector";
export type PropertyNameNonComputed =
| Identifier
| NumberLiteral
| StringLiteral;
Identifier | NumberLiteral | StringLiteral;
export type PropertyNameNon_____80 = Identifier | NumberLiteral | StringLiteral;
type ClassLikeDeclaration =
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression;
TSESTree.ClassDeclaration | TSESTree.ClassExpression;
type ClassLike________80 = TSESTree.ClassDeclaration | TSESTree.ClassExpression;
================================================================================
`;
exports[`18647.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
printWidth: 80 (default) |
=====================================input======================================
type T = any extends B
/**
* Comment
*/
? B | C
: D;
type T2 = any extends B
? B | C
/**
* Comment
*/
: D;
T = any instanceof B
/**
* Comment
*/
? B | C
: D;
=====================================output=====================================
type T = any extends B
? /**
* Comment
*/
B | C
: D;
type T2 = any extends B
? B | C
: /**
* Comment
*/
D;
T =
any instanceof B
? /**
* Comment
*/
B | C
: D;
================================================================================
`;
exports[`18819.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
printWidth: 80 (default) |
=====================================input======================================
functionArg = a as
// comment
TSESTree.ArrowFunctionExpression | TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression | undefined;
=====================================output=====================================
functionArg = a as
// comment
| TSESTree.ArrowFunctionExpression
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| undefined;
================================================================================
`;
exports[`comment.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
@@ -183,6 +237,26 @@ type B3 = a | /* 1 */ /* 2 */ b;
================================================================================
`;
exports[`conditional.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
printWidth: 80 (default) |
=====================================input======================================
// https://github.com/excalidraw/excalidraw/blob/f12ae80ba1e1364952c6c6c440ac4212acd06eab/packages/excalidraw/tests/helpers/api.ts#L208C1-L210C15
type A = T extends "arrow"
? ExcalidrawArrowElement["startBinding"] | ExcalidrawElbowArrowElement["startBinding"]
: never;
=====================================output=====================================
// https://github.com/excalidraw/excalidraw/blob/f12ae80ba1e1364952c6c6c440ac4212acd06eab/packages/excalidraw/tests/helpers/api.ts#L208C1-L210C15
type A = T extends "arrow"
? | ExcalidrawArrowElement["startBinding"]
| ExcalidrawElbowArrowElement["startBinding"]
: never;
================================================================================
`;
exports[`prettier-ignore.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow"]
@@ -358,8 +432,8 @@ type A5 =
| ({ key: string } | { key: string } | { key: string } | { key: string })
| { key: string }
| { key: string };
type A6 = /*1*/
| A
type A6 =
/*1*/ | A
// A comment to force break
| B;
@@ -0,0 +1,4 @@
// https://github.com/excalidraw/excalidraw/blob/f12ae80ba1e1364952c6c6c440ac4212acd06eab/packages/excalidraw/tests/helpers/api.ts#L208C1-L210C15
type A = T extends "arrow"
? ExcalidrawArrowElement["startBinding"] | ExcalidrawElbowArrowElement["startBinding"]
: never;
@@ -0,0 +1,8 @@
export const CONDITIONAL_AXIS_PROP_INDEX: Record<
ConditionalAxisProp,
{
part: keyof AxisEncode;
vgProp: VgEncodeChannel;
} | null // null if we need to convert condition to signal
> = {
}