Removed unnecessary XPath function "hasChildren"

This commit is contained in:
Andreas Dangel 2022-03-04 20:11:23 +01:00
parent 2a73c2b312
commit 3db42ba1e9
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 1 additions and 84 deletions

View File

@ -172,18 +172,3 @@ langs:
outcome: "Matches calls to any method with 2 `int` parameters (!= argument)"
- code: '//ConstructorCall[pmd-java:matchesSig("java.util.ArrayList#new(int)")]'
outcome: "Matches constructors calls of ArrayList with a single int parameter"
- name: "Kotlin"
ns: "pmd-kotlin"
funs:
- name: hasChildren
returnType: "xs:boolean"
shortDescription: "Tests if a node has children"
description: "Returns true if the Node has children. This is for test purposes only."
parameters:
- name: "count"
type: "xs:decimal"
description: "The number of children."
examples:
- code: '//Identifier[pmd-kotlin:hasChildren(3)]'
outcome: "true or false"

View File

@ -7,16 +7,12 @@ package net.sourceforge.pmd.lang.kotlin;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.kotlin.ast.PmdKotlinParser;
import net.sourceforge.pmd.lang.kotlin.rule.xpath.internal.BaseContextNodeTestFun;
import net.sourceforge.pmd.lang.rule.xpath.impl.XPathHandler;
public class KotlinHandler extends AbstractPmdLanguageVersionHandler {
private static final XPathHandler XPATH_HANDLER =
XPathHandler.getHandlerForFunctionDefs(
BaseContextNodeTestFun.HAS_CHILDREN
);
private static final XPathHandler XPATH_HANDLER = XPathHandler.noFunctionDefinitions();
@Override
public XPathHandler getXPathHandler() {

View File

@ -1,64 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.kotlin.rule.xpath.internal;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.kotlin.ast.KotlinNode;
import net.sourceforge.pmd.lang.rule.xpath.internal.AstElementNode;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.BooleanValue;
import net.sf.saxon.value.SequenceType;
/**
* XPath function {@code pmd-kotlin:hasChildren(count as xs:decimal) as xs:boolean}
*
* <p>Example XPath 3.1: {@code //Identifier[pmd-kotlin:hasChildren(3)]}
*
* <p>Returns true if the node has children, false otherwise.
*/
public class BaseContextNodeTestFun<T extends KotlinNode> extends BaseKotlinXPathFunction {
static final SequenceType[] NO_ARGUMENTS = { SequenceType.SINGLE_INTEGER };
private final Class<T> klass;
public static final BaseKotlinXPathFunction HAS_CHILDREN = new BaseContextNodeTestFun<>(KotlinNode.class, "hasChildren");
protected BaseContextNodeTestFun(Class<T> klass, String localName) {
super(localName);
this.klass = klass;
}
@Override
public SequenceType[] getArgumentTypes() {
return NO_ARGUMENTS;
}
@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_BOOLEAN;
}
@Override
public boolean dependsOnFocus() {
return true;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
Node contextNode = ((AstElementNode) context.getContextItem()).getUnderlyingNode();
boolean hasChildren = contextNode.getNumChildren() == Integer.parseInt(arguments[0].head().getStringValue());
return BooleanValue.get(klass.isInstance(contextNode) && hasChildren);
}
};
}
}