Skip to content

Commit b8e30b0

Browse files
committed
support unary operator spread over objects
1 parent 24b2f71 commit b8e30b0

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

lib/ast.js

Lines changed: 22 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast.ls

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,19 +1241,23 @@ class exports.Unary extends Node
12411241
if o.level < LEVEL_CALL then sn(this, ...code) else sn(this, "(", ...code, ")")
12421242
12431243
# `^delete ...o[p, ...q]` => `[^delete o[p], ...^delete o[q]]`
1244+
# `^delete ...o{p, ...q}` => `{p: ^delete o[p], ...^delete o[q]}`
12441245
compile-spread: (o) ->
12451246
{it} = this
12461247
ops = [this]
12471248
while it instanceof constructor, it.=it then ops.push it
12481249
return '' unless it instanceof Splat
1249-
and it.=it.expand-slice(o)unwrap! instanceof Arr
1250+
and it.=it.expand-slice(o)unwrap! instanceof List
1251+
ob = it instanceof Obj
12501252
them = it.items
12511253
for node, i in them
12521254
node.=it if sp = node instanceof Splat
1255+
node.=val if ob and not sp
12531256
for op in ops by -1 then node = constructor op.op, node, op.post
1254-
them[i] = if sp then lat = Splat node else node
1257+
node = lat = Splat node if sp
1258+
if ob and not sp then them[i].val = node else them[i] = node
12551259
if not lat and (@void or not o.level)
1256-
it = Block(them) <<< {@front, +void}
1260+
it = Block(if ob then [..val for them] else them) <<< {@front, +void}
12571261
it.compile o, LEVEL_PAREN
12581262
12591263
# `v = delete o.k`

test/operator.ls

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ eq 3, a.2
257257
fn = null
258258
eq void fn? do then 4;5
259259

260+
loaders =
261+
a: -> 1
262+
b: -> 2
263+
more: -> c: 3, d: 4
264+
x = do ...loaders{a, b, ...more}
265+
deep-equal {a: 1 b: 2 c: 3 d: 4} x
266+
260267

261268
### `import`
262269
x = 'xx'
@@ -371,6 +378,11 @@ a = delete a.0
371378
eq 1 a
372379
eq 0 b.0
373380

381+
x = a: 1 b: 2 c: 3
382+
y = delete ...x{a, z: b}
383+
deep-equal {c: 3} x
384+
deep-equal {a: 1, z: 2} y
385+
374386
### `jsdelete`
375387

376388
x =
@@ -379,6 +391,13 @@ x =
379391
ok delete! x.1
380392
ok not delete! Math.PI
381393

394+
x = a: 1 b: 2 c: 3
395+
Object.defineProperty x, \d, writable: false, enumerable: true
396+
deep-equal {+a, +b} delete! ...x{a, b}
397+
deep-equal {c: 3 d: undefined} x
398+
deep-equal {+c, -d} delete! ...x{c, d}
399+
deep-equal {d: undefined} x
400+
382401

383402
### [[Class]] sniffing
384403
eq \RegExp typeof! /^/
@@ -496,6 +515,16 @@ deep-equal [1 2] x
496515
eq 2 a
497516
eq 3 b
498517

518+
o = a: 1 b: 2 c: 3
519+
x = ...o{a, b}++
520+
deep-equal {a: 1 b: 2} x
521+
deep-equal {a: 2 b: 3 c: 3} o
522+
523+
o = a: 1 b: 2 c: 3
524+
x = ++...o{a, b}
525+
deep-equal {a: 2 b: 3} x
526+
deep-equal {a: 2 b: 3 c: 3} o
527+
499528

500529
### Overloaded
501530
a = b = [0 1]

0 commit comments

Comments
 (0)