New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pmd/7.0.x-kotlin/hasImport #2
Conversation
…d language" except $. Generate your Parser did not succeed because combining lexer and parser grammars has issues.
…ammar; and move PmdKotlinParser.java to the right place
…lasses.g4 file, added <libDirectory> to maven antlr plugin to make it work
… with unit test cases
…for custom kotlin xpath functions
…found by pmd-sonar plugin
…T in sonatype snapshots repo
…le partly replacement of missing typeIs function pmd#4858
} | ||
}; | ||
} | ||
|
||
} | ||
|
||
class TestUtil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we put TestUtil in the Base class? And do we need an intermediate KotlinXPath class?
See: java functions in pmd master
and: children example Kotlin
Better create one sub class per Function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Base in the name is misleading, it is not a (abstract) base class, it inherits from BaseKotlinXPathFunction, the base class where specific functions should inherit from. hasImport is combined with hasChildren, at least for now. 'Test' may be misleading, it is not like a unit test, but rather for testing nodes for properties. Renamed TestUtil to TestFunUtil.
private static boolean headerHasImport(final @NonNull String reqPackage, final KotlinParser.KtImportList node) { | ||
int numChildren = node.getNumChildren(); | ||
for (int i = 0; i < numChildren; i++) { | ||
if (node.getChild(i) instanceof KotlinParser.KtImportHeader) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put node.getChild(i) in variable: is used twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
int numChildren = root.getNumChildren(); | ||
for (int i = 0; i < numChildren; i++) { | ||
if (root.getChild(i) instanceof KotlinParser.KtImportList) { | ||
aHeaderHasImport = headerHasImport(reqPackage, (KotlinParser.KtImportList)node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to pass root.getChild(i)
instead? put in variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well spotted, fixed
Node root = node.getRoot(); | ||
int numChildren = root.getNumChildren(); | ||
for (int i = 0; i < numChildren; i++) { | ||
if (root.getChild(i) instanceof KotlinParser.KtImportList) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use import static for readability? Can use KtImportList
instead of KotlinParser.KtImportList
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
int numChildren = node.getNumChildren(); | ||
for (int i = 0; i < numChildren; i++) { | ||
if (node.getChild(i) instanceof KotlinParser.KtImportHeader) { | ||
List<KotlinParser.KtSimpleIdentifier> simpleIdentifiers = ((KotlinParser.KtImportHeader) node.getChild(i)).identifier().simpleIdentifier(); // multiple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any change on NPE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on node? Made argument @nonnull
for (int i = 0; i < numChildren; i++) { | ||
if (node.getChild(i) instanceof KotlinParser.KtImportHeader) { | ||
List<KotlinParser.KtSimpleIdentifier> simpleIdentifiers = ((KotlinParser.KtImportHeader) node.getChild(i)).identifier().simpleIdentifier(); // multiple | ||
List<String> importParts = new ArrayList<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
candidate for function extractIndentifierTexts(node)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe. Extracted method joinIdentifiersText.
return node.getNumChildren() == Integer.parseInt(reqNumChildren); | ||
} | ||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment can be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
This PR will not be processed further, taken over by the proper other PR. |
Describe the PR
Introduction of the hasImport XPath function for Kotlin, to replace boilerplate XPath code we need because of missing typeIs function.
This PR is incorrectly on master, should be on pmd/7.0.x branch, see other PR.
Related issues
pmd#4858
pmd#3808
Ready?
./mvnw clean verify
passes (checked automatically by github actions)