Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ilyas RIAH #19

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/com/ensao/gi5/lint/constantes/Constantes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ public class Constantes {
public static final String LINT_REG_000 = "LINT_REG_000";
public static final String LINT_REG_001 = "LINT_REG_001";

public static final String LINT_REG_002 = "LINT_REG_002";

public static final String LINT_REG_006 = "LINT_REG_006";//

public static final String LINT_REG_007 = "LINT_REG_007";//

public static final String LINT_REG_008 = "LINT_REG_008";//

public static final String LINT_REG_011 = "LINT_REG_011";//

public static final String LINT_REG_012 = "LINT_REG_012";//

public static final String LINT_REG_016 = "LINT_REG_016";

public static final String LINT_REG_018 = "LINT_REG_018"; //


private Constantes() {
throw new IllegalStateException("not to be instantiated");
}
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ensao.gi5.lint.rules;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.IfElseVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.StmtWrapper;

public class BoolExpRule extends Rule{

public BoolExpRule() {
super(Constantes.LINT_REG_006, Level.HIGHEST);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<StmtWrapper> BoolExpWrappers = new ArrayList<>();
compilationUnit.accept(new IfElseVisitor(), BoolExpWrappers);

for (StmtWrapper BoolExpWrapper : BoolExpWrappers) {
Matcher matcher = Pattern
.compile("(([\\\\w\\\\d\\\\s.])+(==|!=|<|>|>=|<=)[\\\\w\\\\d\\\\s.]+([&\\\\|]{2})?)+")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regex expression is too long, maintenance will be hard

.matcher(BoolExpWrapper.getStatement().toString());

if (!matcher.find()) {
final Violation violation = new Violation();
violation.setDescription("Boolean expressions must have no more than 2 operands");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(BoolExpWrapper.getLigne());
violation.setRuleId(Constantes.LINT_REG_006);
violation.setLevel(Level.HIGHEST);
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}

}
81 changes: 81 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ClassRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.ensao.gi5.lint.rules;

import java.util.ArrayList;
import java.util.List;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.ClassVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.classe.ClassWrapper;
import com.ensao.gi5.lint.wrapper.classe.Construct;
import com.ensao.gi5.lint.wrapper.classe.Methode;

public class ClassRule extends Rule {

public ClassRule() {
super(Constantes.LINT_REG_002, Level.HIGHEST);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {

List<ClassWrapper> classes = new ArrayList<>();
compilationUnit.accept(new ClassVisitor(), classes);

for (ClassWrapper classe : classes) {
if (classe.getMethodes().size() > 20) {
final Violation violation = new Violation();
violation.setDescription("The number of methods must not exceed 20 methods declared per class");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(classe.getLigne());
violation.setRuleId(Constantes.LINT_REG_011);
violation.setLevel(Level.HIGHEST);
violation.setDescription(classe.getName() + " " + classe.getMethodes().size());
addViolation(violation);
}

for (Construct construct : classe.getConstructs()) {
if (construct.getParametres().size() > 2) {
final Violation violation = new Violation();
violation.setDescription("The number of parameters of a constructor must not exceed 2");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(classe.getLigne());
violation.setRuleId(Constantes.LINT_REG_012);
violation.setLevel(Level.HIGHEST);
violation.setDescription(construct.getName() + " " + construct.getParametres().size());
addViolation(violation);
}
}

for (Methode methode : classe.getMethodes()) {
if (methode.getLinesCount() > 30) {
final Violation violation = new Violation();
violation.setDescription("The body of a method must not exceed 30 lines");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(methode.getLigne());
violation.setRuleId(Constantes.LINT_REG_008);
violation.setLevel(Level.HIGHEST);
violation.setDescription(methode.getName() + " in " + classe.getName() + " " + methode.getLinesCount());
addViolation(violation);
}
if (methode.getParametres().size() > 2) {
final Violation violation = new Violation();
violation.setDescription("The number of parameters of a method must not exceed 2");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(classe.getLigne());
violation.setRuleId(Constantes.LINT_REG_012);
violation.setLevel(Level.HIGHEST);
violation.setDescription(methode.getName() + " " + methode.getParametres().size());
addViolation(violation);
}
}
}
}

@Override
public boolean isActive() {
return true;
}

}
39 changes: 39 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/EnumRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ensao.gi5.lint.rules;

import java.util.ArrayList;
import java.util.List;
import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.EnumVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.enumeration.EnumWrapper;

public class EnumRule extends Rule {

public EnumRule() {
super(Constantes.LINT_REG_007, Level.LOW);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<EnumWrapper> EnumWrappers = new ArrayList<>();
compilationUnit.accept(new EnumVisitor(), EnumWrappers);

EnumWrappers.forEach(enumWrapper -> enumWrapper.getElements().forEach(enumElement -> {
if (!enumElement.getName().matches("[A-Z_]+")) {
final Violation violation = new Violation();
violation.setDescription("The elements of an enumeration are in uppercase, with _ as separator");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(enumElement.getLigne());
violation.setRuleId(Constantes.LINT_REG_007);
violation.setLevel(Level.LOW);
addViolation(violation);
}
}));
}

@Override
public boolean isActive() {
return true;
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ensao.gi5.lint.rules;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.IfElseVisitor;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.StmtWrapper;

public class IfElseRule extends Rule{

public IfElseRule() {
super(Constantes.LINT_REG_018, Level.LOW);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<StmtWrapper> ifElseWrappers = new ArrayList<>();
compilationUnit.accept(new IfElseVisitor(),ifElseWrappers);

for(StmtWrapper ifElseWrapper: ifElseWrappers){
Matcher matcher = Pattern.compile(".*\\{([\\S\\s]*)\\}").matcher(ifElseWrapper.getStatement().toString());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use visitor pattern


if(!matcher.find()){
final Violation violation = new Violation();
violation.setDescription("if/else clauses must have braces" );
violation.setFileName(compilationUnit.getFileName());
violation.setLine(ifElseWrapper.getLigne());
violation.setRuleId(Constantes.LINT_REG_018);
violation.setLevel(Level.LOW);
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/UnusedVariablesRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ensao.gi5.lint.rules;

import java.util.ArrayList;
import java.util.List;

import com.ensao.gi5.lint.constantes.Constantes;
import com.ensao.gi5.lint.rules.violations.Violation;
import com.ensao.gi5.lint.visitor.UnusedVariablesVisitors;
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper;
import com.ensao.gi5.lint.wrapper.VariableWrapper;

public class UnusedVariablesRule extends Rule {

public UnusedVariablesRule() {
super(Constantes.LINT_REG_016, Level.MEDIUM);
}

@Override
public void apply(CompilationUnitWrapper compilationUnit) {
List<VariableWrapper> variableWrappers = new ArrayList<>();
compilationUnit.accept(new UnusedVariablesVisitors(), variableWrappers);

for(VariableWrapper variableWrapper: variableWrappers){

if(variableWrapper.getUsageCount()==1){
final Violation violation = new Violation();
violation.setDescription("Unused variables should be deleted");
violation.setFileName(compilationUnit.getFileName());
violation.setLine(variableWrapper.getLigne());
violation.setRuleId(Constantes.LINT_REG_016);
violation.setLevel(Level.MEDIUM);
addViolation(violation);
}
}
}

@Override
public boolean isActive() {
return true;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/ensao/gi5/lint/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import com.ensao.gi5.lint.Linter;
import com.ensao.gi5.lint.printer.ConsolePrinter;
import com.ensao.gi5.lint.rules.BoolExpRule;
import com.ensao.gi5.lint.rules.ClassRule;
import com.ensao.gi5.lint.rules.EnumRule;
import com.ensao.gi5.lint.rules.IfElseRule;
import com.ensao.gi5.lint.rules.UnusedImportsRule;
import com.ensao.gi5.lint.rules.UnusedVariablesRule;

public class Runner {
public static void main(String[] args) {
Expand All @@ -26,6 +31,11 @@ public static void main(String[] args) {
final Linter linter = new Linter();
linter.registerRule(new UnusedImportsRule());
linter.registerPrinter(new ConsolePrinter());
linter.registerRule(new ClassRule());
linter.registerRule(new IfElseRule());
linter.registerRule(new BoolExpRule());
linter.registerRule(new EnumRule());
linter.registerRule(new UnusedVariablesRule());
linter.registerSource(directory);
linter.run();
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/ensao/gi5/lint/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.SimpleName;

import org.apache.commons.io.FileUtils;

import java.io.File;
Expand Down Expand Up @@ -52,4 +54,9 @@ public static Collection<File> getFilesFromDirectory(String directoryPath) {
public static Collection<File> getFilesFromDirectory(File directory) {
return FileUtils.listFiles(directory, new String[]{"java"}, true);
}

public static int getLine(SimpleName simpleName){
return simpleName.getBegin().map(sN -> sN.line).orElse(-1);
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ensao.gi5.lint.visitor;

import java.util.List;
import java.util.regex.Pattern;

import com.ensao.gi5.lint.wrapper.StmtWrapper;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class BoolExpVisitor extends VoidVisitorAdapter<List<StmtWrapper>> {

@Override
public void visit(BlockStmt blockStmt, List<StmtWrapper> arg) {

blockStmt.getStatements().stream().filter(s -> Pattern.compile("==|!=|<|>|>=|<=")
.matcher(s.toString()).find()).forEach(s ->
arg.add(new StmtWrapper(s))
);
super.visit(blockStmt, arg);
}

}
39 changes: 39 additions & 0 deletions src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ensao.gi5.lint.visitor;

import java.util.List;

import com.ensao.gi5.lint.util.Utils;
import com.ensao.gi5.lint.wrapper.classe.ClassWrapper;
import com.ensao.gi5.lint.wrapper.classe.Construct;
import com.ensao.gi5.lint.wrapper.classe.Methode;
import com.ensao.gi5.lint.wrapper.classe.Parametre;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class ClassVisitor extends VoidVisitorAdapter<List<ClassWrapper>> {

@Override
public void visit(ClassOrInterfaceDeclaration CID, List<ClassWrapper> arg) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CID is not a valid variable name

ClassWrapper classWrapper = new ClassWrapper(CID.getNameAsString(), Utils.getLine(CID.getName()));

CID.getConstructors().forEach(c -> {
Construct construct = new Construct(c.getNameAsString(), Utils.getLine(c.getName()));
c.getParameters().forEach(param -> {
construct.getParametres().add(new Parametre(param.getNameAsString(), param.getTypeAsString()));
});
classWrapper.getConstructs().add(construct);
});

CID.getMethods().forEach(m -> {
Methode methode = new Methode(m.getNameAsString(), m.getTypeAsString(), m.getAccessSpecifier().asString(),
m.getBegin().map(p -> p.line).orElse(-1));

m.getParameters()
.forEach(p -> methode.getParametres().add(new Parametre(p.getNameAsString(), p.getTypeAsString())));
classWrapper.getMethodes().add(methode);
});

arg.add(classWrapper);
super.visit(CID, arg);
}
}
Loading