mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
8d81804fee
* 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.
462 lines
10 KiB
CoffeeScript
462 lines
10 KiB
CoffeeScript
# Operators
|
|
# ---------
|
|
|
|
# * Operators
|
|
# * Existential Operator (Binary)
|
|
# * Existential Operator (Unary)
|
|
# * Aliased Operators
|
|
# * [not] in/of
|
|
# * Chained Comparison
|
|
|
|
test "binary (2-ary) math operators do not require spaces", ->
|
|
a = 1
|
|
b = -1
|
|
eq +1, a*-b
|
|
eq -1, a*+b
|
|
eq +1, a/-b
|
|
eq -1, a/+b
|
|
|
|
test "operators should respect new lines as spaced", ->
|
|
a = 123 +
|
|
456
|
|
eq 579, a
|
|
|
|
b = "1#{2}3" +
|
|
"456"
|
|
eq '123456', b
|
|
|
|
test "multiple operators should space themselves", ->
|
|
eq (+ +1), (- -1)
|
|
|
|
test "compound operators on successive lines", ->
|
|
a = 1
|
|
a +=
|
|
1
|
|
eq a, 2
|
|
|
|
test "bitwise operators", ->
|
|
eq 2, (10 & 3)
|
|
eq 11, (10 | 3)
|
|
eq 9, (10 ^ 3)
|
|
eq 80, (10 << 3)
|
|
eq 1, (10 >> 3)
|
|
eq 1, (10 >>> 3)
|
|
num = 10; eq 2, (num &= 3)
|
|
num = 10; eq 11, (num |= 3)
|
|
num = 10; eq 9, (num ^= 3)
|
|
num = 10; eq 80, (num <<= 3)
|
|
num = 10; eq 1, (num >>= 3)
|
|
num = 10; eq 1, (num >>>= 3)
|
|
|
|
test "`instanceof`", ->
|
|
ok new String instanceof String
|
|
ok new Boolean instanceof Boolean
|
|
# `instanceof` supports negation by prefixing the operator with `not`
|
|
ok new Number not instanceof String
|
|
ok new Array not instanceof Boolean
|
|
|
|
test "use `::` operator on keywords `this` and `@`", ->
|
|
nonce = {}
|
|
obj =
|
|
withAt: -> @::prop
|
|
withThis: -> this::prop
|
|
obj.prototype = prop: nonce
|
|
eq nonce, obj.withAt()
|
|
eq nonce, obj.withThis()
|
|
|
|
|
|
# Existential Operator (Binary)
|
|
|
|
test "binary existential operator", ->
|
|
nonce = {}
|
|
|
|
b = a ? nonce
|
|
eq nonce, b
|
|
|
|
a = null
|
|
b = undefined
|
|
b = a ? nonce
|
|
eq nonce, b
|
|
|
|
a = false
|
|
b = a ? nonce
|
|
eq false, b
|
|
|
|
a = 0
|
|
b = a ? nonce
|
|
eq 0, b
|
|
|
|
test "binary existential operator conditionally evaluates second operand", ->
|
|
i = 1
|
|
func = -> i -= 1
|
|
result = func() ? func()
|
|
eq result, 0
|
|
|
|
test "binary existential operator with negative number", ->
|
|
a = null ? - 1
|
|
eq -1, a
|
|
|
|
|
|
# Existential Operator (Unary)
|
|
|
|
test "postfix existential operator", ->
|
|
ok (if nonexistent? then false else true)
|
|
defined = true
|
|
ok defined?
|
|
defined = false
|
|
ok defined?
|
|
|
|
test "postfix existential operator only evaluates its operand once", ->
|
|
semaphore = 0
|
|
fn = ->
|
|
ok false if semaphore
|
|
++semaphore
|
|
ok(if fn()? then true else false)
|
|
|
|
test "negated postfix existential operator", ->
|
|
ok !nothing?.value
|
|
|
|
test "postfix existential operator on expressions", ->
|
|
eq true, (1 or 0)?, true
|
|
|
|
|
|
# `is`,`isnt`,`==`,`!=`
|
|
|
|
test "`==` and `is` should be interchangeable", ->
|
|
a = b = 1
|
|
ok a is 1 and b == 1
|
|
ok a == b
|
|
ok a is b
|
|
|
|
test "`!=` and `isnt` should be interchangeable", ->
|
|
a = 0
|
|
b = 1
|
|
ok a isnt 1 and b != 0
|
|
ok a != b
|
|
ok a isnt b
|
|
|
|
|
|
# [not] in/of
|
|
|
|
# - `in` should check if an array contains a value using `indexOf`
|
|
# - `of` should check if a property is defined on an object using `in`
|
|
test "in, of", ->
|
|
arr = [1]
|
|
ok 0 of arr
|
|
ok 1 in arr
|
|
# prefixing `not` to `in and `of` should negate them
|
|
ok 1 not of arr
|
|
ok 0 not in arr
|
|
|
|
test "`in` should be able to operate on an array literal", ->
|
|
ok 2 in [0, 1, 2, 3]
|
|
ok 4 not in [0, 1, 2, 3]
|
|
arr = [0, 1, 2, 3]
|
|
ok 2 in arr
|
|
ok 4 not in arr
|
|
# should cache the value used to test the array
|
|
arr = [0]
|
|
val = 0
|
|
ok val++ in arr
|
|
ok val++ not in arr
|
|
val = 0
|
|
ok val++ of arr
|
|
ok val++ not of arr
|
|
|
|
test "`of` and `in` should be able to operate on instance variables", ->
|
|
obj = {
|
|
list: [2,3]
|
|
in_list: (value) -> value in @list
|
|
not_in_list: (value) -> value not in @list
|
|
of_list: (value) -> value of @list
|
|
not_of_list: (value) -> value not of @list
|
|
}
|
|
ok obj.in_list 3
|
|
ok obj.not_in_list 1
|
|
ok obj.of_list 0
|
|
ok obj.not_of_list 2
|
|
|
|
test "#???: `in` with cache and `__indexOf` should work in argument lists", ->
|
|
eq 1, [Object() in Array()].length
|
|
|
|
test "#737: `in` should have higher precedence than logical operators", ->
|
|
eq 1, 1 in [1] and 1
|
|
|
|
test "#768: `in` should preserve evaluation order", ->
|
|
share = 0
|
|
a = -> share++ if share is 0
|
|
b = -> share++ if share is 1
|
|
c = -> share++ if share is 2
|
|
ok a() not in [b(),c()]
|
|
eq 3, share
|
|
|
|
test "#1099: empty array after `in` should compile to `false`", ->
|
|
eq 1, [5 in []].length
|
|
eq false, do -> return 0 in []
|
|
|
|
test "#1354: optimized `in` checks should not happen when splats are present", ->
|
|
a = [6, 9]
|
|
eq 9 in [3, a...], true
|
|
|
|
test "#1100: precedence in or-test compilation of `in`", ->
|
|
ok 0 in [1 and 0]
|
|
ok 0 in [1, 1 and 0]
|
|
ok not (0 in [1, 0 or 1])
|
|
|
|
test "#1630: `in` should check `hasOwnProperty`", ->
|
|
ok undefined not in length: 1
|
|
|
|
test "#1714: lexer bug with raw range `for` followed by `in`", ->
|
|
0 for [1..2]
|
|
ok not ('a' in ['b'])
|
|
|
|
0 for [1..2]; ok not ('a' in ['b'])
|
|
|
|
0 for [1..10] # comment ending
|
|
ok not ('a' in ['b'])
|
|
|
|
# lexer state (specifically @seenFor) should be reset before each compilation
|
|
CoffeeScript.compile "0 for [1..2]"
|
|
CoffeeScript.compile "'a' in ['b']"
|
|
|
|
test "#1099: statically determined `not in []` reporting incorrect result", ->
|
|
ok 0 not in []
|
|
|
|
test "#1099: make sure expression tested gets evaluted when array is empty", ->
|
|
a = 0
|
|
(do -> a = 1) in []
|
|
eq a, 1
|
|
|
|
# Chained Comparison
|
|
|
|
test "chainable operators", ->
|
|
ok 100 > 10 > 1 > 0 > -1
|
|
ok -1 < 0 < 1 < 10 < 100
|
|
|
|
test "`is` and `isnt` may be chained", ->
|
|
ok true is not false is true is not false
|
|
ok 0 is 0 isnt 1 is 1
|
|
|
|
test "different comparison operators (`>`,`<`,`is`,etc.) may be combined", ->
|
|
ok 1 < 2 > 1
|
|
ok 10 < 20 > 2+3 is 5
|
|
|
|
test "some chainable operators can be negated by `unless`", ->
|
|
ok (true unless 0==10!=100)
|
|
|
|
test "operator precedence: `|` lower than `<`", ->
|
|
eq 1, 1 | 2 < 3 < 4
|
|
|
|
test "preserve references", ->
|
|
a = b = c = 1
|
|
# `a == b <= c` should become `a === b && b <= c`
|
|
# (this test does not seem to test for this)
|
|
ok a == b <= c
|
|
|
|
test "chained operations should evaluate each value only once", ->
|
|
a = 0
|
|
ok 1 > a++ < 1
|
|
|
|
test "#891: incorrect inversion of chained comparisons", ->
|
|
ok (true unless 0 > 1 > 2)
|
|
ok (true unless (this.NaN = 0/0) < 0/0 < this.NaN)
|
|
|
|
test "#1234: Applying a splat to :: applies the splat to the wrong object", ->
|
|
nonce = {}
|
|
class C
|
|
method: -> @nonce
|
|
nonce: nonce
|
|
|
|
arr = []
|
|
eq nonce, C::method arr... # should be applied to `C::`
|
|
|
|
test "#1102: String literal prevents line continuation", ->
|
|
eq "': '", '' +
|
|
"': '"
|
|
|
|
test "#1703, ---x is invalid JS", ->
|
|
x = 2
|
|
eq (- --x), -1
|
|
|
|
test "Regression with implicit calls against an indented assignment", ->
|
|
eq 1, a =
|
|
1
|
|
|
|
eq a, 1
|
|
|
|
test "#2155 ... conditional assignment to a closure", ->
|
|
x = null
|
|
func = -> x ?= (-> if true then 'hi')
|
|
func()
|
|
eq x(), 'hi'
|
|
|
|
test "#2197: Existential existential double trouble", ->
|
|
counter = 0
|
|
func = -> counter++
|
|
func()? ? 100
|
|
eq counter, 1
|
|
|
|
test "#2567: Optimization of negated existential produces correct result", ->
|
|
a = 1
|
|
ok !(!a?)
|
|
ok !b?
|
|
|
|
test "#2508: Existential access of the prototype", ->
|
|
eq NonExistent?::nothing, undefined
|
|
ok Object?::toString
|
|
|
|
test "power operator", ->
|
|
eq 27, 3 ** 3
|
|
|
|
test "power operator has higher precedence than other maths operators", ->
|
|
eq 55, 1 + 3 ** 3 * 2
|
|
eq -4, -2 ** 2
|
|
eq false, !2 ** 2
|
|
eq 0, (!2) ** 2
|
|
eq -2, ~1 ** 5
|
|
|
|
test "power operator is right associative", ->
|
|
eq 2, 2 ** 1 ** 3
|
|
|
|
test "power operator compound assignment", ->
|
|
a = 2
|
|
a **= 3
|
|
eq 8, a
|
|
|
|
test "floor division operator", ->
|
|
eq 2, 7 // 3
|
|
eq -3, -7 // 3
|
|
eq NaN, 0 // 0
|
|
|
|
test "floor division operator compound assignment", ->
|
|
a = 7
|
|
a //= 1 + 1
|
|
eq 3, a
|
|
|
|
test "modulo operator", ->
|
|
check = (a, b, expected) ->
|
|
eq expected, a %% b, "expected #{a} %%%% #{b} to be #{expected}"
|
|
check 0, 1, 0
|
|
check 0, -1, -0
|
|
check 1, 0, NaN
|
|
check 1, 2, 1
|
|
check 1, -2, -1
|
|
check 1, 3, 1
|
|
check 2, 3, 2
|
|
check 3, 3, 0
|
|
check 4, 3, 1
|
|
check -1, 3, 2
|
|
check -2, 3, 1
|
|
check -3, 3, 0
|
|
check -4, 3, 2
|
|
check 5.5, 2.5, 0.5
|
|
check -5.5, 2.5, 2.0
|
|
|
|
test "modulo operator compound assignment", ->
|
|
a = -2
|
|
a %%= 5
|
|
eq 3, a
|
|
|
|
test "modulo operator converts arguments to numbers", ->
|
|
eq 1, 1 %% '42'
|
|
eq 1, '1' %% 42
|
|
eq 1, '1' %% '42'
|
|
|
|
test "#3361: Modulo operator coerces right operand once", ->
|
|
count = 0
|
|
res = 42 %% valueOf: -> count += 1
|
|
eq 1, count
|
|
eq 0, res
|
|
|
|
test "#3363: Modulo operator coercing order", ->
|
|
count = 2
|
|
a = valueOf: -> count *= 2
|
|
b = valueOf: -> count += 1
|
|
eq 4, a %% b
|
|
eq 5, count
|
|
|
|
test "#3598: Unary + and - coerce the operand once when it is an identifier", ->
|
|
# Unary + and - do not generate `_ref`s when the operand is a number, for
|
|
# readability. To make sure that they do when the operand is an identifier,
|
|
# test that they are consistent with another unary operator as well as another
|
|
# complex expression.
|
|
# Tip: Making one of the tests temporarily fail lets you easily inspect the
|
|
# compiled JavaScript.
|
|
|
|
assertOneCoercion = (fn) ->
|
|
count = 0
|
|
value = valueOf: -> count++; 1
|
|
fn value
|
|
eq 1, count
|
|
|
|
eq 1, 1 ? 0
|
|
eq 1, +1 ? 0
|
|
eq -1, -1 ? 0
|
|
assertOneCoercion (a) ->
|
|
eq 1, +a ? 0
|
|
assertOneCoercion (a) ->
|
|
eq -1, -a ? 0
|
|
assertOneCoercion (a) ->
|
|
eq -2, ~a ? 0
|
|
assertOneCoercion (a) ->
|
|
eq 0.5, a / 2 ? 0
|
|
|
|
ok -2 <= 1 < 2
|
|
ok -2 <= +1 < 2
|
|
ok -2 <= -1 < 2
|
|
assertOneCoercion (a) ->
|
|
ok -2 <= +a < 2
|
|
assertOneCoercion (a) ->
|
|
ok -2 <= -a < 2
|
|
assertOneCoercion (a) ->
|
|
ok -2 <= ~a < 2
|
|
assertOneCoercion (a) ->
|
|
ok -2 <= a / 2 < 2
|
|
|
|
arrayEq [0], (n for n in [0] by 1)
|
|
arrayEq [0], (n for n in [0] by +1)
|
|
arrayEq [0], (n for n in [0] by -1)
|
|
assertOneCoercion (a) ->
|
|
arrayEq [0], (n for n in [0] by +a)
|
|
assertOneCoercion (a) ->
|
|
arrayEq [0], (n for n in [0] by -a)
|
|
assertOneCoercion (a) ->
|
|
arrayEq [0], (n for n in [0] by ~a)
|
|
assertOneCoercion (a) ->
|
|
arrayEq [0], (n for n in [0] by a * 2 / 2)
|
|
|
|
ok 1 in [0, 1]
|
|
ok +1 in [0, 1]
|
|
ok -1 in [0, -1]
|
|
assertOneCoercion (a) ->
|
|
ok +a in [0, 1]
|
|
assertOneCoercion (a) ->
|
|
ok -a in [0, -1]
|
|
assertOneCoercion (a) ->
|
|
ok ~a in [0, -2]
|
|
assertOneCoercion (a) ->
|
|
ok a / 2 in [0, 0.5]
|
|
|
|
test "'new' target", ->
|
|
nonce = {}
|
|
ctor = -> nonce
|
|
|
|
eq (new ctor), nonce
|
|
eq (new ctor()), nonce
|
|
|
|
ok new class
|
|
|
|
ctor = class
|
|
ok (new ctor) instanceof ctor
|
|
ok (new ctor()) instanceof ctor
|
|
|
|
# Force an executable class body
|
|
ctor = class then a = 1
|
|
ok (new ctor) instanceof ctor
|
|
|
|
get = -> ctor
|
|
ok (new get()) not instanceof ctor
|
|
ok (new (get())()) instanceof ctor
|
|
|
|
# classes must be called with `new`. In this case `new` applies to `get` only
|
|
throws -> new get()()
|