fix(transform): avoid cycles in transform cache invalidateIfChanged method
#148
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.
Motivation
This PR fixes a "RangeError: Maximum call stack size exceeded" error from transform's
cache.tsinvalidateIfChanged, when import cycles are present. This shows up in@wyw-in-js/transform@0.7.0, and for example can be shown in linaria's transformer tests failing (you would have to link against 0.7.0 and run the tests to validate that).This was also mentioned in a PR comment, and we ran into that same issue while investigating an upgrade of linaria's transformers to 0.7.0, linking against our app.
Summary
We carry forward a
visitedFilesSetin the recursion step, and use it to break cycles. Please let me know if I have missed something, or if you prefer some other idiom for handling cycles.Test plan
I added a test that demonstrates the failing scenario without the changes. If you edit the recursion step to provide an empty
new Set()as the parameter (orundefined), it should be equivalent to the previous version, and the tests should fail. All files are ultimately still visited (and the tests verify that), we just avoid the cycle in a given invalidation call chain.