2013-11-29 23:58:26 -05:00
|
|
|
# Generators
|
|
|
|
# -----------------
|
2013-12-19 17:21:14 -05:00
|
|
|
#
|
2013-11-29 23:58:26 -05:00
|
|
|
# * Generator Definition
|
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "most basic generator support", ->
|
2015-09-13 06:30:59 -04:00
|
|
|
ok -> yield
|
2013-11-29 23:58:26 -05:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "empty generator", ->
|
|
|
|
x = do -> yield return
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is undefined and y.done is true
|
|
|
|
|
|
|
|
test "generator iteration", ->
|
2014-09-06 11:25:44 -04:00
|
|
|
x = do ->
|
2013-11-30 15:26:32 -05:00
|
|
|
yield 0
|
2015-09-13 06:30:59 -04:00
|
|
|
yield
|
2013-11-30 15:26:32 -05:00
|
|
|
yield 2
|
2015-09-13 06:30:59 -04:00
|
|
|
3
|
|
|
|
|
2014-09-06 11:25:44 -04:00
|
|
|
y = x.next()
|
|
|
|
ok y.value is 0 and y.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-09-06 11:25:44 -04:00
|
|
|
y = x.next()
|
2015-09-13 06:30:59 -04:00
|
|
|
ok y.value is undefined and y.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-09-06 11:25:44 -04:00
|
|
|
y = x.next()
|
|
|
|
ok y.value is 2 and y.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-09-06 11:25:44 -04:00
|
|
|
y = x.next()
|
2015-09-13 06:30:59 -04:00
|
|
|
ok y.value is 3 and y.done is true
|
2013-12-19 17:21:14 -05:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "last line yields are returned", ->
|
|
|
|
x = do ->
|
|
|
|
yield 3
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 3 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 42
|
|
|
|
ok y.value is 42 and y.done is true
|
|
|
|
|
2015-02-15 12:57:15 -05:00
|
|
|
test "yield return can be used anywhere in the function body", ->
|
|
|
|
x = do ->
|
2015-02-17 13:04:26 -05:00
|
|
|
if 2 is yield 1
|
|
|
|
yield return 42
|
|
|
|
throw new Error "this code shouldn't be reachable"
|
2015-02-15 12:57:15 -05:00
|
|
|
|
|
|
|
y = x.next()
|
2015-02-17 13:04:26 -05:00
|
|
|
ok y.value is 1 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 2
|
|
|
|
ok y.value is 42 and y.done is true
|
2015-02-15 12:57:15 -05:00
|
|
|
|
2014-09-06 11:25:44 -04:00
|
|
|
test "`yield from` support", ->
|
|
|
|
x = do ->
|
|
|
|
yield from do ->
|
|
|
|
yield i for i in [3..4]
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 3 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 1
|
|
|
|
ok y.value is 4 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 2
|
|
|
|
arrayEq y.value, [1, 2]
|
|
|
|
ok y.done is true
|
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "error if `yield from` occurs outside of a function", ->
|
|
|
|
throws -> CoffeeScript.compile 'yield from 1'
|
2014-09-06 11:25:44 -04:00
|
|
|
|
|
|
|
test "`yield from` at the end of a function errors", ->
|
|
|
|
throws -> CoffeeScript.compile 'x = -> x = 1; yield from'
|
|
|
|
|
|
|
|
test "yield in if statements", ->
|
|
|
|
x = do -> if 1 is yield 2 then 3 else 4
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 2 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 1
|
|
|
|
ok y.value is 3 and y.done is true
|
2014-10-12 13:08:28 -04:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "yielding if statements", ->
|
|
|
|
x = do -> yield if true then 3 else 4
|
2014-10-12 15:32:02 -04:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
y = x.next()
|
|
|
|
ok y.value is 3 and y.done is false
|
2014-10-12 15:32:02 -04:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
y = x.next 42
|
|
|
|
ok y.value is 42 and y.done is true
|
2014-10-12 15:32:02 -04:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "yield in for loop expressions", ->
|
2014-11-21 17:14:53 -05:00
|
|
|
x = do ->
|
|
|
|
y = for i in [1..3]
|
|
|
|
yield i * 2
|
|
|
|
|
|
|
|
z = x.next()
|
|
|
|
ok z.value is 2 and z.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
z = x.next 10
|
|
|
|
ok z.value is 4 and z.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
z = x.next 20
|
|
|
|
ok z.value is 6 and z.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
z = x.next 30
|
|
|
|
arrayEq z.value, [10, 20, 30]
|
|
|
|
ok z.done is true
|
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "yield in switch expressions", ->
|
2014-11-21 17:14:53 -05:00
|
|
|
x = do ->
|
|
|
|
y = switch yield 1
|
|
|
|
when 2 then yield 1337
|
2015-02-15 12:52:18 -05:00
|
|
|
else 1336
|
2014-11-21 17:14:53 -05:00
|
|
|
|
|
|
|
z = x.next()
|
|
|
|
ok z.value is 1 and z.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
z = x.next 2
|
|
|
|
ok z.value is 1337 and z.done is false
|
2015-02-15 12:52:18 -05:00
|
|
|
|
2014-11-21 17:14:53 -05:00
|
|
|
z = x.next 3
|
|
|
|
ok z.value is 3 and z.done is true
|
2015-02-15 13:01:00 -05:00
|
|
|
|
2015-02-15 12:52:18 -05:00
|
|
|
test "yielding switch expressions", ->
|
|
|
|
x = do ->
|
|
|
|
yield switch 1337
|
|
|
|
when 1337 then 1338
|
|
|
|
else 1336
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 1338 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 42
|
|
|
|
ok y.value is 42 and y.done is true
|
|
|
|
|
|
|
|
test "yield in try expressions", ->
|
|
|
|
x = do ->
|
|
|
|
try yield 1 catch
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 1 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 42
|
|
|
|
ok y.value is 42 and y.done is true
|
|
|
|
|
|
|
|
test "yielding try expressions", ->
|
|
|
|
x = do ->
|
|
|
|
yield try 1
|
|
|
|
|
|
|
|
y = x.next()
|
|
|
|
ok y.value is 1 and y.done is false
|
|
|
|
|
|
|
|
y = x.next 42
|
|
|
|
ok y.value is 42 and y.done is true
|
|
|
|
|
2015-02-15 13:01:00 -05:00
|
|
|
test "`yield` can be thrown", ->
|
|
|
|
x = do ->
|
|
|
|
throw yield null
|
|
|
|
x.next()
|
|
|
|
throws -> x.next new Error "boom"
|
|
|
|
|
|
|
|
test "`throw` can be yielded", ->
|
|
|
|
x = do ->
|
|
|
|
yield throw new Error "boom"
|
|
|
|
throws -> x.next()
|
2015-02-15 12:52:18 -05:00
|
|
|
|
|
|
|
test "symbolic operators has precedence over the `yield`", ->
|
|
|
|
symbolic = '+ - * / << >> & | || && ** ^ // or and'.split ' '
|
|
|
|
compound = ("#{op}=" for op in symbolic)
|
|
|
|
relations = '< > == != <= >= is isnt'.split ' '
|
|
|
|
|
|
|
|
operators = [symbolic..., '=', compound..., relations...]
|
|
|
|
|
|
|
|
collect = (gen) -> ref.value until (ref = gen.next()).done
|
|
|
|
|
|
|
|
values = [0, 1, 2, 3]
|
|
|
|
for op in operators
|
|
|
|
expression = "i #{op} 2"
|
|
|
|
|
|
|
|
yielded = CoffeeScript.eval "(arr) -> yield #{expression} for i in arr"
|
|
|
|
mapped = CoffeeScript.eval "(arr) -> (#{expression} for i in arr)"
|
|
|
|
|
|
|
|
arrayEq mapped(values), collect yielded values
|
2015-02-26 07:01:12 -05:00
|
|
|
|
|
|
|
test "yield handles 'this' correctly", ->
|
|
|
|
x = ->
|
|
|
|
yield switch
|
|
|
|
when true then yield => this
|
2015-09-13 06:30:59 -04:00
|
|
|
array = for item in [1]
|
2015-02-26 07:01:12 -05:00
|
|
|
yield => this
|
2015-09-13 06:30:59 -04:00
|
|
|
yield array
|
2015-02-26 07:01:12 -05:00
|
|
|
yield if true then yield => this
|
|
|
|
yield try throw yield => this
|
|
|
|
throw yield => this
|
|
|
|
|
|
|
|
y = x.call [1, 2, 3]
|
|
|
|
|
|
|
|
z = y.next()
|
|
|
|
arrayEq z.value(), [1, 2, 3]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
z = y.next 123
|
|
|
|
ok z.value is 123 and z.done is false
|
|
|
|
|
|
|
|
z = y.next()
|
|
|
|
arrayEq z.value(), [1, 2, 3]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
z = y.next 42
|
|
|
|
arrayEq z.value, [42]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
z = y.next()
|
|
|
|
arrayEq z.value(), [1, 2, 3]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
z = y.next 456
|
|
|
|
ok z.value is 456 and z.done is false
|
|
|
|
|
|
|
|
z = y.next()
|
|
|
|
arrayEq z.value(), [1, 2, 3]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
z = y.next new Error "ignore me"
|
|
|
|
ok z.value is undefined and z.done is false
|
|
|
|
|
|
|
|
z = y.next()
|
|
|
|
arrayEq z.value(), [1, 2, 3]
|
|
|
|
ok z.done is false
|
|
|
|
|
|
|
|
throws -> y.next new Error "boom"
|
2016-11-08 02:40:01 -05:00
|
|
|
|
|
|
|
test "for-from loops over generators", ->
|
|
|
|
array1 = [50, 30, 70, 20]
|
|
|
|
gen = -> yield from array1
|
|
|
|
|
|
|
|
array2 = []
|
|
|
|
array3 = []
|
|
|
|
array4 = []
|
|
|
|
|
|
|
|
iterator = gen()
|
|
|
|
for x from iterator
|
|
|
|
array2.push(x)
|
|
|
|
break if x is 30
|
|
|
|
|
|
|
|
for x from iterator
|
|
|
|
array3.push(x)
|
|
|
|
|
|
|
|
for x from iterator
|
|
|
|
array4.push(x)
|
|
|
|
|
|
|
|
arrayEq array2, [50, 30]
|
|
|
|
# Different JS engines have different opinions on the value of array3:
|
|
|
|
# https://github.com/jashkenas/coffeescript/pull/4306#issuecomment-257066877
|
|
|
|
# As a temporary measure, either result is accepted.
|
|
|
|
ok array3.length is 0 or array3.join(',') is '70,20'
|
|
|
|
arrayEq array4, []
|
|
|
|
|
|
|
|
test "for-from comprehensions over generators", ->
|
|
|
|
gen = ->
|
|
|
|
yield from [30, 41, 51, 60]
|
|
|
|
|
|
|
|
iterator = gen()
|
|
|
|
array1 = (x for x from iterator when x %% 2 is 1)
|
|
|
|
array2 = (x for x from iterator)
|
|
|
|
|
|
|
|
ok array1.join(' ') is '41 51'
|
|
|
|
ok array2.length is 0
|
2016-12-06 15:29:02 -05:00
|
|
|
|
|
|
|
test "from as an iterable variable name in a for loop declaration", ->
|
|
|
|
from = [1, 2, 3]
|
|
|
|
out = []
|
|
|
|
for i from from
|
|
|
|
out.push i
|
|
|
|
arrayEq from, out
|
|
|
|
|
|
|
|
test "from as an iterator variable name in a for loop declaration", ->
|
|
|
|
a = [1, 2, 3]
|
|
|
|
b = []
|
|
|
|
for from from a
|
|
|
|
b.push from
|
|
|
|
arrayEq a, b
|
|
|
|
|
|
|
|
test "from as a destructured object variable name in a for loop declaration", ->
|
|
|
|
a = [
|
|
|
|
from: 1
|
|
|
|
to: 2
|
|
|
|
,
|
|
|
|
from: 3
|
|
|
|
to: 4
|
|
|
|
]
|
|
|
|
b = []
|
|
|
|
for {from, to} in a
|
|
|
|
b.push from
|
|
|
|
arrayEq b, [1, 3]
|
|
|
|
|
|
|
|
c = []
|
|
|
|
for {to, from} in a
|
|
|
|
c.push from
|
|
|
|
arrayEq c, [1, 3]
|
|
|
|
|
|
|
|
test "from as a destructured, aliased object variable name in a for loop declaration", ->
|
|
|
|
a = [
|
|
|
|
b: 1
|
|
|
|
c: 2
|
|
|
|
,
|
|
|
|
b: 3
|
|
|
|
c: 4
|
|
|
|
]
|
|
|
|
out = []
|
|
|
|
|
|
|
|
for {b: from} in a
|
|
|
|
out.push from
|
|
|
|
arrayEq out, [1, 3]
|
|
|
|
|
|
|
|
test "from as a destructured array variable name in a for loop declaration", ->
|
|
|
|
a = [
|
|
|
|
[1, 2]
|
|
|
|
[3, 4]
|
|
|
|
]
|
|
|
|
b = []
|
|
|
|
for [from, to] from a
|
|
|
|
b.push from
|
|
|
|
arrayEq b, [1, 3]
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* Compile classes to ES2015 classes
Rather than compiling classes to named functions with prototype and
class assignments, they are now compiled to ES2015 class declarations.
Backwards compatibility has been maintained by compiling ES2015-
incompatible properties as prototype or class assignments. `super`
continues to be compiled as before.
Where possible, classes will be compiled "bare", without an enclosing
IIFE. This is possible when the class contains only ES2015 compatible
expressions (methods and static methods), and has no parent (this last
constraint is a result of the legacy `super` compilation, and could be
removed once ES2015 `super` is being used). Classes are still assigned
to variables to maintain compatibility for assigned class expressions.
There are a few changes to existing functionality that could break
backwards compatibility:
- Derived constructors that deliberately don't call `super` are no
longer possible. ES2015 derived classes can't use `this` unless the
parent constructor has been called, so it's now called implicitly when
not present.
- As a consequence of the above, derived constructors with @ parameters
or bound methods and explicit `super` calls are not allowed. The
implicit `super` must be used in these cases.
* Add tests to verify class interoperability with ES
* Refactor class nodes to separate executable body logic
Logic has been redistributed amongst the class nodes so that:
- `Class` contains the logic necessary to compile an ES class
declaration.
- `ExecutableClassBody` contains the logic necessary to compile CS'
class extensions that require an executable class body.
`Class` still necessarily contains logic to determine whether an
expression is valid in an ES class initializer or not. If any invalid
expressions are found then `Class` will wrap itself in an
`ExecutableClassBody` when compiling.
* Rename `Code#static` to `Code#isStatic`
This naming is more consistent with other `Code` flags.
* Output anonymous classes when possible
Anonymous classes can be output when:
- The class has no parent. The current super compilation needs a class
variable to reference. This condition will go away when ES2015 super
is in use.
- The class contains no bound static methods. Bound static methods have
their context set to the class name.
* Throw errors at compile time for async or generator constructors
* Improve handling of anonymous classes
Anonymous classes are now always anonymous. If a name is required (e.g.
for bound static methods or derived classes) then the class is compiled
in an `ExecutableClassBody` which will give the anonymous class a stable
reference.
* Add a `replaceInContext` method to `Node`
`replaceInContext` will traverse children looking for a node for which
`match` returns true. Once found, the matching node will be replaced by
the result of calling `replacement`.
* Separate `this` assignments from function parameters
This change has been made to simplify two future changes:
1. Outputting `@`-param assignments after a `super` call.
In this case it is necessary that non-`@` parameters are available
before `super` is called, so destructuring has to happen before
`this` assignment.
2. Compiling destructured assignment to ES6
In this case also destructuring has to happen before `this`,
as destructuring can happen in the arguments list, but `this`
assignment can not.
A bonus side-effect is that default values for `@` params are now output
as ES6 default parameters, e.g.
(@a = 1) ->
becomes
function a (a = 1) {
this.a = a;
}
* Change `super` handling in class constructors
Inside an ES derived constructor (a constructor for a class that extends
another class), it is impossible to access `this` until `super` has been
called. This conflicts with CoffeeScript's `@`-param and bound method
features, which compile to `this` references at the top of a function
body. For example:
class B extends A
constructor: (@param) -> super
method: =>
This would compile to something like:
class B extends A {
constructor (param) {
this.param = param;
this.method = bind(this.method, this);
super(...arguments);
}
}
This would break in an ES-compliant runtime as there are `this`
references before the call to `super`. Before this commit we were
dealing with this by injecting an implicit `super` call into derived
constructors that do not already have an explicit `super` call.
Furthermore, we would disallow explicit `super` calls in derived
constructors that used bound methods or `@`-params, meaning the above
example would need to be rewritten as:
class B extends A
constructor: (@param) ->
method: =>
This would result in a call to `super(...arguments)` being generated as
the first expression in `B#constructor`.
Whilst this approach seems to work pretty well, and is arguably more
convenient than having to manually call `super` when you don't
particularly care about the arguments, it does introduce some 'magic'
and separation from ES, and would likely be a pain point in a project
that made use of significant constructor overriding.
This commit introduces a mechanism through which `super` in constructors
is 'expanded' to include any generated `this` assignments, whilst
retaining the same semantics of a super call. The first example above
now compiles to something like:
class B extends A {
constructor (param) {
var ref
ref = super(...arguments), this.param = param, this.method = bind(this.method, this), ref;
}
}
* Improve `super` handling in constructors
Rather than functions expanding their `super` calls, the `SuperCall`
node can now be given a list of `thisAssignments` to apply when it is
compiled.
This allows us to use the normal compiler machinery to determine whether
the `super` result needs to be cached, whether it appears inline or not,
etc.
* Fix anonymous classes at the top level
Anonymous classes in ES are only valid within expressions. If an
anonymous class is at the top level it will now be wrapped in
parenthses to force it into an expression.
* Re-add Parens wrapper around executable class bodies
This was lost in the refactoring, but it necessary to ensure
`new class ...` works as expected when there's an executable body.
* Throw compiler errors for badly configured derived constructors
Rather than letting them become runtime errors, the following checks are
now performed when compiling a derived constructor:
- The constructor **must** include a call to `super`.
- The constructor **must not** reference `this` in the function body
before `super` has been called.
* Add some tests exercising new class behaviour
- async methods in classes
- `this` access after `super` in extended classes
- constructor super in arrow functions
- constructor functions can't be async
- constructor functions can't be generators
- derived constructors must call super
- derived constructors can't reference `this` before calling super
- generator methods in classes
- 'new' target
* Improve constructor `super` errors
Add a check for `super` in non-extended class constructors, and
explicitly mention derived constructors in the "can't reference this
before super" error.
* Fix compilation of multiple `super` paths in derived constructors
`super` can only be called once, but it can be called conditionally from
multiple locations. The chosen fix is to add the `this` assignments to
every super call.
* Additional class tests, added as a separate file to simplify testing and merging.
Some methods are commented out because they currently throw and I'm not sure how
to test for compilation errors like those.
There is also one test which I deliberately left without passing, `super` in an external prototype override.
This test should 'pass' but is really a variation on the failing `super only allowed in an instance method`
tests above it.
* Changes to the tests. Found bug in super in prototype method. fixed.
* Added failing test back in, dealing with bound functions in external prototype overrides.
* Located a bug in the compiler relating to assertions and escaped ES6 classes.
* Move tests from classes-additional.coffee into classes.coffee; comment out console.log
* Cleaned up tests and made changes based on feedback. Test at the end still has issues, but it's commented out for now.
* Make HoistTarget.expand recursive
It's possible that a hoisted node may itself contain hoisted nodes (e.g.
a class method inside a class method). For this to work the hoisted
fragments need to be expanded recursively.
* Uncomment final test in classes.coffee
The test case now compiles, however another issue is affecting the test
due to the error for `this` before `super` triggering based on source
order rather than execution order. These have been commented out for
now.
* Fixed last test TODOs in test/classes.coffee
Turns out an expression like `this.foo = super()` won't run in JS as it
attempts to lookup `this` before evaluating `super` (i.e. throws "this
is not defined").
* Added more tests for compatability checks, statics, prototypes and ES6 expectations. Cleaned test "nested classes with super".
* Changes to reflect feedback and to comment out issues that will be addressed seperately.
* Clean up test/classes.coffee
- Trim trailing whitespace.
- Rephrase a condition to be more idiomatic.
* Remove check for `super` in derived constructors
In order to be usable at runtime, an extended ES class must call `super`
OR return an alternative object. This check prevented the latter case,
and checking for an alternative return can't be completed statically
without control flow analysis.
* Disallow 'super' in constructor parameter defaults
There are many edge cases when combining 'super' in parameter defaults
with @-parameters and bound functions (and potentially property
initializers in the future).
Rather than attempting to resolve these edge cases, 'super' is now
explicitly disallowed in constructor parameter defaults.
* Disallow @-params in derived constructors without 'super'
@-parameters can't be assigned unless 'super' is called.
2017-01-13 00:55:30 -05:00
|
|
|
|
|
|
|
test "generator methods in classes", ->
|
|
|
|
class Base
|
|
|
|
@static: ->
|
|
|
|
yield 1
|
|
|
|
method: ->
|
|
|
|
yield 2
|
|
|
|
|
|
|
|
arrayEq [1], Array.from Base.static()
|
|
|
|
arrayEq [2], Array.from new Base().method()
|
|
|
|
|
|
|
|
class Child extends Base
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
@static: -> super()
|
|
|
|
method: -> super()
|
[CS2] Compile class constructors to ES2015 classes (#4354)
* Compile classes to ES2015 classes
Rather than compiling classes to named functions with prototype and
class assignments, they are now compiled to ES2015 class declarations.
Backwards compatibility has been maintained by compiling ES2015-
incompatible properties as prototype or class assignments. `super`
continues to be compiled as before.
Where possible, classes will be compiled "bare", without an enclosing
IIFE. This is possible when the class contains only ES2015 compatible
expressions (methods and static methods), and has no parent (this last
constraint is a result of the legacy `super` compilation, and could be
removed once ES2015 `super` is being used). Classes are still assigned
to variables to maintain compatibility for assigned class expressions.
There are a few changes to existing functionality that could break
backwards compatibility:
- Derived constructors that deliberately don't call `super` are no
longer possible. ES2015 derived classes can't use `this` unless the
parent constructor has been called, so it's now called implicitly when
not present.
- As a consequence of the above, derived constructors with @ parameters
or bound methods and explicit `super` calls are not allowed. The
implicit `super` must be used in these cases.
* Add tests to verify class interoperability with ES
* Refactor class nodes to separate executable body logic
Logic has been redistributed amongst the class nodes so that:
- `Class` contains the logic necessary to compile an ES class
declaration.
- `ExecutableClassBody` contains the logic necessary to compile CS'
class extensions that require an executable class body.
`Class` still necessarily contains logic to determine whether an
expression is valid in an ES class initializer or not. If any invalid
expressions are found then `Class` will wrap itself in an
`ExecutableClassBody` when compiling.
* Rename `Code#static` to `Code#isStatic`
This naming is more consistent with other `Code` flags.
* Output anonymous classes when possible
Anonymous classes can be output when:
- The class has no parent. The current super compilation needs a class
variable to reference. This condition will go away when ES2015 super
is in use.
- The class contains no bound static methods. Bound static methods have
their context set to the class name.
* Throw errors at compile time for async or generator constructors
* Improve handling of anonymous classes
Anonymous classes are now always anonymous. If a name is required (e.g.
for bound static methods or derived classes) then the class is compiled
in an `ExecutableClassBody` which will give the anonymous class a stable
reference.
* Add a `replaceInContext` method to `Node`
`replaceInContext` will traverse children looking for a node for which
`match` returns true. Once found, the matching node will be replaced by
the result of calling `replacement`.
* Separate `this` assignments from function parameters
This change has been made to simplify two future changes:
1. Outputting `@`-param assignments after a `super` call.
In this case it is necessary that non-`@` parameters are available
before `super` is called, so destructuring has to happen before
`this` assignment.
2. Compiling destructured assignment to ES6
In this case also destructuring has to happen before `this`,
as destructuring can happen in the arguments list, but `this`
assignment can not.
A bonus side-effect is that default values for `@` params are now output
as ES6 default parameters, e.g.
(@a = 1) ->
becomes
function a (a = 1) {
this.a = a;
}
* Change `super` handling in class constructors
Inside an ES derived constructor (a constructor for a class that extends
another class), it is impossible to access `this` until `super` has been
called. This conflicts with CoffeeScript's `@`-param and bound method
features, which compile to `this` references at the top of a function
body. For example:
class B extends A
constructor: (@param) -> super
method: =>
This would compile to something like:
class B extends A {
constructor (param) {
this.param = param;
this.method = bind(this.method, this);
super(...arguments);
}
}
This would break in an ES-compliant runtime as there are `this`
references before the call to `super`. Before this commit we were
dealing with this by injecting an implicit `super` call into derived
constructors that do not already have an explicit `super` call.
Furthermore, we would disallow explicit `super` calls in derived
constructors that used bound methods or `@`-params, meaning the above
example would need to be rewritten as:
class B extends A
constructor: (@param) ->
method: =>
This would result in a call to `super(...arguments)` being generated as
the first expression in `B#constructor`.
Whilst this approach seems to work pretty well, and is arguably more
convenient than having to manually call `super` when you don't
particularly care about the arguments, it does introduce some 'magic'
and separation from ES, and would likely be a pain point in a project
that made use of significant constructor overriding.
This commit introduces a mechanism through which `super` in constructors
is 'expanded' to include any generated `this` assignments, whilst
retaining the same semantics of a super call. The first example above
now compiles to something like:
class B extends A {
constructor (param) {
var ref
ref = super(...arguments), this.param = param, this.method = bind(this.method, this), ref;
}
}
* Improve `super` handling in constructors
Rather than functions expanding their `super` calls, the `SuperCall`
node can now be given a list of `thisAssignments` to apply when it is
compiled.
This allows us to use the normal compiler machinery to determine whether
the `super` result needs to be cached, whether it appears inline or not,
etc.
* Fix anonymous classes at the top level
Anonymous classes in ES are only valid within expressions. If an
anonymous class is at the top level it will now be wrapped in
parenthses to force it into an expression.
* Re-add Parens wrapper around executable class bodies
This was lost in the refactoring, but it necessary to ensure
`new class ...` works as expected when there's an executable body.
* Throw compiler errors for badly configured derived constructors
Rather than letting them become runtime errors, the following checks are
now performed when compiling a derived constructor:
- The constructor **must** include a call to `super`.
- The constructor **must not** reference `this` in the function body
before `super` has been called.
* Add some tests exercising new class behaviour
- async methods in classes
- `this` access after `super` in extended classes
- constructor super in arrow functions
- constructor functions can't be async
- constructor functions can't be generators
- derived constructors must call super
- derived constructors can't reference `this` before calling super
- generator methods in classes
- 'new' target
* Improve constructor `super` errors
Add a check for `super` in non-extended class constructors, and
explicitly mention derived constructors in the "can't reference this
before super" error.
* Fix compilation of multiple `super` paths in derived constructors
`super` can only be called once, but it can be called conditionally from
multiple locations. The chosen fix is to add the `this` assignments to
every super call.
* Additional class tests, added as a separate file to simplify testing and merging.
Some methods are commented out because they currently throw and I'm not sure how
to test for compilation errors like those.
There is also one test which I deliberately left without passing, `super` in an external prototype override.
This test should 'pass' but is really a variation on the failing `super only allowed in an instance method`
tests above it.
* Changes to the tests. Found bug in super in prototype method. fixed.
* Added failing test back in, dealing with bound functions in external prototype overrides.
* Located a bug in the compiler relating to assertions and escaped ES6 classes.
* Move tests from classes-additional.coffee into classes.coffee; comment out console.log
* Cleaned up tests and made changes based on feedback. Test at the end still has issues, but it's commented out for now.
* Make HoistTarget.expand recursive
It's possible that a hoisted node may itself contain hoisted nodes (e.g.
a class method inside a class method). For this to work the hoisted
fragments need to be expanded recursively.
* Uncomment final test in classes.coffee
The test case now compiles, however another issue is affecting the test
due to the error for `this` before `super` triggering based on source
order rather than execution order. These have been commented out for
now.
* Fixed last test TODOs in test/classes.coffee
Turns out an expression like `this.foo = super()` won't run in JS as it
attempts to lookup `this` before evaluating `super` (i.e. throws "this
is not defined").
* Added more tests for compatability checks, statics, prototypes and ES6 expectations. Cleaned test "nested classes with super".
* Changes to reflect feedback and to comment out issues that will be addressed seperately.
* Clean up test/classes.coffee
- Trim trailing whitespace.
- Rephrase a condition to be more idiomatic.
* Remove check for `super` in derived constructors
In order to be usable at runtime, an extended ES class must call `super`
OR return an alternative object. This check prevented the latter case,
and checking for an alternative return can't be completed statically
without control flow analysis.
* Disallow 'super' in constructor parameter defaults
There are many edge cases when combining 'super' in parameter defaults
with @-parameters and bound functions (and potentially property
initializers in the future).
Rather than attempting to resolve these edge cases, 'super' is now
explicitly disallowed in constructor parameter defaults.
* Disallow @-params in derived constructors without 'super'
@-parameters can't be assigned unless 'super' is called.
2017-01-13 00:55:30 -05:00
|
|
|
|
|
|
|
arrayEq [1], Array.from Child.static()
|
|
|
|
arrayEq [2], Array.from new Child().method()
|