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

Fix for class method default parameters that use this #3334

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/typing/func_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ let toplevels id cx this super ~decls ~stmts ~expr
(* push the scope early so default exprs can reference earlier params *)
Env.push_var_scope cx function_scope;

(* add `this` before looking at parameter bindings as when using `this`
in default parameter values it refers to the function scope
*)
Scope.add_entry (internal_name "this") this function_scope;

(* bind type params *)
SMap.iter (fun name t ->
let r = reason_of_t t in
Expand Down Expand Up @@ -290,7 +295,6 @@ let toplevels id cx this super ~decls ~stmts ~expr
new_entry yield_t, new_entry next_t, new_entry return_t
) in

Scope.add_entry (internal_name "this") this function_scope;
Scope.add_entry (internal_name "super") super function_scope;
Scope.add_entry (internal_name "yield") yield function_scope;
Scope.add_entry (internal_name "next") next function_scope;
Expand Down
2 changes: 2 additions & 0 deletions tests/class_method_default_parameters/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[options]
no_flowlib=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class_method_default_parameters.js:17
17: h(i: number = this.b) { // error
^^^^^^ string. This type is incompatible with
17: h(i: number = this.b) { // error
^^^^^^ number


Found 1 error
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class A {

b: string;

c(d = this.b) { // ok - can use `this` in function default parameter values

}

e() {
return this.b;
}

f(g = this.e()) { // ok - can use `this` in function default parameter values

}

h(i: number = this.b) { // error

}

}