Skip to content

Destructuring requires#298

Merged
davidaurelio merged 3 commits intofacebook:masterfrom
mjesun:destructuring-requires
Jul 24, 2018
Merged

Destructuring requires#298
davidaurelio merged 3 commits intofacebook:masterfrom
mjesun:destructuring-requires

Conversation

@mjesun
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

4 participants