-
Notifications
You must be signed in to change notification settings - Fork 37
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
ElkhalifiZahia-GhammouriAsmae #27
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
public class BooleanExpressionRule extends Rule{ | ||
public BooleanExpressionRule() { | ||
super(Constantes.LINT_REG_006, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.ensao.gi5.lint.rules; | ||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.FieldDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class ClassFieldVisibilityRule extends Rule { | ||
public ClassFieldVisibilityRule() { | ||
super(Constantes.LINT_REG_013, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<FieldDeclaration> fields = compilationUnit.getCompilationUnit().findAll(FieldDeclaration.class); | ||
fields | ||
.forEach(fieldDeclaration -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use visitor pattern |
||
if (!fieldDeclaration.isPrivate() && !fieldDeclaration.isProtected() && !fieldDeclaration.isPublic()) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Le champ de classe doit avoir une visibilité déclarée: " + fieldDeclaration.getVariable(0).getNameAsString()); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.ensao.gi5.lint.rules; | ||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; | ||
import com.github.javaparser.ast.body.TypeDeclaration; | ||
|
||
import java.util.List; | ||
|
||
|
||
public class ClassOrInterfaceNameRule extends Rule{ | ||
public ClassOrInterfaceNameRule() { | ||
super(Constantes.LINT_REG_002, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<TypeDeclaration<?>> types = compilationUnit.getCompilationUnit().getTypes(); | ||
types.stream() | ||
.filter(ClassOrInterfaceDeclaration.class::isInstance) | ||
.map(ClassOrInterfaceDeclaration.class::cast) | ||
.forEach(classOrInterface -> { | ||
String name = classOrInterface.getNameAsString(); | ||
int lineNumber = classOrInterface.getName().getBegin().get().line; | ||
if (!Character.isUpperCase(name.charAt(0))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
final Violation violation = new Violation(); | ||
violation.setDescription("Le nom de la classe ou de l'interface ou bien enum doit commencer par une majuscule: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
if (name.contains("_")) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Le nom de la classe ou de l'interface ou enum ne doit pas contenir de traits de soulignement: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.FieldDeclaration; | ||
|
||
public class ClassVariableNameRule extends Rule { | ||
public ClassVariableNameRule() { | ||
super(Constantes.LINT_REG_004, Level.HIGH); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
compilationUnit.getCompilationUnit().findAll(FieldDeclaration.class).forEach(fieldDeclaration -> { | ||
String name = fieldDeclaration.getVariables().get(0).getNameAsString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if a field declaration contains more than one variable, this will not work private String foo, bar, alice; |
||
int lineNumber = fieldDeclaration.getVariables().get(0).getBegin().get().line; | ||
if (!Character.isLowerCase(name.charAt(0))) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Les attributs d'une classe commencent par une minuscule: " + name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. attributs d'instance et non pas de classe |
||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ensao.gi5.lint.rules; | ||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.stmt.BlockStmt; | ||
import com.github.javaparser.ast.stmt.IfStmt; | ||
import com.github.javaparser.ast.stmt.Statement; | ||
|
||
import java.util.Optional; | ||
|
||
public class ClausesRule extends Rule { | ||
|
||
public ClausesRule() { | ||
super(Constantes.LINT_REG_018, Level.LOW); | ||
} | ||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
|
||
for (IfStmt clause : compilationUnit.getCompilationUnit().findAll(IfStmt.class)) { | ||
Statement ifClause = clause.getThenStmt(); | ||
Optional<Statement> elseClause = clause.getElseStmt(); | ||
if (!(ifClause instanceof BlockStmt)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use visitor pattern to avoid type checks |
||
final Violation violation = new Violation(); | ||
violation.setDescription("les clauses if , doit avoir des accolades"); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
if (elseClause.isPresent() && !(elseClause.get() instanceof BlockStmt)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use Optional chaining |
||
final Violation violation = new Violation(); | ||
violation.setDescription("les clauses else, doit avoir des accolades"); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.FieldDeclaration; | ||
|
||
public class ConstantClassNameRule extends Rule { | ||
public ConstantClassNameRule() { | ||
super(Constantes.LINT_REG_005, Level.MEDIUM); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
compilationUnit.getCompilationUnit().findAll(FieldDeclaration.class).forEach(fieldDeclaration -> { | ||
if (fieldDeclaration.isFinal() && fieldDeclaration.isStatic()) { | ||
String name = fieldDeclaration.getVariables().get(0).getNameAsString(); | ||
int lineNumber = fieldDeclaration.getVariables().get(0).getBegin().get().line; | ||
if (!name.equals(name.toUpperCase())) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Les constantes d'une classe sont écrites en majuscule: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
if (!name.contains("_")) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Les constantes d'une classe sont écrites avec des _ comme séparateur: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.EnumDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class EnumElementRule extends Rule{ | ||
public EnumElementRule() { | ||
super(Constantes.LINT_REG_007, Level.LOW); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
// Get a list of all enum declarations in the file | ||
List<EnumDeclaration> enums = compilationUnit.getCompilationUnit().findAll(EnumDeclaration.class); | ||
// Use a stream to check the names of the enum elements | ||
enums.stream() | ||
.forEach(enumDeclaration -> { | ||
// Iterate over the elements of the enum | ||
for (int i = 0; i < enumDeclaration.getEntries().size(); i++) { | ||
String name = enumDeclaration.getEntries().get(i).getNameAsString(); | ||
// Check if the name is written in uppercase | ||
if (!name.equals(name.toUpperCase())) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Enum element name must be written in uppercase: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
// Check if the name contains underscores | ||
if (!name.contains("_")) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Enum element name must contain underscores: " + name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a must, if there is a need to separate things it will be by using an underscore |
||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true ; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.VariableDeclarator; | ||
|
||
public class LocalVariableSyntaxRule extends Rule{ | ||
public LocalVariableSyntaxRule() { | ||
super(Constantes.LINT_REG_003, Level.HIGH); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
compilationUnit.getCompilationUnit().findAll(VariableDeclarator.class).forEach(variableDeclarator -> { | ||
String name = variableDeclarator.getNameAsString(); | ||
int lineNumber = variableDeclarator.getName().getBegin().get().line; | ||
if (!Character.isLowerCase(name.charAt(0))) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Les variables locales commencent par une minuscule: " + name); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
violation.setLine(lineNumber); | ||
addViolation(violation); | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.rules.violations.Violation; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
|
||
import java.util.List; | ||
|
||
public class MethodBodyLengthRule extends Rule{ | ||
public MethodBodyLengthRule() { | ||
super(Constantes.LINT_REG_008, Level.HIGHEST); | ||
|
||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<MethodDeclaration> methods = compilationUnit.getCompilationUnit().findAll(MethodDeclaration.class); | ||
methods.stream() | ||
.filter(method -> method.getBody().isPresent()) | ||
.forEach(method -> { | ||
if (method.getBody().get().toString().split("\n").length > 30) { | ||
final Violation violation = new Violation(); | ||
violation.setDescription("Le corps d'une méthode ne doit pas dépasser 30 lignes"); | ||
violation.setFileName(compilationUnit.getFileName()); | ||
addViolation(violation); | ||
} | ||
}); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no implementation found