Skip to content

Commit 3b6cc42

Browse files
committed
Space assignment rule handles ambiguous DSL for getting a project property
1 parent 2f1249d commit 3b6cc42

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.netflix.nebula.lint.rule.dsl
22

3+
34
import com.netflix.nebula.lint.rule.BuildFiles
45
import com.netflix.nebula.lint.rule.ModelAwareGradleLintRule
56
import com.netflix.nebula.lint.utils.IndentUtils
@@ -35,6 +36,13 @@ class SpaceAssignmentRule extends ModelAwareGradleLintRule {
3536
return // no matching property
3637
}
3738

39+
if (call.methodAsString == 'property' && call.objectExpression != null) {
40+
if (call.objectExpression.text?.toLowerCase()?.contains("project") || call.objectExpression.toString().contains("PropertyExpression")) {
41+
addViolationForGettingAProperty(call)
42+
return
43+
}
44+
}
45+
3846
// check if it's a generated method for space assignment
3947
def exactMethod = receiverClass.getMethods().find { it.name == invokedMethodName }
4048
if (exactMethod != null) {
@@ -71,4 +79,17 @@ class SpaceAssignmentRule extends ModelAwareGradleLintRule {
7179
def originalLine = getSourceCode().line(call.lineNumber-1)
7280
return originalLine.replaceFirst(call.methodAsString, call.methodAsString + " =")
7381
}
82+
83+
private void addViolationForGettingAProperty(MethodCallExpression call) {
84+
BuildFiles.Original originalFile = buildFiles.original(call.lineNumber)
85+
86+
def violation = addBuildLintViolation(description + ". Getters for properties should use unambiguous DSL", call)
87+
def violationLineText = violation.files.text.readLines().subList(call.lineNumber - 1, call.lastLineNumber)[0]
88+
def replacement = violationLineText.replaceFirst(".property", ".findProperty")
89+
90+
violation.insertBefore(call, replacement)
91+
.deleteLines(originalFile.file, originalFile.line..originalFile.line)
92+
}
93+
94+
7495
}

src/test/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRuleSpec.groovy

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import spock.lang.Subject
66
@Subject(SpaceAssignmentRule)
77
class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
88

9+
def setup() {
10+
System.setProperty("ignoreDeprecations", "true")
11+
}
12+
13+
def cleanup() {
14+
System.setProperty("ignoreDeprecations", "false")
15+
}
16+
917
def 'reports and fixes a violation if space assignment syntax is used - simple cases'() {
1018
buildFile << """
1119
import java.util.regex.Pattern;
@@ -182,4 +190,80 @@ class SpaceAssignmentRuleSpec extends BaseIntegrationTestKitSpec {
182190
and:
183191
runTasks('help', '--warning-mode', 'none')
184192
}
193+
194+
def 'reports and fixes a violation if space assignment syntax is used as a property getter - simple cases'() {
195+
buildFile << """
196+
plugins {
197+
id 'java'
198+
id 'nebula.lint'
199+
}
200+
gradleLint.rules = ['space-assignment']
201+
tasks.register('task1') {
202+
if (project.hasProperty('myCustomDescription')) {
203+
description = project.property('myCustomDescription')
204+
}
205+
doLast {
206+
logger.warn "myCustomDescription: \${description}"
207+
}
208+
}
209+
tasks.register('task2', Exec) {
210+
commandLine "echo"
211+
args ('--task2', project.property("myCustomProperty1"))
212+
doLast {
213+
logger.warn "myCustomProperty1: \${project.property('myCustomProperty1')}"
214+
}
215+
}
216+
def a = project.property "myCustomProperty2"
217+
218+
def subA = project(':subA')
219+
subA.afterEvaluate { evaluatedProject ->
220+
def b = project.rootProject.allprojects.find { it.name == "subA"}.property("myCustomPropertySubA1")
221+
def c = rootProject.childProjects.subA.property("myCustomPropertySubA2")
222+
def d = project.findProject(':subA').property("myCustomPropertySubA3")
223+
tasks.register('showProperties') {
224+
doLast {
225+
logger.warn "myCustomProperty2: \${a}"
226+
logger.warn "subA.myCustomPropertySubA1: \${b}"
227+
logger.warn "subA.myCustomPropertySubA2: \${c}"
228+
logger.warn "subA.myCustomPropertySubA3: \${d}"
229+
}
230+
}
231+
}
232+
""".stripIndent()
233+
234+
new File(projectDir, "gradle.properties") << """)
235+
myCustomDescription=This is a custom description
236+
myCustomProperty1=property1
237+
myCustomProperty2=property2
238+
""".stripIndent()
239+
240+
addSubproject('subA', """
241+
ext {
242+
myCustomPropertySubA1 = "subA-one"
243+
myCustomPropertySubA2 = "subA-two"
244+
myCustomPropertySubA3 = "subA-three"
245+
}
246+
""".stripIndent())
247+
248+
when:
249+
runTasks('fixLintGradle', '--warning-mode', 'none')
250+
def results = runTasks('task1', 'task2', 'showProperties')
251+
252+
then: "fixes are applied correctly"
253+
buildFile.text.contains("description = project.findProperty('myCustomDescription')")
254+
buildFile.text.contains("args ('--task2', project.findProperty(\"myCustomProperty1\"))")
255+
buildFile.text.contains("logger.warn \"myCustomProperty1: \${project.findProperty('myCustomProperty1')}\"")
256+
buildFile.text.contains("def a = project.findProperty \"myCustomProperty2\"")
257+
buildFile.text.contains("def b = project.rootProject.allprojects.find { it.name == \"subA\"}.findProperty(\"myCustomPropertySubA1\")")
258+
buildFile.text.contains("def c = rootProject.childProjects.subA.findProperty(\"myCustomPropertySubA2\")")
259+
buildFile.text.contains("def d = project.findProject(':subA').findProperty(\"myCustomPropertySubA3\")")
260+
261+
and: "properties are read and printed correctly"
262+
results.output.contains("myCustomDescription: This is a custom description")
263+
results.output.contains("myCustomProperty1: property1")
264+
results.output.contains("myCustomProperty2: property2")
265+
results.output.contains("subA.myCustomPropertySubA1: subA-one")
266+
results.output.contains("subA.myCustomPropertySubA2: subA-two")
267+
results.output.contains("subA.myCustomPropertySubA3: subA-three")
268+
}
185269
}

0 commit comments

Comments
 (0)