-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix reactive declaration cycle detection + clearer error on cycle
- fixes #3459 - uses DFS traversal to inspect reactive declarations for cycles - returns the cycle detected (e.g.; `a → b → a`) for error messaging
- Loading branch information
1 parent
cc10714
commit ee8825d
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export default function checkGraphForCycles(edges: Array<[any, any]>): any[] { | ||
const graph: Map<any, any[]> = edges.reduce((g, edge) => { | ||
const [u, v] = edge; | ||
if (!g.has(u)) g.set(u, []); | ||
if (!g.has(v)) g.set(v, []); | ||
g.get(u).push(v); | ||
return g; | ||
}, new Map()); | ||
|
||
const visited = new Set(); | ||
const onStack = new Set(); | ||
const cycles = []; | ||
|
||
function visit (v) { | ||
visited.add(v); | ||
onStack.add(v); | ||
|
||
graph.get(v).forEach(w => { | ||
if (!visited.has(w)) { | ||
visit(w); | ||
} else if (onStack.has(w)) { | ||
cycles.push([...onStack, w]); | ||
} | ||
}); | ||
|
||
onStack.delete(v); | ||
} | ||
|
||
graph.forEach((_, v) => { | ||
if (!visited.has(v)) { | ||
visit(v); | ||
} | ||
}); | ||
|
||
return cycles[0]; | ||
} |
11 changes: 11 additions & 0 deletions
11
test/runtime/samples/reactive-values-non-cyclical-declaration-order-independent/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default { | ||
html: ` | ||
<p>2+2=4</p> | ||
`, | ||
|
||
test({ assert, target }) { | ||
assert.htmlEqual(target.innerHTML, ` | ||
<p>2+2=4</p> | ||
`); | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
test/runtime/samples/reactive-values-non-cyclical-declaration-order-independent/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
$: c = a + b; | ||
$: a = 2; | ||
$: b = a; | ||
</script> | ||
|
||
<p>{a}+{b}={c}</p> |
4 changes: 2 additions & 2 deletions
4
test/validator/samples/reactive-declaration-cyclical/errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[{ | ||
"message": "Cyclical dependency detected", | ||
"message": "Cyclical dependency detected: a → b → a", | ||
"code": "cyclical-reactive-declaration", | ||
"start": { "line": 5, "column": 1, "character": 35 }, | ||
"end": { "line": 5, "column": 14, "character": 48 }, | ||
"pos": 35 | ||
}] | ||
}] |