Skip to content

Commit

Permalink
When JSDoc in a goog.module references an "inner class" make fix mode…
Browse files Browse the repository at this point in the history
… do the correct replacement.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=148934826
  • Loading branch information
tbreisacher authored and brad4d committed Mar 2, 2017
1 parent 1e487e4 commit 488845b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/com/google/javascript/refactoring/ErrorToFixMapper.java
Expand Up @@ -201,19 +201,25 @@ private static SuggestedFix getFixForReferenceToShortImportByLongName(
NodeMetadata metadata = new NodeMetadata(compiler); NodeMetadata metadata = new NodeMetadata(compiler);
Match match = new Match(error.node, metadata); Match match = new Match(error.node, metadata);


Matcher fullNameMatcher = FULLY_QUALIFIED_NAME.matcher(error.description);
Preconditions.checkState(fullNameMatcher.matches(), error.description);
String fullName = fullNameMatcher.group(1);

Matcher shortNameMatcher = USE_SHORT_NAME.matcher(error.description); Matcher shortNameMatcher = USE_SHORT_NAME.matcher(error.description);
String shortName; String shortName;
if (shortNameMatcher.matches()) { if (shortNameMatcher.matches()) {
shortName = shortNameMatcher.group(1); shortName = shortNameMatcher.group(1);
} else { } else {
Matcher fullNameMatcher = FULLY_QUALIFIED_NAME.matcher(error.description); shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
Preconditions.checkState(fullNameMatcher.matches(), error.description); fix.addLhsToGoogRequire(match, fullName);
String namespace = fullNameMatcher.group(1);
shortName = namespace.substring(namespace.lastIndexOf('.') + 1);
fix.addLhsToGoogRequire(match, namespace);
} }


return fix.replace(error.node, NodeUtil.newQName(compiler, shortName), compiler).build(); String oldName =
error.node.isQualifiedName() ? error.node.getQualifiedName() : error.node.getString();

return fix.replace(
error.node, NodeUtil.newQName(compiler, oldName.replace(fullName, shortName)), compiler)
.build();
} }


private static List<SuggestedFix> getFixesForImplicitlyNullableJsDoc( private static List<SuggestedFix> getFixesForImplicitlyNullableJsDoc(
Expand Down
34 changes: 34 additions & 0 deletions test/com/google/javascript/refactoring/ErrorToFixMapperTest.java
Expand Up @@ -892,6 +892,40 @@ public void testSwitchToShorthand_JSDoc6() {
"var animals;")); "var animals;"));
} }


@Test
public void testSwitchToShorthand_JSDoc7() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"var Animal = goog.require('world.util.Animal');",
"",
"/** @type {?Array<world.util.Animal.Turtle>} */",
"var turtles;"),
LINE_JOINER.join(
"goog.module('m');",
"var Animal = goog.require('world.util.Animal');",
"",
"/** @type {?Array<Animal.Turtle>} */",
"var turtles;"));
}

@Test
public void testSwitchToShorthand_JSDoc8() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"var AnimalAltName = goog.require('world.util.Animal');",
"",
"/** @type {?Array<world.util.Animal.Turtle>} */",
"var turtles;"),
LINE_JOINER.join(
"goog.module('m');",
"var AnimalAltName = goog.require('world.util.Animal');",
"",
"/** @type {?Array<AnimalAltName.Turtle>} */",
"var turtles;"));
}

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

0 comments on commit 488845b

Please sign in to comment.