Skip to content

Commit

Permalink
Fix for class method default parameters that use this
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Bretman committed Feb 3, 2017
1 parent 75ce770 commit 0a49076
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
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

}

}

0 comments on commit 0a49076

Please sign in to comment.