Skip to content

Commit e2a0902

Browse files
committed
Tolerate trailing spaces before the '=' sign in placeholder names
1 parent fdf4b27 commit e2a0902

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

mug-errorprone/src/main/java/com/google/mu/errorprone/FormatStringUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.mu.errorprone;
22

33

4+
import static com.google.common.base.CharMatcher.whitespace;
45
import static com.google.common.collect.ImmutableList.toImmutableList;
56
import static com.google.mu.util.Optionals.optionally;
67
import static com.google.mu.util.Substring.consecutive;
@@ -36,9 +37,11 @@ final class FormatStringUtils {
3637
consecutive(CharMatcher.noneOf("{}")::matches).immediatelyBetween("{", "}").repeatedly();
3738

3839
static ImmutableList<String> placeholderVariableNames(String formatString) {
40+
Substring.Pattern beforeEqualSign = Substring.before(first('='));
3941
return PLACEHOLDER_NAMES_PATTERN
4042
.from(formatString)
41-
.map(first('=').toEnd()::removeFrom) // for Cloud resource name syntax
43+
// for Cloud resource name syntax
44+
.map(n -> beforeEqualSign.from(n).map(whitespace()::trimTrailingFrom).orElse(n))
4245
.collect(toImmutableList());
4346
}
4447

mug-errorprone/src/test/java/com/google/mu/errorprone/StringFormatPlaceholderNamesCheckTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ public void trailingSpacesNotAllowed() {
231231
.doTest();
232232
}
233233

234+
@Test
235+
public void trailingSpacesBeforeEqualSignIgnored() {
236+
helper
237+
.addSourceLines(
238+
"Test.java",
239+
"import com.google.mu.util.StringFormat;",
240+
"class Test {",
241+
" private static final StringFormat FORMAT =",
242+
" new StringFormat(\"{shows_id => id,}\");",
243+
"}")
244+
.doTest();
245+
}
246+
234247
@Ignore
235248
@Test
236249
public void squareBracketedPlaceholdersChecked() {

0 commit comments

Comments
 (0)