Skip to content

Commit

Permalink
Add Early Return Violations
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumIddon committed Nov 22, 2023
1 parent ac19743 commit 14e88b8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ github.feedback=true
# Style category configuration.
# Valid <category>: Formatting, ClassNames, MethodNames, VariableNames,
# PackageNames, Commenting, JavaDoc, PrivateMembers,
# Ordering, Useless, StringConcatenation, Clones, JavaFX.
# Ordering, Useless, StringConcatenation, Clones, JavaFX,
# EarlyReturn.

# If scoring for <category> is enabled.
# Required: No, Default: false.
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/gradestyle/validator/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.stmt.IfStmt;
import gradestyle.Repo;
import gradestyle.config.CategoryConfig;
import gradestyle.util.FileUtils;
Expand Down Expand Up @@ -39,7 +40,8 @@ public enum Category {
Useless,
StringConcatenation,
Clones,
JavaFX;
JavaFX,
EarlyReturn;

public static Map<Category, Integer> getCategoryScores(
ValidationResult result, List<CategoryConfig> configs) throws IOException {
Expand Down Expand Up @@ -150,6 +152,9 @@ private long getNormalisation(Repo repo) throws IOException {
.mapToInt(Range::getLineCount)
.sum();
break;
case EarlyReturn:
normalisation += ifStatementCounter(cu);
break;
default:
throw new IllegalArgumentException("Unknown category: " + this);
}
Expand Down Expand Up @@ -187,6 +192,10 @@ private int constructorCounter(CompilationUnit cu) {
return cu.findAll(ConstructorDeclaration.class).size();
}

private int ifStatementCounter(CompilationUnit cu) {
return cu.findAll(IfStmt.class).size();
}

public List<Type> getTypes() {
return Arrays.stream(Type.values()).filter(type -> type.getCategory() == this).toList();
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/gradestyle/validator/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public enum Type {
JavaFX_Initializer,
JavaFX_EventHandlerName,
JavaFX_EventHandlerAnnotation,
JavaFX_EventHandlerPrivate;
JavaFX_EventHandlerPrivate,

EarlyReturn;

public Category getCategory() {
return Category.valueOf(name().split("_")[0]);
Expand Down Expand Up @@ -280,6 +282,9 @@ public String getMessage() {
case JavaFX_EventHandlerPrivate:
return "This event handler should have a different access modifier.";

case EarlyReturn:
return "This conditional could have been an early return.";

default:
throw new IllegalArgumentException("Unknown type: " + this);
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/gradestyle/validator/pmd/EarlyReturn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gradestyle.validator.pmd;

import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;

public class EarlyReturn extends AbstractJavaRule {
public EarlyReturn() {
addRuleChainVisit(ASTMethodDeclaration.class);
}

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
ASTBlock body = method.getBody();

if (body == null || body.getNumChildren() == 0) {
return data;
}

JavaNode blockNode = body.getChild(body.getNumChildren() - 1);

if (blockNode instanceof ASTBlockStatement blockStmt) {
JavaNode stmtNode = blockStmt.getChild(0);

if (stmtNode instanceof ASTStatement stmt) {
if (stmt.getChild(0) instanceof ASTIfStatement conditional) {
asCtx(data).addViolation(conditional);
}
}
}

return data;
}
}
2 changes: 2 additions & 0 deletions src/main/java/gradestyle/validator/pmd/Pmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private Type getType(Rule rule) {
return Type.VariableNames_FinalStaticUppercase;
case "StringConcatenation":
return Type.StringConcatenation;
case "EarlyReturn":
return Type.EarlyReturn;
default:
throw new IllegalArgumentException("Unknown rule: " + rule.getName());
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/gradestyle/validator/pmd/pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
</rule>

<rule name="StringConcatenation" language="java" class="gradestyle.validator.pmd.StringConcatenation"/>
<rule name="EarlyReturn" language="java" class="gradestyle.validator.pmd.EarlyReturn"/>
</ruleset>

0 comments on commit 14e88b8

Please sign in to comment.