-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lint rule to fix the space assignment syntax (#415)
- Loading branch information
Showing
7 changed files
with
402 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/groovy/com/netflix/nebula/lint/rule/dsl/SpaceAssignmentRule.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.netflix.nebula.lint.rule.dsl | ||
|
||
import com.netflix.nebula.lint.rule.GradleLintRule | ||
import com.netflix.nebula.lint.rule.GradleModelAware | ||
import org.codehaus.groovy.ast.expr.ClosureExpression | ||
import org.codehaus.groovy.ast.expr.MethodCallExpression | ||
|
||
class SpaceAssignmentRule extends GradleLintRule implements GradleModelAware { | ||
|
||
String description = "space-assignment syntax is deprecated" | ||
|
||
@Override | ||
void visitMethodCallExpression(MethodCallExpression call) { | ||
if (call.arguments.size() != 1 || call.arguments[-1] instanceof ClosureExpression) { | ||
return | ||
} | ||
|
||
def receiverClass = receiver(call)?.clazz | ||
if (receiverClass == null) { | ||
return // no enough data to analyze | ||
} | ||
|
||
def invokedMethodName = call.method.value | ||
|
||
// check if the method has a matching property | ||
def setter = receiverClass.getMethods().find { it.name == "set${invokedMethodName.capitalize()}" } | ||
if (setter == null) { | ||
return // no matching property | ||
} | ||
|
||
// check if it's a generated method for space assignment | ||
def exactMethod = receiverClass.getMethods().find { it.name == invokedMethodName } | ||
if (exactMethod != null) { | ||
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)) | ||
} | ||
} else { | ||
addBuildLintViolation(description, call) | ||
.replaceWith(call, getReplacement(call)) | ||
} | ||
} | ||
|
||
def getReplacement(MethodCallExpression call){ | ||
def originalLine = getSourceCode().line(call.lineNumber-1) | ||
return originalLine.replaceFirst(call.methodAsString, call.methodAsString + " =") | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/lint-rules/space-assignment.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
implementation-class=com.netflix.nebula.lint.rule.dsl.SpaceAssignmentRule |
Oops, something went wrong.