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
Nested class, interface or enum shouldn't be publicly visible #446
Nested class, interface or enum shouldn't be publicly visible #446
Conversation
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.
Thanks for your contribution. You also need to add this rule to the StyleGuideProvider
and the detekt-config
.
|
||
private fun isInternal(klass: KtClass): Boolean { | ||
return klass.visibilityModifierType() == KtTokens.INTERNAL_KEYWORD | ||
} |
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.
Move this to Junk.kt
right under the following function.
fun KtModifierListOwner.isPublic()
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
private fun isPrivate(klass: KtClass): Boolean { | ||
return klass.visibilityModifierType() == KtTokens.PRIVATE_KEYWORD | ||
} | ||
|
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.
Same here
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
report(CodeSmell(issue, Entity.from(klass))) | ||
} | ||
|
||
private class VisibilityVisitor(val rule: NestedClassesVisibility) : DetektVisitor() { |
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.
You can also use an inner class here.
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.
yes, thx!
@schalkms hello, can you review again? |
|| this.hasModifier(KtTokens.INTERNAL_KEYWORD)) | ||
} | ||
|
||
fun KtModifierListOwner.isInternal(): Boolean { |
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.
let's do this?
fun KtModifierListOwner.isInternal() =
hasModifier(KtTokens.INTERNAL_KEYWORD)
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.
@vanniktech Thx! done
|
||
interface Test {} // report | ||
|
||
private interface PrivateTest {} // valid |
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.
Please add some cases for nested classes (nest-level > 2)
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 + fix for enums
} | ||
} | ||
|
||
private fun handleClass(klass: KtClass) { |
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.
This method could be inlined.
} | ||
|
||
private fun handleClass(klass: KtClass) { | ||
report(CodeSmell(issue, Entity.from(klass))) |
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 report the issue with a concrete description? Something along the lines of: `issue.copy(description = "Nested types are often used for implementing private functionality. However the visiblilty of ${klass.name} makes it visible externally.")
That mentions the class name and might make the description of the user a bit clearer to the user.
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 agree. Done
Ok cool, thanks for the changes and contribution. I will wait for @schalkms to approve this now too and merge it :). |
Hello, Implementation for #444
Hacktoberfest!