Skip to content

Commit 045c141

Browse files
authored
don't use String.length for literal count in MultipleStringLiterals, … (#319)
* don't use String.length for literal count in MultipleStringLiterals, fixes #318 * added SuppressWarnings test for MultipleStringLiterals
1 parent a590151 commit 045c141

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/checkstyle/checks/literal/MultipleStringLiteralsCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class MultipleStringLiteralsCheck extends Check {
4040
if (!filterLiteral(literalToken.parent)) continue;
4141
// skip string object keys issue #116
4242
if (literalToken.parent.tok.match(BrOpen)) continue;
43+
if (isPosSuppressed(literalToken.pos)) continue;
4344

4445
switch (literalToken.tok) {
4546
case Const(CString(s)):
4647
if (StringUtils.isStringInterpolation(s, checker.file.content, literalToken.pos)) continue;
4748
if (ignoreRE.match(s)) continue;
4849
if (s.length < minLength) continue;
4950
if (checkLiteralCount(s, allLiterals)) {
50-
if (isPosSuppressed(literalToken.pos)) continue;
51-
logPos('String "$s" appears ${s.length} times in the file', literalToken.pos);
51+
logPos('String "$s" appears ${allLiterals.get(s)} times in the file', literalToken.pos);
5252
}
5353
default:
5454
}

test/checks/literal/MultipleStringLiteralsCheckTest.hx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ class MultipleStringLiteralsCheckTest extends CheckTestCase<MultipleStringLitera
1111
assertNoMsg(check, SINGLE_CHARS);
1212
assertNoMsg(check, THREE_SPACE);
1313
assertNoMsg(check, OBJECT_FIELD_KEYS_ISSUE_116);
14+
assertNoMsg(check, SUPPRESSION);
1415
}
1516

1617
public function testMultipleStringLiterals() {
1718
var check = new MultipleStringLiteralsCheck();
1819
assertMsg(check, THREE_XML, 'String "xml" appears 3 times in the file');
1920
assertMsg(check, THREE_XML_SWITCH, 'String "xml" appears 3 times in the file');
20-
assertMsg(check, OBJECT_FIELD_VALUES_ISSUE_116, 'String "duplicate" appears 9 times in the file');
21+
assertMsg(check, OBJECT_FIELD_VALUES_ISSUE_116, 'String "duplicate" appears 4 times in the file');
22+
assertMsg(check, EXAGGERATION_ISSUE_318, 'String "user.name" appears 3 times in the file');
2123
}
2224

2325
public function testIgnoreRegEx() {
@@ -32,8 +34,8 @@ class MultipleStringLiteralsCheckTest extends CheckTestCase<MultipleStringLitera
3234
check.allowDuplicates = 1;
3335
assertNoMsg(check, INTERPOLATION_ISSUE_109);
3436
#if (haxeparser < "3.3.0")
35-
assertMsg(check, NO_INTERPOLATION_ISSUE_109, 'String "value $$$$is i" appears 12 times in the file');
36-
assertMsg(check, NO_INTERPOLATION_AT_START_ISSUE_109, 'String "$$$$is i" appears 6 times in the file');
37+
assertMsg(check, NO_INTERPOLATION_ISSUE_109, 'String "value $$$$is i" appears 2 times in the file');
38+
assertMsg(check, NO_INTERPOLATION_AT_START_ISSUE_109, 'String "$$$$is i" appears 2 times in the file');
3739
#end
3840
}
3941
}
@@ -158,4 +160,25 @@ abstract MultipleStringLiteralsCheckTests(String) to String {
158160
trace('$$is i');
159161
}
160162
}";
163+
164+
var EXAGGERATION_ISSUE_318 = "
165+
class Test {
166+
function foo() {
167+
name = new Value<String>(System.storage.get('user.name'), function(to, _) System.storage.set('user.name', to));
168+
}
169+
function clear() {
170+
System.storage.remove('user.name');
171+
}
172+
}";
173+
174+
var SUPPRESSION = "
175+
class Test {
176+
function foo() {
177+
name = new Value<String>(System.storage.get('user.name'), function(to, _) System.storage.set('user.name', to));
178+
}
179+
@SuppressWarnings('checkstyle:MultipleStringLiterals')
180+
function clear() {
181+
System.storage.remove('user.name');
182+
}
183+
}";
161184
}

0 commit comments

Comments
 (0)