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

ElkhalifiZahia-GhammouriAsmae #27

Open
wants to merge 1 commit 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
21 changes: 21 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 @@ -3,6 +3,27 @@
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_003 = "LINT_REG_003";
public static final String LINT_REG_004 = "LINT_REG_004";
public static final String LINT_REG_005 = "LINT_REG_005";
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_009 = "LINT_REG_009";
public static final String LINT_REG_010 = "LINT_REG_010";
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_013 = "LINT_REG_013";

public static final String LINT_REG_014 = "LINT_REG_014";

public static final String LINT_REG_015 = "LINT_REG_015";
public static final String LINT_REG_016 = "LINT_REG_016";

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


private Constantes() {
throw new IllegalStateException("not to be instantiated");
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/BooleanExpressionRule.java
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) {

Copy link
Owner

Choose a reason for hiding this comment

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

no implementation found

}

@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 -> {
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 (!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))) {
Copy link
Owner

Choose a reason for hiding this comment

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

!Character.isUpperCase ==> Character.isLowerCase

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;
}
}

33 changes: 33 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ClassVariableNameRule.java
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();
Copy link
Owner

Choose a reason for hiding this comment

The 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
example :

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);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ClausesRule.java
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)) {
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 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)) {
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/ConstantClassNameRule.java
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;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/EnumElementRule.java
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);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/ensao/gi5/lint/rules/MethodBodyLengthRule.java
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;
}
}
Loading