2010-12-29 14:06:57 -05:00
|
|
|
# Scope
|
|
|
|
# -----
|
|
|
|
|
|
|
|
# * Variable Safety
|
|
|
|
# * Variable Shadowing
|
|
|
|
# * Auto-closure (`do`)
|
|
|
|
# * Global Scope Leaks
|
|
|
|
|
|
|
|
test "reference `arguments` inside of functions", ->
|
|
|
|
sumOfArgs = ->
|
|
|
|
sum = (a,b) -> a + b
|
|
|
|
sum = 0
|
|
|
|
sum += num for num in arguments
|
|
|
|
sum
|
|
|
|
eq 10, sumOfArgs(0, 1, 2, 3, 4)
|
|
|
|
|
|
|
|
test "assignment to an Object.prototype-named variable should not leak to outer scope", ->
|
|
|
|
# FIXME: fails on IE
|
|
|
|
(->
|
|
|
|
constructor = 'word'
|
|
|
|
)()
|
|
|
|
ok constructor isnt 'word'
|
2011-05-07 19:55:27 -04:00
|
|
|
|
2011-08-01 17:28:52 -04:00
|
|
|
test "siblings of splat parameters shouldn't leak to surrounding scope", ->
|
2011-05-07 19:55:27 -04:00
|
|
|
x = 10
|
2011-08-01 17:28:52 -04:00
|
|
|
oops = (x, args...) ->
|
|
|
|
oops(20, 1, 2, 3)
|
2011-05-07 19:55:27 -04:00
|
|
|
eq x, 10
|
2011-06-25 10:34:52 -04:00
|
|
|
|
|
|
|
test "catch statements should introduce their argument to scope", ->
|
|
|
|
try throw ''
|
|
|
|
catch e
|
|
|
|
do -> e = 5
|
|
|
|
eq 5, e
|
2012-01-10 17:00:06 -05:00
|
|
|
|
2014-05-20 17:37:33 -04:00
|
|
|
test "loop variable should be accessible after for-of loop", ->
|
|
|
|
d = (x for x of {1:'a',2:'b'})
|
2014-05-21 10:40:44 -04:00
|
|
|
ok x in ['1','2']
|
2014-05-20 17:37:33 -04:00
|
|
|
|
|
|
|
test "loop variable should be accessible after for-in loop", ->
|
|
|
|
d = (x for x in [1,2])
|
|
|
|
eq x, 2
|
|
|
|
|
2016-11-08 02:40:01 -05:00
|
|
|
test "loop variable should be accessible after for-from loop", ->
|
|
|
|
d = (x for x from [1,2])
|
|
|
|
eq x, 2
|
|
|
|
|
2012-01-10 17:00:06 -05:00
|
|
|
class Array then slice: fail # needs to be global
|
|
|
|
class Object then hasOwnProperty: fail
|
|
|
|
test "#1973: redefining Array/Object constructors shouldn't confuse __X helpers", ->
|
|
|
|
arr = [1..4]
|
|
|
|
arrayEq [3, 4], arr[2..]
|
|
|
|
obj = {arr}
|
|
|
|
for own k of obj
|
|
|
|
eq arr, obj[k]
|
2012-04-12 23:46:28 -04:00
|
|
|
|
|
|
|
test "#2255: global leak with splatted @-params", ->
|
|
|
|
ok not x?
|
|
|
|
arrayEq [0], ((@x...) -> @x).call {}, 0
|
|
|
|
ok not x?
|
2012-04-24 17:23:37 -04:00
|
|
|
|
2012-04-11 18:22:52 -04:00
|
|
|
test "#1183: super + fat arrows", ->
|
2012-04-11 18:05:33 -04:00
|
|
|
dolater = (cb) -> cb()
|
|
|
|
|
|
|
|
class A
|
2016-09-20 17:32:41 -04:00
|
|
|
constructor: ->
|
|
|
|
@_i = 0
|
|
|
|
foo : (cb) ->
|
|
|
|
dolater =>
|
|
|
|
@_i += 1
|
|
|
|
cb()
|
2012-04-11 18:05:33 -04:00
|
|
|
|
|
|
|
class B extends A
|
2016-09-20 17:32:41 -04:00
|
|
|
constructor : ->
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
super()
|
2016-09-20 17:32:41 -04:00
|
|
|
foo : (cb) ->
|
|
|
|
dolater =>
|
|
|
|
dolater =>
|
|
|
|
@_i += 2
|
|
|
|
super cb
|
2015-08-16 15:47:04 -04:00
|
|
|
|
2012-05-15 12:30:51 -04:00
|
|
|
b = new B
|
2012-04-11 18:05:33 -04:00
|
|
|
b.foo => eq b._i, 3
|
2012-04-11 18:22:52 -04:00
|
|
|
|
|
|
|
test "#1183: super + wrap", ->
|
|
|
|
class A
|
|
|
|
m : -> 10
|
2015-08-16 15:47:04 -04:00
|
|
|
|
2012-04-11 18:22:52 -04:00
|
|
|
class B extends A
|
[CS2] Compile all super calls to ES2015 super (#4424)
* Compile all super calls to ES2015 super
This breaks using `super` in non-methods, meaning several tests are
failing. Self-compilation still works.
* Use bound functions for IIFEs containing `super`
`super` can only be called directly in a method, or in an arrow
function.
* Fix handling of `class @A extends A`
This behaviour worked 'for free' when the parent reference was being
cached by the executable class body wrapper. There now needs to be
special handling in place to check if the parent name matches the class
name, and if so to cache the parent reference.
* Fix tests broken by compiling ES2015 `super`
* Disallow bare super
This removes syntax support for 'bare' super calls, e.g.:
class B extends A
constructor: -> super
`super` must now always be followed with arguments like a regular
function call. This also removes the capability of implicitly forwarding
arguments. The above can be equivalently be written as:
class B extends A
constructor: -> super arguments...
* Support super with accessors
`super` with following accessor(s) is now compiled to ES2015
equivalents. In particular, expressions such as `super.name`,
`super[name]`, and also `super.name.prop` are all now valid, and can be
used as expected as calls (i.e. `super.name()`) or in expressions (i.e.
`if super.name? ...`).
`super` without accessors is compiled to a constructor super call in a
constructor, and otherwise, as before, to a super call to the method of
the same name, i.e.
speak: -> super()
...is equivalent to
speak: -> super.speak()
A neat side-effect of the changes is that existential calls now work
properly with super, meaning `super?()` will only call if the super
property exists (and is a function). This is not valid for super in
constructors.
* Prevent calling `super` methods with `new`
This fixes a bug in the previous super handling whereby using the `new`
operator with a `super` call would silently drop the `new`. This is now
an explicit compiler error, as it is invalid JS at runtime.
* Clean up some old super handling code
This was mostly code for tracking the source classes and variables for
methods, which were needed to build the old lookups on `__super__`.
* Add TODO to improve bare super parse error
* Add some TODOs to improve some of the class tests
2017-02-04 15:03:17 -05:00
|
|
|
constructor : -> super()
|
|
|
|
m: -> r = try super()
|
|
|
|
m: -> r = super()
|
2015-08-16 15:47:04 -04:00
|
|
|
|
2012-05-15 12:30:51 -04:00
|
|
|
eq (new B).m(), 10
|
2012-04-24 21:48:18 -04:00
|
|
|
|
|
|
|
test "#1183: super + closures", ->
|
|
|
|
class A
|
|
|
|
constructor: ->
|
|
|
|
@i = 10
|
|
|
|
foo : -> @i
|
2015-08-16 15:47:04 -04:00
|
|
|
|
2012-04-24 21:48:18 -04:00
|
|
|
class B extends A
|
|
|
|
foo : ->
|
|
|
|
ret = switch 1
|
|
|
|
when 0 then 0
|
|
|
|
when 1 then super()
|
|
|
|
ret
|
2012-05-15 12:30:51 -04:00
|
|
|
eq (new B).foo(), 10
|
2015-08-16 15:47:04 -04:00
|
|
|
|
2017-06-14 18:11:53 -04:00
|
|
|
test "#2331: bound super regression", ->
|
|
|
|
class A
|
|
|
|
@value = 'A'
|
|
|
|
method: -> @constructor.value
|
|
|
|
|
|
|
|
class B extends A
|
|
|
|
method: => super()
|
|
|
|
|
|
|
|
eq (new B).method(), 'A'
|
|
|
|
|
2013-11-26 00:40:09 -05:00
|
|
|
test "#3259: leak with @-params within destructured parameters", ->
|
|
|
|
fn = ({@foo}, [@bar], [{@baz}]) ->
|
|
|
|
foo = bar = baz = false
|
|
|
|
|
|
|
|
fn.call {}, {foo: 'foo'}, ['bar'], [{baz: 'baz'}]
|
|
|
|
|
|
|
|
eq 'undefined', typeof foo
|
|
|
|
eq 'undefined', typeof bar
|
2014-05-20 17:37:33 -04:00
|
|
|
eq 'undefined', typeof baz
|