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

Complete Rewrite of the ProcessCommonJsModules Pass #2085

Closed

Conversation

ChadKillingsworth
Copy link
Collaborator

The commonjs module rewriting was a very old pass and had many incremental changes. In addition, it suffered from some of the same problems as the old goog.module rewriting in that it relied on the early inling pass to handle all of the aliases created.

This is an almost complete rewrite. It had the following goals:

  • Avoids creating aliases for imports and exports thus removing the dependency on the early inlining pass.
  • Only traverses the AST twice.

I've tested it on my medium sized project that uses commonjs modules extensively. With this change, the number of warnings created with NTI is drastically reduced.

There's some other ancillary benefits: namely the PolymerPass requires behaviors to be global symbols so it now works with CommonJS modules.

@ChadKillingsworth
Copy link
Collaborator Author

This avoids the pattern highlighted in #2083 as much as possible. There are still cases where that pattern can be generated (as highlighted in the issue), but they aren't prevalent in any real world code I've encountered.

Copy link
Contributor

@blickly blickly left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few minor comments

private final boolean allowFullRewrite;
private final ImmutableCollection<Node> exports;
private List<Node> imports = new ArrayList<>();
private List<Node> rewrittenClassExpressions = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like these members can all be final

while (refChild.getNext() != null & refChild.getNext().isExprResult() &&
refChild.getNext().getFirstChild().isCall() &&
("goog.require".equals(refChild.getNext().getFirstChild().getFirstChild().getQualifiedName()) ||
"goog.provide".equals(refChild.getNext().getFirstChild().getFirstChild().getQualifiedName()))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these could be simplified with .matchesQualifiedName

If you don't mind the slightly strange name, there's also getFirstFirstChild.

@@ -78,246 +88,185 @@ public ProcessCommonJSModules(Compiler compiler, boolean reportDependencies) {

@Override
public void process(Node externs, Node root) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was a bit confused at first, just because the root node isn't the usual jsRoot (the parent of all input files). Can we add a Preconditions check that root must be a SCRIPT node?

if (amdFinder.isFound()) {
visitAMDIfStatement(n);
if (n.isGetProp()) {
String qualifiedName = n.getQualifiedName();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these might be slightly simpler as n.matchesQualifiedName

Avoids creating aliases for imports and exports thus removing the dependency on the early inlining pass.
Only traverses the AST twice.
@ChadKillingsworth
Copy link
Collaborator Author

@blickly done.

@ChadKillingsworth
Copy link
Collaborator Author

I think the travis failures may be wrong - the tests all pass locally for me.

@blickly
Copy link
Contributor

blickly commented Oct 18, 2016

Yeah, I think I've figured out the breakage and have a fix out. (And yes, it's unrelated to this PR)

@blickly blickly closed this in d43e6c4 Oct 18, 2016
@ChadKillingsworth ChadKillingsworth deleted the commonjs-refactor branch October 28, 2016 12:46
alexeykomov pushed a commit to alexeykomov/closure-compiler that referenced this pull request Feb 8, 2017
Avoids creating aliases for imports and exports thus removing the dependency on the early inlining pass.
Only traverses the AST twice.

Closes google#2085

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136504299
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