diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index ffac6e1f07..f7d8e8f568 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -794,20 +794,31 @@ export default class Component { } let deep = false; - let names: string[] | undefined; + let names: string[] = []; if (node.type === 'AssignmentExpression') { - deep = node.left.type === 'MemberExpression'; - names = deep - ? [get_object(node.left).name] - : extract_names(node.left); + if (node.left.type === 'ArrayPattern') { + walk(node.left, { + enter(node: Node, parent: Node) { + if (node.type === 'Identifier' && + parent.type !== 'MemberExpression' && + (parent.type !== 'AssignmentPattern' || parent.right !== node)) { + names.push(node.name); + } + } + }); + } else { + deep = node.left.type === 'MemberExpression'; + names = deep + ? [get_object(node.left).name] + : extract_names(node.left); + } } else if (node.type === 'UpdateExpression') { deep = node.argument.type === 'MemberExpression'; const { name } = get_object(node.argument); - names = [name]; + names.push(name); } - - if (names) { + if (names.length > 0) { names.forEach(name => { let current_scope = scope; let declaration; diff --git a/test/runtime/samples/assignment-to-const1/_config.js b/test/runtime/samples/assignment-to-const1/_config.js new file mode 100644 index 0000000000..2fd0447458 --- /dev/null +++ b/test/runtime/samples/assignment-to-const1/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

2, 1

' +}; diff --git a/test/runtime/samples/assignment-to-const1/main.svelte b/test/runtime/samples/assignment-to-const1/main.svelte new file mode 100644 index 0000000000..f221249193 --- /dev/null +++ b/test/runtime/samples/assignment-to-const1/main.svelte @@ -0,0 +1,6 @@ + + +

{arr[0]}, {arr[1]}

diff --git a/test/runtime/samples/assignment-to-const2/_config.js b/test/runtime/samples/assignment-to-const2/_config.js new file mode 100644 index 0000000000..9802ca2c44 --- /dev/null +++ b/test/runtime/samples/assignment-to-const2/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

[{"a":2},100]

' +}; diff --git a/test/runtime/samples/assignment-to-const2/main.svelte b/test/runtime/samples/assignment-to-const2/main.svelte new file mode 100644 index 0000000000..c805e4b589 --- /dev/null +++ b/test/runtime/samples/assignment-to-const2/main.svelte @@ -0,0 +1,7 @@ + + +

{JSON.stringify(arr)}

diff --git a/test/validator/samples/assignment-to-const-5/errors.json b/test/validator/samples/assignment-to-const-5/errors.json new file mode 100644 index 0000000000..21de70863c --- /dev/null +++ b/test/validator/samples/assignment-to-const-5/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 3, + "column": 1, + "character": 31 + }, + "end": { + "line": 3, + "column": 33, + "character": 63 + }, + "pos": 31 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-5/input.svelte b/test/validator/samples/assignment-to-const-5/input.svelte new file mode 100644 index 0000000000..76907b94be --- /dev/null +++ b/test/validator/samples/assignment-to-const-5/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-6/errors.json b/test/validator/samples/assignment-to-const-6/errors.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/test/validator/samples/assignment-to-const-6/errors.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-6/input.svelte b/test/validator/samples/assignment-to-const-6/input.svelte new file mode 100644 index 0000000000..8da59b261e --- /dev/null +++ b/test/validator/samples/assignment-to-const-6/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-7/errors.json b/test/validator/samples/assignment-to-const-7/errors.json new file mode 100644 index 0000000000..34c88bebf6 --- /dev/null +++ b/test/validator/samples/assignment-to-const-7/errors.json @@ -0,0 +1,17 @@ +[ + { + "code": "assignment-to-const", + "message": "You are assigning to a const", + "start": { + "line": 3, + "column": 1, + "character": 43 + }, + "end": { + "line": 3, + "column": 42, + "character": 84 + }, + "pos": 43 + } +] \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-7/input.svelte b/test/validator/samples/assignment-to-const-7/input.svelte new file mode 100644 index 0000000000..ed7b37ae26 --- /dev/null +++ b/test/validator/samples/assignment-to-const-7/input.svelte @@ -0,0 +1,6 @@ + + +{arr} \ No newline at end of file diff --git a/test/validator/samples/assignment-to-const-8/errors.json b/test/validator/samples/assignment-to-const-8/errors.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/test/validator/samples/assignment-to-const-8/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/assignment-to-const-8/input.svelte b/test/validator/samples/assignment-to-const-8/input.svelte new file mode 100644 index 0000000000..8af787f4f0 --- /dev/null +++ b/test/validator/samples/assignment-to-const-8/input.svelte @@ -0,0 +1,6 @@ + + +{arr}