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

Destructuring requires #298

Merged
merged 3 commits into from
Jul 24, 2018
Merged

Destructuring requires #298

merged 3 commits into from
Jul 24, 2018

Conversation

mjesun
Copy link
Contributor

@mjesun mjesun commented Jul 24, 2018

Enables the inlining of expressions like var x = require('foo').x, which can come from a) an import {x} from 'foo', b) const {x} = require('foo') or c) const foo = require('foo'); const x = foo.x. This will enable further optimizations when detecting dead requires.

I simplified the plugin by making use of other Babel utilities, so that we don't have to manually track variables and scopes, and we equally treat all inlinable expressions. Also fixed a bug where var foo = require('foo'), bar = require('bar') was not properly getting inlined.

Added tests to ensure that new cases are covered, made sure all existing ones still pass.

CallExpression: function(path, state) {
var node = path.node;
if (declarator) {
declaratorPath.scope.crawl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that I don’t understand is why you have to crawl here, and not …

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to crawl because another plugin might have changed the scope already; for instance, the import / export Babel plugin. We need to have accurate variable binding, counting and referencing for the plugin to work.

// And the associated binding in the scope.
path.scope.removeBinding(varName);
binding.referencePaths.forEach(ref => ref.replaceWith(init));
declaratorPath.remove();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… and not here.

@davidaurelio davidaurelio merged commit 11f270b into facebook:master Jul 24, 2018
@mjesun mjesun deleted the destructuring-requires branch July 24, 2018 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants