Skip to content

Commit

Permalink
Support violations that result in insertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jan 6, 2016
1 parent 1d28409 commit 8975ff5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CorrectableStringSource extends AbstractSourceCode {

Map<ASTNode, String> replacements = [:]
List<ASTNode> deletions = []
Map<ASTNode, String> additions = [:]

CorrectableStringSource(String source) {
assert source != null
Expand All @@ -24,6 +25,7 @@ class CorrectableStringSource extends AbstractSourceCode {

def replacement = replacements.find { it.key.lineNumber-1 == i }
def deletion = deletions.find { it.lineNumber-1 == i }
def addition = additions.find { it.key.lineNumber-1 == i }

if(replacement) {
corrections.append(doReplacement(replacement.key, replacement.value))
Expand All @@ -33,6 +35,10 @@ class CorrectableStringSource extends AbstractSourceCode {
} else {
corrections.append(lines[i])
}

if(addition) {
corrections.append(addition.value)
}
}
corrections.toString()
}
Expand All @@ -58,6 +64,10 @@ class CorrectableStringSource extends AbstractSourceCode {
this.deletions += node
}

void add(ASTNode node, String addition) {
this.additions[node] = addition
}

@Override
String getText() {
lines.join('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ class GradleLintCorrectionTask extends DefaultTask {
textOutput.withStyle(StyledTextOutput.Style.UserInput).println('replaced with:')
textOutput.println(v.replacement)
correctedViolations++
} else if(v.shouldDelete) {
textOutput.withStyle(StyledTextOutput.Style.UserInput).println('deleted')
} else if(v.deleteLine) {
textOutput.withStyle(StyledTextOutput.Style.UserInput).println("deleted line $v.deleteLine")
correctedViolations++
} else if(v.addition) {
textOutput.withStyle(StyledTextOutput.Style.UserInput).println("adding:")
textOutput.print(v.addition)
correctedViolations++
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ abstract class AbstractGradleLintVisitor extends AbstractAstVisitor {

Stack<String> closureStack = new Stack<String>()

/**
* Used to preserve the location of a block of code so that it can be affected in some way
* later in the AST visit
*/
Map<String, ASTNode> bookmarks = [:]

boolean isIgnored() {
globalIgnoreOn || rulesToIgnore.collect { LintRuleRegistry.findVisitorClassNames(it) }
.flatten().contains(getClass())
Expand Down Expand Up @@ -185,7 +191,7 @@ abstract class AbstractGradleLintVisitor extends AbstractAstVisitor {
super.addViolation(node, message)
}

void addViolationWithReplacement(ASTNode node, String message, String replacement) {
void addViolationWithReplacement(ASTNode node, String message, String replacement, ASTNode replaceAt = null) {
if (isIgnored())
return

Expand All @@ -194,19 +200,32 @@ abstract class AbstractGradleLintVisitor extends AbstractAstVisitor {
replacement: replacement)
violations.add(v)
if (replacement != null && isCorrectable())
correctableSourceCode.replace(node, replacement)
correctableSourceCode.replace(replaceAt ?: node, replacement)
}

void addViolationToDelete(ASTNode node, String message) {
void addViolationToDelete(ASTNode node, String message, ASTNode deleteAt = null) {
if (isIgnored())
return

def v = new GradleViolation(rule: rule, lineNumber: node.lineNumber,
sourceLine: formattedViolation(node), message: message,
shouldDelete: true)
deleteLine: true)
violations.add(v)
if (isCorrectable())
correctableSourceCode.delete(node)
correctableSourceCode.delete(deleteAt ?: node)
}

void addViolationInsert(ASTNode node, String message, String addition, ASTNode insertAt = null) {
if(isIgnored())
return

def v = new GradleViolation(rule: rule, lineNumber: node.lineNumber,
sourceLine: formattedViolation(node), message: message,
addition: addition)
violations.add(v)
if (isCorrectable()) {
correctableSourceCode.add(insertAt ?: node, addition)
}
}

void addViolationNoCorrection(ASTNode node, String message) {
Expand Down Expand Up @@ -275,6 +294,10 @@ abstract class AbstractGradleLintVisitor extends AbstractAstVisitor {
*/
void visitExtensionProperty(ExpressionStatement expression, String extension, String prop) {}

void bookmark(String label, ASTNode node) {
bookmarks[label] = node
}

protected static Map<String, String> collectEntryExpressions(MethodCallExpression call) {
call.arguments.expressions
.findAll { it instanceof MapExpression }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import org.codenarc.rule.Violation

class GradleViolation extends Violation {
String replacement
boolean shouldDelete = false
Integer deleteLine
String addition

boolean isFixable() {
replacement || shouldDelete
replacement || deleteLine || addition
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,49 @@ class AbstractGradleLintVisitorSpec extends AbstractRuleSpec {
correct(rule) == ''
}

def 'add violation with insertion'() {
when:
def rule = new AbstractAstVisitorRule() {
String name = 'no-apply-plugin'
int priority = 2

@Override
AstVisitor getAstVisitor() {
return new AbstractGradleLintVisitor() {
@Override
void visitApplyPlugin(MethodCallExpression call, String plugin) {
bookmark('lastApplyPlugin', call)
}

@Override
void visitGradleDependency(MethodCallExpression call, String conf, GradleDependency dep) {
if(bookmarks.lastApplyPlugin) {
addViolationInsert(call, 'should generate source jar', "\napply plugin: 'nebula.source-jar'", bookmarks.lastApplyPlugin)
}
}
}
}
}

project.buildFile << """
apply plugin: 'java'
dependencies {
compile 'com.google.guava:guava:18.0'
}
""".stripIndent().trim()

then:
correct(rule) == """
apply plugin: 'java'
apply plugin: 'nebula.source-jar'
dependencies {
compile 'com.google.guava:guava:18.0'
}
""".stripIndent().trim()
}

@Unroll
def 'violations are suppressed inside of ignore blocks when ignored rule(s) is `#rules`'() {
setup:
Expand Down

0 comments on commit 8975ff5

Please sign in to comment.