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

Allow regex configuration support for Forbidden Import rule #1963

Merged

Conversation

sowmyav24
Copy link
Contributor

Fixes #1430

  • Introduce configuration forbiddenPatterns to specify forbidden regex for imports.
  • Report imports that match any of the configured patterns even though they are not in imports list

@@ -21,6 +21,8 @@ import org.jetbrains.kotlin.psi.KtImportDirective
* </noncompliant>
*
* @configuration imports - imports which should not be used (default: `''`)
* @configuration forbiddenPatterns - reports imports which match the specified regular expression.
For example `net.*R`. (default: `""`)
Copy link
Member

Choose a reason for hiding this comment

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

This should probably not be on a new line

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 !

@sowmyav24 sowmyav24 force-pushed the regex-support-for-forbidden-import branch from 43b5bfc to a5cebcc Compare October 1, 2019 10:16
@sowmyav24 sowmyav24 force-pushed the regex-support-for-forbidden-import branch from a5cebcc to d6943e0 Compare October 1, 2019 14:12
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 picking up this topic! This really improves this rule.
Please see my comments and only use force push if it’s really necessary.
This makes it easier for us to review the changes you made. Thanks!

@@ -53,7 +56,12 @@ class ForbiddenImport(config: Config = Config.empty) : Rule(config) {
}
}

private fun containsForbiddenPattern(import: String): Boolean {
return forbiddenPatterns.pattern.isNotEmpty() && forbiddenPatterns.containsMatchIn(import)
Copy link
Member

Choose a reason for hiding this comment

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

You could use the shorthand syntax for this function.

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 !

}

it("should report import when it matches any one of the pattern") {
val findings =
Copy link
Member

Choose a reason for hiding this comment

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

You could remove this test, because essentially we test the regex engine here. One test that asserts the positive case is enough.

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 !

@schalkms schalkms merged commit 842161d into detekt:master Oct 2, 2019
@tasomaniac
Copy link
Contributor

This is awesome. Thanks @sowmyav24

@arturbosch arturbosch added this to the 1.1.0 milestone Oct 4, 2019
smyachenkov pushed a commit to smyachenkov/detekt that referenced this pull request Dec 9, 2019
)

* Allow regex configuration named forbiddenPatterns for ForbiddenImport rule

* Use shorthand function and remove redundant test
smyachenkov pushed a commit to smyachenkov/detekt that referenced this pull request Dec 9, 2019
)

* Allow regex configuration named forbiddenPatterns for ForbiddenImport rule

* Use shorthand function and remove redundant test
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.

Support regex for forbidden imports
5 participants