Skip to content

Commit

Permalink
Split out provide/require handling from ProcessClosurePrimitives
Browse files Browse the repository at this point in the history
Motivations:
 - We soon will move provide/require rewriting after typechecking, but don't care about
   the remaining primitives and will keep this before typechecking.
 - This pass is huge.
 - This potentially makes it easier to share goog.provide logic with other parts of the compiler.

Eventually this should also be its own pass, but for now continues to run as part of ProcessClosurePrimitives.

We also plan to remove the require rewriting eventually, and consolidate rewriting for requires in ES modules/goog.modules/goog.provide files/scripts.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=237530052
  • Loading branch information
lauraharker authored and blickly committed Mar 9, 2019
1 parent fd3c19c commit 2ff09bb
Show file tree
Hide file tree
Showing 3 changed files with 952 additions and 727 deletions.
35 changes: 35 additions & 0 deletions src/com/google/javascript/jscomp/PreprocessorSymbolTable.java
Expand Up @@ -156,4 +156,39 @@ public PreprocessorSymbolTable getInstanceOrNull() {
return instance;
}
}

/**
* Adds a synthetic reference for a 'string' node representing a reference name.
*
* <p>This does some work to set the source info for the reference as well.
*/
void addStringNode(Node n, AbstractCompiler compiler) {
String name = n.getString();
Node syntheticRef =
NodeUtil.newQName(
compiler, name, n /* real source offsets will be filled in below */, name);

// Offsets to add to source. Named for documentation purposes.
final int forQuote = 1;
final int forDot = 1;

Node current = null;
for (current = syntheticRef; current.isGetProp(); current = current.getFirstChild()) {
int fullLen = current.getQualifiedName().length();
int namespaceLen = current.getFirstChild().getQualifiedName().length();

current.setSourceEncodedPosition(n.getSourcePosition() + forQuote);
current.setLength(fullLen);

current
.getLastChild()
.setSourceEncodedPosition(n.getSourcePosition() + namespaceLen + forQuote + forDot);
current.getLastChild().setLength(current.getLastChild().getString().length());
}

current.setSourceEncodedPosition(n.getSourcePosition() + forQuote);
current.setLength(current.getString().length());

addReference(syntheticRef);
}
}

0 comments on commit 2ff09bb

Please sign in to comment.