Prevent @babel/traverse
path cache side-effects in generateFunctionMap
#906
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:
Motivation
For performance reasons, we'd like to have the option to re-use an AST (without cloning) after deriving source map information from it. However, due to babel/babel#6437, traversing the AST within
generateFunctionMap
causes thebabel/traverse
path cache to be populated with entries Babel transform plugins can't use - in particular becausehub
is assumed to be set.Change
This diffs saves and clears the traverse cache before traversal, and then restores it afterwards. This isn't pretty but it's the cleanest approach I could find - we include tests to verify that this is (and continues to be) necessary.
Other approaches
I tried some other things before resorting to manipulating
babel/traverse
internals:hub
so that it's set on the cache means adding a dependency onbabel/core
, and requires a similar level of hackery / assumptions of Babel internals.traverse.clearNode(ast)
ortraverse.cache.clearPath()
doesn't work because other tools (eg, Jest) rely onproperties
held in the path cache.Changelog: [Internal]
Differential Revision: D42068914