Skip to content

Commit 6d21307

Browse files
committed
Apply regex to check if name isn't pascal case already
1 parent 1faedcb commit 6d21307

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

rewrite-csharp/src/main/java/org/openrewrite/csharp/service/CSharpNamingService.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,41 @@
2222

2323
public class CSharpNamingService implements NamingService {
2424

25+
private static final Pattern STANDARD_METHOD_NAME = Pattern.compile("^[A-Z][a-zA-Z0-9]*$");
2526
private static final Pattern SNAKE_CASE = Pattern.compile("^[a-zA-Z0-9]+_\\w+$");
2627

2728
@Override
28-
public String getMethodName(String oldMethodName) {
29-
StringBuilder result = new StringBuilder();
30-
if (SNAKE_CASE.matcher(oldMethodName).matches()) {
31-
result.append(NameCaseConvention.format(NameCaseConvention.UPPER_CAMEL, oldMethodName));
32-
} else {
33-
int nameLength = oldMethodName.length();
34-
for (int i = 0; i < nameLength; i++) {
35-
char c = oldMethodName.charAt(i);
29+
public String standardizeMethodName(String oldMethodName) {
30+
if (!STANDARD_METHOD_NAME.matcher(oldMethodName).matches()) {
31+
StringBuilder result = new StringBuilder();
32+
if (SNAKE_CASE.matcher(oldMethodName).matches()) {
33+
result.append(NameCaseConvention.format(NameCaseConvention.UPPER_CAMEL, oldMethodName));
34+
} else {
35+
int nameLength = oldMethodName.length();
36+
for (int i = 0; i < nameLength; i++) {
37+
char c = oldMethodName.charAt(i);
3638

37-
if (i == 0) {
38-
// the java specification requires identifiers to start with [a-zA-Z$_]
39-
if (c != '$' && c != '_') {
40-
result.append(Character.toUpperCase(c));
41-
}
42-
} else {
43-
if (!Character.isLetterOrDigit(c)) {
44-
while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) {
45-
c = oldMethodName.charAt(i++);
46-
}
47-
if (i < nameLength) {
39+
if (i == 0) {
40+
// the java specification requires identifiers to start with [a-zA-Z$_]
41+
if (c != '$' && c != '_') {
4842
result.append(Character.toUpperCase(c));
4943
}
5044
} else {
51-
result.append(c);
45+
if (!Character.isLetterOrDigit(c)) {
46+
while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) {
47+
c = oldMethodName.charAt(i++);
48+
}
49+
if (i < nameLength) {
50+
result.append(Character.toUpperCase(c));
51+
}
52+
} else {
53+
result.append(c);
54+
}
5255
}
5356
}
5457
}
58+
return result.toString();
5559
}
56-
return result.toString();
60+
return oldMethodName;
5761
}
5862
}

0 commit comments

Comments
 (0)