diff --git a/changelog_unreleased/flow/19053.md b/changelog_unreleased/flow/19053.md new file mode 100644 index 000000000..bd5b4e806 --- /dev/null +++ b/changelog_unreleased/flow/19053.md @@ -0,0 +1,13 @@ +#### Print `async` keyword for component declarations (#19053 by @mvitousek) + + +```flow +// Input +async component MyAsyncComponent() {} + +// Prettier stable +component MyAsyncComponent() {} + +// Prettier main +async component MyAsyncComponent() {} +``` diff --git a/src/language-js/print/component.js b/src/language-js/print/component.js index 4901aed67..2a64888a7 100644 --- a/src/language-js/print/component.js +++ b/src/language-js/print/component.js @@ -15,7 +15,11 @@ import { printDeclareToken, printSemicolon } from "./miscellaneous.js"; function printComponent(path, options, print) { const { node } = path; - const parts = [printDeclareToken(path), "component"]; + const parts = [ + printDeclareToken(path), + node.async ? "async " : "", + "component", + ]; if (node.id) { parts.push(" ", print("id")); } diff --git a/src/language-js/types/flow-estree.d.ts b/src/language-js/types/flow-estree.d.ts index c6ebcab2c..5059e02a3 100644 --- a/src/language-js/types/flow-estree.d.ts +++ b/src/language-js/types/flow-estree.d.ts @@ -431,6 +431,7 @@ export interface ComponentParameter extends BaseNode { export interface ComponentDeclaration extends BaseNode { type: "ComponentDeclaration"; + async: boolean; body: BlockStatement; id: Identifier; params: ReadonlyArray; @@ -440,6 +441,7 @@ export interface ComponentDeclaration extends BaseNode { export interface HookDeclaration extends BaseNode { type: "HookDeclaration"; + async: boolean; id: Identifier; body: BlockStatement; params: ReadonlyArray; diff --git a/tests/format/flow/component/__snapshots__/format.test.js.snap b/tests/format/flow/component/__snapshots__/format.test.js.snap index 78324d7d9..4de3d6ce1 100644 --- a/tests/format/flow/component/__snapshots__/format.test.js.snap +++ b/tests/format/flow/component/__snapshots__/format.test.js.snap @@ -72,6 +72,16 @@ component MyComponent( component MyComponent() /* Trailing comment */ {} +async component MyAsyncComponent() {} + +async component MyAsyncComponent() renders SomeComponent {} + +async component MyAsyncComponent(bar: string, baz: number) { + return ; +} + +export async component MyExportedAsyncComponent() {} + =====================================output===================================== component MyComponent() {} @@ -174,6 +184,16 @@ component MyComponent( component MyComponent() /* Trailing comment */ {} +async component MyAsyncComponent() {} + +async component MyAsyncComponent() renders SomeComponent {} + +async component MyAsyncComponent(bar: string, baz: number) { + return ; +} + +export async component MyExportedAsyncComponent() {} + ================================================================================ `; diff --git a/tests/format/flow/component/component-declaration.js b/tests/format/flow/component/component-declaration.js index 29c45e02b..aa2781f56 100644 --- a/tests/format/flow/component/component-declaration.js +++ b/tests/format/flow/component/component-declaration.js @@ -64,3 +64,13 @@ component MyComponent( ) {} component MyComponent() /* Trailing comment */ {} + +async component MyAsyncComponent() {} + +async component MyAsyncComponent() renders SomeComponent {} + +async component MyAsyncComponent(bar: string, baz: number) { + return ; +} + +export async component MyExportedAsyncComponent() {} diff --git a/tests/format/flow/hook/__snapshots__/format.test.js.snap b/tests/format/flow/hook/__snapshots__/format.test.js.snap index 399b3f29f..6073a2cba 100644 --- a/tests/format/flow/hook/__snapshots__/format.test.js.snap +++ b/tests/format/flow/hook/__snapshots__/format.test.js.snap @@ -297,6 +297,14 @@ hook useFoo14< T: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo, >(): any {}; +async hook useAsyncFoo1() {} + +export async hook useAsyncFoo2() {} + +async hook useAsyncFoo3(): string {} + +async hook useAsyncFoo4(foo: Foo, ...bar: Bar) {} + =====================================output===================================== hook useFoo1() {} @@ -337,6 +345,14 @@ hook useFoo14< T: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo, >(): any {} +async hook useAsyncFoo1() {} + +export async hook useAsyncFoo2() {} + +async hook useAsyncFoo3(): string {} + +async hook useAsyncFoo4(foo: Foo, ...bar: Bar) {} + ================================================================================ `; diff --git a/tests/format/flow/hook/hook-declaration.js b/tests/format/flow/hook/hook-declaration.js index 089ca05ce..9e3f5d45a 100644 --- a/tests/format/flow/hook/hook-declaration.js +++ b/tests/format/flow/hook/hook-declaration.js @@ -28,3 +28,11 @@ hook useFoo13(foo: Array(): any {}; + +async hook useAsyncFoo1() {} + +export async hook useAsyncFoo2() {} + +async hook useAsyncFoo3(): string {} + +async hook useAsyncFoo4(foo: Foo, ...bar: Bar) {}