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

fix: Ensure correct scope after traversal #283

Closed
wants to merge 1 commit into from

Conversation

leebyron
Copy link

@leebyron leebyron commented Jan 28, 2019

This fixes a bug that can be seen when istanbul coverage is enabled for a babel@7 project which uses reference replacement, such as babel-plugin-lodash or babel-plugin-ramda.

The root issue is that plugins like these assume that scope references will always be Identifiers but Istanbul may replace an identifier with a SequenceExpression. In babel@7 this replacement overwrites the existing scope reference and later plugin scope reference replacement is at best unsafe and at worst may result in an invalid AST.

This seems like a pretty unfortunate situation, and ideally Babel automatically maintains these scope references during transforms or the many packages using scope would account for this situation. The pattern I see in other first-class babel plugins prefer to re-crawl scope after a transform which can affect identifiers (example: https://github.com/babel/minify/blob/master/packages/babel-plugin-minify-dead-code-elimination/src/index.js#L969)

To follow that example, I'm proposing this fix that crawls the scope after the path traversal - ensuring any subsequent references to scope get correct references.

I tested this in a project that used istanbul via jest along with babel-plugin-lodash that suffered from this issue and found that with this patch the issue was resolved and the inspected compiled output looked correct.

This fixes a bug that can be seen when istanbul coverage is enabled for a babel@7 project which uses reference replacement, such as `babel-plugin-lodash`. 

The root issue is that plugins like these assume that scope references will always be Identifiers but Istanbul may replace an identifier with a SequenceExpression. In babel@7 this replacement overwrites the existing scope reference and later plugin scope reference replacement is at best unsafe and at worst may result in an invalid AST.

This seems like a pretty unfortunate situation, and ideally Babel automatically maintains these scope references during transforms or the many packages using scope would account for this situation. The pattern I see in other first-class babel plugins prefer to re-crawl scope after a transform which can affect identifiers (example: https://github.com/babel/minify/blob/master/packages/babel-plugin-minify-dead-code-elimination/src/index.js#L969)

To follow that example, I'm proposing this fix that crawls the scope after the path traversal - ensuring any subsequent references to scope get correct references.

I tested this in a project that used istanbul via jest along with babel-plugin-lodash that suffered from this issue and found that with this patch the issue was resolved and the inspected compiled output looked correct.
@coreyfarrell
Copy link
Member

This is difficult. From @loganfsmyth on babeljs slack (in response to someone else asking about scope.crawl():

Im not sure you'll get a definitive answer because the scope apis aren't super well defined. Realistically I'd recommend against anyone using .crawl without an extremely good reason

So to move forward I think I'm going to need a minimal test-case. Preferably without jest (sorry jest adds a lot of moving parts). Something which uses babel-plugin-istanbul and an incompatible plugin from a babelrc showing a transformation becoming broken would be best to isolate this issue. A secondary goal will be to add a test to istanbul-lib-instrument to demonstrate that this issue is fixed and doesn't resurface.

@leebyron
Copy link
Author

Closing in favor of istanbuljs/babel-plugin-istanbul#192

@leebyron leebyron closed this Jan 29, 2019
@leebyron leebyron deleted the patch-1 branch January 30, 2019 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants