From f588ecb28834eeb9266a3bb03266f82709982278 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Tue, 25 Aug 2015 19:19:21 +0200 Subject: [PATCH] Fix #4070: Improve error message for lone expansion --- lib/coffee-script/nodes.js | 6 +++++- src/nodes.coffee | 5 ++++- test/error_messages.coffee | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 4b940dd7..e71e0881 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1725,8 +1725,12 @@ return code; } } + obj = objects[0]; + if (olen === 1 && obj instanceof Expansion) { + obj.error('Destructuring assignment has no target'); + } isObject = this.variable.isObject(); - if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) { + if (top && olen === 1 && !(obj instanceof Splat)) { if (obj instanceof Assign) { ref3 = obj, (ref4 = ref3.variable, idx = ref4.base), obj = ref3.value; } else { diff --git a/src/nodes.coffee b/src/nodes.coffee index 42f0fd58..39b8d268 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1234,8 +1234,11 @@ exports.Assign = class Assign extends Base unless olen = objects.length code = value.compileToFragments o return if o.level >= LEVEL_OP then @wrapInBraces code else code + [obj] = objects + if olen is 1 and obj instanceof Expansion + obj.error 'Destructuring assignment has no target' isObject = @variable.isObject() - if top and olen is 1 and (obj = objects[0]) not instanceof Splat + if top and olen is 1 and obj not instanceof Splat # Unroll simplest cases: `{v} = x` -> `v = x.v` if obj instanceof Assign {variable: {base: idx}, value: obj} = obj diff --git a/test/error_messages.coffee b/test/error_messages.coffee index 50b60a4d..75fb2531 100644 --- a/test/error_messages.coffee +++ b/test/error_messages.coffee @@ -776,3 +776,19 @@ test "invalid object keys", -> @a: 1 ^^ ''' + +test "#4070: lone expansion", -> + assertErrorFormat ''' + [...] = a + ''', ''' + [stdin]:1:2: error: Destructuring assignment has no target + [...] = a + ^^^ + ''' + assertErrorFormat ''' + [ ..., ] = a + ''', ''' + [stdin]:1:3: error: Destructuring assignment has no target + [ ..., ] = a + ^^^ + '''