2010-12-29 00:48:54 -05:00
|
|
|
# Comments
|
|
|
|
# --------
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
# * Single-Line Comments
|
|
|
|
# * Block Comments
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
# Note: awkward spacing seen in some tests is likely intentional.
|
|
|
|
|
|
|
|
test "comments in objects", ->
|
|
|
|
obj1 = {
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
one: 1
|
|
|
|
# comment
|
|
|
|
two: 2
|
|
|
|
# comment
|
|
|
|
}
|
|
|
|
|
|
|
|
ok Object::hasOwnProperty.call(obj1,'one')
|
|
|
|
eq obj1.one, 1
|
|
|
|
ok Object::hasOwnProperty.call(obj1,'two')
|
|
|
|
eq obj1.two, 2
|
|
|
|
|
|
|
|
test "comments in YAML-style objects", ->
|
|
|
|
obj2 =
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
three: 3
|
|
|
|
# comment
|
|
|
|
four: 4
|
|
|
|
# comment
|
|
|
|
|
|
|
|
ok Object::hasOwnProperty.call(obj2,'three')
|
|
|
|
eq obj2.three, 3
|
|
|
|
ok Object::hasOwnProperty.call(obj2,'four')
|
|
|
|
eq obj2.four, 4
|
|
|
|
|
|
|
|
test "comments following operators that continue lines", ->
|
|
|
|
sum =
|
|
|
|
1 +
|
|
|
|
1 + # comment
|
|
|
|
1
|
|
|
|
eq 3, sum
|
|
|
|
|
|
|
|
test "comments in functions", ->
|
|
|
|
fn = ->
|
|
|
|
# comment
|
|
|
|
false
|
|
|
|
false # comment
|
|
|
|
false
|
|
|
|
# comment
|
|
|
|
|
|
|
|
# comment
|
|
|
|
true
|
|
|
|
|
|
|
|
ok fn()
|
|
|
|
|
|
|
|
fn2 = -> #comment
|
|
|
|
fn()
|
|
|
|
# comment
|
|
|
|
|
|
|
|
ok fn2()
|
|
|
|
|
|
|
|
test "trailing comment before an outdent", ->
|
|
|
|
nonce = {}
|
|
|
|
fn3 = ->
|
|
|
|
if true
|
|
|
|
undefined # comment
|
|
|
|
nonce
|
|
|
|
|
|
|
|
eq nonce, fn3()
|
|
|
|
|
|
|
|
test "comments in a switch", ->
|
|
|
|
nonce = {}
|
|
|
|
result = switch nonce #comment
|
|
|
|
# comment
|
|
|
|
when false then undefined
|
|
|
|
# comment
|
|
|
|
when null #comment
|
|
|
|
undefined
|
|
|
|
else nonce # comment
|
|
|
|
|
|
|
|
eq nonce, result
|
|
|
|
|
|
|
|
test "comment with conditional statements", ->
|
|
|
|
nonce = {}
|
|
|
|
result = if false # comment
|
|
|
|
undefined
|
|
|
|
#comment
|
|
|
|
else # comment
|
|
|
|
nonce
|
|
|
|
# comment
|
|
|
|
eq nonce, result
|
|
|
|
|
|
|
|
test "spaced comments with conditional statements", ->
|
|
|
|
nonce = {}
|
|
|
|
result = if false
|
|
|
|
undefined
|
|
|
|
|
|
|
|
# comment
|
|
|
|
else if false
|
|
|
|
undefined
|
|
|
|
|
|
|
|
# comment
|
|
|
|
else
|
|
|
|
nonce
|
|
|
|
|
|
|
|
eq nonce, result
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Block Comments
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
###
|
|
|
|
This is a here-comment.
|
|
|
|
Kind of like a heredoc.
|
|
|
|
###
|
|
|
|
|
|
|
|
test "block comments in objects", ->
|
|
|
|
a = {}
|
|
|
|
b = {}
|
|
|
|
obj = {
|
|
|
|
a: a
|
|
|
|
###
|
|
|
|
comment
|
|
|
|
###
|
|
|
|
b: b
|
|
|
|
}
|
|
|
|
|
|
|
|
eq a, obj.a
|
|
|
|
eq b, obj.b
|
|
|
|
|
|
|
|
test "block comments in YAML-style", ->
|
|
|
|
a = {}
|
|
|
|
b = {}
|
|
|
|
obj =
|
|
|
|
a: a
|
|
|
|
###
|
|
|
|
comment
|
|
|
|
###
|
|
|
|
b: b
|
|
|
|
|
|
|
|
eq a, obj.a
|
|
|
|
eq b, obj.b
|
|
|
|
|
|
|
|
|
|
|
|
test "block comments in functions", ->
|
|
|
|
nonce = {}
|
|
|
|
|
|
|
|
fn1 = ->
|
|
|
|
true
|
|
|
|
###
|
|
|
|
false
|
|
|
|
###
|
|
|
|
|
|
|
|
ok fn1()
|
|
|
|
|
|
|
|
fn2 = ->
|
|
|
|
###
|
|
|
|
block comment
|
|
|
|
###
|
|
|
|
nonce
|
|
|
|
|
|
|
|
eq nonce, fn2()
|
|
|
|
|
|
|
|
fn3 = ->
|
|
|
|
nonce
|
|
|
|
###
|
|
|
|
block comment
|
|
|
|
###
|
|
|
|
|
|
|
|
eq nonce, fn3()
|
|
|
|
|
|
|
|
fn4 = ->
|
|
|
|
one = ->
|
|
|
|
###
|
|
|
|
block comment
|
|
|
|
###
|
|
|
|
two = ->
|
|
|
|
three = ->
|
|
|
|
nonce
|
|
|
|
|
|
|
|
eq nonce, fn4()()()()
|
|
|
|
|
|
|
|
test "block comments inside class bodies", ->
|
|
|
|
class A
|
|
|
|
a: ->
|
|
|
|
|
|
|
|
###
|
|
|
|
Comment
|
|
|
|
###
|
|
|
|
b: ->
|
|
|
|
|
|
|
|
ok A.prototype.b instanceof Function
|
|
|
|
|
|
|
|
class B
|
|
|
|
###
|
|
|
|
Comment
|
|
|
|
###
|
|
|
|
a: ->
|
|
|
|
b: ->
|
|
|
|
|
|
|
|
ok B.prototype.a instanceof Function
|
2012-01-14 11:32:09 -05:00
|
|
|
|
|
|
|
test "#2037: herecomments shouldn't imply line terminators", ->
|
|
|
|
do (-> ### ###; fail)
|
2013-04-17 19:20:28 -04:00
|
|
|
|
|
|
|
test "#2916: block comment before implicit call with implicit object", ->
|
|
|
|
fn = (obj) -> ok obj.a
|
|
|
|
### ###
|
|
|
|
fn
|
|
|
|
a: yes
|
2013-08-23 09:55:44 -04:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
test "#3132: Format single-line block comment nicely", ->
|
|
|
|
input = """
|
|
|
|
### Single-line block comment without additional space here => ###"""
|
|
|
|
|
|
|
|
result = """
|
|
|
|
|
|
|
|
/* Single-line block comment without additional space here => */
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
|
|
|
|
2013-08-23 09:55:44 -04:00
|
|
|
test "#3132: Format multi-line block comment nicely", ->
|
|
|
|
input = """
|
|
|
|
###
|
|
|
|
# Multi-line
|
|
|
|
# block
|
|
|
|
# comment
|
|
|
|
###"""
|
|
|
|
|
|
|
|
result = """
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multi-line
|
|
|
|
* block
|
|
|
|
* comment
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
|
|
|
|
|
|
|
test "#3132: Format simple block comment nicely", ->
|
|
|
|
input = """
|
|
|
|
###
|
|
|
|
No
|
|
|
|
Preceding hash
|
|
|
|
###"""
|
|
|
|
|
|
|
|
result = """
|
|
|
|
|
|
|
|
/*
|
|
|
|
No
|
|
|
|
Preceding hash
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
|
|
|
|
|
|
|
test "#3132: Format indented block-comment nicely", ->
|
|
|
|
input = """
|
|
|
|
fn = () ->
|
|
|
|
###
|
|
|
|
# Indented
|
|
|
|
Multiline
|
|
|
|
###
|
|
|
|
1"""
|
|
|
|
|
|
|
|
result = """
|
|
|
|
var fn;
|
|
|
|
|
|
|
|
fn = function() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Indented
|
|
|
|
Multiline
|
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
2013-09-17 12:09:15 -04:00
|
|
|
|
|
|
|
# Although adequately working, block comment-placement is not yet perfect.
|
|
|
|
# (Considering a case where multiple variables have been declared …)
|
|
|
|
test "#3132: Format jsdoc-style block-comment nicely", ->
|
|
|
|
input = """
|
|
|
|
###*
|
|
|
|
# Multiline for jsdoc-"@doctags"
|
2016-11-29 21:13:12 -05:00
|
|
|
#
|
2013-09-17 12:09:15 -04:00
|
|
|
# @type {Function}
|
|
|
|
###
|
|
|
|
fn = () -> 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = """
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
/**
|
|
|
|
* Multiline for jsdoc-"@doctags"
|
2016-11-29 21:13:12 -05:00
|
|
|
*
|
2013-09-17 12:09:15 -04:00
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
var fn;
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
fn = function() {
|
|
|
|
return 1;
|
|
|
|
};
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
|
|
|
|
|
|
|
# Although adequately working, block comment-placement is not yet perfect.
|
|
|
|
# (Considering a case where multiple variables have been declared …)
|
|
|
|
test "#3132: Format hand-made (raw) jsdoc-style block-comment nicely", ->
|
|
|
|
input = """
|
|
|
|
###*
|
|
|
|
* Multiline for jsdoc-"@doctags"
|
2016-11-29 21:13:12 -05:00
|
|
|
*
|
2013-09-17 12:09:15 -04:00
|
|
|
* @type {Function}
|
|
|
|
###
|
|
|
|
fn = () -> 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = """
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
/**
|
|
|
|
* Multiline for jsdoc-"@doctags"
|
2016-11-29 21:13:12 -05:00
|
|
|
*
|
2013-09-17 12:09:15 -04:00
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
var fn;
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
fn = function() {
|
|
|
|
return 1;
|
|
|
|
};
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
|
|
|
|
|
|
|
# Although adequately working, block comment-placement is not yet perfect.
|
|
|
|
# (Considering a case where multiple variables have been declared …)
|
|
|
|
test "#3132: Place block-comments nicely", ->
|
|
|
|
input = """
|
|
|
|
###*
|
|
|
|
# A dummy class definition
|
2016-11-29 21:13:12 -05:00
|
|
|
#
|
2013-09-17 12:09:15 -04:00
|
|
|
# @class
|
|
|
|
###
|
|
|
|
class DummyClass
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
###*
|
|
|
|
# @constructor
|
|
|
|
###
|
|
|
|
constructor: ->
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
###*
|
|
|
|
# Singleton reference
|
2016-11-29 21:13:12 -05:00
|
|
|
#
|
2013-09-17 12:09:15 -04:00
|
|
|
# @type {DummyClass}
|
|
|
|
###
|
|
|
|
@instance = new DummyClass()
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
result = """
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
/**
|
|
|
|
* A dummy class definition
|
2016-11-29 21:13:12 -05:00
|
|
|
*
|
2013-09-17 12:09:15 -04:00
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var DummyClass;
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
DummyClass = (function() {
|
[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
|
|
|
class DummyClass {
|
2016-11-29 21:13:12 -05:00
|
|
|
|
[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
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
};
|
2016-11-29 21:13:12 -05:00
|
|
|
|
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
/**
|
|
|
|
* Singleton reference
|
2016-11-29 21:13:12 -05:00
|
|
|
*
|
2013-09-17 12:09:15 -04:00
|
|
|
* @type {DummyClass}
|
|
|
|
*/
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
DummyClass.instance = new DummyClass();
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
return DummyClass;
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
})();
|
2016-11-29 21:13:12 -05:00
|
|
|
|
2013-09-17 12:09:15 -04:00
|
|
|
"""
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
2014-09-17 06:46:50 -04:00
|
|
|
|
|
|
|
test "#3638: Demand a whitespace after # symbol", ->
|
|
|
|
input = """
|
|
|
|
###
|
|
|
|
#No
|
|
|
|
#whitespace
|
|
|
|
###"""
|
|
|
|
|
|
|
|
result = """
|
|
|
|
|
|
|
|
/*
|
|
|
|
#No
|
|
|
|
#whitespace
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
eq CoffeeScript.compile(input, bare: on), result
|
2015-01-23 19:39:26 -05:00
|
|
|
|
|
|
|
test "#3761: Multiline comment at end of an object", ->
|
|
|
|
anObject =
|
|
|
|
x: 3
|
|
|
|
###
|
|
|
|
#Comment
|
|
|
|
###
|
|
|
|
|
|
|
|
ok anObject.x is 3
|
2016-11-29 21:13:12 -05:00
|
|
|
|
|
|
|
test "#4375: UTF-8 characters in comments", ->
|
|
|
|
# 智に働けば角が立つ、情に掉させば流される。
|
|
|
|
ok yes
|