const tag cyclic validation (now runtime, based because of new reactivity system)

pull/10714/head
Simon Holthausen 3 years ago
parent f8eb0d16d6
commit e00f920cf6

@ -461,10 +461,6 @@ const errors = {
// message:
// 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
// },
// cyclical_const_tags: /** @param {string[]} cycle */ (cycle) => ({
// code: 'cyclical-const-tags',
// message: `Cyclical dependency detected: ${cycle.join(' → ')}`
// }),
// invalid_var_declaration: {
// code: 'invalid_var_declaration',
// message: '"var" scope should not extend outside the reactive block'

@ -1800,6 +1800,12 @@ export const template_visitors = {
)
)
);
// we need to eagerly evaluate the expression in order to hit any
// 'Cannot access x before initialization' errors
if (state.options.dev) {
state.init.push(b.stmt(b.call('$.get', declaration.id)));
}
} else {
const identifiers = extract_identifiers(declaration.id);
const tmp = b.id(state.scope.generate('computed_const'));
@ -1829,6 +1835,12 @@ export const template_visitors = {
b.const(tmp, b.call(state.options.runes ? '$.derived' : '$.derived_safe_equal', fn))
);
// we need to eagerly evaluate the expression in order to hit any
// 'Cannot access x before initialization' errors
if (state.options.dev) {
state.init.push(b.stmt(b.call('$.get', tmp)));
}
for (const node of identifiers) {
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(node.name));
binding.expression = b.member(b.call('$.get', tmp), node);

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
error: "Cannot access 'c' before initialization"
});

@ -1,8 +1,8 @@
<script>
export let array;
export let array = [1];
</script>
{#each array as a}
{@const b = a + c}
{@const c = b + a}
{/each}
{/each}

@ -1,3 +0,0 @@
import { test } from '../../test';
export default test({ skip: true });

@ -1,8 +0,0 @@
[
{
"code": "cyclical-const-tags",
"message": "Cyclical dependency detected: b → c → b",
"start": { "line": 6, "column": 2 },
"end": { "line": 6, "column": 20 }
}
]
Loading…
Cancel
Save