Skip to content

Commit

Permalink
Allow multiple named exports of the same symbol.
Browse files Browse the repository at this point in the history
Since it's impossible to avoid aliases in the rewriting of this case, just
make the second export an alias of the first, as if the user had written:
    exports.firstName = localName;
    exports.secondName = exports.firstName;

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146963601
  • Loading branch information
blickly committed Feb 9, 2017
1 parent c76aeab commit db7a7b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/com/google/javascript/jscomp/ClosureRewriteModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ String getExportPostfix() {
return "." + exportName;
}

boolean hasInlinableName() {
boolean hasInlinableName(Set<Var> exportedNames) {
if (nameDecl == null
|| exportedNames.contains(nameDecl)
|| !INLINABLE_NAME_PARENTS.contains(nameDecl.getParentNode().getToken())) {
return false;
}
Expand All @@ -261,10 +262,7 @@ boolean hasInlinableName() {
}

String getLocalName() {
if (hasInlinableName()) {
return nameDecl.getName();
}
return null;
return nameDecl.getName();
}
}

Expand Down Expand Up @@ -868,7 +866,8 @@ private void maybeRecordExportDeclaration(NodeTraversal t, Node n) {
Node rhs = key.hasChildren() ? key.getFirstChild() : key;
ExportDefinition namedExport = ExportDefinition.newNamedExport(t, exportName, rhs);
currentScript.namedExports.add(exportName);
if (currentScript.declareLegacyNamespace || !namedExport.hasInlinableName()) {
if (currentScript.declareLegacyNamespace
|| !namedExport.hasInlinableName(currentScript.exportsToInline.keySet())) {
areAllExportsInlinable = false;
} else {
inlinableExports.add(namedExport);
Expand All @@ -888,7 +887,8 @@ private void maybeRecordExportDeclaration(NodeTraversal t, Node n) {
currentScript.defaultExportRhs = exportRhs;
currentScript.willCreateExportsObject = true;
ExportDefinition defaultExport = ExportDefinition.newDefaultExport(t, exportRhs);
if (!currentScript.declareLegacyNamespace && defaultExport.hasInlinableName()) {
if (!currentScript.declareLegacyNamespace
&& defaultExport.hasInlinableName(currentScript.exportsToInline.keySet())) {
String localName = defaultExport.getLocalName();
currentScript.defaultExportLocalName = localName;
recordExportToInline(defaultExport);
Expand Down Expand Up @@ -1127,7 +1127,7 @@ private void recordExportsPropertyAssignment(NodeTraversal t, Node getpropNode)
ExportDefinition namedExport = ExportDefinition.newNamedExport(t, exportName, exportRhs);
if (!currentScript.declareLegacyNamespace
&& currentScript.defaultExportRhs == null
&& namedExport.hasInlinableName()) {
&& namedExport.hasInlinableName(currentScript.exportsToInline.keySet())) {
recordExportToInline(namedExport);
parent.getParent().detach();
}
Expand Down Expand Up @@ -1461,7 +1461,11 @@ private void markConstAndCopyJsDoc(Node from, Node target, Node value) {
}

private void recordExportToInline(ExportDefinition exportDefinition) {
currentScript.exportsToInline.put(exportDefinition.nameDecl, exportDefinition);
Preconditions.checkState(
exportDefinition.hasInlinableName(currentScript.exportsToInline.keySet()));
Preconditions.checkState(
null == currentScript.exportsToInline.put(exportDefinition.nameDecl, exportDefinition),
"Already found a mapping for inlining export: %s", exportDefinition.nameDecl);
String localName = exportDefinition.getLocalName();
String fullExportedName =
currentScript.getBinaryNamespace() + exportDefinition.getExportPostfix();
Expand Down
15 changes: 15 additions & 0 deletions test/com/google/javascript/jscomp/ClosureRewriteModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,21 @@ public void testRewriteGoogModuleAliasesWithPrototypeGets2() {
});
}

public void testMultiplyExportedSymbolDoesntCrash() {
testEs6(
LINE_JOINER.join(
"goog.module('mod');",
"",
"class Foo {}",
"",
"exports.name1 = Foo;",
"exports.name2 = Foo;"),
LINE_JOINER.join(
"/** @const */ var module$exports$mod = {};",
"module$exports$mod.name1 = class {};",
"/** @const */ module$exports$mod.name2 = module$exports$mod.name1;"));
}

public void testIjsFileInExterns() {
allowExternsChanges(true);
test(
Expand Down

0 comments on commit db7a7b4

Please sign in to comment.