Skip to content

Commit

Permalink
Fix type-only summary generation for export const
Browse files Browse the repository at this point in the history
The export keyword was being incorrectly dropped for all `export const`
statements.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=186046130
  • Loading branch information
blickly authored and dimvar committed Feb 20, 2018
1 parent 892f5aa commit 0936f7c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Expand Up @@ -271,6 +271,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
static void splitNameDeclarationsAndRemoveDestructuring(Node n, NodeTraversal t) {
checkArgument(NodeUtil.isNameDeclaration(n));
JSDocInfo sharedJsdoc = n.getJSDocInfo();
boolean isExport = n.getParent().isExport();
Node statement = isExport ? n.getParent() : n;
while (n.hasChildren()) {
Node lhsToSplit = n.getLastChild();
if (lhsToSplit.isDestructuringLhs()
Expand All @@ -293,7 +295,10 @@ static void splitNameDeclarationsAndRemoveDestructuring(Node n, NodeTraversal t)
Node newDeclaration =
NodeUtil.newDeclaration(lhsToSplit.detach(), rhs, n.getToken()).srcref(n);
newDeclaration.setJSDocInfo(mergedJsdoc);
n.getParent().addChildAfter(newDeclaration, n);
if (isExport) {
newDeclaration = IR.export(newDeclaration).srcref(statement);
}
statement.getParent().addChildAfter(newDeclaration, statement);
t.reportCodeChange();
}
}
Expand Down
Expand Up @@ -217,7 +217,11 @@ void simplify(AbstractCompiler compiler) {
newStatement.useSourceInfoIfMissingFromForTree(nameNode);
Node oldStatement = getRemovableNode();
NodeUtil.deleteChildren(oldStatement, compiler);
oldStatement.replaceWith(newStatement);
if (oldStatement.isExport()) {
oldStatement.addChildToBack(newStatement);
} else {
oldStatement.replaceWith(newStatement);
}
compiler.reportChangeToEnclosingScope(newStatement);
}

Expand Down
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/IR.java
Expand Up @@ -56,6 +56,10 @@ public static Node empty() {
return new Node(Token.EMPTY);
}

public static Node export(Node declaration) {
return new Node(Token.EXPORT, declaration);
}

public static Node importNode(Node name, Node importSpecs, Node moduleIdentifier) {
checkState(name.isName() || name.isEmpty(), name);
checkState(
Expand Down
Expand Up @@ -645,6 +645,20 @@ public void testEs6Modules() {
"export {BLAH};"));
}

public void testEs6ModulesExportedNameDeclarations() {
testSame("/** @type {number} */ export let x;");
testSame("/** @type {number} */ export var x;");

test("/** @type {number} */ export var x = 5;", "/** @type {number} */ export var x;");
test("/** @type {number} */ export const X = 5;", "/** @const {number} */ export var X;");
//TODO(blickly): Ideally, we would leave let declarations alone
test("/** @type {number} */ export let x = 5;", "/** @type {number} */ export var x;");

test(
"/** @type {number} */ export const X = 5, Y = z;",
"/** @const {number} */ export var X; /** @const {number} */ export var Y;");
}

public void testGoogModules() {
testSame(
lines(
Expand Down

0 comments on commit 0936f7c

Please sign in to comment.