1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
|
# Function Invocation
# -------------------
# * Function Invocation
# * Splats in Function Invocations
# * Implicit Returns
# * Explicit Returns
# shared identity function
id = (_) -> if arguments.length is 1 then _ else [arguments...]
test "basic argument passing", ->
a = {}
b = {}
c = {}
eq 1, (id 1)
eq 2, (id 1, 2)[1]
eq a, (id a)
eq c, (id a, b, c)[2]
test "passing arguments on separate lines", ->
a = {}
b = {}
c = {}
ok(id(
a
b
c
)[1] is b)
eq(0, id(
0
10
)[0])
eq(a,id(
a
))
eq b,
(id b)
test "optional parens can be used in a nested fashion", ->
call = (func) -> func()
add = (a,b) -> a + b
result = call ->
inner = call ->
add 5, 5
ok result is 10
test "hanging commas and semicolons in argument list", ->
fn = () -> arguments.length
eq 2, fn(0,1,)
eq 3, fn 0, 1,
2
eq 2, fn(0, 1;)
# TODO: this test fails (the string compiles), but should it?
#throwsCompileError "fn(0,1,;)"
throwsCompileError "fn(0,1,;;)"
throwsCompileError "fn(0, 1;,)"
throwsCompileError "fn(,0)"
throwsCompileError "fn(;0)"
test "function invocation", ->
func = ->
return if true
eq undefined, func()
result = ("hello".slice) 3
ok result is 'lo'
test "And even with strange things like this:", ->
funcs = [((x) -> x), ((x) -> x * x)]
result = funcs[1] 5
ok result is 25
test "More fun with optional parens.", ->
fn = (arg) -> arg
ok fn(fn {prop: 101}).prop is 101
okFunc = (f) -> ok(f())
okFunc -> true
test "chained function calls", ->
nonce = {}
identityWrap = (x) ->
-> x
eq nonce, identityWrap(identityWrap(nonce))()()
eq nonce, (identityWrap identityWrap nonce)()()
test "Multi-blocks with optional parens.", ->
fn = (arg) -> arg
result = fn( ->
fn ->
"Wrapped"
)
ok result()() is 'Wrapped'
test "method calls", ->
fnId = (fn) -> -> fn.apply this, arguments
math = {
add: (a, b) -> a + b
anonymousAdd: (a, b) -> a + b
fastAdd: fnId (a, b) -> a + b
}
ok math.add(5, 5) is 10
ok math.anonymousAdd(10, 10) is 20
ok math.fastAdd(20, 20) is 40
test "Ensure that functions can have a trailing comma in their argument list", ->
mult = (x, mids..., y) ->
x *= n for n in mids
x *= y
#ok mult(1, 2,) is 2
#ok mult(1, 2, 3,) is 6
ok mult(10, (i for i in [1..6])...) is 7200
test "`@` and `this` should both be able to invoke a method", ->
nonce = {}
fn = (arg) -> eq nonce, arg
fn.withAt = -> @ nonce
fn.withThis = -> this nonce
fn.withAt()
fn.withThis()
test "Trying an implicit object call with a trailing function.", ->
a = null
meth = (arg, obj, func) -> a = [obj.a, arg, func()].join ' '
meth 'apple', b: 1, a: 13, ->
'orange'
ok a is '13 apple orange'
test "Ensure that empty functions don't return mistaken values.", ->
obj =
func: (@param, @rest...) ->
ok obj.func(101, 102, 103, 104) is undefined
ok obj.param is 101
ok obj.rest.join(' ') is '102 103 104'
test "Passing multiple functions without paren-wrapping is legal, and should compile.", ->
sum = (one, two) -> one() + two()
result = sum ->
7 + 9
, ->
1 + 3
ok result is 20
test "Implicit call with a trailing if statement as a param.", ->
func = -> arguments[1]
result = func 'one', if false then 100 else 13
ok result is 13
test "Test more function passing:", ->
sum = (one, two) -> one() + two()
result = sum( ->
1 + 2
, ->
2 + 1
)
ok result is 6
sum = (a, b) -> a + b
result = sum(1
, 2)
ok result is 3
test "Chained blocks, with proper indentation levels:", ->
counter =
results: []
tick: (func) ->
@results.push func()
this
counter
.tick ->
3
.tick ->
2
.tick ->
1
arrayEq [3,2,1], counter.results
test "This is a crazy one.", ->
x = (obj, func) -> func obj
ident = (x) -> x
result = x {one: ident 1}, (obj) ->
inner = ident(obj)
ident inner
ok result.one is 1
test "More paren compilation tests:", ->
reverse = (obj) -> obj.reverse()
ok reverse([1, 2].concat 3).join(' ') is '3 2 1'
test "Test for inline functions with parentheses and implicit calls.", ->
combine = (func, num) -> func() * num
result = combine (-> 1 + 2), 3
ok result is 9
test "Test for calls/parens/multiline-chains.", ->
f = (x) -> x
result = (f 1).toString()
.length
ok result is 1
test "Test implicit calls in functions in parens:", ->
result = ((val) ->
[].push val
val
)(10)
ok result is 10
test "Ensure that chained calls with indented implicit object literals below are alright.", ->
result = null
obj =
method: (val) -> this
second: (hash) -> result = hash.three
obj
.method(
101
).second(
one:
two: 2
three: 3
)
eq result, 3
test "Test newline-supressed call chains with nested functions.", ->
obj =
call: -> this
func = ->
obj
.call ->
one two
.call ->
three four
101
eq func(), 101
test "Implicit objects with number arguments.", ->
func = (x, y) -> y
obj =
prop: func "a", 1
ok obj.prop is 1
test "Non-spaced unary and binary operators should cause a function call.", ->
func = (val) -> val + 1
ok (func +5) is 6
ok (func -5) is -4
test "Prefix unary assignment operators are allowed in parenless calls.", ->
func = (val) -> val + 1
val = 5
ok (func --val) is 5
test "#855: execution context for `func arr...` should be `null`", ->
contextTest = -> eq @, if window? then window else global
array = []
contextTest array
contextTest.apply null, array
contextTest array...
test "#904: Destructuring function arguments with same-named variables in scope", ->
a = b = nonce = {}
fn = ([a,b]) -> {a:a,b:b}
result = fn([c={},d={}])
eq c, result.a
eq d, result.b
eq nonce, a
eq nonce, b
test "Simple Destructuring function arguments with same-named variables in scope", ->
x = 1
f = ([x]) -> x
eq f([2]), 2
eq x, 1
test "#4843: Bad output when assigning to @prop in destructuring assignment with defaults", ->
works = "maybe"
drinks = "beer"
class A
constructor: ({@works = 'no', @drinks = 'wine'}) ->
a = new A {works: 'yes', drinks: 'coffee'}
eq a.works, 'yes'
eq a.drinks, 'coffee'
test "caching base value", ->
obj =
index: 0
0: {method: -> this is obj[0]}
ok obj[obj.index++].method([]...)
test "passing splats to functions", ->
arrayEq [0..4], id id [0..4]...
fn = (a, b, c..., d) -> [a, b, c, d]
range = [0..3]
[first, second, others, last] = fn range..., 4, [5...8]...
eq 0, first
eq 1, second
arrayEq [2..6], others
eq 7, last
# Should not trigger implicit call, e.g. rest ... => rest(...)
arrayEq [0..4], id id [0..4] ...
fn = (a, b, c ..., d) -> [a, b, c, d]
range = [0..3]
[first, second, others, last] = fn range ..., 4, [5 ... 8] ...
eq 0, first
eq 1, second
arrayEq [2..6], others
eq 7, last
test "splat variables are local to the function", ->
outer = "x"
clobber = (avar, outer...) -> outer
clobber "foo", "bar"
eq "x", outer
test "Issue 4631: left and right spread dots with preceding space", ->
a = []
f = (a) -> a
eq yes, (f ...a) is (f ... a) is (f a...) is (f a ...) is f(a...) is f(...a) is f(a ...) is f(... a)
test "Issue 894: Splatting against constructor-chained functions.", ->
x = null
class Foo
bar: (y) -> x = y
new Foo().bar([101]...)
eq x, 101
test "Functions with splats being called with too few arguments.", ->
pen = null
method = (first, variable..., penultimate, ultimate) ->
pen = penultimate
method 1, 2, 3, 4, 5, 6, 7, 8, 9
ok pen is 8
method 1, 2, 3
ok pen is 2
method 1, 2
ok pen is 2
test "splats with super() within classes.", ->
class Parent
meth: (args...) ->
args
class Child extends Parent
meth: ->
nums = [3, 2, 1]
super nums...
ok (new Child).meth().join(' ') is '3 2 1'
# Should not trigger implicit call, e.g. rest ... => rest(...)
class Parent
meth: (args ...) ->
args
class Child extends Parent
meth: ->
nums = [3, 2, 1]
super nums ...
ok (new Child).meth().join(' ') is '3 2 1'
test "#1011: passing a splat to a method of a number", ->
eq '1011', 11.toString [2]...
eq '1011', (31).toString [3]...
eq '1011', 69.0.toString [4]...
eq '1011', (131.0).toString [5]...
# Should not trigger implicit call, e.g. rest ... => rest(...)
eq '1011', 11.toString [2] ...
eq '1011', (31).toString [3] ...
eq '1011', 69.0.toString [4] ...
eq '1011', (131.0).toString [5] ...
test "splats and the `new` operator: functions that return `null` should construct their instance", ->
args = []
child = new (constructor = -> null) args...
ok child instanceof constructor
# Should not trigger implicit call, e.g. rest ... => rest(...)
child = new (constructor = -> null) args ...
ok child instanceof constructor
test "splats and the `new` operator: functions that return functions should construct their return value", ->
args = []
fn = ->
child = new (constructor = -> fn) args...
ok child not instanceof constructor
eq fn, child
test "implicit return", ->
eq ok, new ->
ok
### Should `return` implicitly ###
### even with trailing comments. ###
test "implicit returns with multiple branches", ->
nonce = {}
fn = ->
if false
for a in b
return c if d
else
nonce
eq nonce, fn()
test "implicit returns with switches", ->
nonce = {}
fn = ->
switch nonce
when nonce then nonce
else return undefined
eq nonce, fn()
test "preserve context when generating closure wrappers for expression conversions", ->
nonce = {}
obj =
property: nonce
method: ->
this.result = if false
10
else
"a"
"b"
this.property
eq nonce, obj.method()
eq nonce, obj.property
test "don't wrap 'pure' statements in a closure", ->
nonce = {}
items = [0, 1, 2, 3, nonce, 4, 5]
fn = (items) ->
for item in items
return item if item is nonce
eq nonce, fn items
test "usage of `new` is careful about where the invocation parens end up", ->
eq 'object', typeof new try Array
eq 'object', typeof new do -> ->
a = b: ->
eq 'object', typeof new (do -> a).b
test "implicit call against control structures", ->
result = null
save = (obj) -> result = obj
save switch id false
when true
'true'
when false
'false'
eq result, 'false'
save if id false
'false'
else
'true'
eq result, 'true'
save unless id false
'true'
else
'false'
eq result, 'true'
save try
doesnt exist
catch error
'caught'
eq result, 'caught'
save try doesnt(exist) catch error then 'caught2'
eq result, 'caught2'
test "#1420: things like `(fn() ->)`; there are no words for this one", ->
fn = -> (f) -> f()
nonce = {}
eq nonce, (fn() -> nonce)
test "#1416: don't omit one 'new' when compiling 'new new'", ->
nonce = {}
obj = new new -> -> {prop: nonce}
eq obj.prop, nonce
test "#1416: don't omit one 'new' when compiling 'new new fn()()'", ->
nonce = {}
argNonceA = {}
argNonceB = {}
fn = (a) -> (b) -> {a, b, prop: nonce}
obj = new new fn(argNonceA)(argNonceB)
eq obj.prop, nonce
eq obj.a, argNonceA
eq obj.b, argNonceB
test "#1840: accessing the `prototype` after function invocation should compile", ->
doesNotThrowCompileError 'fn()::prop'
nonce = {}
class Test then id: nonce
dotAccess = -> Test::
protoAccess = -> Test
eq dotAccess().id, nonce
eq protoAccess()::id, nonce
test "#960: improved 'do'", ->
do (nonExistent = 'one') ->
eq nonExistent, 'one'
overridden = 1
do (overridden = 2) ->
eq overridden, 2
two = 2
do (one = 1, two, three = 3) ->
eq one, 1
eq two, 2
eq three, 3
ret = do func = (two) ->
eq two, 2
func
eq ret, func
test "#2617: implicit call before unrelated implicit object", ->
pass = ->
true
result = if pass 1
one: 1
eq result.one, 1
test "#2292, b: f (z),(x)", ->
f = (x, y) -> y
one = 1
two = 2
o = b: f (one),(two)
eq o.b, 2
test "#2297, Different behaviors on interpreting literal", ->
foo = (x, y) -> y
bar =
baz: foo 100, on
eq bar.baz, on
qux = (x) -> x
quux = qux
corge: foo 100, true
eq quux.corge, on
xyzzy =
e: 1
f: foo
a: 1
b: 2
,
one: 1
two: 2
three: 3
g:
a: 1
b: 2
c: foo 2,
one: 1
two: 2
three: 3
d: 3
four: 4
h: foo one: 1, two: 2, three: three: three: 3,
2
eq xyzzy.f.two, 2
eq xyzzy.g.c.three, 3
eq xyzzy.four, 4
eq xyzzy.h, 2
test "#2715, Chained implicit calls", ->
first = (x) -> x
second = (x, y) -> y
foo = first first
one: 1
eq foo.one, 1
bar = first second
one: 1, 2
eq bar, 2
baz = first second
one: 1,
2
eq baz, 2
test "Implicit calls and new", ->
first = (x) -> x
foo = (@x) ->
bar = first new foo first 1
eq bar.x, 1
third = (x, y, z) -> z
baz = first new foo new foo third
one: 1
two: 2
1
three: 3
2
eq baz.x.x.three, 3
test "Loose tokens inside of explicit call lists", ->
first = (x) -> x
second = (x, y) -> y
one = 1
foo = second( one
2)
eq foo, 2
bar = first( first
one: 1)
eq bar.one, 1
test "Non-callable literals shouldn't compile", ->
throwsCompileError '1(2)'
throwsCompileError '1 2'
throwsCompileError '/t/(2)'
throwsCompileError '/t/ 2'
throwsCompileError '///t///(2)'
throwsCompileError '///t/// 2'
throwsCompileError "''(2)"
throwsCompileError "'' 2"
throwsCompileError '""(2)'
throwsCompileError '"" 2'
throwsCompileError '""""""(2)'
throwsCompileError '"""""" 2'
throwsCompileError '{}(2)'
throwsCompileError '{} 2'
throwsCompileError '[](2)'
throwsCompileError '[] 2'
throwsCompileError '[2..9] 2'
throwsCompileError '[2..9](2)'
throwsCompileError '[1..10][2..9] 2'
throwsCompileError '[1..10][2..9](2)'
test "implicit invocation with implicit object literal", ->
f = (obj) -> eq 1, obj.a
f
a: 1
obj =
if f
a: 2
else
a: 1
eq 2, obj.a
f
"a": 1
obj =
if f
"a": 2
else
"a": 1
eq 2, obj.a
# #3935: Implicit call when the first key of an implicit object has interpolation.
a = 'a'
f
"#{a}": 1
obj =
if f
"#{a}": 2
else
"#{a}": 1
eq 2, obj.a
test "get and set can be used as function names when not ambiguous with `get`/`set` keywords", ->
get = (val) -> val
set = (val) -> val
eq 2, get(2)
eq 3, set(3)
eq 'a', get('a')
eq 'b', set('b')
eq 4, get 4
eq 5, set 5
eq 'c', get 'c'
eq 'd', set 'd'
@get = get
@set = set
eq 6, @get 6
eq 7, @set 7
get = ({val}) -> val
set = ({val}) -> val
eq 8, get({val: 8})
eq 9, set({val: 9})
eq 'e', get({val: 'e'})
eq 'f', set({val: 'f'})
eq 10, get {val: 10}
eq 11, set {val: 11}
eq 'g', get {val: 'g'}
eq 'h', set {val: 'h'}
test "get and set can be used as variable and property names", ->
get = 2
set = 3
eq 2, get
eq 3, set
{get} = {get: 4}
{set} = {set: 5}
eq 4, get
eq 5, set
test "get and set can be used as class method names", ->
class A
get: -> 2
set: -> 3
a = new A()
eq 2, a.get()
eq 3, a.set()
class B
@get = -> 4
@set = -> 5
eq 4, B.get()
eq 5, B.set()
test "#4524: functions named get or set can be used without parentheses when attached to an object", ->
obj =
get: (x) -> x + 2
set: (x) -> x + 3
class A
get: (x) -> x + 4
set: (x) -> x + 5
a = new A()
class B
get: (x) -> x.value + 6
set: (x) -> x.value + 7
b = new B()
eq 12, obj.get 10
eq 13, obj.set 10
eq 12, obj?.get 10
eq 13, obj?.set 10
eq 14, a.get 10
eq 15, a.set 10
@ten = 10
eq 12, obj.get @ten
eq 13, obj.set @ten
eq 14, a.get @ten
eq 15, a.set @ten
obj.obj = obj
eq 12, obj.obj.get @ten
eq 13, obj.obj.set @ten
eq 16, b.get value: 10
eq 17, b.set value: 10
eq 16, b.get value: @ten
eq 17, b.set value: @ten
test "#4836: functions named get or set can be used without parentheses when attached to this or @", ->
@get = (x) -> x + 2
@set = (x) -> x + 3
@a = 4
eq 12, this.get 10
eq 13, this.set 10
eq 12, this?.get 10
eq 13, this?.set 10
eq 6, this.get @a
eq 7, this.set @a
eq 6, this?.get @a
eq 7, this?.set @a
eq 12, @get 10
eq 13, @set 10
eq 12, @?.get 10
eq 13, @?.set 10
eq 6, @get @a
eq 7, @set @a
eq 6, @?.get @a
eq 7, @?.set @a
test "#4852: functions named get or set can be used without parentheses when attached to this or @, with an argument of an implicit object", ->
@get = ({ x }) -> x + 2
@set = ({ x }) -> x + 3
eq 12, @get x: 10
eq 13, @set x: 10
eq 12, @?.get x: 10
eq 13, @?.set x: 10
eq 12, this?.get x: 10
eq 13, this?.set x: 10
test "#4473: variable scope in chained calls", ->
obj =
foo: -> this
bar: (a) ->
a()
this
obj.foo(a = 1).bar(-> a = 2)
eq a, 2
obj.bar(-> b = 2).foo(b = 1)
eq b, 1
obj.foo(c = 1).bar(-> c = 2).foo(c = 3)
eq c, 3
obj.foo([d, e] = [1, 2]).bar(-> d = 4)
eq d, 4
obj.foo({f} = {f: 1}).bar(-> f = 5)
eq f, 5
test "#5052: implicit call of class with no body", ->
doesNotThrowCompileError 'f class'
doesNotThrowCompileError 'f class A'
doesNotThrowCompileError 'f class A extends B'
f = (args...) -> args
a = 1
[klass, shouldBeA] = f class A, a
eq shouldBeA, a
[shouldBeA] = f a, class A
eq shouldBeA, a
[obj, klass, shouldBeA] =
f
b: 1
class A
a
eq shouldBeA, a
|