Skip to content

Commit

Permalink
Make adding a goog.require/goog.requireType to a goog.module re…
Browse files Browse the repository at this point in the history
…place the existing `goog.forwardDeclare` if present.

PiperOrigin-RevId: 549355839
  • Loading branch information
blickly authored and copybara-github committed Jul 19, 2023
1 parent e6d9f57 commit 2636677
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/com/google/javascript/refactoring/SuggestedFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,13 @@ public Builder addImport(

// Add an alias to a naked require if allowed in this file.
if (existingNode.isExprResult() && alias != null) {
Node newNode = IR.constNode(IR.name(alias), existingNode.getFirstChild().cloneTree());
Node newNode;
// Replace goog.forwardDeclare with the appropriate alternative
if (NodeUtil.isCallTo(existingNode.getFirstChild(), "goog.forwardDeclare")) {
newNode = createImportNode(importType, alias, namespace);
} else {
newNode = IR.constNode(IR.name(alias), existingNode.getFirstChild().cloneTree());
}
replace(existingNode, newNode, m.getMetadata().getCompiler());
scriptMetadata.addAlias(namespace, alias);
}
Expand Down
36 changes: 36 additions & 0 deletions test/com/google/javascript/refactoring/ErrorToFixMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,42 @@ public void testMissingRequireInGoogModule_alwaysInsertsConst() {
"alert(new A(new B(new C())));"));
}

@Test
public void testGoogForwardDeclare_fixedToRequire() {
preexistingCode = "goog.provide('goog.dom.DomHelper');";
assertChanges(
lines(
"goog.module('module');",
"",
"goog.forwardDeclare('goog.dom.DomHelper');",
"",
"new goog.dom.DomHelper();"),
lines(
"goog.module('module');",
"",
"const DomHelper = goog.require('goog.dom.DomHelper');",
"",
"new DomHelper();"));
}

@Test
public void testGoogForwardDeclare_fixedToRequireType() {
preexistingCode = "goog.provide('goog.dom.DomHelper');";
assertChanges(
lines(
"goog.module('module');",
"",
"goog.forwardDeclare('goog.dom.DomHelper');",
"",
"function f(/** !goog.dom.DomHelper */ x) {}"),
lines(
"goog.module('module');",
"",
"const DomHelper = goog.requireType('goog.dom.DomHelper');",
"",
"function f(/** !DomHelper */ x) {}"));
}

@Test
public void testShortRequireInGoogModule1() {
assertChanges(
Expand Down

0 comments on commit 2636677

Please sign in to comment.