mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00

* 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.
1693 lines
35 KiB
CoffeeScript
1693 lines
35 KiB
CoffeeScript
# Classes
|
|
# -------
|
|
|
|
# * Class Definition
|
|
# * Class Instantiation
|
|
# * Inheritance and Super
|
|
# * ES2015+ Class Interoperability
|
|
|
|
test "classes with a four-level inheritance chain", ->
|
|
|
|
class Base
|
|
func: (string) ->
|
|
"zero/#{string}"
|
|
|
|
@static: (string) ->
|
|
"static/#{string}"
|
|
|
|
class FirstChild extends Base
|
|
func: (string) ->
|
|
super('one/') + string
|
|
|
|
SecondChild = class extends FirstChild
|
|
func: (string) ->
|
|
super('two/') + string
|
|
|
|
thirdCtor = ->
|
|
@array = [1, 2, 3]
|
|
|
|
class ThirdChild extends SecondChild
|
|
constructor: ->
|
|
super()
|
|
thirdCtor.call this
|
|
|
|
# Gratuitous comment for testing.
|
|
func: (string) ->
|
|
super('three/') + string
|
|
|
|
result = (new ThirdChild).func 'four'
|
|
|
|
ok result is 'zero/one/two/three/four'
|
|
ok Base.static('word') is 'static/word'
|
|
|
|
FirstChild::func = (string) ->
|
|
super('one/').length + string
|
|
|
|
result = (new ThirdChild).func 'four'
|
|
|
|
ok result is '9two/three/four'
|
|
|
|
ok (new ThirdChild).array.join(' ') is '1 2 3'
|
|
|
|
|
|
test "constructors with inheritance and super", ->
|
|
|
|
identity = (f) -> f
|
|
|
|
class TopClass
|
|
constructor: (arg) ->
|
|
@prop = 'top-' + arg
|
|
|
|
class SuperClass extends TopClass
|
|
constructor: (arg) ->
|
|
identity super 'super-' + arg
|
|
|
|
class SubClass extends SuperClass
|
|
constructor: ->
|
|
identity super 'sub'
|
|
|
|
ok (new SubClass).prop is 'top-super-sub'
|
|
|
|
|
|
test "basic classes, again, but in the manual prototype style", ->
|
|
|
|
Base = ->
|
|
Base::func = (string) ->
|
|
'zero/' + string
|
|
Base::['func-func'] = (string) ->
|
|
"dynamic-#{string}"
|
|
|
|
FirstChild = ->
|
|
SecondChild = ->
|
|
ThirdChild = ->
|
|
@array = [1, 2, 3]
|
|
this
|
|
|
|
ThirdChild extends SecondChild extends FirstChild extends Base
|
|
|
|
FirstChild::func = (string) ->
|
|
super('one/') + string
|
|
|
|
SecondChild::func = (string) ->
|
|
super('two/') + string
|
|
|
|
ThirdChild::func = (string) ->
|
|
super('three/') + string
|
|
|
|
result = (new ThirdChild).func 'four'
|
|
|
|
ok result is 'zero/one/two/three/four'
|
|
|
|
ok (new ThirdChild)['func-func']('thing') is 'dynamic-thing'
|
|
|
|
|
|
test "super with plain ol' prototypes", ->
|
|
|
|
TopClass = ->
|
|
TopClass::func = (arg) ->
|
|
'top-' + arg
|
|
|
|
SuperClass = ->
|
|
SuperClass extends TopClass
|
|
SuperClass::func = (arg) ->
|
|
super 'super-' + arg
|
|
|
|
SubClass = ->
|
|
SubClass extends SuperClass
|
|
SubClass::func = ->
|
|
super 'sub'
|
|
|
|
eq (new SubClass).func(), 'top-super-sub'
|
|
|
|
|
|
test "'@' referring to the current instance, and not being coerced into a call", ->
|
|
|
|
class ClassName
|
|
amI: ->
|
|
@ instanceof ClassName
|
|
|
|
obj = new ClassName
|
|
ok obj.amI()
|
|
|
|
|
|
test "super() calls in constructors of classes that are defined as object properties", ->
|
|
|
|
class Hive
|
|
constructor: (name) -> @name = name
|
|
|
|
class Hive.Bee extends Hive
|
|
constructor: (name) -> super
|
|
|
|
maya = new Hive.Bee 'Maya'
|
|
ok maya.name is 'Maya'
|
|
|
|
|
|
test "classes with JS-keyword properties", ->
|
|
|
|
class Class
|
|
class: 'class'
|
|
name: -> @class
|
|
|
|
instance = new Class
|
|
ok instance.class is 'class'
|
|
ok instance.name() is 'class'
|
|
|
|
|
|
test "Classes with methods that are pre-bound to the instance, or statically, to the class", ->
|
|
|
|
class Dog
|
|
constructor: (name) ->
|
|
@name = name
|
|
|
|
bark: =>
|
|
"#{@name} woofs!"
|
|
|
|
@static = =>
|
|
new this('Dog')
|
|
|
|
spark = new Dog('Spark')
|
|
fido = new Dog('Fido')
|
|
fido.bark = spark.bark
|
|
|
|
ok fido.bark() is 'Spark woofs!'
|
|
|
|
obj = func: Dog.static
|
|
|
|
ok obj.func().name is 'Dog'
|
|
|
|
|
|
test "a bound function in a bound function", ->
|
|
|
|
class Mini
|
|
num: 10
|
|
generate: =>
|
|
for i in [1..3]
|
|
=>
|
|
@num
|
|
|
|
m = new Mini
|
|
eq (func() for func in m.generate()).join(' '), '10 10 10'
|
|
|
|
|
|
test "contructor called with varargs", ->
|
|
|
|
class Connection
|
|
constructor: (one, two, three) ->
|
|
[@one, @two, @three] = [one, two, three]
|
|
|
|
out: ->
|
|
"#{@one}-#{@two}-#{@three}"
|
|
|
|
list = [3, 2, 1]
|
|
conn = new Connection list...
|
|
ok conn instanceof Connection
|
|
ok conn.out() is '3-2-1'
|
|
|
|
|
|
test "calling super and passing along all arguments", ->
|
|
|
|
class Parent
|
|
method: (args...) -> @args = args
|
|
|
|
class Child extends Parent
|
|
method: -> super
|
|
|
|
c = new Child
|
|
c.method 1, 2, 3, 4
|
|
ok c.args.join(' ') is '1 2 3 4'
|
|
|
|
|
|
test "classes wrapped in decorators", ->
|
|
|
|
func = (klass) ->
|
|
klass::prop = 'value'
|
|
klass
|
|
|
|
func class Test
|
|
prop2: 'value2'
|
|
|
|
ok (new Test).prop is 'value'
|
|
ok (new Test).prop2 is 'value2'
|
|
|
|
|
|
test "anonymous classes", ->
|
|
|
|
obj =
|
|
klass: class
|
|
method: -> 'value'
|
|
|
|
instance = new obj.klass
|
|
ok instance.method() is 'value'
|
|
|
|
|
|
test "Implicit objects as static properties", ->
|
|
|
|
class Static
|
|
@static =
|
|
one: 1
|
|
two: 2
|
|
|
|
ok Static.static.one is 1
|
|
ok Static.static.two is 2
|
|
|
|
|
|
test "nothing classes", ->
|
|
|
|
c = class
|
|
ok c instanceof Function
|
|
|
|
|
|
test "classes with static-level implicit objects", ->
|
|
|
|
class A
|
|
@static = one: 1
|
|
two: 2
|
|
|
|
class B
|
|
@static = one: 1,
|
|
two: 2
|
|
|
|
eq A.static.one, 1
|
|
eq A.static.two, undefined
|
|
eq (new A).two, 2
|
|
|
|
eq B.static.one, 1
|
|
eq B.static.two, 2
|
|
eq (new B).two, undefined
|
|
|
|
|
|
test "classes with value'd constructors", ->
|
|
|
|
counter = 0
|
|
classMaker = ->
|
|
inner = ++counter
|
|
->
|
|
@value = inner
|
|
|
|
class One
|
|
constructor: classMaker()
|
|
|
|
class Two
|
|
constructor: classMaker()
|
|
|
|
eq (new One).value, 1
|
|
eq (new Two).value, 2
|
|
eq (new One).value, 1
|
|
eq (new Two).value, 2
|
|
|
|
|
|
test "executable class bodies", ->
|
|
|
|
class A
|
|
if true
|
|
b: 'b'
|
|
else
|
|
c: 'c'
|
|
|
|
a = new A
|
|
|
|
eq a.b, 'b'
|
|
eq a.c, undefined
|
|
|
|
|
|
test "#2502: parenthesizing inner object values", ->
|
|
|
|
class A
|
|
category: (type: 'string')
|
|
sections: (type: 'number', default: 0)
|
|
|
|
eq (new A).category.type, 'string'
|
|
|
|
eq (new A).sections.default, 0
|
|
|
|
|
|
test "conditional prototype property assignment", ->
|
|
debug = false
|
|
|
|
class Person
|
|
if debug
|
|
age: -> 10
|
|
else
|
|
age: -> 20
|
|
|
|
eq (new Person).age(), 20
|
|
|
|
|
|
test "mild metaprogramming", ->
|
|
|
|
class Base
|
|
@attr: (name) ->
|
|
@::[name] = (val) ->
|
|
if arguments.length > 0
|
|
@["_#{name}"] = val
|
|
else
|
|
@["_#{name}"]
|
|
|
|
class Robot extends Base
|
|
@attr 'power'
|
|
@attr 'speed'
|
|
|
|
robby = new Robot
|
|
|
|
ok robby.power() is undefined
|
|
|
|
robby.power 11
|
|
robby.speed Infinity
|
|
|
|
eq robby.power(), 11
|
|
eq robby.speed(), Infinity
|
|
|
|
|
|
test "namespaced classes do not reserve their function name in outside scope", ->
|
|
|
|
one = {}
|
|
two = {}
|
|
|
|
class one.Klass
|
|
@label = "one"
|
|
|
|
class two.Klass
|
|
@label = "two"
|
|
|
|
eq typeof Klass, 'undefined'
|
|
eq one.Klass.label, 'one'
|
|
eq two.Klass.label, 'two'
|
|
|
|
|
|
test "nested classes", ->
|
|
|
|
class Outer
|
|
constructor: ->
|
|
@label = 'outer'
|
|
|
|
class @Inner
|
|
constructor: ->
|
|
@label = 'inner'
|
|
|
|
eq (new Outer).label, 'outer'
|
|
eq (new Outer.Inner).label, 'inner'
|
|
|
|
|
|
test "variables in constructor bodies are correctly scoped", ->
|
|
|
|
class A
|
|
x = 1
|
|
constructor: ->
|
|
x = 10
|
|
y = 20
|
|
y = 2
|
|
captured: ->
|
|
{x, y}
|
|
|
|
a = new A
|
|
eq a.captured().x, 10
|
|
eq a.captured().y, 2
|
|
|
|
|
|
test "Issue #924: Static methods in nested classes", ->
|
|
|
|
class A
|
|
@B: class
|
|
@c = -> 5
|
|
|
|
eq A.B.c(), 5
|
|
|
|
|
|
test "`class extends this`", ->
|
|
|
|
class A
|
|
func: -> 'A'
|
|
|
|
B = null
|
|
makeClass = ->
|
|
B = class extends this
|
|
func: -> super + ' B'
|
|
|
|
makeClass.call A
|
|
|
|
eq (new B()).func(), 'A B'
|
|
|
|
|
|
test "ensure that constructors invoked with splats return a new object", ->
|
|
|
|
args = [1, 2, 3]
|
|
Type = (@args) ->
|
|
type = new Type args
|
|
|
|
ok type and type instanceof Type
|
|
ok type.args and type.args instanceof Array
|
|
ok v is args[i] for v, i in type.args
|
|
|
|
Type1 = (@a, @b, @c) ->
|
|
type1 = new Type1 args...
|
|
|
|
ok type1 instanceof Type1
|
|
eq type1.constructor, Type1
|
|
ok type1.a is args[0] and type1.b is args[1] and type1.c is args[2]
|
|
|
|
# Ensure that constructors invoked with splats cache the function.
|
|
called = 0
|
|
get = -> if called++ then false else class Type
|
|
new (get()) args...
|
|
|
|
test "`new` shouldn't add extra parens", ->
|
|
|
|
ok new Date().constructor is Date
|
|
|
|
|
|
test "`new` works against bare function", ->
|
|
|
|
eq Date, new ->
|
|
Date
|
|
|
|
|
|
test "#1182: a subclass should be able to set its constructor to an external function", ->
|
|
ctor = ->
|
|
@val = 1
|
|
return
|
|
class A
|
|
class B extends A
|
|
constructor: ctor
|
|
eq (new B).val, 1
|
|
|
|
test "#1182: external constructors continued", ->
|
|
ctor = ->
|
|
class A
|
|
class B extends A
|
|
method: ->
|
|
constructor: ctor
|
|
ok B::method
|
|
|
|
test "#1313: misplaced __extends", ->
|
|
nonce = {}
|
|
class A
|
|
class B extends A
|
|
prop: nonce
|
|
constructor: -> super
|
|
eq nonce, B::prop
|
|
|
|
test "#1182: execution order needs to be considered as well", ->
|
|
counter = 0
|
|
makeFn = (n) -> eq n, ++counter; ->
|
|
class B extends (makeFn 1)
|
|
@B: makeFn 2
|
|
constructor: makeFn 3
|
|
|
|
test "#1182: external constructors with bound functions", ->
|
|
fn = ->
|
|
{one: 1}
|
|
this
|
|
class B
|
|
class A
|
|
constructor: fn
|
|
method: => this instanceof A
|
|
ok (new A).method.call(new B)
|
|
|
|
test "#1372: bound class methods with reserved names", ->
|
|
class C
|
|
delete: =>
|
|
ok C::delete
|
|
|
|
test "#1380: `super` with reserved names", ->
|
|
class C
|
|
do: -> super
|
|
ok C::do
|
|
|
|
class B
|
|
0: -> super
|
|
ok B::[0]
|
|
|
|
test "#1464: bound class methods should keep context", ->
|
|
nonce = {}
|
|
nonce2 = {}
|
|
class C
|
|
constructor: (@id) ->
|
|
@boundStaticColon: => new this(nonce)
|
|
@boundStaticEqual= => new this(nonce2)
|
|
eq nonce, C.boundStaticColon().id
|
|
eq nonce2, C.boundStaticEqual().id
|
|
|
|
test "#1009: classes with reserved words as determined names", -> (->
|
|
eq 'function', typeof (class @for)
|
|
ok not /\beval\b/.test (class @eval).toString()
|
|
ok not /\barguments\b/.test (class @arguments).toString()
|
|
).call {}
|
|
|
|
test "#1482: classes can extend expressions", ->
|
|
id = (x) -> x
|
|
nonce = {}
|
|
class A then nonce: nonce
|
|
class B extends id A
|
|
eq nonce, (new B).nonce
|
|
|
|
test "#1598: super works for static methods too", ->
|
|
|
|
class Parent
|
|
method: ->
|
|
'NO'
|
|
@method: ->
|
|
'yes'
|
|
|
|
class Child extends Parent
|
|
@method: ->
|
|
'pass? ' + super
|
|
|
|
eq Child.method(), 'pass? yes'
|
|
|
|
test "#1842: Regression with bound functions within bound class methods", ->
|
|
|
|
class Store
|
|
@bound: =>
|
|
do =>
|
|
eq this, Store
|
|
|
|
Store.bound()
|
|
|
|
# And a fancier case:
|
|
|
|
class Store
|
|
|
|
eq this, Store
|
|
|
|
@bound: =>
|
|
do =>
|
|
eq this, Store
|
|
|
|
@unbound: ->
|
|
eq this, Store
|
|
|
|
instance: =>
|
|
ok this instanceof Store
|
|
|
|
Store.bound()
|
|
Store.unbound()
|
|
(new Store).instance()
|
|
|
|
test "#1876: Class @A extends A", ->
|
|
class A
|
|
class @A extends A
|
|
|
|
ok (new @A) instanceof A
|
|
|
|
test "#1813: Passing class definitions as expressions", ->
|
|
ident = (x) -> x
|
|
|
|
result = ident class A then x = 1
|
|
|
|
eq result, A
|
|
|
|
result = ident class B extends A
|
|
x = 1
|
|
|
|
eq result, B
|
|
|
|
test "#1966: external constructors should produce their return value", ->
|
|
ctor = -> {}
|
|
class A then constructor: ctor
|
|
ok (new A) not instanceof A
|
|
|
|
test "#1980: regression with an inherited class with static function members", ->
|
|
|
|
class A
|
|
|
|
class B extends A
|
|
@static: => 'value'
|
|
|
|
eq B.static(), 'value'
|
|
|
|
test "#1534: class then 'use strict'", ->
|
|
# [14.1 Directive Prologues and the Use Strict Directive](http://es5.github.com/#x14.1)
|
|
nonce = {}
|
|
error = 'do -> ok this'
|
|
strictTest = "do ->'use strict';#{error}"
|
|
return unless (try CoffeeScript.run strictTest, bare: yes catch e then nonce) is nonce
|
|
|
|
throws -> CoffeeScript.run "class then 'use strict';#{error}", bare: yes
|
|
doesNotThrow -> CoffeeScript.run "class then #{error}", bare: yes
|
|
doesNotThrow -> CoffeeScript.run "class then #{error};'use strict'", bare: yes
|
|
|
|
# comments are ignored in the Directive Prologue
|
|
comments = ["""
|
|
class
|
|
### comment ###
|
|
'use strict'
|
|
#{error}""",
|
|
"""
|
|
class
|
|
### comment 1 ###
|
|
### comment 2 ###
|
|
'use strict'
|
|
#{error}""",
|
|
"""
|
|
class
|
|
### comment 1 ###
|
|
### comment 2 ###
|
|
'use strict'
|
|
#{error}
|
|
### comment 3 ###"""
|
|
]
|
|
throws (-> CoffeeScript.run comment, bare: yes) for comment in comments
|
|
|
|
# [ES5 §14.1](http://es5.github.com/#x14.1) allows for other directives
|
|
directives = ["""
|
|
class
|
|
'directive 1'
|
|
'use strict'
|
|
#{error}""",
|
|
"""
|
|
class
|
|
'use strict'
|
|
'directive 2'
|
|
#{error}""",
|
|
"""
|
|
class
|
|
### comment 1 ###
|
|
'directive 1'
|
|
'use strict'
|
|
#{error}""",
|
|
"""
|
|
class
|
|
### comment 1 ###
|
|
'directive 1'
|
|
### comment 2 ###
|
|
'use strict'
|
|
#{error}"""
|
|
]
|
|
throws (-> CoffeeScript.run directive, bare: yes) for directive in directives
|
|
|
|
test "#2052: classes should work in strict mode", ->
|
|
try
|
|
do ->
|
|
'use strict'
|
|
class A
|
|
catch e
|
|
ok no
|
|
|
|
test "directives in class with extends ", ->
|
|
strictTest = """
|
|
class extends Object
|
|
### comment ###
|
|
'use strict'
|
|
do -> eq this, undefined
|
|
"""
|
|
CoffeeScript.run strictTest, bare: yes
|
|
|
|
test "#2630: class bodies can't reference arguments", ->
|
|
throws ->
|
|
CoffeeScript.compile('class Test then arguments')
|
|
|
|
# #4320: Don't be too eager when checking, though.
|
|
class Test
|
|
arguments: 5
|
|
eq 5, Test::arguments
|
|
|
|
test "#2319: fn class n extends o.p [INDENT] x = 123", ->
|
|
first = ->
|
|
|
|
base = onebase: ->
|
|
|
|
first class OneKeeper extends base.onebase
|
|
one = 1
|
|
one: -> one
|
|
|
|
eq new OneKeeper().one(), 1
|
|
|
|
|
|
test "#2599: other typed constructors should be inherited", ->
|
|
class Base
|
|
constructor: -> return {}
|
|
|
|
class Derived extends Base
|
|
|
|
ok (new Derived) not instanceof Derived
|
|
ok (new Derived) not instanceof Base
|
|
ok (new Base) not instanceof Base
|
|
|
|
test "extending native objects works with and without defining a constructor", ->
|
|
class MyArray extends Array
|
|
method: -> 'yes!'
|
|
|
|
myArray = new MyArray
|
|
ok myArray instanceof MyArray
|
|
ok 'yes!', myArray.method()
|
|
|
|
class OverrideArray extends Array
|
|
constructor: -> super
|
|
method: -> 'yes!'
|
|
|
|
overrideArray = new OverrideArray
|
|
ok overrideArray instanceof OverrideArray
|
|
eq 'yes!', overrideArray.method()
|
|
|
|
|
|
test "#2782: non-alphanumeric-named bound functions", ->
|
|
class A
|
|
'b:c': =>
|
|
'd'
|
|
|
|
eq (new A)['b:c'](), 'd'
|
|
|
|
|
|
test "#2781: overriding bound functions", ->
|
|
class A
|
|
a: ->
|
|
@b()
|
|
b: =>
|
|
1
|
|
|
|
class B extends A
|
|
b: =>
|
|
2
|
|
|
|
b = (new A).b
|
|
eq b(), 1
|
|
|
|
b = (new B).b
|
|
eq b(), 2
|
|
|
|
|
|
test "#2791: bound function with destructured argument", ->
|
|
class Foo
|
|
method: ({a}) => 'Bar'
|
|
|
|
eq (new Foo).method({a: 'Bar'}), 'Bar'
|
|
|
|
|
|
test "#2796: ditto, ditto, ditto", ->
|
|
answer = null
|
|
|
|
outsideMethod = (func) ->
|
|
func.call message: 'wrong!'
|
|
|
|
class Base
|
|
constructor: ->
|
|
@message = 'right!'
|
|
outsideMethod @echo
|
|
|
|
echo: =>
|
|
answer = @message
|
|
|
|
new Base
|
|
eq answer, 'right!'
|
|
|
|
test "#3063: Class bodies cannot contain pure statements", ->
|
|
throws -> CoffeeScript.compile """
|
|
class extends S
|
|
return if S.f
|
|
@f: => this
|
|
"""
|
|
|
|
test "#2949: super in static method with reserved name", ->
|
|
class Foo
|
|
@static: -> 'baz'
|
|
|
|
class Bar extends Foo
|
|
@static: -> super
|
|
|
|
eq Bar.static(), 'baz'
|
|
|
|
test "#3232: super in static methods (not object-assigned)", ->
|
|
class Foo
|
|
@baz = -> true
|
|
@qux = -> true
|
|
|
|
class Bar extends Foo
|
|
@baz = -> super
|
|
Bar.qux = -> super
|
|
|
|
ok Bar.baz()
|
|
ok Bar.qux()
|
|
|
|
test "#1392 calling `super` in methods defined on namespaced classes", ->
|
|
class Base
|
|
m: -> 5
|
|
n: -> 4
|
|
namespace =
|
|
A: ->
|
|
B: ->
|
|
namespace.A extends Base
|
|
|
|
namespace.A::m = -> super
|
|
eq 5, (new namespace.A).m()
|
|
namespace.B::m = namespace.A::m
|
|
namespace.A::m = null
|
|
eq 5, (new namespace.B).m()
|
|
|
|
count = 0
|
|
getNamespace = -> count++; namespace
|
|
getNamespace().A::n = -> super
|
|
eq 4, (new namespace.A).n()
|
|
eq 1, count
|
|
|
|
class C
|
|
@a: (->)
|
|
@a extends Base
|
|
@a::m = -> super
|
|
eq 5, (new C.a).m()
|
|
|
|
test "dynamic method names and super", ->
|
|
class Base
|
|
@m: -> 6
|
|
m: -> 5
|
|
m2: -> 4.5
|
|
n: -> 4
|
|
A = ->
|
|
A extends Base
|
|
|
|
m = 'm'
|
|
A::[m] = -> super
|
|
m = 'n'
|
|
eq 5, (new A).m()
|
|
|
|
name = -> count++; 'n'
|
|
|
|
count = 0
|
|
A::[name()] = -> super
|
|
eq 4, (new A).n()
|
|
eq 1, count
|
|
|
|
m = 'm'
|
|
m2 = 'm2'
|
|
count = 0
|
|
class B extends Base
|
|
@[name()] = -> super
|
|
@::[m] = -> super
|
|
"#{m2}": -> super
|
|
b = new B
|
|
m = m2 = 'n'
|
|
eq 6, B.m()
|
|
eq 5, b.m()
|
|
eq 4.5, b.m2()
|
|
eq 1, count
|
|
|
|
class C extends B
|
|
m: -> super
|
|
eq 5, (new C).m()
|
|
|
|
# ES2015+ class interoperability
|
|
# Based on https://github.com/balupton/es6-javascript-class-interop
|
|
# Helper functions to generate true ES classes to extend:
|
|
getBasicClass = ->
|
|
```
|
|
class BasicClass {
|
|
constructor (greeting) {
|
|
this.greeting = greeting || 'hi'
|
|
}
|
|
}
|
|
```
|
|
BasicClass
|
|
|
|
getExtendedClass = (BaseClass) ->
|
|
```
|
|
class ExtendedClass extends BaseClass {
|
|
constructor (greeting, name) {
|
|
super(greeting || 'hello')
|
|
this.name = name
|
|
}
|
|
}
|
|
```
|
|
ExtendedClass
|
|
|
|
test "can instantiate a basic ES class", ->
|
|
BasicClass = getBasicClass()
|
|
i = new BasicClass 'howdy!'
|
|
eq i.greeting, 'howdy!'
|
|
|
|
test "can instantiate an extended ES class", ->
|
|
BasicClass = getBasicClass()
|
|
ExtendedClass = getExtendedClass BasicClass
|
|
i = new ExtendedClass 'yo', 'buddy'
|
|
eq i.greeting, 'yo'
|
|
eq i.name, 'buddy'
|
|
|
|
test "can extend a basic ES class", ->
|
|
BasicClass = getBasicClass()
|
|
class ExtendedClass extends BasicClass
|
|
constructor: (@name) ->
|
|
super()
|
|
i = new ExtendedClass 'dude'
|
|
eq i.name, 'dude'
|
|
|
|
test "can extend an extended ES class", ->
|
|
BasicClass = getBasicClass()
|
|
ExtendedClass = getExtendedClass BasicClass
|
|
|
|
class ExtendedExtendedClass extends ExtendedClass
|
|
constructor: (@value) ->
|
|
super()
|
|
getDoubledValue: ->
|
|
@value * 2
|
|
|
|
i = new ExtendedExtendedClass 7
|
|
eq i.getDoubledValue(), 14
|
|
|
|
test "CoffeeScript class can be extended in ES", ->
|
|
class CoffeeClass
|
|
constructor: (@favoriteDrink = 'latte', @size = 'grande') ->
|
|
getDrinkOrder: ->
|
|
"#{@size} #{@favoriteDrink}"
|
|
|
|
```
|
|
class ECMAScriptClass extends CoffeeClass {
|
|
constructor (favoriteDrink) {
|
|
super(favoriteDrink);
|
|
this.favoriteDrink = this.favoriteDrink + ' with a dash of semicolons';
|
|
}
|
|
}
|
|
```
|
|
|
|
e = new ECMAScriptClass 'coffee'
|
|
eq e.getDrinkOrder(), 'grande coffee with a dash of semicolons'
|
|
|
|
test "extended CoffeeScript class can be extended in ES", ->
|
|
class CoffeeClass
|
|
constructor: (@favoriteDrink = 'latte') ->
|
|
|
|
class CoffeeClassWithDrinkOrder extends CoffeeClass
|
|
constructor: (@favoriteDrink, @size = 'grande') ->
|
|
super()
|
|
getDrinkOrder: ->
|
|
"#{@size} #{@favoriteDrink}"
|
|
|
|
```
|
|
class ECMAScriptClass extends CoffeeClassWithDrinkOrder {
|
|
constructor (favoriteDrink) {
|
|
super(favoriteDrink);
|
|
this.favoriteDrink = this.favoriteDrink + ' with a dash of semicolons';
|
|
}
|
|
}
|
|
```
|
|
|
|
e = new ECMAScriptClass 'coffee'
|
|
eq e.getDrinkOrder(), 'grande coffee with a dash of semicolons'
|
|
|
|
test "`this` access after `super` in extended classes", ->
|
|
class Base
|
|
|
|
class Test extends Base
|
|
constructor: (param, @param) ->
|
|
eq param, nonce
|
|
|
|
result = { super: super(), @param, @method }
|
|
eq result.super, this
|
|
eq result.param, @param
|
|
eq result.method, @method
|
|
ok result.method isnt Test::method
|
|
|
|
method: =>
|
|
|
|
nonce = {}
|
|
new Test nonce, {}
|
|
|
|
test "`@`-params and bound methods with multiple `super` paths (blocks)", ->
|
|
nonce = {}
|
|
|
|
class Base
|
|
constructor: (@name) ->
|
|
|
|
class Test extends Base
|
|
constructor: (param, @param) ->
|
|
if param
|
|
super 'param'
|
|
eq @name, 'param'
|
|
else
|
|
super 'not param'
|
|
eq @name, 'not param'
|
|
eq @param, nonce
|
|
ok @method isnt Test::method
|
|
method: =>
|
|
new Test true, nonce
|
|
new Test false, nonce
|
|
|
|
|
|
test "`@`-params and bound methods with multiple `super` paths (expressions)", ->
|
|
nonce = {}
|
|
|
|
class Base
|
|
constructor: (@name) ->
|
|
|
|
class Test extends Base
|
|
constructor: (param, @param) ->
|
|
# Contrived example: force each path into an expression with inline assertions
|
|
if param
|
|
result = (
|
|
eq (super 'param'), @;
|
|
eq @name, 'param';
|
|
eq @param, nonce;
|
|
ok @method isnt Test::method
|
|
)
|
|
else
|
|
result = (
|
|
eq (super 'not param'), @;
|
|
eq @name, 'not param';
|
|
eq @param, nonce;
|
|
ok @method isnt Test::method
|
|
)
|
|
method: =>
|
|
new Test true, nonce
|
|
new Test false, nonce
|
|
|
|
test "constructor super in arrow functions", ->
|
|
class Test extends (class)
|
|
constructor: (@param) ->
|
|
do => super
|
|
eq @param, nonce
|
|
|
|
new Test nonce = {}
|
|
|
|
# Ensure that we always throw if we experience more than one super()
|
|
# call in a constructor. This ends up being a runtime error.
|
|
# Should be caught at compile time.
|
|
test "multiple super calls", ->
|
|
throwsA = """
|
|
class A
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
|
|
class MultiSuper extends A
|
|
constructor: (drink) ->
|
|
super(drink)
|
|
super(drink)
|
|
@newDrink = drink
|
|
new MultiSuper('Late').make()
|
|
"""
|
|
throws -> CoffeeScript.run throwsA, bare: yes
|
|
|
|
# Basic test to ensure we can pass @params in a constuctor and
|
|
# inheritance works correctly
|
|
test "@ params", ->
|
|
class A
|
|
constructor: (@drink, @shots, @flavor) ->
|
|
make: -> "Making a #{@flavor} #{@drink} with #{@shots} shot(s)"
|
|
|
|
a = new A('Machiato', 2, 'chocolate')
|
|
eq a.make(), "Making a chocolate Machiato with 2 shot(s)"
|
|
|
|
class B extends A
|
|
b = new B('Machiato', 2, 'chocolate')
|
|
eq b.make(), "Making a chocolate Machiato with 2 shot(s)"
|
|
|
|
# Ensure we can accept @params with default parameters in a constructor
|
|
test "@ params with defaults in a constructor", ->
|
|
class A
|
|
# Multiple @ params with defaults
|
|
constructor: (@drink = 'Americano', @shots = '1', @flavor = 'caramel') ->
|
|
make: -> "Making a #{@flavor} #{@drink} with #{@shots} shot(s)"
|
|
|
|
a = new A()
|
|
eq a.make(), "Making a caramel Americano with 1 shot(s)"
|
|
|
|
# Ensure we can handle default constructors with class params
|
|
test "@ params with class params", ->
|
|
class Beverage
|
|
drink: 'Americano'
|
|
shots: '1'
|
|
flavor: 'caramel'
|
|
|
|
class A
|
|
# Class creation as a default param with `this`
|
|
constructor: (@drink = new Beverage()) ->
|
|
a = new A()
|
|
eq a.drink.drink, 'Americano'
|
|
|
|
beverage = new Beverage
|
|
class B
|
|
# class costruction with a default external param
|
|
constructor: (@drink = beverage) ->
|
|
|
|
b = new B()
|
|
eq b.drink.drink, 'Americano'
|
|
|
|
class C
|
|
# Default constructor with anonymous empty class
|
|
constructor: (@meta = class) ->
|
|
c = new C()
|
|
ok c.meta instanceof Function
|
|
|
|
test "@ params without super, including errors", ->
|
|
classA = """
|
|
class A
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
a = new A('Machiato')
|
|
"""
|
|
|
|
throwsB = """
|
|
class B extends A
|
|
#implied super
|
|
constructor: (@drink) ->
|
|
b = new B('Machiato')
|
|
"""
|
|
throws -> CoffeeScript.compile classA + throwsB, bare: yes
|
|
|
|
test "@ params super race condition", ->
|
|
classA = """
|
|
class A
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
"""
|
|
|
|
throwsB = """
|
|
class B extends A
|
|
constructor: (@params) ->
|
|
|
|
b = new B('Machiato')
|
|
"""
|
|
throws -> CoffeeScript.compile classA + throwsB, bare: yes
|
|
|
|
# Race condition with @ and super
|
|
throwsC = """
|
|
class C extends A
|
|
constructor: (@params) ->
|
|
super(@params)
|
|
|
|
c = new C('Machiato')
|
|
"""
|
|
throws -> CoffeeScript.compile classA + throwsC, bare: yes
|
|
|
|
|
|
test "@ with super call", ->
|
|
class D
|
|
make: -> "Making a #{@drink}"
|
|
|
|
class E extends D
|
|
constructor: (@drink) ->
|
|
super()
|
|
|
|
e = new E('Machiato')
|
|
eq e.make(), "Making a Machiato"
|
|
|
|
test "@ with splats and super call", ->
|
|
class A
|
|
make: -> "Making a #{@drink}"
|
|
|
|
class B extends A
|
|
constructor: (@drink...) ->
|
|
super()
|
|
|
|
B = new B('Machiato')
|
|
eq B.make(), "Making a Machiato"
|
|
|
|
|
|
test "super and external constructors", ->
|
|
# external constructor with @ param is allowed
|
|
ctorA = (@drink) ->
|
|
class A
|
|
constructor: ctorA
|
|
make: -> "Making a #{@drink}"
|
|
a = new A('Machiato')
|
|
eq a.make(), "Making a Machiato"
|
|
|
|
# External constructor with super
|
|
throwsC = """
|
|
class B
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
|
|
ctorC = (drink) ->
|
|
super(drink)
|
|
|
|
class C extends B
|
|
constructor: ctorC
|
|
c = new C('Machiato')
|
|
"""
|
|
throws -> CoffeeScript.compile throwsC, bare: yes
|
|
|
|
|
|
test "super in external prototype", ->
|
|
class A
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
|
|
class B extends A
|
|
B::make = (@flavor) -> super() + " with #{@flavor}"
|
|
b = new B('Machiato')
|
|
eq b.make('caramel'), "Making a Machiato with caramel"
|
|
|
|
# Fails, bound
|
|
# TODO: Could this throw a compile error?
|
|
class C extends A
|
|
C::make = (@flavor) => super() + " with #{@flavor}"
|
|
c = new C('Machiato')
|
|
ok c.make('caramel') isnt "Making a Machiato with caramel"
|
|
|
|
|
|
test "bound functions without super", ->
|
|
# Bound function with @
|
|
# Throw on compile, since bound
|
|
# constructors are illegal
|
|
throwsA = """
|
|
class A
|
|
constructor: (drink) =>
|
|
@drink = drink
|
|
|
|
"""
|
|
throws -> CoffeeScript.compile throwsA, bare: yes
|
|
|
|
test "super in a bound function in a constructor", ->
|
|
throwsB = """
|
|
class A
|
|
class B extends A
|
|
constructor: do => super
|
|
"""
|
|
throws -> CoffeeScript.compile throwsB, bare: yes
|
|
|
|
test "super in a bound function", ->
|
|
class A
|
|
constructor: (@drink) ->
|
|
make: -> "Making a #{@drink}"
|
|
|
|
class B extends A
|
|
make: (@flavor) =>
|
|
super + " with #{@flavor}"
|
|
|
|
b = new B('Machiato')
|
|
eq b.make('vanilla'), "Making a Machiato with vanilla"
|
|
|
|
# super in a bound function in a bound function
|
|
class C extends A
|
|
make: (@flavor) =>
|
|
func = () =>
|
|
super + " with #{@flavor}"
|
|
func()
|
|
|
|
c = new C('Machiato')
|
|
eq c.make('vanilla'), "Making a Machiato with vanilla"
|
|
|
|
# bound function in a constructor
|
|
class D extends A
|
|
constructor: (drink) ->
|
|
super(drink)
|
|
x = =>
|
|
eq @drink, "Machiato"
|
|
x()
|
|
d = new D('Machiato')
|
|
eq d.make(), "Making a Machiato"
|
|
|
|
# duplicate
|
|
test "super in a try/catch", ->
|
|
classA = """
|
|
class A
|
|
constructor: (param) ->
|
|
throw "" unless param
|
|
"""
|
|
|
|
throwsB = """
|
|
class B extends A
|
|
constructor: ->
|
|
try
|
|
super
|
|
"""
|
|
|
|
throwsC = """
|
|
ctor = ->
|
|
try
|
|
super
|
|
|
|
class C extends A
|
|
constructor: ctor
|
|
"""
|
|
throws -> CoffeeScript.run classA + throwsB, bare: yes
|
|
throws -> CoffeeScript.run classA + throwsC, bare: yes
|
|
|
|
test "mixed ES6 and CS6 classes with a four-level inheritance chain", ->
|
|
# Extended test
|
|
# ES2015+ class interoperability
|
|
|
|
```
|
|
class Base {
|
|
constructor (greeting) {
|
|
this.greeting = greeting || 'hi';
|
|
}
|
|
func (string) {
|
|
return 'zero/' + string;
|
|
}
|
|
static staticFunc (string) {
|
|
return 'static/' + string;
|
|
}
|
|
}
|
|
```
|
|
|
|
class FirstChild extends Base
|
|
func: (string) ->
|
|
super('one/') + string
|
|
|
|
|
|
```
|
|
class SecondChild extends FirstChild {
|
|
func (string) {
|
|
return super.func('two/' + string);
|
|
}
|
|
}
|
|
```
|
|
|
|
thirdCtor = ->
|
|
@array = [1, 2, 3]
|
|
|
|
class ThirdChild extends SecondChild
|
|
constructor: ->
|
|
super()
|
|
thirdCtor.call this
|
|
func: (string) ->
|
|
super('three/') + string
|
|
|
|
result = (new ThirdChild).func 'four'
|
|
ok result is 'zero/one/two/three/four'
|
|
ok Base.staticFunc('word') is 'static/word'
|
|
|
|
# exercise extends in a nested class
|
|
test "nested classes with super", ->
|
|
class Outer
|
|
constructor: ->
|
|
@label = 'outer'
|
|
|
|
class @Inner
|
|
constructor: ->
|
|
@label = 'inner'
|
|
|
|
class @ExtendedInner extends @Inner
|
|
constructor: ->
|
|
tmp = super()
|
|
@label = tmp.label + ' extended'
|
|
|
|
@extender: () =>
|
|
class ExtendedSelf extends @
|
|
constructor: ->
|
|
tmp = super()
|
|
@label = tmp.label + ' from this'
|
|
new ExtendedSelf
|
|
|
|
eq (new Outer).label, 'outer'
|
|
eq (new Outer.Inner).label, 'inner'
|
|
eq (new Outer.ExtendedInner).label, 'inner extended'
|
|
eq (Outer.extender()).label, 'outer from this'
|
|
|
|
test "Static methods generate 'static' keywords", ->
|
|
compile = """
|
|
class CheckStatic
|
|
constructor: (@drink) ->
|
|
@className: -> 'CheckStatic'
|
|
|
|
c = new CheckStatic('Machiato')
|
|
"""
|
|
result = CoffeeScript.compile compile, bare: yes
|
|
ok result.match(' static ')
|
|
|
|
test "Static methods in nested classes", ->
|
|
class Outer
|
|
@name: -> 'Outer'
|
|
|
|
class @Inner
|
|
@name: -> 'Inner'
|
|
|
|
eq Outer.name(), 'Outer'
|
|
eq Outer.Inner.name(), 'Inner'
|
|
|
|
|
|
test "mixed constructors with inheritance and ES6 super", ->
|
|
identity = (f) -> f
|
|
|
|
class TopClass
|
|
constructor: (arg) ->
|
|
@prop = 'top-' + arg
|
|
|
|
```
|
|
class SuperClass extends TopClass {
|
|
constructor (arg) {
|
|
identity(super('super-' + arg));
|
|
}
|
|
}
|
|
```
|
|
class SubClass extends SuperClass
|
|
constructor: ->
|
|
identity super 'sub'
|
|
|
|
ok (new SubClass).prop is 'top-super-sub'
|
|
|
|
test "ES6 static class methods can be overriden", ->
|
|
class A
|
|
@name: -> 'A'
|
|
|
|
class B extends A
|
|
@name: -> 'B'
|
|
|
|
eq A.name(), 'A'
|
|
eq B.name(), 'B'
|
|
|
|
# If creating static by direct assignment rather than ES6 static keyword
|
|
test "ES6 Static methods should set `this` to undefined // ES6 ", ->
|
|
class A
|
|
@test: ->
|
|
eq this, undefined
|
|
|
|
# Ensure that our object prototypes work with ES6
|
|
test "ES6 prototypes can be overriden", ->
|
|
class A
|
|
className: 'classA'
|
|
|
|
```
|
|
class B {
|
|
test () {return "B";};
|
|
}
|
|
```
|
|
b = new B
|
|
a = new A
|
|
eq a.className, 'classA'
|
|
eq b.test(), 'B'
|
|
Object.setPrototypeOf(b, a)
|
|
eq b.className, 'classA'
|
|
# This shouldn't throw,
|
|
# as we only change inheritance not object construction
|
|
# This may be an issue with ES, rather than CS construction?
|
|
#eq b.test(), 'B'
|
|
|
|
class D extends B
|
|
B::test = () -> 'D'
|
|
eq (new D).test(), 'D'
|
|
|
|
# TODO: implement this error check
|
|
# test "ES6 conformance to extending non-classes", ->
|
|
# A = (@title) ->
|
|
# 'Title: ' + @
|
|
|
|
# class B extends A
|
|
# b = new B('caffeinated')
|
|
# eq b.title, 'caffeinated'
|
|
|
|
# # Check inheritance chain
|
|
# A::getTitle = () -> @title
|
|
# eq b.getTitle(), 'caffeinated'
|
|
|
|
# throwsC = """
|
|
# C = {title: 'invalid'}
|
|
# class D extends {}
|
|
# """
|
|
# # This should catch on compile and message should be "class can only extend classes and functions."
|
|
# throws -> CoffeeScript.run throwsC, bare: yes
|
|
|
|
# TODO: Evaluate future compliance with "strict mode";
|
|
# test "Class function environment should be in `strict mode`, ie as if 'use strict' was in use", ->
|
|
# class A
|
|
# # this might be a meaningless test, since these are likely to be runtime errors and different
|
|
# # for every browser. Thoughts?
|
|
# constructor: () ->
|
|
# # Ivalid: prop reassignment
|
|
# @state = {prop: [1], prop: {a: 'a'}}
|
|
# # eval reassignment
|
|
# @badEval = eval;
|
|
|
|
# # Should throw, but doesn't
|
|
# a = new A
|
|
|
|
# TODO: new.target needs support Separate issue
|
|
# test "ES6 support for new.target (functions and constructors)", ->
|
|
# throwsA = """
|
|
# class A
|
|
# constructor: () ->
|
|
# a = new.target.name
|
|
# """
|
|
# throws -> CoffeeScript.compile throwsA, bare: yes
|
|
|
|
test "only one method named constructor allowed", ->
|
|
throwsA = """
|
|
class A
|
|
constructor: (@first) ->
|
|
constructor: (@last) ->
|
|
"""
|
|
throws -> CoffeeScript.compile throwsA, bare: yes
|
|
|
|
test "If the constructor of a child class does not call super,it should return an object.", ->
|
|
nonce = {}
|
|
|
|
class A
|
|
class B extends A
|
|
constructor: ->
|
|
return nonce
|
|
|
|
eq nonce, new B
|
|
|
|
|
|
test "super can only exist in extended classes", ->
|
|
throwsA = """
|
|
class A
|
|
constructor: (@name) ->
|
|
super()
|
|
"""
|
|
throws -> CoffeeScript.compile throwsA, bare: yes
|
|
|
|
# --- CS1 classes compatability breaks ---
|
|
test "CS6 Class extends a CS1 compiled class", ->
|
|
```
|
|
// Generated by CoffeeScript 1.11.1
|
|
var BaseCS1, ExtendedCS1,
|
|
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
hasProp = {}.hasOwnProperty;
|
|
|
|
BaseCS1 = (function() {
|
|
function BaseCS1(drink) {
|
|
this.drink = drink;
|
|
}
|
|
|
|
BaseCS1.prototype.make = function() {
|
|
return "making a " + this.drink;
|
|
};
|
|
|
|
BaseCS1.className = function() {
|
|
return 'BaseCS1';
|
|
};
|
|
|
|
return BaseCS1;
|
|
|
|
})();
|
|
|
|
ExtendedCS1 = (function(superClass) {
|
|
extend(ExtendedCS1, superClass);
|
|
|
|
function ExtendedCS1(flavor) {
|
|
this.flavor = flavor;
|
|
ExtendedCS1.__super__.constructor.call(this, 'cafe ole');
|
|
}
|
|
|
|
ExtendedCS1.prototype.make = function() {
|
|
return "making a " + this.drink + " with " + this.flavor;
|
|
};
|
|
|
|
ExtendedCS1.className = function() {
|
|
return 'ExtendedCS1';
|
|
};
|
|
|
|
return ExtendedCS1;
|
|
|
|
})(BaseCS1);
|
|
|
|
```
|
|
class B extends BaseCS1
|
|
eq B.className(), 'BaseCS1'
|
|
b = new B('machiato')
|
|
eq b.make(), "making a machiato"
|
|
|
|
|
|
test "CS6 Class extends an extended CS1 compiled class", ->
|
|
```
|
|
// Generated by CoffeeScript 1.11.1
|
|
var BaseCS1, ExtendedCS1,
|
|
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
hasProp = {}.hasOwnProperty;
|
|
|
|
BaseCS1 = (function() {
|
|
function BaseCS1(drink) {
|
|
this.drink = drink;
|
|
}
|
|
|
|
BaseCS1.prototype.make = function() {
|
|
return "making a " + this.drink;
|
|
};
|
|
|
|
BaseCS1.className = function() {
|
|
return 'BaseCS1';
|
|
};
|
|
|
|
return BaseCS1;
|
|
|
|
})();
|
|
|
|
ExtendedCS1 = (function(superClass) {
|
|
extend(ExtendedCS1, superClass);
|
|
|
|
function ExtendedCS1(flavor) {
|
|
this.flavor = flavor;
|
|
ExtendedCS1.__super__.constructor.call(this, 'cafe ole');
|
|
}
|
|
|
|
ExtendedCS1.prototype.make = function() {
|
|
return "making a " + this.drink + " with " + this.flavor;
|
|
};
|
|
|
|
ExtendedCS1.className = function() {
|
|
return 'ExtendedCS1';
|
|
};
|
|
|
|
return ExtendedCS1;
|
|
|
|
})(BaseCS1);
|
|
|
|
```
|
|
class B extends ExtendedCS1
|
|
eq B.className(), 'ExtendedCS1'
|
|
b = new B('vanilla')
|
|
eq b.make(), "making a cafe ole with vanilla"
|
|
|
|
test "CS6 Class extends a CS1 compiled class with super()", ->
|
|
```
|
|
// Generated by CoffeeScript 1.11.1
|
|
var BaseCS1, ExtendedCS1,
|
|
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
|
hasProp = {}.hasOwnProperty;
|
|
|
|
BaseCS1 = (function() {
|
|
function BaseCS1(drink) {
|
|
this.drink = drink;
|
|
}
|
|
|
|
BaseCS1.prototype.make = function() {
|
|
return "making a " + this.drink;
|
|
};
|
|
|
|
BaseCS1.className = function() {
|
|
return 'BaseCS1';
|
|
};
|
|
|
|
return BaseCS1;
|
|
|
|
})();
|
|
|
|
ExtendedCS1 = (function(superClass) {
|
|
extend(ExtendedCS1, superClass);
|
|
|
|
function ExtendedCS1(flavor) {
|
|
this.flavor = flavor;
|
|
ExtendedCS1.__super__.constructor.call(this, 'cafe ole');
|
|
}
|
|
|
|
ExtendedCS1.prototype.make = function() {
|
|
return "making a " + this.drink + " with " + this.flavor;
|
|
};
|
|
|
|
ExtendedCS1.className = function() {
|
|
return 'ExtendedCS1';
|
|
};
|
|
|
|
return ExtendedCS1;
|
|
|
|
})(BaseCS1);
|
|
|
|
```
|
|
class B extends ExtendedCS1
|
|
constructor: (@shots) ->
|
|
super('caramel')
|
|
make: () ->
|
|
super + " and #{@shots} shots of espresso"
|
|
|
|
eq B.className(), 'ExtendedCS1'
|
|
b = new B('three')
|
|
eq b.make(), "making a cafe ole with caramel and three shots of espresso"
|