-
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
ensao-lint (Mohamed El kadiri) #28
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.ensao.gi5.lint.rules; | ||
package com.ensao.gi5.lint.enumeration; | ||
|
||
public enum Level { | ||
BLOCKER, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ensao.gi5.lint.enumeration; | ||
|
||
public enum RulesEnum { | ||
UNUSED_IMPORT_STATEMENT, | ||
PARSE_ERROR, | ||
CLASS_NAME, | ||
BOOLEAN_EXPRESSION, | ||
METHOD_LINES, | ||
ATTRIBUTE_VISIBILITY, | ||
ATTRIBUTE_NAME, | ||
CLASS_METHODS | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ensao.gi5.lint.factory; | ||
|
||
import com.ensao.gi5.lint.enumeration.RulesEnum; | ||
import com.ensao.gi5.lint.rules.*; | ||
|
||
public class RuleFactory { | ||
public Rule getRule(RulesEnum ruleSubClass) { | ||
return switch(ruleSubClass) { | ||
case UNUSED_IMPORT_STATEMENT -> new UnusedImportsRule(); | ||
case PARSE_ERROR -> new ParseErrorRule(); | ||
case CLASS_NAME -> new TypeNameRule(); | ||
case BOOLEAN_EXPRESSION -> new BooleanExpressionRule(); | ||
case METHOD_LINES -> new MethodStatementsRule(); | ||
case ATTRIBUTE_VISIBILITY -> new AttributeVisibilityRule(); | ||
case ATTRIBUTE_NAME -> new AttributeNameRule(); | ||
case CLASS_METHODS -> new ClassMethodsRule(); | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.enumeration.Level; | ||
import com.ensao.gi5.lint.visitor.AttributeVisitor; | ||
import com.ensao.gi5.lint.wrapper.AttributeWrapper; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AttributeNameRule extends Rule { | ||
|
||
public AttributeNameRule() { | ||
super(Constantes.LINT_REG_004, Level.HIGH); | ||
} | ||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<AttributeWrapper> attributes = new ArrayList<>(); | ||
compilationUnit.accept(new AttributeVisitor(), attributes); | ||
for (AttributeWrapper attribute: attributes) { | ||
if ( | ||
Character.isUpperCase(attribute.getFieldName().charAt(0)) | ||
&& isAttributeNotConstant(attribute) | ||
) { | ||
buildViolationThenAddToCollection( | ||
attribute.getLine(), | ||
attribute + " should start with a lower case", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
} | ||
|
||
public boolean isAttributeNotConstant(AttributeWrapper attribute) { | ||
return !attribute.isStatic() && !attribute.isFinal(); | ||
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 it is final, it doesn't mean it is static private final String foo; 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. I had to ckeck for constants because they should start with an uppercase thats why i checked for both in this case 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. I see. 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. I see, thanks for the clarification |
||
} | ||
|
||
@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.enumeration.Level; | ||
import com.ensao.gi5.lint.visitor.AttributeVisitor; | ||
import com.ensao.gi5.lint.wrapper.AttributeWrapper; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AttributeVisibilityRule extends Rule{ | ||
|
||
public AttributeVisibilityRule() { | ||
super(Constantes.LINT_REG_013, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<AttributeWrapper> attributes = new ArrayList<>(); | ||
compilationUnit.accept(new AttributeVisitor(), attributes); | ||
for (AttributeWrapper attribute: attributes) { | ||
if (attribute.getAccessModifier().isEmpty()) { | ||
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. Given that this is a wrapper, put this logic in wrapped method if(attribute.hasNoAccessModifiers()) { ..... |
||
buildViolationThenAddToCollection( | ||
attribute.getLine(), | ||
attribute + " should have an access modifier declared", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.enumeration.Level; | ||
import com.ensao.gi5.lint.visitor.BooleanExpressionVisitor; | ||
import com.ensao.gi5.lint.wrapper.BooleanExpressionWrapper; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BooleanExpressionRule extends Rule { | ||
|
||
public BooleanExpressionRule() { | ||
super(Constantes.LINT_REG_006, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<BooleanExpressionWrapper> booleanExpressions = new ArrayList<>(); | ||
compilationUnit.accept(new BooleanExpressionVisitor(), booleanExpressions); | ||
for (BooleanExpressionWrapper booleanExpression: booleanExpressions) { | ||
if (checkIfBooleanExpressionHasMoreThanTwoOperands(booleanExpression)) { | ||
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.
|
||
buildViolationThenAddToCollection( | ||
booleanExpression.getLine(), | ||
"Boolean Expression " + booleanExpression + " should have 2 operands at most", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
} | ||
|
||
public boolean checkIfBooleanExpressionHasMoreThanTwoOperands(BooleanExpressionWrapper booleanWrapper) { | ||
String[] booleanOperands = booleanWrapper.getCondition().split("(&&) | (\\|\\|)"); | ||
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. could be done by a visitor pattern |
||
return booleanOperands.length > 2; | ||
} | ||
@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.enumeration.Level; | ||
import com.ensao.gi5.lint.visitor.TypeVisitor; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.ensao.gi5.lint.wrapper.TypeWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ClassMethodsRule extends Rule { | ||
|
||
public ClassMethodsRule() { | ||
super(Constantes.LINT_REG_011, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<TypeWrapper> typesDeclared = new ArrayList<>(); | ||
compilationUnit.accept(new TypeVisitor(), typesDeclared); | ||
for (TypeWrapper type: typesDeclared) { | ||
if (type.getMethodsNumber() > 20) { | ||
buildViolationThenAddToCollection( | ||
type.getLine(), | ||
type + " shouldn't have more than 20 methods declared inside of it", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isActive() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.ensao.gi5.lint.rules; | ||
|
||
import com.ensao.gi5.lint.constantes.Constantes; | ||
import com.ensao.gi5.lint.enumeration.Level; | ||
import com.ensao.gi5.lint.visitor.MethodVisitor; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.ensao.gi5.lint.wrapper.MethodWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MethodStatementsRule extends Rule { | ||
|
||
public MethodStatementsRule() { | ||
super(Constantes.LINT_REG_008, Level.HIGHEST); | ||
} | ||
|
||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<MethodWrapper> methods = new ArrayList<>(); | ||
compilationUnit.accept(new MethodVisitor(), methods); | ||
for (MethodWrapper method: methods) { | ||
if (checkIfMethodHasMoreThanThirtyLines(method)) { | ||
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.
|
||
buildViolationThenAddToCollection( | ||
method.getLine(), | ||
method + "shouldn't contain more than 30 statements", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
} | ||
|
||
public boolean checkIfMethodHasMoreThanThirtyLines(MethodWrapper methodWrapper) { | ||
return methodWrapper.getStatements().size() > 30; | ||
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. put this logic into the wrapper class |
||
} | ||
@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.enumeration.Level; | ||
import com.ensao.gi5.lint.util.Utils; | ||
import com.ensao.gi5.lint.visitor.BooleanExpressionVisitor; | ||
import com.ensao.gi5.lint.visitor.TypeVisitor; | ||
import com.ensao.gi5.lint.wrapper.BooleanExpressionWrapper; | ||
import com.ensao.gi5.lint.wrapper.CompilationUnitWrapper; | ||
import com.ensao.gi5.lint.wrapper.TypeWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TypeNameRule extends Rule { | ||
public TypeNameRule() { | ||
super(Constantes.LINT_REG_002, Level.HIGHEST); | ||
} | ||
@Override | ||
public void apply(CompilationUnitWrapper compilationUnit) { | ||
List<TypeWrapper> typeNames = new ArrayList<>(); | ||
compilationUnit.accept(new TypeVisitor(), typeNames); | ||
typeNames = Utils.checkIfNamesDontFollowsRule(typeNames); | ||
for (TypeWrapper typeName: typeNames) { | ||
buildViolationThenAddToCollection( | ||
typeName.getLine(), | ||
typeName + " should start with uppercase and shouldn't contain underscores", | ||
compilationUnit.getFileName() | ||
); | ||
} | ||
} | ||
|
||
@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.
👍🏽