Skip to content

Commit

Permalink
SpaceAssignmentRule: insert new line instead of replacing existing me…
Browse files Browse the repository at this point in the history
…thod call to handle complex single line scenarios (#416)
  • Loading branch information
rpalcolea authored Jan 8, 2025
1 parent 5b03c88 commit 3d9c3e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.netflix.nebula.lint.rule.dsl

import com.netflix.nebula.lint.rule.BuildFiles
import com.netflix.nebula.lint.rule.ModelAwareGradleLintRule
import com.netflix.nebula.lint.utils.IndentUtils
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression

Expand Down Expand Up @@ -33,16 +35,22 @@ class SpaceAssignmentRule extends ModelAwareGradleLintRule {
def deprecatedAnnotation = exactMethod.getAnnotation(Deprecated)
if (deprecatedAnnotation != null) {
// may be false positive when the explicit method is deprecated
addBuildLintViolation(description, call)
.replaceWith(call, getReplacement(call))
addViolation(call)
}
} else {
addBuildLintViolation(description, call)
.replaceWith(call, getReplacement(call))
addViolation(call)
}
}

def getReplacement(MethodCallExpression call){
private void addViolation(MethodCallExpression call) {
BuildFiles.Original originalFile = buildFiles.original(call.lineNumber)
String replacement = IndentUtils.indentText(call, getReplacement(call))
addBuildLintViolation(description, call)
.insertBefore(call, replacement)
.deleteLines(originalFile.file, originalFile.line..originalFile.line)
}

private String getReplacement(MethodCallExpression call){
def originalLine = getSourceCode().line(call.lineNumber-1)
return originalLine.replaceFirst(call.methodAsString, call.methodAsString + " =")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.netflix.nebula.lint.utils

class IndentUtils {
static String indentText(def node, String text) {
return (' ' * (node.columnNumber - 1)) + text
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
maven {
url = "https://another.example.com"
}
}
maven { url "https://another.example2.com" }
maven { url 'https://repo.spring.io/milestone' }
}
java {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -47,16 +49,21 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
def result = runTasks('autoLintGradle', '--warning-mode', 'none')

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

when:
runTasks('fixLintGradle', '--warning-mode', 'none')
runTasks('fixLintGradle', '--warning-mode', 'none', '--no-configuration-cache')

then:
buildFile.text.contains('buildDir = file("out")')
buildFile.text.contains('url = "https://example.com"')
buildFile.text.contains('url =("https://example2.com")')
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')

and:
runTasks('help')
}

def 'reports and fixes a violation if space assignment syntax is used in some complex cases'() {
Expand Down Expand Up @@ -145,5 +152,8 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
buildFile.text.contains('description = "t15"')
buildFile.text.contains('distributionPath = "t18"')
buildFile.text.contains('distributionPath = "t22"')

and:
runTasks('help', '--warning-mode', 'none')
}
}

0 comments on commit 3d9c3e0

Please sign in to comment.