Skip to content

Commit c7ae7e1

Browse files
cushonError Prone Team
authored andcommitted
Handle $ in IdentifierName
* Escape replacement characters in `Matcher#replaceAll`, since dollar signs in a replacement string are treated as references to captured subsequences. * Add `$` to the list of accepted characters for the upper and lower underscore heuristics Fixes #5515 PiperOrigin-RevId: 872826736
1 parent 4ba14b4 commit c7ae7e1

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/IdentifierName.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private static boolean isStaticVariable(Symbol symbol) {
290290
return symbol instanceof VarSymbol && isStatic(symbol);
291291
}
292292

293-
private static final Pattern LOWER_UNDERSCORE_PATTERN = Pattern.compile("[a-z0-9_]+");
294-
private static final Pattern UPPER_UNDERSCORE_PATTERN = Pattern.compile("[A-Z0-9_]+");
293+
private static final Pattern LOWER_UNDERSCORE_PATTERN = Pattern.compile("[a-z0-9$_]+");
294+
private static final Pattern UPPER_UNDERSCORE_PATTERN = Pattern.compile("[A-Z0-9$_]+");
295295
private static final Splitter UNDERSCORE_SPLITTER = Splitter.on('_');
296296
}

core/src/main/java/com/google/errorprone/bugpatterns/IdentifierNames.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static java.lang.Character.isDigit;
2323

2424
import com.google.errorprone.ErrorProneFlags;
25+
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
2627
import javax.inject.Inject;
2728

@@ -68,7 +69,9 @@ String fixInitialismsIfNeeded(String input) {
6869
}
6970

7071
static String fixInitialisms(String input) {
71-
return PROBABLE_INITIALISM.matcher(input).replaceAll(r -> titleCase(r.group(1)) + r.group(2));
72+
return PROBABLE_INITIALISM
73+
.matcher(input)
74+
.replaceAll(r -> Matcher.quoteReplacement(titleCase(r.group(1)) + r.group(2)));
7275
}
7376

7477
private static String titleCase(String input) {

core/src/test/java/com/google/errorprone/bugpatterns/IdentifierNameTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,4 +799,17 @@ void test(Test this, int x) {}
799799
""")
800800
.doTest();
801801
}
802+
803+
@Test
804+
public void dollarSign() {
805+
helper
806+
.addSourceLines(
807+
"Test.java",
808+
"""
809+
class Test {
810+
private static final String JSON_KEY_$SCHEMA = "$schema";
811+
}
812+
""")
813+
.doTest();
814+
}
802815
}

0 commit comments

Comments
 (0)