[plsql] Add new sample rule AvoidTabCharacter

This commit is contained in:
Andreas Dangel
2019-03-11 17:08:54 +01:00
committed by Andreas Dangel
parent 9f80002250
commit d1414bccb8
4 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,38 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule.codestyle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule;
public class AvoidTabCharacterRule extends AbstractPLSQLRule {
public AvoidTabCharacterRule() {
addRuleChainVisit(ASTInput.class);
}
@Override
public Object visit(ASTInput node, Object data) {
try (BufferedReader in = new BufferedReader(new StringReader(node.getSourcecode()))) {
String line;
int lineNumber = 0;
while ((line = in.readLine()) != null) {
lineNumber++;
if (line.indexOf('\t') != -1) {
addViolationWithMessage(data, null, "Tab characters are not allowed. Use spaces for intendation",
lineNumber, lineNumber);
break;
}
}
} catch (IOException e) {
throw new RuntimeException("Error while executing rule AvoidTabCharacter", e);
}
return data;
}
}

View File

@ -9,6 +9,24 @@
Rules which enforce a specific coding style.
</description>
<rule name="AvoidTabCharacter"
language="plsql"
since="6.13.0"
message="Avoid tab characters for indentation. Use spaces instead."
class="net.sourceforge.pmd.lang.plsql.rule.codestyle.AvoidTabCharacterRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_codestyle.html#avoidtabcharacter">
<description>
This rule checks, that there are no tab characters (`\t`) in the source file.
It reports only the first occurrence per file.
Using tab characters for indentation is not recommended, since this requires that every developer
uses the same tab with in their editor.
This rule is the PMD equivalent of checkstyle's [FileTabCharacter](http://checkstyle.sourceforge.net/config_whitespace.html#FileTabCharacter) check.
</description>
<priority>3</priority>
</rule>
<rule name="CodeFormat"
language="plsql"
since="6.9.0"

View File

@ -0,0 +1,11 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule.codestyle;
import net.sourceforge.pmd.testframework.PmdRuleTst;
public class AvoidTabCharacterTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Using tabs - only the first tab character is reported</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<code><![CDATA[
BEGIN
SELECT cmer_id
,version
,cmp_id
BULK COLLECT INTO v_cmer_ids
,v_versions
,v_cmp_ids
FROM cmer;
END;
/
]]></code>
<source-type>plsql</source-type>
</test-code>
<test-code>
<description>No tabs</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
BEGIN
SELECT cmer_id
,version
,cmp_id
BULK COLLECT INTO v_cmer_ids
,v_versions
,v_cmp_ids
FROM cmer;
END;
/
]]></code>
<source-type>plsql</source-type>
</test-code>
</test-data>