Skip to content

Commit

Permalink
Preserve enum declarations inside namespaces in .i.js files
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=191516095
  • Loading branch information
blickly authored and lauraharker committed Apr 4, 2018
1 parent 4ccb965 commit 68b02eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/com/google/javascript/jscomp/ijs/PotentialDeclaration.java
Expand Up @@ -166,13 +166,7 @@ void simplify(AbstractCompiler compiler) {
Node nameNode = getLhs();
JSDocInfo jsdoc = getJsDoc();
if (jsdoc != null && jsdoc.hasEnumParameterType()) {
// Remove values from enums
if (getRhs().isObjectLit() && getRhs().hasChildren()) {
for (Node key : getRhs().children()) {
removeStringKeyValue(key);
}
compiler.reportChangeToEnclosingScope(getRhs());
}
super.simplifyEnumValues(compiler);
return;
}
if (NodeUtil.isNamespaceDecl(nameNode)) {
Expand Down Expand Up @@ -298,16 +292,18 @@ void simplify(AbstractCompiler compiler) {
if (shouldPreserve()) {
return;
}
if (!isTypedRhs(getRhs())) {
Node key = getLhs();
removeStringKeyValue(key);
JSDocInfo jsdoc = getJsDoc();
if (jsdoc == null
|| !jsdoc.containsDeclaration()
|| isConstToBeInferred()) {
key.setJSDocInfo(JsdocUtil.getUnusableTypeJSDoc(jsdoc));
}
compiler.reportChangeToEnclosingScope(key);
JSDocInfo jsdoc = getJsDoc();
if (jsdoc != null && jsdoc.hasEnumParameterType()) {
super.simplifyEnumValues(compiler);
return;
}
Node key = getLhs();
removeStringKeyValue(key);
compiler.reportChangeToEnclosingScope(key);
if (jsdoc == null
|| !jsdoc.containsDeclaration()
|| isConstToBeInferred()) {
key.setJSDocInfo(JsdocUtil.getUnusableTypeJSDoc(jsdoc));
}
}

Expand Down Expand Up @@ -336,6 +332,16 @@ Node getRemovableNode() {

}

/** Remove values from enums */
private void simplifyEnumValues(AbstractCompiler compiler) {
if (getRhs().isObjectLit() && getRhs().hasChildren()) {
for (Node key : getRhs().children()) {
removeStringKeyValue(key);
}
compiler.reportChangeToEnclosingScope(getRhs());
}
}

boolean isDefiniteDeclaration() {
Node parent = getLhs().getParent();
switch (parent.getToken()) {
Expand Down
Expand Up @@ -887,6 +887,12 @@ public void testEnums() {
"/** @const {*} */ var x; /** @enum {number} */ var E = { A: 0 };");
}

public void testEnumInsideNamespace() {
test(
"const ns = { /** @enum {number} */ ENUM: { A: 1, B: 2, C: 3} };",
"const ns = { /** @enum {number} */ ENUM: { A: 0, B: 0, C: 0} };");
}

public void testTryCatch() {
test(
"try { /** @type {number} */ var n = foo(); } catch (e) { console.log(e); }",
Expand Down

0 comments on commit 68b02eb

Please sign in to comment.