Skip to content

Commit

Permalink
Issue checkstyle#12409: Inconsistent allowedAbbreviations
Browse files Browse the repository at this point in the history
  • Loading branch information
arinmodi committed Nov 29, 2022
1 parent 62a765a commit 9e26e08
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
Expand Up @@ -297,6 +297,38 @@
* }
* </pre>
* <p>
* To configure to check variables, enforce
* no abbreviations (essentially camel case) except for
* words like 'ORDERS', 'OBSERVATION', 'UNDERSCORE', 'TEST'.
* </p>
* <p>Configuration:</p>
* <pre>
* &lt;module name="AbbreviationAsWordInName"&gt;
* &lt;property name="allowedAbbreviations" value="ORDER, OBSERVATION, UNDERSCORE, TEST"/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
* <pre>
* public class InputAbbreviationAsWordInNameType7 {
* void getTEST() {
* } // OK
*
* void getORDER_OBSERVATION() {} // OK
*
* void getUNDERSCORE() {} // OK
*
* void getTEST_OBSERVATION() {} // OK
*
* void getTEST_UNDERSCORE() {} // OK
*
* void getORDER() {} // OK
*
* void getOBSERVATION() {} // OK
*
* void getORDER_UNDERSCORE() {} // OK
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
Expand Down Expand Up @@ -583,7 +615,13 @@ private String getDisallowedAbbreviation(String str) {
else if (abbrStarted) {
abbrStarted = false;

final int endIndex = index - 1;
final int endIndex;
if (Character.isLetterOrDigit(symbol)) {
endIndex = index - 1;
}
else {
endIndex = index;
}
result = getAbbreviationIfIllegal(str, beginIndex, endIndex);
if (result != null) {
break;
Expand Down
Expand Up @@ -467,4 +467,13 @@ public void testReceiver() throws Exception {
expected);
}

@Test
public void demoTest() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verifyWithInlineConfigParser(
getPath("InputAbbreviationAsWordInNameType7.java"),
expected);
}

}
@@ -0,0 +1,28 @@
/*
AbbreviationAsWordInName
allowedAbbreviations = ORDER, OBSERVATION, UNDERSCORE, TEST
*/


package com.puppycrawl.tools.checkstyle.checks.naming.abbreviationaswordinname;

public class InputAbbreviationAsWordInNameType7 {
void getTEST() {
} // OK

void getORDER_OBSERVATION() {} // ok

void getUNDERSCORE() {} // ok

void getTEST_OBSERVATION() {} // ok

void getTEST_UNDERSCORE() {} // ok

void getORDER() {} // ok

void getOBSERVATION() {} // ok

void getORDER_UNDERSCORE() {} // ok
}
32 changes: 32 additions & 0 deletions src/xdocs/config_naming.xml
Expand Up @@ -344,6 +344,38 @@ public class MyClass {
public final int customerID = 2; // violation
public static int nextID = 3; // OK, ignored
public static final int MAX_ALLOWED = 4; // violation
}
</source>
<p>
To configure to check variables, enforce
no abbreviations (essentially camel case) except for
words like 'ORDERS', 'OBSERVATION', 'UNDERSCORE', 'TEST'.
</p>
<p>Configuration:</p>
<source>
&lt;module name="AbbreviationAsWordInName"&gt;
&lt;property name="allowedAbbreviations" value="ORDER, OBSERVATION, UNDERSCORE, TEST"/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
public class InputAbbreviationAsWordInNameType7 {
void getTEST() {
} // OK

void getORDER_OBSERVATION() {} // OK

void getUNDERSCORE() {} // OK

void getTEST_OBSERVATION() {} // OK

void getTEST_UNDERSCORE() {} // OK

void getORDER() {} // OK

void getOBSERVATION() {} // OK

void getORDER_UNDERSCORE() {} // OK
}
</source>
</subsection>
Expand Down

0 comments on commit 9e26e08

Please sign in to comment.