Skip to content

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

Closed
robhogan wants to merge 1 commit intofacebook:mainfrom
robhogan:export-D42068914
Closed

Prevent @babel/traverse path cache side-effects in generateFunctionMap#906
robhogan wants to merge 1 commit intofacebook:mainfrom
robhogan:export-D42068914

Conversation

@robhogan
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

@facebook-github-bot
Copy link
Copy Markdown
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.

2 participants