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
|
# Basic chained function calls.
identityWrap = (x) -> -> x
eq true, identityWrap(identityWrap(true))()()
# Chained accesses split on dot + newline, backwards and forwards.
$ = {}
$.self = $
$.begin = $.do = $.end = -> this
ok $.
begin().
do().
self.
self.
end().
self
# Leading dots introduce dummy blocks and/or close implicit indentations.
r = $.self
.begin()
.do()
.do -> 0; 1
.begin()
.do ->
2
3
.end 4, ->
.end()
eq r, $
# Paren-free method chains
eq \56, if \0 is 10.toString 2 .slice 3
\4 .replace /\d/ 5 .concat 6
eq 1,
Array 0, 1 ?.1
# Ensure that indented array literals don't trigger whitespace rewriting.
eq 2 [
[[[[],
[]],
[[]]]],
[]].length
eq 'Hello', String(
"""
Hello
""")
eq msg = 'special accessors should also continue lines',
msg
.~toString
?.valueOf()()
eq 0,
[0]
?.0
# Bracketless Accesses
a = [0]
eq 0, a.0
eq 0, a."0"
eq 0, a."#{0}"
eq 0, a.(0)
eq 0, [a].0.0
eq a.* = 1, a.1
eq '0'.0, '10'.1
eq 1, [-> it]. 0 1
eq 1, [-> it].'0' 1
# `prototype` shorthand, `constructor`
eq Array::toString, Array.prototype.toString
eq 12345@@toString, 123@@toString
eq 0 (:::0)::
eq 0 (@@:0)@@
# Length Star
a = [[1], 2, 3]
eq 3, a[*-1]
eq 1, a[0][*-*]
eq 2, a[--*-1]
eq 2, a.length
compileThrows 'stray star' 1 '[*-1]'
# Binding Access
parent =
child:
method: -> @member
member: 42
eq 42, do(0; parent.child.~method)
eq 42, do(0; parent.child~"me#{'th'}od")
eq 42, parent.child. ~ [\method] null
eq 42, parent.child.~{method}.method!
eq 42, parent.child.~{renamed:method}.renamed!
eq "42" 42~toString!
compileThrows 'invalid assign' 1 'o~m=g'
# Dots have to workaround syntax error when accessing a simple number.
eq '0 .go;' , LiveScript.compile '0.go', {+bare,-header}
# Brackets don't.
eq "0['do'];", LiveScript.compile '0.do', {+bare,-header}
# Array/Object Slice
eq '2,3', '' + [3,2][1,0]
eq '2,3', '' + [0,1,2,3][*-2,*-1]
eq '2,3', '' + {2,3}<[2 3]>
eq '-Infinity,Infinity', '' + Number[\NEGATIVE_INFINITY, \POSITIVE_INFINITY]
k = \y
o = {\x \y \z}{x, (k[0]), 2: z}
eq \x o.x
eq \y o.y
eq \z o.2
a = [0 1 2][[0 1], {2}]
eq '0,1' ''+a.0
eq '0,1' ''+a[...0]
eq 2 a.1.2
compileThrows 'calling a slice' 1 'a{0}()'
compileThrows 'empty slice' 1 'o{}'
compileThrows 'empty slice' 1 'o[,,]'
compile-throws 'value in object slice is not a key' 1 'a{b: x + y}'
compile-throws 'value in object slice is not a key' 1 'a{b.c}'
compile-throws 'value in object slice is not a key' 1 'a{b: c.d}'
compile-throws 'value in object slice is not a key' 1 'a{b: c.d ? 0}'
# Don't permit the label syntax outside of patterns
compile-throws 'unexpected label' 1 'foo{[0 1]:k}'
compile-throws 'unexpected label' 1 'foo{k0:[0 1]:k1}'
if 0 then @front{ne,ss}
x = 3
y = 1
l = [1 to 5]
eq '2,3,4' "#{ l[1 to x] }"
eq '2,3' "#{ l[1 til x] }"
eq '2,3,4' "#{ l[y to 3] }"
eq '2,3' "#{ l[y til 3] }"
eq '2,3,4' "#{ l[y to x] }"
eq '2,3' "#{ l[y til x] }"
eq '3,4,5' "#{ l[2 til] }"
eq '1,2' "#{ l[til 2] }"
z = 2
eq '3,4,5' "#{ l[z til] }"
eq '1,2' "#{ l[til z] }"
eq '1,2,3,4,5' "#{ l[to] }"
eq '1,2,3' "#{ l[til -2] }"
eq '2,3' "#{ l[1 til -2] }"
eq '1,2,3,4,5' "#{ l[to -1] }"
eq '1,2,3,4' "#{ l[til -1] }"
# splice
l = [1 to 5]
x = 3
eq '8,9' "#{ l[2 to x] = [8 9] }"
eq '1,2,8,9,5' "#{ l }"
y = -> 2
l = [1 to 5]
eq '8,9' "#{ l[y! til 4] = [8 9] }"
eq '1,2,8,9,5' "#{ l }"
l = [1 to 5]
eq '8,9' "#{ l[2 to 3] = [8 9] }"
eq '1,2,8,9,5' "#{ l }"
# `BY` keyword in Slices
[x, f, list] = [2, (/2), [1 to 6]]
eq '2,4' String list[1 til 4 by 2]
eq '3,4,5' String list[x to 4 by 1]
eq '2,4' String list[1 til 4 by x]
eq '1,3' String list[to x by f 4][to x-1 by 1]
# Automatic Dot Insertion
eq @toString, @\toString
eq @toString, @"to#{\S}tring"
{ok}\ok 1
[ok]0 1
eq 0 [[0]]0.0
eq null [null]?0
eq void {0}?1?p
v = void
x = y: {\z}
eq void v?=y.z
eq void v
eq \z x?=y.z
eq \z x
# Semiautovivification
o = {}
o.{}a.[]b .push 0 1
o.a{}c[]d .push 2 3
o?.{}a?.[]b?{}e?{}f.4 = 5
eq '0,1' ''+o.a.b
eq '2,3' ''+o.a.c.d
eq 5 o.a.b.e.f.4
eq '0,1' ''+ (o[]g++o.a.b)
eq '0,1' ''+ (o[]h ++ o.a.b)
eq '0,1' ''+ (o[]h ++ [0 1])
a = []
eq 2 a{}[0, 1].length
eq \Object typeof! a.0
eq \Object typeof! a.1
# [LiveScript#1028](https://github.com/gkz/LiveScript/issues/1028)
a = {}
a[]b ++= [1 2]
eq ''+a.b, '1,2'
a[]b ++= [3 4]
eq ''+a.b, '1,2,3,4'
a = {}
counter = 0
get-key = -> counter++; \foo
a[][get-key!] ++= [1 2]
eq ''+a.foo, '1,2'
eq counter, 1
a = {}
a[]b = [a.b.length, a.b.length]
eq ''+a.b, '0,0'
# Bang Call
eq '' String!
(-> ok true)!
f = -> null
eq null f?!
eq void f!?p
f = void
eq void f?!
# Keyword literals in object slices
keywords = arguments: 1 eval: 2 void: 3 on: 4 debugger: 5
eq 1 keywords{arguments}arguments
eq 2 keywords{eval}eval
eq 3 keywords{void}void
eq 4 keywords{on}on
eq 5 keywords{debugger}debugger
# Existence checks and slices
a = null
eq void a?{x}
eq void a?[0 1]
a?{x} = a
++a?{x}[\x]y
delete a?{x}[\x]y
delete! a?{x}[\x]y
eq void a?[0 1] <<< {2}
a = {}
b = x: y: 1
a?{x} = b
eq b.x, a.x
++a?{x}[\x]y
eq 2 delete a?{x}[\x]y
eq void b.x.y
b.x.y = 2
eq true delete! a?{x}[\x]y
eq void b.x.y
a = [4 3]
eq '4,3,2' "#{a?[0 1] <<< {2}}"
# Assigning through computed properties in slices
p = -> \q
o = q: r: 1
o{(p!)}q.r = 1
eq 1 o.q.r
|