Skip to content

Commit

Permalink
SpaceAssignmentRule: handle scenario where group method can belong to…
Browse files Browse the repository at this point in the history
… non gradle classes
rpalcolea committed Jan 13, 2025
1 parent aa38863 commit 103235f
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -15,6 +15,9 @@ class SpaceAssignmentRule extends ModelAwareGradleLintRule {
if(dslStack().contains("plugins")) {
return
}
if(call.methodAsString == 'group' && !isGradleGroup(call)) {
return
}
if (call.arguments.size() != 1 || call.arguments[-1] instanceof ClosureExpression) {
return
}
@@ -45,6 +48,18 @@ class SpaceAssignmentRule extends ModelAwareGradleLintRule {
}
}

private boolean isGradleGroup(MethodCallExpression call) {
if(call.methodAsString != 'group') {
return false
}

return dslStack().empty ||
dslStack().containsAll(['subprojects']) ||
dslStack().containsAll(['allprojects']) ||
dslStack().contains('configureEach') ||
dslStack().contains('tasks')
}

private void addViolation(MethodCallExpression call) {
BuildFiles.Original originalFile = buildFiles.original(call.lineNumber)
String replacement = IndentUtils.indentText(call, getReplacement(call))
Original file line number Diff line number Diff line change
@@ -43,13 +43,28 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
println(Pattern.quote("test"))
SystemProperties i = SystemProperties.getInstance()
i.getJavaIoTmpDir()
group 'com.netflix.test'
subprojects {
group 'com.netflix.test'
}
allprojects {
group 'com.netflix.test'
}
def matcher = ("test" =~ /ab[d|f]/)
if (matcher.find()) {
def x = matcher.group(1).replace(".", "/")
}
"""

when:
def result = runTasks('autoLintGradle', '--warning-mode', 'none')

then:
result.output.contains("6 problems (0 errors, 6 warnings)")
result.output.contains("9 problems (0 errors, 9 warnings)")

when:
runTasks('fixLintGradle', '--warning-mode', 'none', '--no-configuration-cache')
@@ -61,6 +76,10 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
buildFile.text.contains('maven { url = "https://another.example2.com" }')
!buildFile.text.contains('maven { maven { url = "https://another.example2.com" } }')
buildFile.text.contains('sourceCompatibility = JavaVersion.VERSION_1_8')
buildFile.text.contains('group = \'com.netflix.test\'')
!buildFile.text.contains('group \'com.netflix.test\'')
buildFile.text.contains('matcher.group(1)')
!buildFile.text.contains('matcher.group = (1)')

and:
runTasks('help')

0 comments on commit 103235f

Please sign in to comment.