Add heuristics to detect assignments whose id has complex type annotations (#10916)
This commit is contained in:
1 parent
f8cb785758
commit
c40a0cf2b8
7 files changed
+148
-1
No files matched your search
@@ -0,0 +1,21 @@
|
||||
#### Break the LHS of assignments that has complex type parameters (#10916 by @sosukesuzuki)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```ts
|
||||
// Input
|
||||
const map: Map<
|
||||
Function,
|
||||
Map<string | void, { value: UnloadedDescriptor }>
|
||||
> = new Map();
|
||||
|
||||
// Prettier stable
|
||||
const map: Map<Function, Map<string | void, { value: UnloadedDescriptor }>> =
|
||||
new Map();
|
||||
|
||||
// Prettier main
|
||||
const map: Map<
|
||||
Function,
|
||||
Map<string | void, { value: UnloadedDescriptor }>
|
||||
> = new Map();
|
||||
|
||||
```
|
||||
@@ -136,7 +136,11 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) {
|
||||
return "never-break-after-operator";
|
||||
}
|
||||
|
||||
if (isComplexDestructuring(node) || isComplexTypeAliasParams(node)) {
|
||||
if (
|
||||
isComplexDestructuring(node) ||
|
||||
isComplexTypeAliasParams(node) ||
|
||||
hasComplexTypeAnnotation(node)
|
||||
) {
|
||||
return "break-lhs";
|
||||
}
|
||||
|
||||
@@ -269,6 +273,45 @@ function isTypeAlias(node) {
|
||||
return node.type === "TSTypeAliasDeclaration" || node.type === "TypeAlias";
|
||||
}
|
||||
|
||||
function hasComplexTypeAnnotation(node) {
|
||||
if (node.type !== "VariableDeclarator") {
|
||||
return false;
|
||||
}
|
||||
const { typeAnnotation } = node.id;
|
||||
if (!typeAnnotation || !typeAnnotation.typeAnnotation) {
|
||||
return false;
|
||||
}
|
||||
const typeParams = getTypeParametersFromTypeReference(
|
||||
typeAnnotation.typeAnnotation
|
||||
);
|
||||
return (
|
||||
isNonEmptyArray(typeParams) &&
|
||||
typeParams.length > 1 &&
|
||||
typeParams.some(
|
||||
(param) =>
|
||||
isNonEmptyArray(getTypeParametersFromTypeReference(param)) ||
|
||||
param.type === "TSConditionalType"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function getTypeParametersFromTypeReference(node) {
|
||||
if (
|
||||
isTypeReference(node) &&
|
||||
node.typeParameters &&
|
||||
node.typeParameters.params
|
||||
) {
|
||||
return node.typeParameters.params;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isTypeReference(node) {
|
||||
return (
|
||||
node.type === "TSTypeReference" || node.type === "GenericTypeAnnotation"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A chain with no calls at all or whose calls are all without arguments or with lone short arguments,
|
||||
* excluding chains printed by `printMemberChain`
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`issue-10850.js format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["babel-flow", "flow"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
const map: Map<Function, Map<string | void, { value: UnloadedDescriptor }>> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
|
||||
=====================================output=====================================
|
||||
const map: Map<
|
||||
Function,
|
||||
Map<string | void, { value: UnloadedDescriptor }>
|
||||
> = new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -0,0 +1,7 @@
|
||||
const map: Map<Function, Map<string | void, { value: UnloadedDescriptor }>> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
@@ -0,0 +1 @@
|
||||
run_spec(__dirname, ["babel-flow", "flow"]);
|
||||
@@ -180,6 +180,42 @@ const firestorePersonallyIdentifiablePaths: Array<Collections.Users.Entity> =
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`issue-10850.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
const map: Map<Function, Map<string | void, { value: UnloadedDescriptor }>> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Condition extends Foo ? FooFooFoo : BarBarBar> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
|
||||
=====================================output=====================================
|
||||
const map: Map<
|
||||
Function,
|
||||
Map<string | void, { value: UnloadedDescriptor }>
|
||||
> = new Map();
|
||||
|
||||
const map: Map<
|
||||
Function,
|
||||
Condition extends Foo ? FooFooFoo : BarBarBar
|
||||
> = new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`lone-arg.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
const map: Map<Function, Map<string | void, { value: UnloadedDescriptor }>> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Condition extends Foo ? FooFooFoo : BarBarBar> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, FunctionFunctionFunctionFunctionffFunction> =
|
||||
new Map();
|
||||
|
||||
const map: Map<Function, Foo<S>> = new Map();
|
||||
Reference in new issue
Block a user