Custom PMD rules for HTML #4657
-
Is it possible to write custom PMD rules for HTML using Java? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Yes, this is possible. |
Beta Was this translation helpful? Give feedback.
-
What's the latest maven library that contains this change? net.sourceforge.pmd pmd-core 6.55.0 |
Beta Was this translation helpful? Give feedback.
-
Sample Rule: <?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Sample PMD"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>CustomPMDRule</description>
<rule name="CustomPMDRule"
language="html"
class="pmd.CustomPMDRule"
message="Hello">
<description>Custom HTML rule example</description>
</rule>
</ruleset> Java File: package pmd;
import net.sourceforge.pmd.lang.html.ast.ASTHtmlElement;
import net.sourceforge.pmd.lang.html.rule.AbstractHtmlRule;
public class CustomPMDRule extends AbstractHtmlRule {
@Override
public Object visit(ASTHtmlElement node, Object data) {
System.out.println("Hello World");
addViolation("Error in HTML.", node);
return super.visit(node, data);
}
} Execution : Error: Cannot load ruleset- An XML validation error occurred Specifically it says : Error at class="pmd.CustomPMDRule" Steps I followed to install pmd Is there anything I'm missing? |
Beta Was this translation helpful? Give feedback.
Yes, this is possible.
See https://github.com/pmd/pmd/blob/master/pmd-html/src/main/java/net/sourceforge/pmd/lang/html/rule/bestpractices/UnnecessaryTypeAttributeRule.java for an example. The rule doc for this example is here: https://docs.pmd-code.org/latest/pmd_rules_html_bestpractices.html#unnecessarytypeattribute