Skip to content

Commit 590cd3f

Browse files
committed
Comment Assign::compilePatternMatch a bit
1 parent 3d0d04e commit 590cd3f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/nodes.coffee

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,23 @@ exports.Assign = class Assign extends Base
14581458
top = o.level is LEVEL_TOP
14591459
{value} = this
14601460
{objects} = @variable.base
1461+
1462+
# Special-case for `{} = a` and `[] = a` (empty patterns). Compile to simply
1463+
# `a`.
14611464
unless olen = objects.length
14621465
code = value.compileToFragments o
14631466
return if o.level >= LEVEL_OP then @wrapInBraces code else code
1467+
14641468
[obj] = objects
1469+
1470+
# Disallow `[...] = a` for some reason. (Could be equivalent to `[] = a`?)
14651471
if olen is 1 and obj instanceof Expansion
14661472
obj.error 'Destructuring assignment has no target'
1473+
14671474
isObject = @variable.isObject()
1475+
1476+
# Special case for when there's only one thing destructured off of
1477+
# something. `{b} = a` and `[b] = a`.
14681478
if top and olen is 1 and obj not instanceof Splat
14691479
# Pick the property straight off the value when there’s just one to pick
14701480
# (no need to cache the value into a variable).
@@ -1495,15 +1505,30 @@ exports.Assign = class Assign extends Base
14951505
obj.error message if message
14961506
value = new Op '?', value, defaultValue if defaultValue
14971507
return new Assign(obj, value, null, param: @param).compileToFragments o, LEVEL_TOP
1508+
14981509
vvar = value.compileToFragments o, LEVEL_LIST
14991510
vvarText = fragmentsToText vvar
15001511
assigns = []
15011512
expandedIdx = false
1502-
# Make vvar into a simple variable if it isn't already.
1513+
1514+
# At this point, there are several things to destructure. So the `fn()` in
1515+
# `{a, b} = fn()` must be cached, for example. Make vvar into a simple
1516+
# variable if it isn't already.
15031517
if value.unwrap() not instanceof IdentifierLiteral or @variable.assigns(vvarText)
15041518
assigns.push [@makeCode("#{ ref = o.scope.freeVariable 'ref' } = "), vvar...]
15051519
vvar = [@makeCode ref]
15061520
vvarText = ref
1521+
1522+
# And here comes the big loop that handles all of these cases:
1523+
# `[a, b] = c`
1524+
# `[a..., b] = c`
1525+
# `[..., a, b] = c`
1526+
# `[@a, b] = c`
1527+
# `[a = 1, b] = c`
1528+
# `{a, b} = c`
1529+
# `{@a, b} = c`
1530+
# `{a = 1, b} = c`
1531+
# etc.
15071532
for obj, i in objects
15081533
idx = i
15091534
if not expandedIdx and obj instanceof Splat
@@ -1558,6 +1583,7 @@ exports.Assign = class Assign extends Base
15581583
message = isUnassignable name
15591584
obj.error message if message
15601585
assigns.push new Assign(obj, val, null, param: @param, subpattern: yes).compileToFragments o, LEVEL_LIST
1586+
15611587
assigns.push vvar unless top or @subpattern
15621588
fragments = @joinFragmentArrays assigns, ', '
15631589
if o.level < LEVEL_LIST then fragments else @wrapInBraces fragments

0 commit comments

Comments
 (0)