-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added code climate configurations and updated circle ci configurations.
- Loading branch information
1 parent
1677855
commit 97afa68
Showing
14 changed files
with
752 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "2" | ||
|
||
plugins: | ||
codenarc: | ||
enabled: true | ||
channel: "beta" | ||
config: "./code-climate-codenarc/codenarc.groovy" | ||
|
||
exclude_patterns: | ||
- "code-climate-codenarc/" |
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 @@ | ||
changelog.md merge=union |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (c) 2011-Present, CauseCode Technologies Pvt Ltd, India. | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are not permitted. | ||
*/ | ||
package com.causecode.quality.configurations.codenarc | ||
|
||
import org.codehaus.groovy.ast.MethodNode | ||
import org.codehaus.groovy.ast.Parameter | ||
import org.codehaus.groovy.ast.expr.BinaryExpression | ||
import org.codehaus.groovy.ast.expr.ClosureExpression | ||
import org.codehaus.groovy.ast.expr.PropertyExpression | ||
import org.codenarc.rule.AbstractAstVisitor | ||
import org.codenarc.rule.AbstractAstVisitorRule | ||
|
||
import java.util.stream.Collectors | ||
|
||
/** | ||
* Codenarc rule to avoid manipulating method/closure parameter. | ||
* @author Kshitij Mandloi | ||
* @since 1.0.1 | ||
*/ | ||
class CanNotModifyReferenceRule extends AbstractAstVisitorRule { | ||
|
||
String name = 'CannotModifyReference' | ||
int priority = 1 | ||
String description = 'Parameters of method and closure cannot be modified. Use a temporary variable.' | ||
Class astVisitorClass = CanNotModifyReferenceAstVisitor | ||
|
||
} | ||
|
||
class CanNotModifyReferenceAstVisitor extends AbstractAstVisitor { | ||
|
||
private List<String> currentMethodParameterNames = [] | ||
private List<String> currentClosureParameterNames = [] | ||
|
||
@Override | ||
protected void visitMethodEx(MethodNode node) { | ||
currentMethodParameterNames = Arrays.stream(node.parameters) | ||
.map { Parameter parameter -> parameter.name }.collect(Collectors.toList()) | ||
super.visitMethodEx(node) | ||
} | ||
|
||
@Override | ||
protected void visitMethodComplete(MethodNode node) { | ||
super.visitMethodComplete(node) | ||
currentMethodParameterNames = [] | ||
} | ||
|
||
@Override | ||
void visitClosureExpression(ClosureExpression expression) { | ||
String parameterNames = expression.parameters?.text ?: [] | ||
currentClosureParameterNames.addAll(parameterNames) | ||
super.visitClosureExpression(expression) | ||
currentClosureParameterNames.removeAll(parameterNames) | ||
} | ||
|
||
@Override | ||
void visitBinaryExpression(BinaryExpression expression) { | ||
if (isFirstVisit(expression) && isManipulatingAParameter(expression)) { | ||
String name = expression.leftExpression.text | ||
addViolation(expression, "The method parameter [$name] in class $currentClassName was manipulated. " + | ||
"Use a temporary variable instead.") | ||
} | ||
super.visitBinaryExpression(expression) | ||
} | ||
|
||
/** | ||
* Utility method to check if a method is manipulating a parameter. | ||
* @param expression | ||
* @return true, if parameter manipulation is happening | ||
*/ | ||
private boolean isManipulatingAParameter(BinaryExpression expression) { | ||
expression.operation.text in ['=', '+=', '-=', '*=', '/=', '**'] && | ||
expression.leftExpression instanceof PropertyExpression && | ||
isCurrentParameterName(expression.leftExpression.text) | ||
} | ||
|
||
/** | ||
* Utility method to check if the leftExpression contains current method closure/parameter or not. | ||
* @param name | ||
* @return true, if leftExpression is current method/closure parameter | ||
*/ | ||
private boolean isCurrentParameterName(String name) { | ||
if (!name) { | ||
return false | ||
} | ||
|
||
List tokens = [] | ||
tokens.addAll(currentMethodParameterNames) | ||
tokens.addAll(currentClosureParameterNames) | ||
|
||
return tokens.stream().anyMatch() { String param -> | ||
name.split('\\.')[0] == param | ||
} | ||
} | ||
} | ||
|
42 changes: 42 additions & 0 deletions
42
code-climate-codenarc/EmptyLineAfterClosingBraceRule.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,42 @@ | ||
/* | ||
* Copyright (c) 2011-Present, CauseCode Technologies Pvt Ltd, India. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are not permitted. | ||
*/ | ||
package com.causecode.quality.configurations.codenarc | ||
|
||
import org.codenarc.rule.AbstractRule | ||
import org.codenarc.source.SourceCode | ||
|
||
/** | ||
* This is a custom rule that validates the presence of an empty line after the closing brace. | ||
* The case is ignored if there is another closing brace on the next line or it's a loop. | ||
* | ||
* @author Ankit Agrawal | ||
* @since 0.0.9 | ||
*/ | ||
class EmptyLineAfterClosingBraceRule extends AbstractRule { | ||
|
||
String name = 'EmptyLineAfterClosingBraceRule' | ||
String description = 'There should be an empty line after the closing (\"}\") brace.' | ||
int priority = 2 | ||
|
||
@Override | ||
void applyTo(SourceCode sourceCode, List violations) { | ||
|
||
List<String> lines = sourceCode.lines | ||
|
||
for (int index = 1; index < lines.size(); index++) { | ||
String line = lines[index].trim() | ||
String previousLine = lines[index - 1].trim() | ||
|
||
if (previousLine.contains('}') && !(previousLine.contains('{')) && !(previousLine.contains('})')) && | ||
!line.contains('}') && !(line.isEmpty())) { | ||
violations.add(createViolation(index, previousLine, | ||
'There should be an empty line after the closing brace.')) | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
code-climate-codenarc/GrailsDomainTimestampFieldsRule.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,38 @@ | ||
/* | ||
* Copyright (c) 2011-Present, CauseCode Technologies Pvt Ltd, India. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are not permitted. | ||
*/ | ||
package com.causecode.quality.configurations.codenarc | ||
|
||
import org.codenarc.rule.AbstractAstVisitorRule | ||
import org.codenarc.rule.AbstractAstVisitor | ||
import org.codenarc.rule.grails.GrailsUtil | ||
import org.codehaus.groovy.ast.ClassNode | ||
|
||
/** | ||
* This is a custom codenarc rule that checks that all domains should have timestamp fields | ||
* | ||
* @author Ankit Agrawal | ||
* @since 0.0.9 | ||
*/ | ||
class GrailsDomainTimestampFieldsRule extends AbstractAstVisitorRule { | ||
String name = 'GrailsDomainTimestampFields' | ||
String description = 'All domain classes should have timestamp fields.' | ||
int priority = 1 | ||
Class astVisitorClass = GrailsDomainTimestampFieldsRuleAstVisitor | ||
String applyToFilesMatching = GrailsUtil.DOMAIN_FILES | ||
} | ||
|
||
class GrailsDomainTimestampFieldsRuleAstVisitor extends AbstractAstVisitor { | ||
|
||
@Override | ||
void visitClassComplete(ClassNode classNode) { | ||
if (!classNode.enum && !classNode.fields.name.containsAll(['dateCreated', 'lastUpdated'])) { | ||
addViolation(classNode, "The domain class $classNode.name should contain timestamp fields " + | ||
"dateCreated and lastUpdated") | ||
} | ||
} | ||
} |
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,71 @@ | ||
/* | ||
* Copyright (c) 2011-Present, CauseCode Technologies Pvt Ltd, India. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or | ||
* without modification, are not permitted. | ||
*/ | ||
package com.causecode.quality.configurations.codenarc | ||
|
||
import org.codehaus.groovy.ast.expr.ClosureExpression | ||
import org.codehaus.groovy.ast.expr.MapExpression | ||
import org.codehaus.groovy.ast.expr.MethodCallExpression | ||
import org.codenarc.rule.AbstractAstVisitorRule | ||
import org.codenarc.rule.AbstractMethodCallExpressionVisitor | ||
import org.codenarc.util.AstUtil | ||
|
||
/** | ||
* This is a custom codenarc rule that checks that all methods that returns a list from database must implement max | ||
* parameters. | ||
* | ||
* @author Nikhil Sharma | ||
* @since 0.0.1 | ||
*/ | ||
class GrailsMaxForListQueriesRule extends AbstractAstVisitorRule { | ||
String name = 'GrailsMaxForListQueries' | ||
String description = 'Any call to database that returns a list must have max value set.' | ||
int priority = 1 | ||
Class astVisitorClass = GrailsMaxForListQueriesRuleAstVisitor | ||
} | ||
|
||
class GrailsMaxForListQueriesRuleAstVisitor extends AbstractMethodCallExpressionVisitor { | ||
|
||
@SuppressWarnings(['Instanceof']) | ||
@Override | ||
void visitMethodCallExpression(MethodCallExpression call) { | ||
String errorMessage = 'Add max parameter for this call' | ||
boolean isViolation | ||
|
||
def arguments = AstUtil.getMethodArguments(call) | ||
String methodName = call.methodAsString | ||
|
||
if (methodName == 'withCriteria') { | ||
arguments.each { argument -> | ||
if (argument instanceof ClosureExpression) { | ||
if (!argument.code.text.contains('maxResults') && !argument.code.text.contains('rowCount')) { | ||
isViolation = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (methodName == 'findAll' || methodName == 'list') { | ||
if (!arguments) { | ||
isViolation = true | ||
} | ||
|
||
arguments.each { argument -> | ||
if (argument instanceof MapExpression) { | ||
def maxAsKey = argument.mapEntryExpressions.find { mapEntryExpression -> | ||
mapEntryExpression.keyExpression.text == 'max' | ||
} | ||
isViolation = !maxAsKey | ||
} | ||
} | ||
} | ||
|
||
if (isViolation) { | ||
addViolation(call, errorMessage) | ||
} | ||
} | ||
} |
Oops, something went wrong.