Skip to content
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

Support Flow this parameter annotations #12234

Merged
merged 22 commits into from Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/babel-generator/src/generators/flow.ts
Expand Up @@ -289,6 +289,18 @@ export function FunctionTypeAnnotation(
) {
this.print(node.typeParameters, node);
this.token("(");

if (node.this) {
this.word("this");
this.token(":");
this.space();
this.print(node.this.typeAnnotation, node);
if (node.params.length || node.rest) {
this.token(",");
this.space();
}
}

this.printList(node.params, node);

if (node.rest) {
Expand Down
45 changes: 45 additions & 0 deletions packages/babel-generator/test/fixtures/flow/this-param/input.js
@@ -0,0 +1,45 @@
function one (this : number) {}
function two (this : number, a) {}
function three (this : number, ...a) {}
function four (this : number, a, b, ...c) {}
function five<T> (this : T) {}

type six = (this : number) => void
type seven = (this : number, a : number) => void
type eight = (this : number, ...a : any) => void
type nine = <T>(this : T) => void

type ten = {
m1(this : string) : void,
m2(this : string, a : number) : void,
m3(this : string, ...a : any) : void,
m4<T>(this : T) : void
}

declare function eleven (this : number) : void
declare function twelve (this : string, a : number) : void
declare function thirteen (this : string, ...a : any) : void
declare function fourteen<T>(this : T) : void

declare class Fifteen {
m1(this : string) : void,
m2(this : string, a : number) : void,
m3(this : string, ...a : any) : void,
m4<T>(this : T) : void
}

class Sixteen {
m1 (this : number) {}
m2 (this : number, a) {}
m3 (this : number, ...a) {}
m4 (this : number, a, b, ...c) {}
m5<T> (this : T) {}
}

let seventeen = {
m1 (this : number) {},
m2 (this : number, a) {},
m3 (this : number, ...a) {},
m4 (this : number, a, b, ...c) {},
m5<T> (this : T) {}
}
56 changes: 56 additions & 0 deletions packages/babel-generator/test/fixtures/flow/this-param/output.js
@@ -0,0 +1,56 @@
function one(this: number) {}

function two(this: number, a) {}

function three(this: number, ...a) {}

function four(this: number, a, b, ...c) {}

function five<T>(this: T) {}

type six = (this: number) => void;
type seven = (this: number, a: number) => void;
type eight = (this: number, ...a: any) => void;
type nine = <T>(this: T) => void;
type ten = {
m1(this: string): void,
m2(this: string, a: number): void,
m3(this: string, ...a: any): void,
m4<T>(this: T): void,
};
declare function eleven(this: number): void;
declare function twelve(this: string, a: number): void;
declare function thirteen(this: string, ...a: any): void;
declare function fourteen<T>(this: T): void;
declare class Fifteen {
m1(this: string): void,
m2(this: string, a: number): void,
m3(this: string, ...a: any): void,
m4<T>(this: T): void,
}

class Sixteen {
m1(this: number) {}

m2(this: number, a) {}

m3(this: number, ...a) {}

m4(this: number, a, b, ...c) {}

m5<T>(this: T) {}

}

let seventeen = {
m1(this: number) {},

m2(this: number, a) {},

m3(this: number, ...a) {},

m4(this: number, a, b, ...c) {},

m5<T>(this: T) {}

};