From 31de60ece661516fa8dd210c51c2891313f40f57 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Fri, 2 Feb 2018 00:20:03 -0600 Subject: [PATCH] Add destructured context container to usedContexts Fixes #1139 --- src/generators/Generator.ts | 10 ++++-- .../_config.js | 32 +++++++++++++++++ .../main.html | 36 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 test/runtime/samples/event-handler-custom-each-destructured/_config.js create mode 100644 test/runtime/samples/event-handler-custom-each-destructured/main.html diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index ab74b40c0df3..e43dcee95a9a 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); @@ -774,7 +781,6 @@ export default class Generator { if (node.destructuredContexts) { for (let i = 0; i < node.destructuredContexts.length; i += 1) { const name = node.destructuredContexts[i]; - const value = `${node.context}[${i}]`; contextDependencies.set(name, node.metadata.dependencies); } 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 000000000000..9373d4dfee25 --- /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 000000000000..6c5f24455dac --- /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}}

+ +