Skip to content
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

Closed
wants to merge 24 commits into from
Closed

Conversation

jborgers
Copy link
Owner

@jborgers jborgers commented Mar 11, 2024

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

  • Fixes #

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

stokpop and others added 21 commits January 20, 2022 16:06
…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
…le partly replacement of missing typeIs function pmd#4858
docs/_data/xpath_funs.yml Show resolved Hide resolved
}
};
}

}

class TestUtil {
Copy link
Collaborator

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?

Copy link
Owner Author

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) {
Copy link
Collaborator

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

Copy link
Owner Author

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);
Copy link
Collaborator

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

Copy link
Owner Author

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) {
Copy link
Collaborator

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?

Copy link
Owner Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any change on NPE?

Copy link
Owner Author

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<>();
Copy link
Collaborator

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)?

Copy link
Owner Author

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);
}

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment can be removed?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jborgers
Copy link
Owner Author

This PR will not be processed further, taken over by the proper other PR.

@jborgers jborgers closed this Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants