From 790ed20c4f54190f33da967ab5dc083c15dd4191 Mon Sep 17 00:00:00 2001 From: ilyas Date: Tue, 10 Jan 2023 23:48:21 +0100 Subject: [PATCH 1/4] First Commit Adding Enum, BoolExpression and IfElse rules. --- .../ensao/gi5/lint/constantes/Constantes.java | 7 +++++ .../java/com/ensao/gi5/lint/util/Utils.java | 7 +++++ .../gi5/lint/visitor/BoolExpVisitor.java | 26 +++++++++++++++++ .../ensao/gi5/lint/visitor/EnumVisitor.java | 21 ++++++++++++++ .../ensao/gi5/lint/visitor/IfElseVisitor.java | 23 +++++++++++++++ .../ensao/gi5/lint/wrapper/StmtWrapper.java | 28 +++++++++++++++++++ .../lint/wrapper/enumeration/EnumElement.java | 19 +++++++++++++ .../lint/wrapper/enumeration/EnumWrapper.java | 21 ++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java create mode 100644 src/main/java/com/ensao/gi5/lint/visitor/EnumVisitor.java create mode 100644 src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumElement.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumWrapper.java diff --git a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java index 32a57fb..59a7e6e 100644 --- a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java +++ b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java @@ -4,6 +4,13 @@ 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_006 = "LINT_REG_006"; + + public static final String LINT_REG_007 = "LINT_REG_007"; + + public static final String LINT_REG_018 = "LINT_REG_018"; + + private Constantes() { throw new IllegalStateException("not to be instantiated"); } diff --git a/src/main/java/com/ensao/gi5/lint/util/Utils.java b/src/main/java/com/ensao/gi5/lint/util/Utils.java index 7f4eb61..3fef445 100644 --- a/src/main/java/com/ensao/gi5/lint/util/Utils.java +++ b/src/main/java/com/ensao/gi5/lint/util/Utils.java @@ -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; @@ -52,4 +54,9 @@ public static Collection getFilesFromDirectory(String directoryPath) { public static Collection getFilesFromDirectory(File directory) { return FileUtils.listFiles(directory, new String[]{"java"}, true); } + + public static int getLine(SimpleName simpleName){ + return simpleName.getBegin().map(p -> p.line).orElse(-1); + } + } diff --git a/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java new file mode 100644 index 0000000..8b0c6ee --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java @@ -0,0 +1,26 @@ +package com.ensao.gi5.lint.visitor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import com.ensao.gi5.lint.constantes.Constantes; +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>> { + + @Override + public void visit(BlockStmt n, Map> arg) { + + arg.putIfAbsent(Constantes.LINT_REG_006, new ArrayList<>()); + n.getStatements().stream().filter(s -> Pattern.compile("==|!=|<|>|>=|<=") + .matcher(s.toString()).find()).forEach(s -> + arg.get(Constantes.LINT_REG_006).add(new StmtWrapper(Constantes.LINT_REG_006, s)) + ); + super.visit(n, arg); + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/visitor/EnumVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/EnumVisitor.java new file mode 100644 index 0000000..3aa5059 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/visitor/EnumVisitor.java @@ -0,0 +1,21 @@ +package com.ensao.gi5.lint.visitor; + +import com.ensao.gi5.lint.util.Utils; +import com.ensao.gi5.lint.wrapper.enumeration.EnumElement; +import com.ensao.gi5.lint.wrapper.enumeration.EnumWrapper; + +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; +import java.util.List; +import java.util.stream.Collectors; + +public class EnumVisitor extends VoidVisitorAdapter> { + + @Override + public void visit(EnumDeclaration n, List arg) { + arg.add(new EnumWrapper(n.getNameAsString(), + n.getEntries().stream() + .map(e -> new EnumElement(e.getNameAsString(), Utils.getLine(e.getName()))).collect(Collectors.toList()))); + super.visit(n, arg); + } +} diff --git a/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java new file mode 100644 index 0000000..24816bb --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java @@ -0,0 +1,23 @@ +package com.ensao.gi5.lint.visitor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.wrapper.StmtWrapper; +import com.github.javaparser.ast.stmt.IfStmt; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; + +public class IfElseVisitor extends VoidVisitorAdapter>> { + + @Override + public void visit(IfStmt n, Map> arg) { + + arg.putIfAbsent(Constantes.LINT_REG_006, new ArrayList<>()); + arg.get(Constantes.LINT_REG_018).add(new StmtWrapper(Constantes.LINT_REG_018, n.getThenStmt())); + n.getElseStmt().ifPresent(s -> arg.get(Constantes.LINT_REG_018) + .add(new StmtWrapper(Constantes.LINT_REG_018, s))); + super.visit(n, arg); + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java new file mode 100644 index 0000000..2ad0927 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java @@ -0,0 +1,28 @@ +package com.ensao.gi5.lint.wrapper; + +import com.github.javaparser.ast.stmt.Statement; + +public class StmtWrapper { + final String ruleId; + final Statement statement; + final int ligne; + + + public StmtWrapper(String ruleId, Statement statement) { + this.ruleId = ruleId; + this.statement = statement; + this.ligne = statement.getBegin().map(p->p.line).orElse(-1); + } + + public String getRuleId() { + return ruleId; + } + + public Statement getStatement() { + return statement; + } + + public int getLigne() { + return ligne; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumElement.java b/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumElement.java new file mode 100644 index 0000000..f8e1a32 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumElement.java @@ -0,0 +1,19 @@ +package com.ensao.gi5.lint.wrapper.enumeration; + +public class EnumElement { + final private String name; + final private int ligne; + + public EnumElement(String name, int ligne) { + this.name = name; + this.ligne = ligne; + } + + public String getName() { + return name; + } + + public int getLigne() { + return ligne; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumWrapper.java new file mode 100644 index 0000000..292064d --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/enumeration/EnumWrapper.java @@ -0,0 +1,21 @@ +package com.ensao.gi5.lint.wrapper.enumeration; + +import java.util.List; + +public class EnumWrapper { + final private String name; + final private List elements; + + public EnumWrapper(String name, List elements) { + this.name = name; + this.elements = elements; + } + + public String getName() { + return name; + } + + public List getElements() { + return elements; + } +} From cdc364a459cceb6b4b5121cfa087956648f6e11d Mon Sep 17 00:00:00 2001 From: ilyas Date: Wed, 11 Jan 2023 14:51:01 +0100 Subject: [PATCH 2/4] Fixing some issues with the code of Enum, BoolExp and IfElse rules. --- .../com/ensao/gi5/lint/rules/BoolExpRule.java | 43 +++++++++++++++++++ .../com/ensao/gi5/lint/rules/EnumRule.java | 37 ++++++++++++++++ .../com/ensao/gi5/lint/rules/IfElseRule.java | 43 +++++++++++++++++++ .../gi5/lint/visitor/BoolExpVisitor.java | 14 +++--- .../ensao/gi5/lint/visitor/IfElseVisitor.java | 16 +++---- .../lint/wrapper/CompilationUnitWrapper.java | 3 +- .../ensao/gi5/lint/wrapper/StmtWrapper.java | 10 +---- 7 files changed, 137 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java create mode 100644 src/main/java/com/ensao/gi5/lint/rules/EnumRule.java create mode 100644 src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java diff --git a/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java b/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java new file mode 100644 index 0000000..012b860 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java @@ -0,0 +1,43 @@ +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 BoolExpWrappers = new ArrayList<>(); + compilationUnit.accept(new IfElseVisitor(),BoolExpWrappers); + + for(StmtWrapper BoolExpWrapper: BoolExpWrappers){ + Matcher matcher = Pattern.compile("(([\\\\w\\\\d\\\\s.])+(==|!=|<|>|>=|<=)[\\\\w\\\\d\\\\s.]+([&\\\\|]{2})?)+").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()); + addViolation(violation); + } + } + } + + @Override + public boolean isActive() { + return true; + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java b/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java new file mode 100644 index 0000000..81de0b3 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java @@ -0,0 +1,37 @@ +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 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()); + addViolation(violation); + } + })); + } + + @Override + public boolean isActive() { + return true; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java b/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java new file mode 100644 index 0000000..df2e421 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java @@ -0,0 +1,43 @@ +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 ifElseWrappers = new ArrayList<>(); + compilationUnit.accept(new IfElseVisitor(),ifElseWrappers); + + for(StmtWrapper ifElseWrapper: ifElseWrappers){ + Matcher matcher = Pattern.compile(".*\\{([\\S\\s]*)\\}").matcher(ifElseWrapper.getStatement().toString()); + + if(!matcher.find()){ + final Violation violation = new Violation(); + violation.setDescription("if/else clauses must have braces" ); + violation.setFileName(compilationUnit.getFileName()); + violation.setLine(ifElseWrapper.getLigne()); + addViolation(violation); + } + } + } + + @Override + public boolean isActive() { + return true; + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java index 8b0c6ee..06759d7 100644 --- a/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java +++ b/src/main/java/com/ensao/gi5/lint/visitor/BoolExpVisitor.java @@ -1,26 +1,22 @@ package com.ensao.gi5.lint.visitor; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.regex.Pattern; -import com.ensao.gi5.lint.constantes.Constantes; 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>> { +public class BoolExpVisitor extends VoidVisitorAdapter> { @Override - public void visit(BlockStmt n, Map> arg) { + public void visit(BlockStmt blockStmt, List arg) { - arg.putIfAbsent(Constantes.LINT_REG_006, new ArrayList<>()); - n.getStatements().stream().filter(s -> Pattern.compile("==|!=|<|>|>=|<=") + blockStmt.getStatements().stream().filter(s -> Pattern.compile("==|!=|<|>|>=|<=") .matcher(s.toString()).find()).forEach(s -> - arg.get(Constantes.LINT_REG_006).add(new StmtWrapper(Constantes.LINT_REG_006, s)) + arg.add(new StmtWrapper(s)) ); - super.visit(n, arg); + super.visit(blockStmt, arg); } } diff --git a/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java index 24816bb..ac6d911 100644 --- a/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java +++ b/src/main/java/com/ensao/gi5/lint/visitor/IfElseVisitor.java @@ -1,23 +1,17 @@ package com.ensao.gi5.lint.visitor; -import java.util.ArrayList; import java.util.List; -import java.util.Map; - -import com.ensao.gi5.lint.constantes.Constantes; import com.ensao.gi5.lint.wrapper.StmtWrapper; import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; -public class IfElseVisitor extends VoidVisitorAdapter>> { +public class IfElseVisitor extends VoidVisitorAdapter> { @Override - public void visit(IfStmt n, Map> arg) { + public void visit(IfStmt ifStmt, List arg) { - arg.putIfAbsent(Constantes.LINT_REG_006, new ArrayList<>()); - arg.get(Constantes.LINT_REG_018).add(new StmtWrapper(Constantes.LINT_REG_018, n.getThenStmt())); - n.getElseStmt().ifPresent(s -> arg.get(Constantes.LINT_REG_018) - .add(new StmtWrapper(Constantes.LINT_REG_018, s))); - super.visit(n, arg); + arg.add(new StmtWrapper(ifStmt.getThenStmt())); + ifStmt.getElseStmt().ifPresent(st->arg.add(new StmtWrapper(st))); + super.visit(ifStmt,arg); } } diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/CompilationUnitWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/CompilationUnitWrapper.java index 0768852..9013673 100644 --- a/src/main/java/com/ensao/gi5/lint/wrapper/CompilationUnitWrapper.java +++ b/src/main/java/com/ensao/gi5/lint/wrapper/CompilationUnitWrapper.java @@ -23,7 +23,8 @@ public CompilationUnitWrapper(CompilationUnit compilationUnit, String fileName) public NodeList getImports() {return compilationUnit.getImports();} public void accept(VoidVisitor v, A arg) { - compilationUnit.accept(v, arg);} + compilationUnit.accept(v, arg); + } public String getFileName() { return fileName; diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java index 2ad0927..335055d 100644 --- a/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java +++ b/src/main/java/com/ensao/gi5/lint/wrapper/StmtWrapper.java @@ -3,19 +3,13 @@ import com.github.javaparser.ast.stmt.Statement; public class StmtWrapper { - final String ruleId; final Statement statement; final int ligne; - public StmtWrapper(String ruleId, Statement statement) { - this.ruleId = ruleId; + public StmtWrapper(Statement statement) { this.statement = statement; - this.ligne = statement.getBegin().map(p->p.line).orElse(-1); - } - - public String getRuleId() { - return ruleId; + this.ligne = statement.getBegin().map(c->c.line).orElse(-1); } public Statement getStatement() { From a05ff854b7a80b44fe4037ca3088acc1881c8dd9 Mon Sep 17 00:00:00 2001 From: ilyas Date: Thu, 12 Jan 2023 01:42:18 +0100 Subject: [PATCH 3/4] Adding rules : 002, 016 --- .../ensao/gi5/lint/constantes/Constantes.java | 4 ++ .../com/ensao/gi5/lint/rules/BoolExpRule.java | 56 ++++++++++--------- .../com/ensao/gi5/lint/rules/ClassRule.java | 30 ++++++++++ .../com/ensao/gi5/lint/rules/EnumRule.java | 48 ++++++++-------- .../com/ensao/gi5/lint/rules/IfElseRule.java | 2 + .../gi5/lint/rules/UnusedVariablesRule.java | 42 ++++++++++++++ .../java/com/ensao/gi5/lint/util/Utils.java | 2 +- .../ensao/gi5/lint/visitor/ClassVisitor.java | 39 +++++++++++++ .../lint/visitor/UnusedVariablesVisitors.java | 54 ++++++++++++++++++ .../gi5/lint/wrapper/VariableWrapper.java | 38 +++++++++++++ .../gi5/lint/wrapper/classe/ClassWrapper.java | 36 ++++++++++++ .../gi5/lint/wrapper/classe/Construct.java | 30 ++++++++++ .../gi5/lint/wrapper/classe/Methode.java | 48 ++++++++++++++++ .../gi5/lint/wrapper/classe/Parametre.java | 26 +++++++++ 14 files changed, 405 insertions(+), 50 deletions(-) create mode 100644 src/main/java/com/ensao/gi5/lint/rules/ClassRule.java create mode 100644 src/main/java/com/ensao/gi5/lint/rules/UnusedVariablesRule.java create mode 100644 src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java create mode 100644 src/main/java/com/ensao/gi5/lint/visitor/UnusedVariablesVisitors.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/VariableWrapper.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/classe/ClassWrapper.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/classe/Construct.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java create mode 100644 src/main/java/com/ensao/gi5/lint/wrapper/classe/Parametre.java diff --git a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java index 59a7e6e..598f777 100644 --- a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java +++ b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java @@ -4,10 +4,14 @@ 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_016 = "LINT_REG_016"; + public static final String LINT_REG_018 = "LINT_REG_018"; diff --git a/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java b/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java index 012b860..c2d1acc 100644 --- a/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java +++ b/src/main/java/com/ensao/gi5/lint/rules/BoolExpRule.java @@ -13,31 +13,35 @@ public class BoolExpRule extends Rule{ - public BoolExpRule() { - super(Constantes.LINT_REG_006, Level.HIGHEST); - } - - @Override - public void apply(CompilationUnitWrapper compilationUnit) { - List BoolExpWrappers = new ArrayList<>(); - compilationUnit.accept(new IfElseVisitor(),BoolExpWrappers); - - for(StmtWrapper BoolExpWrapper: BoolExpWrappers){ - Matcher matcher = Pattern.compile("(([\\\\w\\\\d\\\\s.])+(==|!=|<|>|>=|<=)[\\\\w\\\\d\\\\s.]+([&\\\\|]{2})?)+").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()); - addViolation(violation); - } - } - } - - @Override - public boolean isActive() { - return true; - } + public BoolExpRule() { + super(Constantes.LINT_REG_006, Level.HIGHEST); + } + + @Override + public void apply(CompilationUnitWrapper compilationUnit) { + List BoolExpWrappers = new ArrayList<>(); + compilationUnit.accept(new IfElseVisitor(), BoolExpWrappers); + + for (StmtWrapper BoolExpWrapper : BoolExpWrappers) { + Matcher matcher = Pattern + .compile("(([\\\\w\\\\d\\\\s.])+(==|!=|<|>|>=|<=)[\\\\w\\\\d\\\\s.]+([&\\\\|]{2})?)+") + .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; + } } diff --git a/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java new file mode 100644 index 0000000..f204e52 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java @@ -0,0 +1,30 @@ +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.visitor.ClassVisitor; +import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; +import com.ensao.gi5.lint.wrapper.classe.ClassWrapper; + +public class ClassRule extends Rule{ + + public ClassRule() { + super(Constantes.LINT_REG_002, Level.HIGHEST); + } + + @Override + public void apply(CompilationUnitWrapper compilationUnit) { + + List classes = new ArrayList<>(); + compilationUnit.accept(new ClassVisitor(), classes); + + } + + @Override + public boolean isActive() { + return true; + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java b/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java index 81de0b3..29d8796 100644 --- a/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java +++ b/src/main/java/com/ensao/gi5/lint/rules/EnumRule.java @@ -8,30 +8,32 @@ import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; import com.ensao.gi5.lint.wrapper.enumeration.EnumWrapper; -public class EnumRule extends Rule{ - +public class EnumRule extends Rule { + public EnumRule() { - super(Constantes.LINT_REG_007, Level.LOW); - } + super(Constantes.LINT_REG_007, Level.LOW); + } + + @Override + public void apply(CompilationUnitWrapper compilationUnit) { + List EnumWrappers = new ArrayList<>(); + compilationUnit.accept(new EnumVisitor(), EnumWrappers); - @Override - public void apply(CompilationUnitWrapper compilationUnit) { - List 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()); - addViolation(violation); - } - })); - } + 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; - } + @Override + public boolean isActive() { + return true; + } } diff --git a/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java b/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java index df2e421..9be8881 100644 --- a/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java +++ b/src/main/java/com/ensao/gi5/lint/rules/IfElseRule.java @@ -30,6 +30,8 @@ public void apply(CompilationUnitWrapper compilationUnit) { 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); } } diff --git a/src/main/java/com/ensao/gi5/lint/rules/UnusedVariablesRule.java b/src/main/java/com/ensao/gi5/lint/rules/UnusedVariablesRule.java new file mode 100644 index 0000000..ee46285 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/rules/UnusedVariablesRule.java @@ -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 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; + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/util/Utils.java b/src/main/java/com/ensao/gi5/lint/util/Utils.java index 3fef445..4e5b1d0 100644 --- a/src/main/java/com/ensao/gi5/lint/util/Utils.java +++ b/src/main/java/com/ensao/gi5/lint/util/Utils.java @@ -56,7 +56,7 @@ public static Collection getFilesFromDirectory(File directory) { } public static int getLine(SimpleName simpleName){ - return simpleName.getBegin().map(p -> p.line).orElse(-1); + return simpleName.getBegin().map(sN -> sN.line).orElse(-1); } } diff --git a/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java b/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java new file mode 100644 index 0000000..fb2ffc6 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/visitor/ClassVisitor.java @@ -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> { + + @Override + public void visit(ClassOrInterfaceDeclaration CID, List arg) { + 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); + } +} diff --git a/src/main/java/com/ensao/gi5/lint/visitor/UnusedVariablesVisitors.java b/src/main/java/com/ensao/gi5/lint/visitor/UnusedVariablesVisitors.java new file mode 100644 index 0000000..51d03e2 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/visitor/UnusedVariablesVisitors.java @@ -0,0 +1,54 @@ +package com.ensao.gi5.lint.visitor; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.ensao.gi5.lint.wrapper.VariableWrapper; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.stmt.BlockStmt; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; + +public class UnusedVariablesVisitors extends VoidVisitorAdapter> { + + @Override + public void visit(MethodDeclaration n, List arg) { + n.getBody().ifPresent(b -> b.getStatements().forEach(st -> { + String vr = variableDeclaration(st); + if (vr != null) { + VariableWrapper w = new VariableWrapper(vr, n.getNameAsString(), resolveClassName(n), + variableCount(b, vr), st.getBegin().map(p -> p.line).orElse(-1)); + arg.add(w); + } + })); + super.visit(n, arg); + } + + private String variableDeclaration(Statement statement) { + Matcher m = Pattern.compile("[\\w]+\\s+([\\w]+)\\s*(=\\s*[\\w\\s()]+)?;") + .matcher(statement.toString().replace("return", "")); + if (m.find()) + return m.group(1); + else + return null; + } + + private int variableCount(BlockStmt blockStmt, String variable) { + int count = 0; + Matcher matcher = Pattern.compile("\\b" + variable + "\\b").matcher(blockStmt.toString()); + while (matcher.find()) { + count++; + } + return count; + } + + private String resolveClassName(MethodDeclaration m) { + final String[] fqn = { null }; + m.getParentNode().ifPresent(p -> { + fqn[0] = ((ClassOrInterfaceDeclaration) p).getFullyQualifiedName().orElse(null); + }); + return fqn[0]; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/VariableWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/VariableWrapper.java new file mode 100644 index 0000000..6a6c2ad --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/VariableWrapper.java @@ -0,0 +1,38 @@ +package com.ensao.gi5.lint.wrapper; + +public class VariableWrapper { + + final private String name; + final private String method; + final private String className; + final private int usageCount; + final private int ligne; + + public VariableWrapper(String name, String method, String className, int count, int ligne) { + this.name = name; + this.method = method; + this.className = className; + this.usageCount = count; + this.ligne = ligne; + } + + public String getName() { + return name; + } + + public String getMethod() { + return method; + } + + public String getClassName() { + return className; + } + + public int getUsageCount() { + return usageCount; + } + + public int getLigne() { + return ligne; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/classe/ClassWrapper.java b/src/main/java/com/ensao/gi5/lint/wrapper/classe/ClassWrapper.java new file mode 100644 index 0000000..4ee5ee8 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/classe/ClassWrapper.java @@ -0,0 +1,36 @@ +package com.ensao.gi5.lint.wrapper.classe; + +import java.util.ArrayList; +import java.util.List; + +public class ClassWrapper { + + final private String name; + final private List constructs; + final private List methodes; + final private int ligne; + + + public ClassWrapper(String name, int ligne) { + this.name = name; + this.ligne = ligne; + this.constructs = new ArrayList<>(); + this.methodes = new ArrayList<>(); + } + + public String getName() { + return name; + } + + public int getLigne() { + return ligne; + } + + public List getMethodes() { + return methodes; + } + + public List getConstructs() { + return constructs; + } +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/classe/Construct.java b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Construct.java new file mode 100644 index 0000000..0b349ae --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Construct.java @@ -0,0 +1,30 @@ +package com.ensao.gi5.lint.wrapper.classe; + +import java.util.ArrayList; +import java.util.List; + +public class Construct { + + final private String name; + final private List parametres; + final private int ligne; + + public Construct(String name, int ligne) { + this.name = name; + this.parametres = new ArrayList<>(); + this.ligne = ligne; + } + + public String getName() { + return name; + } + + public List getParametres() { + return parametres; + } + + public int getLigne() { + return ligne; + } + +} diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java new file mode 100644 index 0000000..1844081 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java @@ -0,0 +1,48 @@ +package com.ensao.gi5.lint.wrapper.classe; + +import java.util.ArrayList; +import java.util.List; + +public class Methode { + + final private String name; + final private String returnType; + final private List parametres; + final private String accessSpecifier; + final private int ligne; + + + public Methode(String name, String returnType, String accessSpecifier, int ligne) { + this.name = name; + this.returnType = returnType; + this.accessSpecifier = accessSpecifier; + this.parametres = new ArrayList<>(); + this.ligne = ligne; + } + + public String getName() { + return name; + } + + public String getAccessSpecifier() { + return accessSpecifier; + } + + public String getReturnType() { + return returnType; + } + + public List getParametres() { + return parametres; + } + + public int getLigne() { + return ligne; + } + + @Override + public String toString() { + return String.format("%s %s(%s)", this.returnType, this.name, + String.join(", ", parametres.stream().map(Parametre::getType).toArray(String[]::new))); + } +} \ No newline at end of file diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/classe/Parametre.java b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Parametre.java new file mode 100644 index 0000000..1daab73 --- /dev/null +++ b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Parametre.java @@ -0,0 +1,26 @@ +package com.ensao.gi5.lint.wrapper.classe; + +public class Parametre { + + final private String name; + final private String type; + + public Parametre(String name, String type) { + this.name = name; + this.type = type; + } + + public String getName() { + return name; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + return this.type + " " + this.name; + } + +} From 279ca738b651db4eec354a87b07b51332fb6596c Mon Sep 17 00:00:00 2001 From: ilyas Date: Fri, 13 Jan 2023 00:33:28 +0100 Subject: [PATCH 4/4] Adding Unit Tests --- .../ensao/gi5/lint/constantes/Constantes.java | 12 +++- .../com/ensao/gi5/lint/rules/ClassRule.java | 59 +++++++++++++++++-- .../com/ensao/gi5/lint/runner/Runner.java | 10 ++++ .../gi5/lint/wrapper/classe/Methode.java | 10 ++++ .../ensao/gi5/lint/rules/BoolExpRuleTest.java | 24 ++++++++ .../ensao/gi5/lint/rules/ClassRuleTest.java | 35 +++++++++++ .../ensao/gi5/lint/rules/EnumRuleTest.java | 23 ++++++++ .../ensao/gi5/lint/rules/IfElseRuleTest.java | 23 ++++++++ .../com/ensao/gi5/lint/rules/RuleTest.java | 21 +++++++ .../lint/rules/UnusedVariablesRuleTest.java | 27 +++++++++ .../com/ensao/gi5/lint/runner/RunnerTest.java | 8 ++- 11 files changed, 242 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/ensao/gi5/lint/rules/BoolExpRuleTest.java create mode 100644 src/test/java/com/ensao/gi5/lint/rules/ClassRuleTest.java create mode 100644 src/test/java/com/ensao/gi5/lint/rules/EnumRuleTest.java create mode 100644 src/test/java/com/ensao/gi5/lint/rules/IfElseRuleTest.java create mode 100644 src/test/java/com/ensao/gi5/lint/rules/RuleTest.java create mode 100644 src/test/java/com/ensao/gi5/lint/rules/UnusedVariablesRuleTest.java diff --git a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java index 598f777..0df7cca 100644 --- a/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java +++ b/src/main/java/com/ensao/gi5/lint/constantes/Constantes.java @@ -6,13 +6,19 @@ public class Constantes { 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_006 = "LINT_REG_006";// - public static final String LINT_REG_007 = "LINT_REG_007"; + 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"; + public static final String LINT_REG_018 = "LINT_REG_018"; // private Constantes() { diff --git a/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java index f204e52..5e261e2 100644 --- a/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java +++ b/src/main/java/com/ensao/gi5/lint/rules/ClassRule.java @@ -4,11 +4,14 @@ 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 class ClassRule extends Rule { public ClassRule() { super(Constantes.LINT_REG_002, Level.HIGHEST); @@ -16,10 +19,58 @@ public ClassRule() { @Override public void apply(CompilationUnitWrapper compilationUnit) { - - List classes = new ArrayList<>(); - compilationUnit.accept(new ClassVisitor(), classes); + List 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 diff --git a/src/main/java/com/ensao/gi5/lint/runner/Runner.java b/src/main/java/com/ensao/gi5/lint/runner/Runner.java index 588d674..59a3d2c 100644 --- a/src/main/java/com/ensao/gi5/lint/runner/Runner.java +++ b/src/main/java/com/ensao/gi5/lint/runner/Runner.java @@ -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) { @@ -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(); } diff --git a/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java index 1844081..1639be3 100644 --- a/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java +++ b/src/main/java/com/ensao/gi5/lint/wrapper/classe/Methode.java @@ -10,6 +10,7 @@ public class Methode { final private List parametres; final private String accessSpecifier; final private int ligne; + private int lignesCounts; public Methode(String name, String returnType, String accessSpecifier, int ligne) { @@ -18,6 +19,7 @@ public Methode(String name, String returnType, String accessSpecifier, int ligne this.accessSpecifier = accessSpecifier; this.parametres = new ArrayList<>(); this.ligne = ligne; + this.lignesCounts = 0; } public String getName() { @@ -40,6 +42,14 @@ public int getLigne() { return ligne; } + public void setLinesCount(int lignes) { + this.lignesCounts = lignes; + } + + public int getLinesCount() { + return lignesCounts; + } + @Override public String toString() { return String.format("%s %s(%s)", this.returnType, this.name, diff --git a/src/test/java/com/ensao/gi5/lint/rules/BoolExpRuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/BoolExpRuleTest.java new file mode 100644 index 0000000..afcd45b --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/BoolExpRuleTest.java @@ -0,0 +1,24 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class BoolExpRuleTest extends RuleTest{ + + private static final String STATEMENT_TEST_FOLDER = "testFiles/Statement"; + + private BoolExpRule boolExpRule; + + @Test + public void test_LINT_REG_006(){ + Set violations = getViolations(STATEMENT_TEST_FOLDER, boolExpRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_006).size(), 1); + } +} diff --git a/src/test/java/com/ensao/gi5/lint/rules/ClassRuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/ClassRuleTest.java new file mode 100644 index 0000000..1cf9071 --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/ClassRuleTest.java @@ -0,0 +1,35 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class ClassRuleTest extends RuleTest { + private static final String CLASS_TEST_FOLDER = "testFiles/Class"; + + private ClassRule classRule; + + @Test + public void test_LINT_REG_008(){ + Set violations = getViolations(CLASS_TEST_FOLDER, classRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_008).size(), 1); + } + + @Test + public void test_LINT_REG_011(){ + Set violations = getViolations(CLASS_TEST_FOLDER, classRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_011).size(), 1); + } + + @Test + public void test_LINT_REG_012(){ + Set violations = getViolations(CLASS_TEST_FOLDER, classRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_012).size(), 1); + } +} diff --git a/src/test/java/com/ensao/gi5/lint/rules/EnumRuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/EnumRuleTest.java new file mode 100644 index 0000000..95c4db6 --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/EnumRuleTest.java @@ -0,0 +1,23 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class EnumRuleTest extends RuleTest{ + private static final String STATEMENT_TEST_FOLDER = "testFiles/Statement"; + + private EnumRule enumRule; + + @Test + public void test_LINT_REG_007(){ + Set violations = getViolations(STATEMENT_TEST_FOLDER, enumRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_007).size(), 1); + } +} \ No newline at end of file diff --git a/src/test/java/com/ensao/gi5/lint/rules/IfElseRuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/IfElseRuleTest.java new file mode 100644 index 0000000..258d7f9 --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/IfElseRuleTest.java @@ -0,0 +1,23 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class IfElseRuleTest extends RuleTest{ + private static final String STATEMENT_TEST_FOLDER = "testFiles/Statement"; + + private IfElseRule ifElseRule; + + @Test + public void test_LINT_REG_018(){ + Set violations = getViolations(STATEMENT_TEST_FOLDER, ifElseRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_018).size(), 1); + } +} diff --git a/src/test/java/com/ensao/gi5/lint/rules/RuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/RuleTest.java new file mode 100644 index 0000000..20458dd --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/RuleTest.java @@ -0,0 +1,21 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import com.ensao.gi5.lint.Linter; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class RuleTest { + + protected Set getViolations(String folder, Rule... rules){ + Set violations = new TreeSet<>(); + Linter linter = new Linter(); + for(Rule rule : rules) linter.registerRule(rule); + linter.registerSource(folder); + linter.run(); + return violations; + } + +} diff --git a/src/test/java/com/ensao/gi5/lint/rules/UnusedVariablesRuleTest.java b/src/test/java/com/ensao/gi5/lint/rules/UnusedVariablesRuleTest.java new file mode 100644 index 0000000..7b6b60c --- /dev/null +++ b/src/test/java/com/ensao/gi5/lint/rules/UnusedVariablesRuleTest.java @@ -0,0 +1,27 @@ +package com.ensao.gi5.lint.rules; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.ensao.gi5.lint.constantes.Constantes; +import com.ensao.gi5.lint.rules.violations.Violation; + +public class UnusedVariablesRuleTest extends RuleTest{ + + private static final String STATEMENT_TEST_FOLDER = "testFiles/Statement"; + + private UnusedVariablesRule unusedVariablesRule; + + + @Test + public void test_LINT_REG_016(){ + Set violations = getViolations(STATEMENT_TEST_FOLDER, unusedVariablesRule); + Assertions.assertEquals(violations.get(Constantes.LINT_REG_016).size(), 1); + } + } + +} diff --git a/src/test/java/com/ensao/gi5/lint/runner/RunnerTest.java b/src/test/java/com/ensao/gi5/lint/runner/RunnerTest.java index 67f6209..9ea0a34 100644 --- a/src/test/java/com/ensao/gi5/lint/runner/RunnerTest.java +++ b/src/test/java/com/ensao/gi5/lint/runner/RunnerTest.java @@ -5,9 +5,11 @@ public class RunnerTest { + public static final String ARG_PARAM = "-s"; + @Test public void testMain() { - Runner.main(new String[]{"-d", "testFiles"}); + Runner.main(new String[]{ARG_PARAM, "testFiles"}); } @Test @@ -32,7 +34,7 @@ public void testMainEmptyArgs() { @Test public void testMainEmptyDirectory() { Assertions.assertThrows(IllegalStateException.class, () -> { - Runner.main(new String[]{"-d", ""}); + Runner.main(new String[]{ARG_PARAM, ""}); }); } @@ -40,7 +42,7 @@ public void testMainEmptyDirectory() { @Test public void testMainNotAssignedDirectory() { Assertions.assertThrows(IllegalStateException.class, () -> { - Runner.main(new String[]{"-d"}); + Runner.main(new String[]{ARG_PARAM}); }); }