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

Nested class, interface or enum shouldn't be publicly visible #446

Merged
merged 11 commits into from Oct 5, 2017
Merged

Nested class, interface or enum shouldn't be publicly visible #446

merged 11 commits into from Oct 5, 2017

Conversation

tagantroy
Copy link
Contributor

@tagantroy tagantroy commented Oct 1, 2017

Hello, Implementation for #444
Hacktoberfest!

Copy link
Member

@schalkms schalkms left a 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 StyleGuideProviderand the detekt-config.


private fun isInternal(klass: KtClass): Boolean {
return klass.visibilityModifierType() == KtTokens.INTERNAL_KEYWORD
}
Copy link
Member

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

Copy link
Contributor Author

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
}

Copy link
Member

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thx!

@tagantroy
Copy link
Contributor Author

@schalkms hello, can you review again?

|| this.hasModifier(KtTokens.INTERNAL_KEYWORD))
}

fun KtModifierListOwner.isInternal(): Boolean {
Copy link
Contributor

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)

Copy link
Contributor Author

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

@arturbosch arturbosch Oct 2, 2017

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)

Copy link
Contributor Author

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Done

@arturbosch
Copy link
Member

Ok cool, thanks for the changes and contribution. I will wait for @schalkms to approve this now too and merge it :).

@arturbosch arturbosch added this to the RC5 milestone Oct 5, 2017
@arturbosch arturbosch merged commit 500fa2a into detekt:master Oct 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants