diff --git a/src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy b/src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy index 6ef333aa..ada8978f 100644 --- a/src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy +++ b/src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy @@ -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 @@ -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 + " =") } diff --git a/src/main/groovy/com/netflix/nebula/lint/utils/IndentUtils.groovy b/src/main/groovy/com/netflix/nebula/lint/utils/IndentUtils.groovy new file mode 100644 index 00000000..2873fe82 --- /dev/null +++ b/src/main/groovy/com/netflix/nebula/lint/utils/IndentUtils.groovy @@ -0,0 +1,7 @@ +package com.netflix.nebula.lint.utils + +class IndentUtils { + static String indentText(def node, String text) { + return (' ' * (node.columnNumber - 1)) + text + } +} diff --git a/src/test/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRuleSpec.groovy b/src/test/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRuleSpec.groovy index 0c0ee0e0..f2c802c5 100644 --- a/src/test/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRuleSpec.groovy +++ b/src/test/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRuleSpec.groovy @@ -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 @@ -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'() { @@ -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') } }