Skip to content

Commit

Permalink
[PR] Fix for class method default parameters that use this
Browse files Browse the repository at this point in the history
Summary:
Example of bug - https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVBjGBDAzrsAQTAG9UwxyxsAuMXAFwCcBLAOwHMqqAjACgxgAvGAYALFrgB02AJSkqFKgF9uyoA

Fixes #3353
Fixes #1234

Closes #3334

Reviewed By: samwgoldman

Differential Revision: D4523083

Pulled By: jeffmo

fbshipit-source-id: 6e8be65711263182d8eef568ae34a46d77b399a3
  • Loading branch information
Jon Bretman authored and facebook-github-bot committed Feb 7, 2017
1 parent 3e4d589 commit d3887c2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/typing/func_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ 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` and `super` before looking at parameter bindings as when using
* `this` in default parameter values it refers to the function scope and
* `super` should resolve to the method's [[HomeObject]]
*)
Scope.add_entry (internal_name "this") this function_scope;
Scope.add_entry (internal_name "super") super function_scope;

(* bind type params *)
SMap.iter (fun name t ->
let r = reason_of_t t in
Expand Down Expand Up @@ -290,8 +297,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;
Scope.add_entry (internal_name "return") return 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

}

}

0 comments on commit d3887c2

Please sign in to comment.