Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent @babel/traverse path cache side-effects in generateFunctionMap #906

Closed
wants to merge 1 commit into from

Conversation

robhogan
Copy link
Contributor

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 the babel/traverse path cache to be populated with entries Babel transform plugins can't use - in particular because hub 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:

  • Actually providing a hub so that it's set on the cache means adding a dependency on babel/core, and requires a similar level of hackery / assumptions of Babel internals.
  • Clearing the bad cache with traverse.clearNode(ast) or traverse.cache.clearPath() doesn't work because other tools (eg, Jest) rely on properties held in the path cache.

Changelog: [Internal]

Differential Revision: D42068914

…nMap`

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 the `babel/traverse` path cache to be populated with entries Babel transform plugins can't use - in particular because `hub` 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:
 - Actually providing a `hub` so that it's set on the cache means adding a dependency on `babel/core`, and requires a similar level of hackery / assumptions of Babel internals.
 - Clearing the bad cache with `traverse.clearNode(ast)` or `traverse.cache.clearPath()` doesn't work because other tools (eg, Jest) rely on `properties` held in the path cache.

Changelog: [Internal]

Differential Revision: D42068914

fbshipit-source-id: f473f67f17ffe92c9fc378ef54f3dc40bcf25f7a
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported labels Dec 17, 2022
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D42068914

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 29bb5f2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants