Skip to content

Commit 00e394e

Browse files
authored
[fix] fix crash when using arrow functions in {@const} (#7144)
1 parent 5ccfc3c commit 00e394e

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/compiler/compile/nodes/shared/Expression.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ export default class Expression {
253253

254254
const declaration = b`const ${id} = ${node}`;
255255

256-
if (dependencies.size === 0 && contextual_dependencies.size === 0) {
256+
if (owner.type === 'ConstTag') {
257+
walk(node, {
258+
enter(node: Node) {
259+
if (node.type === 'Identifier') {
260+
this.replace(block.renderer.reference(node, ctx));
261+
}
262+
}
263+
});
264+
} else if (dependencies.size === 0 && contextual_dependencies.size === 0) {
257265
// we can hoist this out of the component completely
258266
component.fully_hoisted.push(declaration);
259267

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export default {
2+
html: `
3+
<p>#FF0000</p>
4+
<p>#00FF00</p>
5+
<p>#0000FF</p>
6+
`,
7+
async test({ component, target, assert }) {
8+
component.constant = 20;
9+
10+
assert.htmlEqual(target.innerHTML, `
11+
<p>#FF0000</p>
12+
<p>#00FF00</p>
13+
<p>#0000FF</p>
14+
`);
15+
16+
component.tags = [
17+
{
18+
name: 'Red',
19+
color: '#FF0000'
20+
},
21+
{
22+
name: 'Green',
23+
color: '#00FF00'
24+
},
25+
{
26+
name: 'Blue',
27+
color: '#0000FF'
28+
},
29+
{
30+
name: 'Black',
31+
color: '#000000'
32+
},
33+
{
34+
name: 'White',
35+
color: '#FFFFFF'
36+
}
37+
];
38+
39+
assert.htmlEqual(target.innerHTML, `
40+
<p>#FF0000</p>
41+
<p>#00FF00</p>
42+
<p>#0000FF</p>
43+
<p>#000000</p>
44+
<p>#FFFFFF</p>
45+
`);
46+
}
47+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script>
2+
export let tags = [
3+
{
4+
name: 'Red',
5+
color: '#FF0000'
6+
},
7+
{
8+
name: 'Green',
9+
color: '#00FF00'
10+
},
11+
{
12+
name: 'Blue',
13+
color: '#0000FF'
14+
}
15+
];
16+
</script>
17+
18+
{#each tags as tag}
19+
{@const tagColor = tags.find(t => t.name === tag.name).color}
20+
<p>{tagColor}</p>
21+
{/each}

0 commit comments

Comments
 (0)