Skip to content

Commit

Permalink
Use the original name when processing goog.getMsgWithFallback in case…
Browse files Browse the repository at this point in the history
… it's been renamed, for example by the goog.module pass.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156234672
  • Loading branch information
tbreisacher authored and brad4d committed May 18, 2017
1 parent 96b5087 commit 2e78d4b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
22 changes: 13 additions & 9 deletions src/com/google/javascript/jscomp/JsMessageVisitor.java
Expand Up @@ -829,19 +829,24 @@ private void visitFallbackFunctionCall(NodeTraversal t, Node call) {
}

Node firstArg = call.getSecondChild();
JsMessage firstMessage = getTrackedMessage(t, firstArg.getString());
String name = firstArg.getOriginalName();
if (name == null) {
name = firstArg.getString();
}
JsMessage firstMessage = getTrackedMessage(t, name);
if (firstMessage == null) {
compiler.report(
t.makeError(firstArg, FALLBACK_ARG_ERROR, firstArg.getString()));
compiler.report(t.makeError(firstArg, FALLBACK_ARG_ERROR, name));
return;
}

Node secondArg = firstArg.getNext();
JsMessage secondMessage = getTrackedMessage(
t, call.getChildAtIndex(2).getString());
name = secondArg.getOriginalName();
if (name == null) {
name = secondArg.getString();
}
JsMessage secondMessage = getTrackedMessage(t, name);
if (secondMessage == null) {
compiler.report(
t.makeError(secondArg, FALLBACK_ARG_ERROR, secondArg.getString()));
compiler.report(t.makeError(secondArg, FALLBACK_ARG_ERROR, name));
return;
}

Expand Down Expand Up @@ -929,8 +934,7 @@ static String toLowerCamelCaseWithNumericSuffixes(String input) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, input);
} else {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,
input.substring(0, suffixStart)) +
input.substring(suffixStart);
input.substring(0, suffixStart)) + input.substring(suffixStart);
}
}

Expand Down
26 changes: 19 additions & 7 deletions test/com/google/javascript/jscomp/JsMessageVisitorTest.java
Expand Up @@ -476,9 +476,9 @@ public void testIncorrectMessage() {
assertThat(messages).isEmpty();
assertThat(compiler.getErrors()).hasLength(1);
JSError error = compiler.getErrors()[0];
assertEquals("Message parse tree malformed. "+
"Message must be initialized using goog.getMsg function.",
error.description);
assertEquals(
"Message parse tree malformed. Message must be initialized using goog.getMsg function.",
error.description);
}

public void testUnrecognizedFunction() {
Expand All @@ -488,10 +488,10 @@ public void testUnrecognizedFunction() {
assertThat(messages).isEmpty();
assertThat(compiler.getErrors()).hasLength(1);
JSError error = compiler.getErrors()[0];
assertEquals("Message parse tree malformed. "+
"Message initialized using unrecognized function. " +
"Please use goog.getMsg() instead.",
error.description);
assertEquals(
"Message parse tree malformed. Message initialized using unrecognized function. "
+ "Please use goog.getMsg() instead.",
error.description);
}

public void testExtractPropertyMessage() {
Expand Down Expand Up @@ -720,6 +720,18 @@ public void testUsingMsgPrefixWithFallback() {
assertNoErrors();
}

public void testUsingMsgPrefixWithFallback_rename() {
renameMessages = true;
extractMessages(
LINE_JOINER.join(
"function f() {",
"/** @desc Hello */ var MSG_A = goog.getMsg('hello');",
"/** @desc Hello */ var MSG_B = goog.getMsg('hello!');",
"var x = goog.getMsgWithFallback(MSG_A, MSG_B);",
"}"));
assertNoErrors();
}

public void testErrorWhenUsingMsgPrefixWithFallback() {
extractMessages(
"/** @desc Hello */ var MSG_HELLO_1 = goog.getMsg('hello');\n" +
Expand Down

0 comments on commit 2e78d4b

Please sign in to comment.