fix: use immutable visited Set copy in resolveNestedTemplates#174
Open
JuliaKalder wants to merge 1 commit into
Open
fix: use immutable visited Set copy in resolveNestedTemplates#174JuliaKalder wants to merge 1 commit into
JuliaKalder wants to merge 1 commit into
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
visited.add()/visited.delete()pattern inresolveNestedTemplateswith an immutable copy:new Set([...visited, nestedTemplate.id])passed to the recursive call.visited.delete()call entirely — no mutation of the caller's Set occurs at any point.visitedparameter to state it is treated as immutable.Why
The original code mutated the caller's
visitedSet around each recursive call. If the recursive call threw (e.g., due to a circular-reference error deeper in the chain), thevisited.delete()cleanup was skipped, leaving the Set in a dirty state for any subsequent sibling iterations in the same call frame.The immutable-copy pattern eliminates the mutation entirely: each recursive frame gets its own snapshot of the ancestor set, and a throw propagates cleanly without any side-effects on the parent frame's Set.
Fixes #164
Test plan
npm test— 96/96 pass, including theresolveNestedTemplatescircular-reference suite)