Ensure CPD uses tab width of 1 for tabs consistently

The columns that are reported by CPD were inconsistent across languages
before. A language like Java (using a JavaCC-based tokenizer) would use
a width of 8 for tabs, whereas a language like C# (using an Antlr-based
tokenizer) would use 1 instead.

This includes unit tests for most languages to ensure a tab character is
counted as 1. The configuration for JavaCC has been adjusted to respect
this as well.
This commit is contained in:
Maikel Steneker
2020-07-20 10:42:21 +02:00
parent 25405eb870
commit 6fb5ac59b9
45 changed files with 724 additions and 62 deletions

View File

@ -47,6 +47,11 @@ public class ApexTokenizerTest extends CpdTextComparisonTest {
doTest("comments");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
private Properties caseSensitive() {
return properties(true);
}

View File

@ -0,0 +1,10 @@
/*
* Some comment
*/
public with sharing class Simple {
public String someParam { get; set; }
public void getInit() {
someParam = "test";
}
}

View File

@ -0,0 +1,35 @@
[Image] or [Truncated image[ Bcol Ecol
L4
[public] 1 6
[with] 8 11
[sharing] 13 19
[class] 21 25
[simple] 27 32
[{] 34 34
L5
[public] 2 7
[string] 9 14
[someparam] 16 24
[{] 26 26
[get] 28 30
[;] 31 31
[set] 33 35
[;] 36 36
[}] 38 38
L7
[public] 2 7
[void] 9 12
[getinit] 14 20
[(] 21 21
[)] 22 22
[{] 24 24
L8
[someparam] 3 11
[=] 13 13
[test] 16 19
[;] 21 21
L9
[}] 2 2
L10
[}] 1 1
EOF

View File

@ -71,6 +71,10 @@
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/JavaCharStream.java"
token="throw new Error(t.getMessage())"
value="throw t" />
<!-- Set tab size of JavaCC to 1 -->
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/JavaCharStream.java"
token="int tabSize = 8;"
value="int tabSize = 1;" />
<move overwrite="true"
file="${target}/net/sourceforge/pmd/lang/ast/dummy/JavaCharStream.java"
@ -94,6 +98,10 @@
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/SimpleCharStream.java"
token="throw new Error(t.getMessage())"
value="throw t" />
<!-- Set tab size of JavaCC to 1 -->
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/SimpleCharStream.java"
token="int tabSize = 8;"
value="int tabSize = 1;" />
<move overwrite="true"

View File

@ -124,6 +124,11 @@ public class CPPTokenizerTest extends CpdTextComparisonTest {
doTest("issue-1784");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
private static Properties skipBlocks(String skipPattern) {
return properties(true, skipPattern);

View File

@ -0,0 +1 @@
int i = 0;

View File

@ -0,0 +1,8 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 2 4
[i] 6 6
[=] 8 8
[0] 10 10
[;] 11 11
EOF

View File

@ -85,6 +85,11 @@ public class CsTokenizerTest extends CpdTextComparisonTest {
doTest("usingDirectives", "_ignored", ignoreUsings());
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
private Properties ignoreUsings() {
return properties(true);
}

View File

@ -0,0 +1 @@
int i = 0;

View File

@ -0,0 +1,8 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 2 4
[i] 6 6
[=] 8 8
[0] 10 10
[;] 11 11
EOF

View File

@ -82,4 +82,9 @@ public class DartTokenizerTest extends CpdTextComparisonTest {
doTest("string_multiline");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

View File

@ -0,0 +1,3 @@
class MyClass {
var s1 = 'test';
}

View File

@ -0,0 +1,13 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[class] 1 5
[MyClass] 7 13
[{] 15 15
L2
[var] 2 4
[s1] 6 7
[=] 9 9
['test'] 11 16
L3
[}] 1 1
EOF

View File

@ -0,0 +1,47 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java;
import java.io.Reader;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.JavaCharStream;
import net.sourceforge.pmd.lang.java.ast.JavaParserTokenManager;
/**
* Java Token Manager implementation.
*
* @deprecated This is internal API, use {@link Parser#getTokenManager(String, Reader)} via
* {@link LanguageVersionHandler#getParser(ParserOptions)}.
*/
@Deprecated
@InternalApi
public class JavaTokenManager implements TokenManager {
private final JavaParserTokenManager tokenManager;
public JavaTokenManager(Reader source) {
final JavaCharStream javaCharStream = new JavaCharStream(source) {
public JavaCharStream withTabSize1() {
this.setTabSize(1);
return this;
}
}.withTabSize1();
tokenManager = new JavaParserTokenManager(javaCharStream);
}
@Override
public Object getNextToken() {
return tokenManager.getNextToken();
}
@Override
public void setFileName(String fileName) {
tokenManager.setFileName(fileName);
}
}

View File

@ -83,6 +83,11 @@ public class JavaTokenizerTest extends CpdTextComparisonTest {
doTest("ignoreLiterals", "_noignore");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
private static Properties ignoreAnnotations() {
return properties(true, false, false);

View File

@ -0,0 +1 @@
int i = 0;

View File

@ -0,0 +1,7 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 2 4
[i] 6 6
[=] 8 8
[0] 10 10
EOF

View File

@ -63,4 +63,9 @@ public class EcmascriptTokenizerTest extends CpdTextComparisonTest {
public void testTemplateStrings() {
doTest("templateStrings");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

View File

@ -0,0 +1,7 @@
function switchToRealPassword() {
var real = $('realPass')
var prompt = $('promptPass')
real.style.display = 'inline'
prompt.style.display = 'none'
real.focus()
}

View File

@ -0,0 +1,48 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[function] 1 8
[switchToRealPassword] 10 29
[(] 30 30
[)] 31 31
[{] 33 33
L2
[var] 2 4
[real] 6 9
[=] 11 11
[$] 13 13
[(] 14 14
['realPass'] 15 24
[)] 25 25
L3
[var] 2 4
[prompt] 6 11
[=] 13 13
[$] 15 15
[(] 16 16
['promptPass'] 17 28
[)] 29 29
L4
[real] 2 5
[.] 6 6
[style] 7 11
[.] 12 12
[display] 13 19
[=] 21 21
['inline'] 23 30
L5
[prompt] 2 7
[.] 8 8
[style] 9 13
[.] 14 14
[display] 15 21
[=] 23 23
['none'] 25 30
L6
[real] 2 5
[.] 6 6
[focus] 7 11
[(] 12 12
[)] 13 13
L7
[}] 1 1
EOF

View File

@ -40,4 +40,9 @@ public class KotlinTokenizerTest extends CpdTextComparisonTest {
public void testImportsIgnored() {
doTest("imports");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

View File

@ -0,0 +1,5 @@
var x = 0
fun increment() {
x += 1
}

View File

@ -0,0 +1,19 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[x] 5 5
[=] 7 7
[0] 9 9
L3
[fun] 1 3
[increment] 5 13
[(] 14 14
[)] 15 15
[{] 17 17
L4
[x] 2 2
[+=] 4 5
[1] 7 7
L5
[}] 1 1
EOF

View File

@ -34,4 +34,9 @@ public class LuaTokenizerTest extends CpdTextComparisonTest {
public void testFactorial() {
doTest("factorial");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

View File

@ -0,0 +1,2 @@
print("Hello World")

View File

@ -0,0 +1,7 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[print] 2 6
[(] 7 7
["Hello World"] 8 20
[)] 21 21
EOF

View File

@ -56,4 +56,9 @@ public class MatlabTokenizerTest extends CpdTextComparisonTest {
public void testDoubleQuotedStrings() {
doTest("doubleQuotedStrings");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

View File

@ -0,0 +1,6 @@
classdef LC
methods
function [obj, c,t, s ] = Classification( obj,m,t, cm )
end
end
end

View File

@ -0,0 +1,35 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[classdef] 1 8
[LC] 10 11
L2
[methods] 2 8
L3
[function] 3 10
[\[] 12 12
[obj] 13 15
[,] 16 16
[c] 18 18
[,] 19 19
[t] 20 20
[,] 21 21
[s] 23 23
[\]] 25 25
[=] 27 27
[Classification] 29 42
[(] 43 43
[obj] 45 47
[,] 48 48
[m] 49 49
[,] 50 50
[t] 51 51
[,] 52 52
[cm] 54 55
[)] 57 57
L4
[end] 3 5
L5
[end] 2 4
L6
[end] 1 3
EOF

View File

@ -41,4 +41,9 @@ public class ObjectiveCTokenizerTest extends CpdTextComparisonTest {
public void testUnicodeCharInIdent() {
doTest("unicodeCharInIdent");
}
@Test
public void testTabWidth() {
doTest("tabWidth");
}
}

Some files were not shown because too many files have changed in this diff Show More