2010-12-29 14:06:57 -05:00
|
|
|
# Array Literals
|
|
|
|
# --------------
|
|
|
|
|
|
|
|
# * Array Literals
|
|
|
|
# * Splats in Array Literals
|
2010-12-30 22:48:31 -05:00
|
|
|
|
2011-01-03 04:17:00 -05:00
|
|
|
# TODO: add indexing and method invocation tests: [1][0] is 1, [].toString()
|
2010-12-30 22:48:31 -05:00
|
|
|
|
2011-03-11 21:55:26 -05:00
|
|
|
test "trailing commas", ->
|
|
|
|
trailingComma = [1, 2, 3,]
|
|
|
|
ok (trailingComma[0] is 1) and (trailingComma[2] is 3) and (trailingComma.length is 3)
|
2010-12-30 22:48:31 -05:00
|
|
|
|
2011-03-11 21:55:26 -05:00
|
|
|
trailingComma = [
|
|
|
|
1, 2, 3,
|
|
|
|
4, 5, 6
|
|
|
|
7, 8, 9,
|
|
|
|
]
|
|
|
|
(sum = (sum or 0) + n) for n in trailingComma
|
2010-12-30 22:48:31 -05:00
|
|
|
|
2011-03-11 21:55:26 -05:00
|
|
|
a = [((x) -> x), ((x) -> x * x)]
|
|
|
|
ok a.length is 2
|
2010-12-30 22:48:31 -05:00
|
|
|
|
2011-03-11 21:55:26 -05:00
|
|
|
test "incorrect indentation without commas", ->
|
|
|
|
result = [['a']
|
|
|
|
{b: 'c'}]
|
|
|
|
ok result[0][0] is 'a'
|
|
|
|
ok result[1]['b'] is 'c'
|
2010-12-30 22:48:31 -05:00
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Splats in Array Literals
|
2010-12-30 22:48:31 -05:00
|
|
|
|
|
|
|
test "array splat expansions with assignments", ->
|
|
|
|
nums = [1, 2, 3]
|
|
|
|
list = [a = 0, nums..., b = 4]
|
|
|
|
eq 0, a
|
|
|
|
eq 4, b
|
|
|
|
arrayEq [0,1,2,3,4], list
|
2011-01-19 22:36:30 -05:00
|
|
|
|
2011-03-11 22:44:18 -05:00
|
|
|
|
2011-01-19 22:36:30 -05:00
|
|
|
test "mixed shorthand objects in array lists", ->
|
|
|
|
arr = [
|
|
|
|
a:1
|
|
|
|
'b'
|
|
|
|
c:1
|
|
|
|
]
|
|
|
|
ok arr.length is 3
|
|
|
|
ok arr[2].c is 1
|
|
|
|
|
|
|
|
arr = [b: 1, a: 2, 100]
|
|
|
|
eq arr[1], 100
|
|
|
|
|
|
|
|
arr = [a:0, b:1, (1 + 1)]
|
|
|
|
eq arr[1], 2
|
|
|
|
|
|
|
|
arr = [a:1, 'a', b:1, 'b']
|
|
|
|
eq arr.length, 4
|
|
|
|
eq arr[2].b, 1
|
|
|
|
eq arr[3], 'b'
|
2011-03-12 08:41:14 -05:00
|
|
|
|
2011-02-17 21:56:28 -05:00
|
|
|
test "array splats with nested arrays", ->
|
2011-02-18 07:15:40 -05:00
|
|
|
nonce = {}
|
|
|
|
a = [nonce]
|
2011-02-17 21:56:28 -05:00
|
|
|
list = [1, 2, a...]
|
|
|
|
eq list[0], 1
|
2011-02-18 07:15:40 -05:00
|
|
|
eq list[2], nonce
|
2011-02-17 21:56:28 -05:00
|
|
|
|
2011-02-18 07:15:40 -05:00
|
|
|
a = [[nonce]]
|
2011-02-17 21:56:28 -05:00
|
|
|
list = [1, 2, a...]
|
2011-02-18 07:15:40 -05:00
|
|
|
arrayEq list, [1, 2, [nonce]]
|
2011-02-17 21:56:28 -05:00
|
|
|
|
2017-08-27 18:11:14 -04:00
|
|
|
test "#4260: splat after existential operator soak", ->
|
|
|
|
a = {b: [3]}
|
|
|
|
foo = (a) -> [a]
|
|
|
|
arrayEq [a?.b...], [3]
|
|
|
|
arrayEq [c?.b ? []...], []
|
|
|
|
arrayEq [...a?.b], [3]
|
|
|
|
arrayEq [...c?.b ? []], []
|
|
|
|
arrayEq foo(a?.b...), [3]
|
|
|
|
arrayEq foo(...a?.b), [3]
|
|
|
|
arrayEq foo(c?.b ? []...), [undefined]
|
|
|
|
arrayEq foo(...c?.b ? []), [undefined]
|
|
|
|
e = yes
|
|
|
|
f = null
|
|
|
|
arrayEq [(a if e)?.b...], [3]
|
|
|
|
arrayEq [(a if f)?.b ? []...], []
|
|
|
|
arrayEq [...(a if e)?.b], [3]
|
|
|
|
arrayEq [...(a if f)?.b ? []], []
|
|
|
|
arrayEq foo((a if e)?.b...), [3]
|
|
|
|
arrayEq foo(...(a if e)?.b), [3]
|
|
|
|
arrayEq foo((a if f)?.b ? []...), [undefined]
|
|
|
|
arrayEq foo(...(a if f)?.b ? []), [undefined]
|
|
|
|
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
arrayEq [... a?.b], [3]
|
|
|
|
arrayEq [... c?.b ? []], []
|
|
|
|
arrayEq [a?.b ...], [3]
|
|
|
|
arrayEq [(a if e)?.b ...], [3]
|
|
|
|
arrayEq foo(a?.b ...), [3]
|
|
|
|
arrayEq foo(... a?.b), [3]
|
|
|
|
|
|
|
|
test "#1349: trailing if after splat", ->
|
|
|
|
a = [3]
|
|
|
|
b = yes
|
|
|
|
c = null
|
|
|
|
foo = (a) -> [a]
|
|
|
|
arrayEq [a if b...], [3]
|
|
|
|
arrayEq [(a if c) ? []...], []
|
|
|
|
arrayEq [...a if b], [3]
|
|
|
|
arrayEq [...(a if c) ? []], []
|
|
|
|
arrayEq foo((a if b)...), [3]
|
|
|
|
arrayEq foo(...(a if b)), [3]
|
|
|
|
arrayEq foo((a if c) ? []...), [undefined]
|
|
|
|
arrayEq foo(...(a if c) ? []), [undefined]
|
|
|
|
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
arrayEq [... a if b], [3]
|
|
|
|
arrayEq [a if b ...], [3]
|
|
|
|
|
2011-04-09 09:53:04 -04:00
|
|
|
test "#1274: `[] = a()` compiles to `false` instead of `a()`", ->
|
|
|
|
a = false
|
|
|
|
fn = -> a = true
|
|
|
|
[] = fn()
|
|
|
|
ok a
|
2015-01-14 15:27:24 -05:00
|
|
|
|
|
|
|
test "#3194: string interpolation in array", ->
|
|
|
|
arr = [ "a"
|
|
|
|
key: 'value'
|
|
|
|
]
|
|
|
|
eq 2, arr.length
|
|
|
|
eq 'a', arr[0]
|
|
|
|
eq 'value', arr[1].key
|
|
|
|
|
|
|
|
b = 'b'
|
|
|
|
arr = [ "a#{b}"
|
|
|
|
key: 'value'
|
|
|
|
]
|
|
|
|
eq 2, arr.length
|
|
|
|
eq 'ab', arr[0]
|
|
|
|
eq 'value', arr[1].key
|
2015-01-15 13:44:14 -05:00
|
|
|
|
|
|
|
test "regex interpolation in array", ->
|
|
|
|
arr = [ /a/
|
|
|
|
key: 'value'
|
|
|
|
]
|
|
|
|
eq 2, arr.length
|
|
|
|
eq 'a', arr[0].source
|
|
|
|
eq 'value', arr[1].key
|
|
|
|
|
|
|
|
b = 'b'
|
|
|
|
arr = [ ///a#{b}///
|
|
|
|
key: 'value'
|
|
|
|
]
|
|
|
|
eq 2, arr.length
|
|
|
|
eq 'ab', arr[0].source
|
|
|
|
eq 'value', arr[1].key
|
Compile splats in arrays and function calls to ES2015 splats (#4353)
Rather than compiling splats to arrays built using `Array#concat`, splats
are now compiled directly to ES2015 splats, e.g.
f foo, arguments..., bar
[ foo, arguments..., bar ]
Which used to be compiled to:
f.apply(null, [foo].concat(slice.call(arguments), [bar]));
[foo].concat(slice.call(arguments), [bar]);
Is now compiled to:
f(foo, ...arguments, bar);
[ foo, ...arguments, bar ];
2016-11-06 11:30:04 -05:00
|
|
|
|
|
|
|
test "splat extraction from generators", ->
|
|
|
|
gen = ->
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
yield 3
|
|
|
|
arrayEq [ gen()... ], [ 1, 2, 3 ]
|
2016-11-08 02:40:01 -05:00
|
|
|
|
|
|
|
test "for-from loops over Array", ->
|
|
|
|
array1 = [50, 30, 70, 20]
|
|
|
|
array2 = []
|
|
|
|
for x from array1
|
|
|
|
array2.push(x)
|
|
|
|
arrayEq array1, array2
|
|
|
|
|
|
|
|
array1 = [[20, 30], [40, 50]]
|
|
|
|
array2 = []
|
|
|
|
for [a, b] from array1
|
|
|
|
array2.push(b)
|
|
|
|
array2.push(a)
|
|
|
|
arrayEq array2, [30, 20, 50, 40]
|
|
|
|
|
|
|
|
array1 = [{a: 10, b: 20, c: 30}, {a: 40, b: 50, c: 60}]
|
|
|
|
array2 = []
|
|
|
|
for {a: a, b, c: d} from array1
|
|
|
|
array2.push([a, b, d])
|
|
|
|
arrayEq array2, [[10, 20, 30], [40, 50, 60]]
|
|
|
|
|
|
|
|
array1 = [[10, 20, 30, 40, 50]]
|
|
|
|
for [a, b..., c] from array1
|
|
|
|
eq 10, a
|
|
|
|
arrayEq [20, 30, 40], b
|
|
|
|
eq 50, c
|
|
|
|
|
|
|
|
test "for-from comprehensions over Array", ->
|
|
|
|
array1 = (x + 10 for x from [10, 20, 30])
|
|
|
|
ok array1.join(' ') is '20 30 40'
|
|
|
|
|
|
|
|
array2 = (x for x from [30, 41, 57] when x %% 3 is 0)
|
|
|
|
ok array2.join(' ') is '30 57'
|
|
|
|
|
|
|
|
array1 = (b + 5 for [a, b] from [[20, 30], [40, 50]])
|
|
|
|
ok array1.join(' ') is '35 55'
|
|
|
|
|
|
|
|
array2 = (a + b for [a, b] from [[10, 20], [30, 40], [50, 60]] when a + b >= 70)
|
|
|
|
ok array2.join(' ') is '70 110'
|