-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[html] Add visitor, add tests for xpath and java rules
- Loading branch information
Showing
14 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/HtmlVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.html.ast; | ||
|
||
public interface HtmlVisitor { | ||
|
||
Object visit(HtmlNode node, Object data); | ||
|
||
Object visit(HtmlCDataNode node, Object data); | ||
|
||
Object visit(HtmlComment node, Object data); | ||
|
||
Object visit(HtmlDocument node, Object data); | ||
|
||
Object visit(HtmlDocumentType node, Object data); | ||
|
||
Object visit(HtmlElement node, Object data); | ||
|
||
Object visit(HtmlTextNode node, Object data); | ||
|
||
Object visit(HtmlXmlDeclaration node, Object data); | ||
} |
51 changes: 51 additions & 0 deletions
51
pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/HtmlVisitorAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.html.ast; | ||
|
||
public class HtmlVisitorAdapter implements HtmlVisitor { | ||
|
||
@Override | ||
public Object visit(HtmlNode node, Object data) { | ||
for (HtmlNode child : node.children()) { | ||
child.acceptVisitor(this, data); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlCDataNode node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlComment node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlDocument node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlDocumentType node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlElement node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlTextNode node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlXmlDeclaration node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
pmd-html/src/main/java/net/sourceforge/pmd/lang/html/rule/AbstractHtmlRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.html.rule; | ||
|
||
import java.util.List; | ||
|
||
import net.sourceforge.pmd.RuleContext; | ||
import net.sourceforge.pmd.lang.LanguageRegistry; | ||
import net.sourceforge.pmd.lang.ast.Node; | ||
import net.sourceforge.pmd.lang.html.HtmlLanguageModule; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlCDataNode; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlComment; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlDocument; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlDocumentType; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlElement; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlNode; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlTextNode; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlVisitor; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlXmlDeclaration; | ||
import net.sourceforge.pmd.lang.rule.AbstractRule; | ||
|
||
public abstract class AbstractHtmlRule extends AbstractRule implements HtmlVisitor { | ||
|
||
public AbstractHtmlRule() { | ||
super.setLanguage(LanguageRegistry.getLanguage(HtmlLanguageModule.NAME)); | ||
} | ||
|
||
@Override | ||
public void apply(List<? extends Node> nodes, RuleContext ctx) { | ||
for (Node node : nodes) { | ||
if (node instanceof HtmlNode) { | ||
((HtmlNode) node).acceptVisitor(this, ctx); | ||
} | ||
} | ||
} | ||
|
||
// | ||
// The following APIs are identical to those in HtmlVisitorAdapter. | ||
// Due to Java single inheritance, it is preferred to extend from the more | ||
// complex Rule base class instead of from relatively simple Visitor. | ||
// | ||
// CPD-OFF | ||
|
||
@Override | ||
public Object visit(HtmlNode node, Object data) { | ||
for (HtmlNode child : node.children()) { | ||
child.acceptVisitor(this, data); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlCDataNode node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlComment node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlDocument node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlDocumentType node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlElement node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlTextNode node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlXmlDeclaration node, Object data) { | ||
return visit((HtmlNode) node, data); | ||
} | ||
|
||
// CPD-ON | ||
} |
74 changes: 74 additions & 0 deletions
74
pmd-html/src/test/java/net/sourceforge/pmd/lang/html/HtmlJavaRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | ||
*/ | ||
|
||
package net.sourceforge.pmd.lang.html; | ||
|
||
import java.io.StringReader; | ||
import java.util.Arrays; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import net.sourceforge.pmd.Report; | ||
import net.sourceforge.pmd.Rule; | ||
import net.sourceforge.pmd.RuleContext; | ||
import net.sourceforge.pmd.lang.LanguageRegistry; | ||
import net.sourceforge.pmd.lang.LanguageVersion; | ||
import net.sourceforge.pmd.lang.Parser; | ||
import net.sourceforge.pmd.lang.ast.Node; | ||
import net.sourceforge.pmd.lang.ast.xpath.Attribute; | ||
import net.sourceforge.pmd.lang.html.ast.HtmlElement; | ||
import net.sourceforge.pmd.lang.html.rule.AbstractHtmlRule; | ||
|
||
public class HtmlJavaRuleTest { | ||
// from https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.js_props_getter | ||
private static final String LIGHTNING_WEB_COMPONENT = "<!-- helloExpressions.html -->\n" | ||
+ "<template>\n" | ||
+ " <p>Hello, { greeting}!</p>\n" | ||
+ " <lightning-input label=\"Name\" value={ greeting} onchange=\"{ handleChange }\"></lightning-input>\n" | ||
+ " <div class=\"slds-m-around_medium\">\n" | ||
+ " <lightning-input name='firstName' label=\"First Name\" onchange={ handleChange }></lightning-input>\n" | ||
+ " <lightning-input name='lastName' label=\"Last Name\" onchange={handleChange}></lightning-input>\n" | ||
+ " <p class=\"slds-m-top_medium\">Uppercased Full Name: {uppercasedFullName}</p>\n" | ||
+ " </div>\n" | ||
+ "</template>"; | ||
|
||
@Test | ||
public void findAllAttributesWithInvalidExpression() { | ||
// "Don’t add spaces around the property, for example, { data } is not valid HTML." | ||
Rule rule = new AbstractHtmlRule() { | ||
@Override | ||
public String getMessage() { | ||
return "Invalid expression"; | ||
} | ||
|
||
@Override | ||
public Object visit(HtmlElement node, Object data) { | ||
for (Attribute attribute : node.getAttributes()) { | ||
if ("{".equals(attribute.getValue())) { | ||
RuleContext ctx = (RuleContext) data; | ||
ctx.addViolation(node); | ||
} | ||
} | ||
return super.visit(node, data); | ||
} | ||
}; | ||
Report report = runRule(LIGHTNING_WEB_COMPONENT, rule); | ||
Assert.assertEquals(2, report.getViolations().size()); | ||
Assert.assertEquals(4, report.getViolations().get(0).getBeginLine()); | ||
Assert.assertEquals(6, report.getViolations().get(1).getBeginLine()); | ||
} | ||
|
||
private Report runRule(String html, Rule rule) { | ||
LanguageVersion htmlLanguage = LanguageRegistry.findLanguageByTerseName(HtmlLanguageModule.TERSE_NAME).getDefaultVersion(); | ||
Parser parser = htmlLanguage.getLanguageVersionHandler().getParser(htmlLanguage.getLanguageVersionHandler().getDefaultParserOptions()); | ||
|
||
Node node = parser.parse("n/a", new StringReader(html)); | ||
RuleContext context = new RuleContext(); | ||
context.setLanguageVersion(htmlLanguage); | ||
context.setCurrentRule(rule); | ||
rule.apply(Arrays.asList(node), context); | ||
return context.getReport(); | ||
} | ||
} |
Oops, something went wrong.