Skip to content

Commit e121eb0

Browse files
authored
[feat] support for new const tag (#161)
1 parent 5f85c67 commit e121eb0

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/preprocess.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ export const preprocess = text => {
172172
} else if (node.type === 'Element' || node.type === 'InlineComponent' || node.type === 'SlotTemplate') {
173173
node.attributes.forEach(node => node.type === 'Let' && find_contextual_names(compiler, node.expression || node.name));
174174
}
175+
if (Array.isArray(node.children)) {
176+
node.children.forEach(node => node.type === 'ConstTag' && find_contextual_names(compiler, node.expression.left.name));
177+
}
175178
if (contextual_names.length) {
176179
nodes_with_contextual_scope.add(node);
177180
block.transformed_code += `{let ${contextual_names.map(name => `${name}=0`).join(',')};`;

test/samples/const-tag/.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
rules: {
3+
'no-undef': 'error',
4+
},
5+
settings: {
6+
'svelte3/ignore-warnings': ({ code }) => code === 'missing-declaration',
7+
},
8+
};

test/samples/const-tag/Input.svelte

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script>
2+
export let boxes;
3+
let p;
4+
</script>
5+
6+
{#each boxes as box}
7+
{@const area = box.width * box.height}
8+
{box.width} * {box.height} = {area}
9+
{/each}
10+
11+
{area} <!-- undef -->
12+
13+
{#await p}
14+
wait
15+
{:then}
16+
{@const box = boxes[0]}
17+
{box.width}
18+
{/await}
19+
20+
{#await p then}
21+
then
22+
{:catch}
23+
{@const box = boxes[0]}
24+
{box.width}
25+
{/await}
26+
27+
<Component>
28+
{@const box = boxes[0]}
29+
{box.width}
30+
</Component>
31+
32+
<Component>
33+
<svelte:fragment slot="foo">
34+
{@const box = boxes[0]}
35+
{box.width}
36+
</svelte:fragment>
37+
</Component>

test/samples/const-tag/expected.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"ruleId": "no-undef",
4+
"line": 11,
5+
"column": 2,
6+
"endLine": 11,
7+
"endColumn": 6
8+
}
9+
]

0 commit comments

Comments
 (0)