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 Dec 1, 2022
1 parent 62a765a commit 12db2b4
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 61 deletions.
120 changes: 60 additions & 60 deletions config/archunit-store/slices_should_be_free_of_cycles_suppressions

Large diffs are not rendered by default.

Expand Up @@ -40,6 +40,7 @@
* <a href="https://checkstyle.org/styleguides/google-java-style-20180523/javaguide.html#s5.3-camel-case">
* Google Style Guide</a> to get to know how to avoid long abbreviations in names.
* </p>
* <p>'_' is considered as word separator in identifier name.</p>
* <p>
* {@code allowedAbbreviationLength} specifies how many consecutive capital letters are
* allowed in the identifier.
Expand Down Expand Up @@ -297,6 +298,23 @@
* }
* </pre>
* <p>
* To configure to check variables, enforce
* no abbreviations (essentially camel case) except for
* words like 'ORDER', 'TEST'.
* </p>
* <p>Configuration:</p>
* <pre>
* &lt;module name="AbbreviationAsWordInName"&gt;
* &lt;property name="allowedAbbreviations" value="ORDER, TEST"/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
* <pre>
* public class Test {
* void getORDER_TEST() {} // OK, both abbreviations are allowed
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
Expand Down Expand Up @@ -583,7 +601,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 @@ -10,6 +10,7 @@
&lt;a href="https://checkstyle.org/styleguides/google-java-style-20180523/javaguide.html#s5.3-camel-case"&gt;
Google Style Guide&lt;/a&gt; to get to know how to avoid long abbreviations in names.
&lt;/p&gt;
&lt;p&gt;'_' is considered as word separator in identifier name.&lt;/p&gt;
&lt;p&gt;
{@code allowedAbbreviationLength} specifies how many consecutive capital letters are
allowed in the identifier.
Expand Down
Expand Up @@ -467,4 +467,17 @@ public void testReceiver() throws Exception {
expected);
}

@Test
public void testInputAbbreviationAsWordInNameTypeWithUnderScore() throws Exception {
final String[] expected = {
"13:17: " + getWarningMessage("HBCK_LOCK_PATH", 4),
"16:15: " + getWarningMessage("BOOL_VALS", 4),
"24:10: " + getWarningMessage("getNONE_Test", 4),
"42:13: " + getWarningMessage("LINE_SEP", 4),
};

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

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


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

public class InputAbbreviationAsWordInNameType7 {
// violation below 'Abb.* in name 'HBCK_LOCK_PATH' must contain no .* than '4' .*cap.* let.*.'
private int HBCK_LOCK_PATH;

// violation below 'Abb.* in name 'BOOL_VALS' must contain no more than '4' .*cap.* let.*.'
Boolean[] BOOL_VALS = { false, true };

void getTEST() {
} // ok

void getORDER_OBSERVATION() {} // ok

// violation below 'Abb.* in name 'getNONE_Test' must contain no more than '4' .*cap.* let.*.'
void getNONE_Test() {}

void getCLR_Test() {} // ok

void getUNDERSCORE() {} // ok

void getTEST_OBSERVATION() {} // ok

void getTEST_UNDERSCORE() {} // ok

void getORDER() {} // ok

void getOBSERVATION() {} // ok

void getORDER_UNDERSCORE() {} // ok

int getCLRTest() { // ok
// violation below 'Abb.* in name 'LINE_SEP' must contain no more than '4' .*cap.* let.*.'
int LINE_SEP = 1;
return LINE_SEP;
}

void getNON_ETest() {} // OK


}
19 changes: 19 additions & 0 deletions src/xdocs/config_naming.xml
Expand Up @@ -37,6 +37,8 @@
to get to know how to avoid long abbreviations in names.
</p>

<p>'_' is considered as word separator in identifier name.</p>

<p>
<code>allowedAbbreviationLength</code> specifies how many consecutive capital letters are
allowed in the identifier.
Expand Down Expand Up @@ -344,6 +346,23 @@ 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 'ORDER', 'TEST'.
</p>
<p>Configuration:</p>
<source>
&lt;module name="AbbreviationAsWordInName"&gt;
&lt;property name="allowedAbbreviations" value="ORDER, TEST"/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
public class Test {
void getORDER_TEST() {} // OK, both abbreviations are allowed
}
</source>
</subsection>
Expand Down

0 comments on commit 12db2b4

Please sign in to comment.