From 488845bf7fe108eb9f53395d5e0600653e0ca1a2 Mon Sep 17 00:00:00 2001 From: tbreisacher Date: Wed, 1 Mar 2017 14:53:19 -0800 Subject: [PATCH] When JSDoc in a goog.module references an "inner class" make fix mode do the correct replacement. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=148934826 --- .../refactoring/ErrorToFixMapper.java | 18 ++++++---- .../refactoring/ErrorToFixMapperTest.java | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/com/google/javascript/refactoring/ErrorToFixMapper.java b/src/com/google/javascript/refactoring/ErrorToFixMapper.java index a80ebeddd1b..312fd6a5e6e 100644 --- a/src/com/google/javascript/refactoring/ErrorToFixMapper.java +++ b/src/com/google/javascript/refactoring/ErrorToFixMapper.java @@ -201,19 +201,25 @@ private static SuggestedFix getFixForReferenceToShortImportByLongName( NodeMetadata metadata = new NodeMetadata(compiler); 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); String shortName; if (shortNameMatcher.matches()) { shortName = shortNameMatcher.group(1); } else { - Matcher fullNameMatcher = FULLY_QUALIFIED_NAME.matcher(error.description); - Preconditions.checkState(fullNameMatcher.matches(), error.description); - String namespace = fullNameMatcher.group(1); - shortName = namespace.substring(namespace.lastIndexOf('.') + 1); - fix.addLhsToGoogRequire(match, namespace); + shortName = fullName.substring(fullName.lastIndexOf('.') + 1); + fix.addLhsToGoogRequire(match, fullName); } - 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 getFixesForImplicitlyNullableJsDoc( diff --git a/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java b/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java index 4b33bcfcac9..bd07b10a40b 100644 --- a/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java +++ b/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java @@ -892,6 +892,40 @@ public void testSwitchToShorthand_JSDoc6() { "var animals;")); } + @Test + public void testSwitchToShorthand_JSDoc7() { + assertChanges( + LINE_JOINER.join( + "goog.module('m');", + "var Animal = goog.require('world.util.Animal');", + "", + "/** @type {?Array} */", + "var turtles;"), + LINE_JOINER.join( + "goog.module('m');", + "var Animal = goog.require('world.util.Animal');", + "", + "/** @type {?Array} */", + "var turtles;")); + } + + @Test + public void testSwitchToShorthand_JSDoc8() { + assertChanges( + LINE_JOINER.join( + "goog.module('m');", + "var AnimalAltName = goog.require('world.util.Animal');", + "", + "/** @type {?Array} */", + "var turtles;"), + LINE_JOINER.join( + "goog.module('m');", + "var AnimalAltName = goog.require('world.util.Animal');", + "", + "/** @type {?Array} */", + "var turtles;")); + } + @Test public void testMissingRequireInGoogModule_atExtends_qname() { assertChanges(