-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hug object types in function parameters in Flow like in TS #13396
Changes from 3 commits
86808ad
6c1b60d
63d43e2
4036178
f0cd175
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,12 @@ import { | |
getComments, | ||
CommentCheckFlags, | ||
isNextLineEmpty, | ||
isObjectType, | ||
} from "../utils/index.js"; | ||
import { locStart, locEnd } from "../loc.js"; | ||
|
||
import { printOptionalToken, printTypeAnnotation } from "./misc.js"; | ||
import { shouldHugFunctionParameters } from "./function-parameters.js"; | ||
import { shouldHugType } from "./type-annotation.js"; | ||
import { printHardlineAfterHeritage } from "./class.js"; | ||
|
||
/** @typedef {import("../../document/builders.js").Doc} Doc */ | ||
|
@@ -201,26 +201,21 @@ function printObject(path, options, print) { | |
if ( | ||
path.match( | ||
(node) => node.type === "ObjectPattern" && !node.decorators, | ||
(node, name, number) => | ||
shouldHugFunctionParameters(node) && | ||
(name === "params" || | ||
name === "parameters" || | ||
name === "this" || | ||
name === "rest") && | ||
number === 0 | ||
) || | ||
path.match( | ||
shouldHugType, | ||
(node, name) => name === "typeAnnotation", | ||
(node, name) => name === "typeAnnotation", | ||
(node, name, number) => | ||
shouldHugFunctionParameters(node) && | ||
(name === "params" || | ||
name === "parameters" || | ||
name === "this" || | ||
name === "rest") && | ||
number === 0 | ||
shouldHugParameter | ||
) || | ||
(isObjectType(node) && | ||
(path.match( | ||
undefined, | ||
(node, name) => name === "typeAnnotation", | ||
(node, name) => name === "typeAnnotation", | ||
shouldHugParameter | ||
) || | ||
path.match( | ||
undefined, | ||
(node, name) => | ||
node.type === "FunctionTypeParam" && name === "typeAnnotation", | ||
shouldHugParameter | ||
))) || | ||
// Assignment printing logic (printAssignment) is responsible | ||
// for adding a group if needed | ||
(!shouldBreak && | ||
|
@@ -237,4 +232,13 @@ function printObject(path, options, print) { | |
return group(content, { shouldBreak }); | ||
} | ||
|
||
function shouldHugParameter(node, name, number) { | ||
return ( | ||
((number === 0 && (name === "params" || name === "parameters")) || | ||
name === "this" || | ||
name === "rest") && | ||
shouldHugFunctionParameters(node) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If function has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little bit confused, the "node" is a function, right? It can't be another function's parameter, how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, this is different from callback of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also renamed |
||
); | ||
} | ||
|
||
export { printObject }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
declare function foo(this: { a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bazFlip({ a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bar(...{ a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bar(...x: { a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
declare function foo(this: { a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bazFlip({ a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bar(...{ a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> | ||
|
||
declare function bar(...x: { a: boolean, b: string, c: number }): | ||
Promise<Array<foo>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool if
path.match
supported alternatives like this: