Skip to content

Commit 62f92e8

Browse files
Merge pull request #477 from sleekbyte/at-460-ignoring-backticks
#460: Ignore enclosing backticks from identifier analysis.
2 parents f5f0ea6 + a584c6f commit 62f92e8

File tree

11 files changed

+76
-8
lines changed

11 files changed

+76
-8
lines changed

src/main/java/com/sleekbyte/tailor/listeners/ConstantNamingListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public void enterTopLevel(TopLevelContext topLevelContext) {
3030

3131
names.forEach(
3232
ctx -> {
33-
String constantName = ctx.getText();
33+
String constantName = CharFormatUtil.unescapeIdentifier(ctx.getText());
3434
ParserRuleContext constantDecContext = ConstantDecHelper.getConstantDeclaration(ctx);
35-
Location location = ListenerUtil.getContextStartLocation(ctx);
35+
Location location = ListenerUtil.getIdentifierStartLocation(ctx);
3636

3737
if (ConstantDecHelper.isGlobal(constantDecContext)
3838
|| ConstantDecHelper.insideClass(constantDecContext)

src/main/java/com/sleekbyte/tailor/listeners/KPrefixListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public void enterTopLevel(TopLevelContext topLevelCtx) {
2828
List<IdentifierContext> names = DeclarationListener.getConstantNames(topLevelCtx);
2929
names.forEach(
3030
ctx -> {
31-
String constantName = ctx.getText();
32-
Location location = ListenerUtil.getContextStartLocation(ctx);
31+
String constantName = CharFormatUtil.unescapeIdentifier(ctx.getText());
32+
Location location = ListenerUtil.getIdentifierStartLocation(ctx);
3333
if (CharFormatUtil.isKPrefixed(constantName)) {
3434
printer.warn(Rules.CONSTANT_K_PREFIX, Messages.CONSTANT + Messages.NAME + Messages.K_PREFIXED,
3535
location);

src/main/java/com/sleekbyte/tailor/listeners/LowerCamelCaseListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public void enterUnionStyleEnumCase(SwiftParser.UnionStyleEnumCaseContext ctx) {
4949
}
5050

5151
private void verifyLowerCamelCase(String constructType, ParserRuleContext ctx) {
52-
String constructName = ctx.getText();
52+
String constructName = CharFormatUtil.unescapeIdentifier(ctx.getText());
5353
if (!CharFormatUtil.isLowerCamelCaseOrAcronym(constructName)) {
54-
Location location = ListenerUtil.getContextStartLocation(ctx);
54+
Location location = ListenerUtil.getIdentifierStartLocation(ctx);
5555
this.printer.error(Rules.LOWER_CAMEL_CASE, constructType + Messages.LOWER_CAMEL_CASE, location);
5656
}
5757
}

src/main/java/com/sleekbyte/tailor/utils/CharFormatUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public static boolean isLowerCamelCaseOrAcronym(String word) {
2424
return startsWithAcronym(word) || isLowerCamelCase(word);
2525
}
2626

27+
public static boolean isEnclosedInBackticks(String identifier) {
28+
int length = identifier.length();
29+
return length >= 2 && identifier.charAt(0) == '`' && identifier.charAt(length - 1) == '`';
30+
}
31+
32+
/**
33+
* Will strip leading and trailing ` character in given string if both present.
34+
*
35+
* @param identifier value to sanitize
36+
* @return sanitized string
37+
*/
38+
public static String unescapeIdentifier(String identifier) {
39+
int length = identifier.length();
40+
if (isEnclosedInBackticks(identifier)) {
41+
return identifier.substring(1, length - 1);
42+
}
43+
return identifier;
44+
}
45+
2746
/**
2847
* Checks if a name is prefixed with a 'k' or 'K'.
2948
*

src/main/java/com/sleekbyte/tailor/utils/ListenerUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public static Location getContextStopLocation(ParserRuleContext ctx) {
3232
return new Location(ctx.getStop().getLine(), ctx.getStop().getCharPositionInLine() + 1);
3333
}
3434

35+
/**
36+
* Gets the start location of the context string.
37+
* @param ctx the context
38+
* @return the start location of the provided context's string
39+
*/
40+
public static Location getIdentifierStartLocation(ParserRuleContext ctx) {
41+
Location location = getContextStartLocation(ctx);
42+
// Ensure that the violation column number reports the character after the opening backtick.
43+
if (CharFormatUtil.isEnclosedInBackticks(ctx.getText())) {
44+
location.column += 1;
45+
}
46+
return location;
47+
}
48+
3549
public static Location getLocationOfChildToken(ParserRuleContext ctx, int childNumber) {
3650
Token token = ((TerminalNodeImpl) ctx.getChild(childNumber)).getSymbol();
3751
return ListenerUtil.getTokenLocation(token);

src/main/java/com/sleekbyte/tailor/utils/Pair.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Couples together a pair of values, which may be of different types (L and R).
55
* The individual values can be accessed via its public getter functions getFirst() and getSecond().
66
*
7-
* @param <L> Type of member first.
8-
* @param <R> Type of member second.
7+
* @param <L> Type of member first
8+
* @param <R> Type of member second
99
*/
1010
public final class Pair<L, R> {
1111
private final L first;

src/test/java/com/sleekbyte/tailor/functional/ConstantNamingTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected void addAllExpectedMsgs() {
6262
+ Messages.K_PREFIXED);
6363
addExpectedMsg(Rules.CONSTANT_K_PREFIX, 134, 20, Severity.WARNING, Messages.CONSTANT + Messages.NAME
6464
+ Messages.K_PREFIXED);
65+
addExpectedMsg(Rules.CONSTANT_NAMING, 152, 14, Severity.WARNING, Messages.CONSTANT + Messages.LOWER_CAMEL_CASE);
6566
}
6667

6768
@Override

src/test/java/com/sleekbyte/tailor/functional/LowerCamelCaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected void addAllExpectedMsgs() {
4444
addExpectedMsg(176, 10, Severity.WARNING, Messages.ENUM_CASE + Messages.NAMES + Messages.LOWER_CAMEL_CASE);
4545
addExpectedMsg(177, 10, Severity.WARNING, Messages.ENUM_CASE + Messages.NAMES + Messages.LOWER_CAMEL_CASE);
4646
addExpectedMsg(190, 10, Severity.WARNING, Messages.ENUM_CASE + Messages.NAMES + Messages.LOWER_CAMEL_CASE);
47+
addExpectedMsg(200, 10, Severity.WARNING, Messages.VARIABLE + Messages.NAMES + Messages.LOWER_CAMEL_CASE);
4748
}
4849

4950
private void addExpectedMsg(int line, int column, Severity severity, String msg) {

src/test/java/com/sleekbyte/tailor/utils/CharFormatUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sleekbyte.tailor.utils;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
56

@@ -88,4 +89,17 @@ public void testStartsWithAcronym() {
8889
assertFalse(CharFormatUtil.startsWithAcronym("xURLS"));
8990
assertFalse(CharFormatUtil.startsWithAcronym("shieldPROGRAMMEmarvel"));
9091
}
92+
93+
@Test
94+
public void testBacktickEscapedIdentifier() {
95+
// Backtick(s) are not part of the identifier
96+
assertTrue(CharFormatUtil.unescapeIdentifier("``").isEmpty());
97+
assertTrue(CharFormatUtil.unescapeIdentifier("").isEmpty());
98+
assertEquals("self", CharFormatUtil.unescapeIdentifier("`self`"));
99+
assertEquals("s",CharFormatUtil.unescapeIdentifier("`s`"));
100+
assertEquals("self",CharFormatUtil.unescapeIdentifier("`self`"));
101+
assertEquals("`self", CharFormatUtil.unescapeIdentifier("`self"));
102+
assertEquals("self`", CharFormatUtil.unescapeIdentifier("self`"));
103+
assertEquals("`self", CharFormatUtil.unescapeIdentifier("``self`"));
104+
}
91105
}

src/test/swift/com/sleekbyte/tailor/functional/ConstantNamingTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,10 @@ private struct Scaling {
145145
init?(URL url: NSURL, statusCode statusCode: Int, HTTPVersion HTTPVersion: String?, headerFields headerFields: [String: String]?) {
146146
}
147147

148+
let `open` = true
149+
var `close` = false
150+
151+
func demo() {
152+
let `Open` = true
153+
}
148154
}

src/test/swift/com/sleekbyte/tailor/functional/LowerCamelCaseTest.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,16 @@ enum SomeEnum: Int {
195195
}
196196
}
197197
}
198+
199+
func demo() {
200+
var `Open` = true
201+
var `close` = false
202+
let `open` = true
203+
}
204+
205+
func `func`() {
206+
}
207+
208+
enum Test {
209+
case `case`, `class`, notReserved
210+
}

0 commit comments

Comments
 (0)