Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #12409: Inconsistent allowedAbbreviations #12472

Merged
merged 1 commit into from Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,27 @@
* }
* </pre>
* <p>
* To configure to check variables, enforce
* no abbreviations (essentially camel case) except for
* words like 'ALLOWED'.
* </p>
* <p>Configuration:</p>
* <pre>
* &lt;module name="AbbreviationAsWordInName"&gt;
* &lt;property name="allowedAbbreviations" value="ALLOWED"/&gt;
* &lt;property name="ignoreStaticFinal" value="false"/&gt;
* &lt;/module&gt;
* </pre>
* <p>Example:</p>
* <pre>
* public class MyClass {
* public int counterXYZ = 1; // OK
* public final int customerID = 2; // OK
* public static int nextID = 3; // OK
* public static final int MAX_ALLOWED = 4; // OK, abbreviation is allowed
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
Expand Down Expand Up @@ -583,8 +605,17 @@ private String getDisallowedAbbreviation(String str) {
else if (abbrStarted) {
abbrStarted = false;

final int endIndex = index - 1;
result = getAbbreviationIfIllegal(str, beginIndex, endIndex);
final int endIndex;
final int allowedLength;
if (symbol == '_') {
endIndex = index;
allowedLength = allowedAbbreviationLength + 1;
rnveach marked this conversation as resolved.
Show resolved Hide resolved
}
else {
endIndex = index - 1;
allowedLength = allowedAbbreviationLength;
}
result = getAbbreviationIfIllegal(str, beginIndex, endIndex, allowedLength);
if (result != null) {
break;
}
Expand All @@ -594,7 +625,7 @@ else if (abbrStarted) {
// if abbreviation at the end of name (example: scaleX)
if (abbrStarted) {
final int endIndex = str.length() - 1;
result = getAbbreviationIfIllegal(str, beginIndex, endIndex);
result = getAbbreviationIfIllegal(str, beginIndex, endIndex, allowedAbbreviationLength);
}
return result;
}
Expand All @@ -606,13 +637,15 @@ else if (abbrStarted) {
* @param str name
* @param beginIndex begin index
* @param endIndex end index
* @param allowedLength maximum allowed length for Abbreviation
* @return the abbreviation if it is bigger than required and not in the
* ignore list, otherwise {@code null}
*/
private String getAbbreviationIfIllegal(String str, int beginIndex, int endIndex) {
private String getAbbreviationIfIllegal(String str, int beginIndex, int endIndex,
int allowedLength) {
String result = null;
final int abbrLength = endIndex - beginIndex;
if (abbrLength > allowedAbbreviationLength) {
if (abbrLength > allowedLength) {
final String abbr = getAbbreviation(str, beginIndex, endIndex);
if (!allowedAbbreviations.contains(abbr)) {
result = abbr;
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,22 @@ public void testReceiver() throws Exception {
expected);
}

@Test
public void testInputAbbreviationAsWordInNameTypeSnakeStyle() throws Exception {
final String[] expected = {
"13:20: " + getWarningMessage("FLAG_IS_FIRST_RUN", 4),
"16:17: " + getWarningMessage("HYBRID_LOCK_PATH", 4),
"21:17: " + getWarningMessage("__DEMOS__TESTS_VAR", 4),
"28:16: " + getWarningMessage("TESTING_FAM_23456", 4),
"33:16: " + getWarningMessage("TESTING_23456_FAM", 4),
"38:16: " + getWarningMessage("_234VIOLATION", 4),
"41:16: " + getWarningMessage("VIOLATION23456", 4),
"72:21: " + getWarningMessage("getIsFIRST_Run", 4),
"77:21: " + getWarningMessage("getBoolean_VALUES", 4),
};

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

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


*/


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

public class InputAbbreviationAsWordInNameTypeSnakeStyle {
// violation below 'name 'FLAG_IS_FIRST_RUN' must contain no more than '4' .* cap.*.'
public boolean FLAG_IS_FIRST_RUN = false;

// violation below 'name 'HYBRID_LOCK_PATH' must contain no more than '4' .* cap.*.'
private int HYBRID_LOCK_PATH;

Boolean[] BOOL_VALS = { false, true };

// violation below 'name '__DEMOS__TESTS_VAR' must contain no more than '4' .* cap.*.'
private int __DEMOS__TESTS_VAR = 5;

private int __DEMO__TEST_VAR = 6;
romani marked this conversation as resolved.
Show resolved Hide resolved

private int TEST_FAM_23456 = 5;

// violation below 'name 'TESTING_FAM_23456' must contain no more than '4' .* cap.*.'
public int TESTING_FAM_23456 = 10;

private int TEST_23456_FAM = 15;

// violation below 'name 'TESTING_23456_FAM' must contain no more than '4' .* cap.*.'
public int TESTING_23456_FAM = 20;

public int TEST23456 = 30;

// violation below 'name '_234VIOLATION' must contain no more than '4' .* cap.*.'
public int _234VIOLATION = 40;

// violation below 'name 'VIOLATION23456' must contain no more than '4' .* cap.*.'
public int VIOLATION23456 = 50;

void getTEST() {
} // ok

void getORDER_OBSERVATION() {} // ok

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
int LINE_SEP = 1;
return LINE_SEP;
}

void getNON_ETest() {} // OK

// violation below 'name 'getIsFIRST_Run' must contain no more than '4' .* cap.*.'
private boolean getIsFIRST_Run() {
return false;
}

// violation below 'name 'getBoolean_VALUES' must contain no more than '4' .* cap.*.'
private boolean getBoolean_VALUES() {
return BOOL_VALS[0];
}
}
23 changes: 23 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,27 @@ 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 'ALLOWED'.
</p>
<p>Configuration:</p>
<source>
&lt;module name="AbbreviationAsWordInName"&gt;
&lt;property name="allowedAbbreviations" value="ALLOWED"/&gt;
&lt;property name="ignoreStaticFinal" value="false"/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
public class MyClass {
public int counterXYZ = 1; // OK
public final int customerID = 2; // OK
public static int nextID = 3; // OK
public static final int MAX_ALLOWED = 4; // OK, abbreviation is allowed
}
</source>
</subsection>
Expand Down