Skip to content

Commit

Permalink
Merge pull request #329 from chali/ImproveConfigurationUsageRecognition
Browse files Browse the repository at this point in the history
Detect 'configuration methodCall()' usecase as a dependency declaration
  • Loading branch information
chali authored Apr 5, 2021
2 parents 9f21a28 + e05874e commit 3b6ff16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ abstract class GradleLintRule extends GroovyAstVisitor implements Rule {
return null
}
dependency = GradleDependency.fromConstant(expr)
} else if (call.arguments.expressions.any { it instanceof PropertyExpression } && project != null) {
} else if ((configurationWithArbitraryProperty(call) || configurationWithArbitraryMethodCall(call) )
&& project != null) {
Object dep
def shell = new GroovyShell()
shell.setVariable('project', project as Project)
Expand Down Expand Up @@ -468,6 +469,16 @@ abstract class GradleLintRule extends GroovyAstVisitor implements Rule {
}
}

//e.g. implementation sourceSets.main.output
private boolean configurationWithArbitraryProperty(MethodCallExpression call) {
call.arguments.expressions.any { it instanceof PropertyExpression }
}

//e.g. implementation fileTree(..)
private boolean configurationWithArbitraryMethodCall(MethodCallExpression call) {
call.arguments.expressions.any { it instanceof MethodCallExpression && it.methodAsString != 'project'}
}

private void visitMethodCallInPlugins(MethodCallExpression call) {
// https://docs.gradle.org/current/javadoc/org/gradle/plugin/use/PluginDependenciesSpec.html
def args = call.arguments.expressions as List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class GradleLintRuleSpec extends AbstractRuleSpec {
project.buildFile << """
dependencies {
customConfig sourceSets.main.output
customConfig fileTree('dir')
customConfig configurations.compile
}
"""
Expand All @@ -310,7 +311,7 @@ class GradleLintRuleSpec extends AbstractRuleSpec {

then:
rule.allGradleDependencies.size() == 1
rule.objectDependencies.size() == 1
rule.objectDependencies.size() == 2
}

def 'visit dependencies in a project path project block'() {
Expand Down

0 comments on commit 3b6ff16

Please sign in to comment.