diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index ab74b40c0d..1f48424c95 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -244,13 +244,20 @@ export default class Generator { } else if (contexts.has(name)) { const contextName = contexts.get(name); if (contextName !== name) { - // this is true for 'reserved' names like `state` and `component` + // this is true for 'reserved' names like `state` and `component`, + // also destructured contexts code.overwrite( node.start, node.start + name.length, contextName, { storeName: true, contentOnly: false } ); + + const destructuredName = contextName.replace(/\[\d+\]/, ''); + if (destructuredName !== contextName) { + // so that hoisting the context works correctly + usedContexts.add(destructuredName); + } } usedContexts.add(name); @@ -772,12 +779,9 @@ export default class Generator { contextDependencies.set(node.context, node.metadata.dependencies); if (node.destructuredContexts) { - for (let i = 0; i < node.destructuredContexts.length; i += 1) { - const name = node.destructuredContexts[i]; - const value = `${node.context}[${i}]`; - + node.destructuredContexts.forEach((name: string) => { contextDependencies.set(name, node.metadata.dependencies); - } + }); } contextDependenciesStack.push(contextDependencies); diff --git a/test/runtime/samples/event-handler-custom-each-destructured/_config.js b/test/runtime/samples/event-handler-custom-each-destructured/_config.js new file mode 100644 index 0000000000..9373d4dfee --- /dev/null +++ b/test/runtime/samples/event-handler-custom-each-destructured/_config.js @@ -0,0 +1,32 @@ +export default { + html: ` + + + + +
first:
+second:
+ `, + + test ( assert, component, target, window ) { + const event = new window.MouseEvent( 'click' ); + + const buttons = target.querySelectorAll( 'button' ); + + buttons[1].dispatchEvent( event ); + + assert.htmlEqual( target.innerHTML, ` + + + + +first: 1
+second: bar
+ ` ); + + assert.equal( component.get( 'first' ), '1' ); + assert.equal( component.get( 'second' ), 'bar' ); + + component.destroy(); + } +}; diff --git a/test/runtime/samples/event-handler-custom-each-destructured/main.html b/test/runtime/samples/event-handler-custom-each-destructured/main.html new file mode 100644 index 0000000000..6c5f24455d --- /dev/null +++ b/test/runtime/samples/event-handler-custom-each-destructured/main.html @@ -0,0 +1,36 @@ +{{#each items as [item0, item1]}} + +{{/each}} + +first: {{first}}
+second: {{second}}
+ +