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

SpaceAssignmentRule: insert new line instead of replacing existing method call to handle complex single line scenarios #416

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
}
}
Loading