-
Notifications
You must be signed in to change notification settings - Fork 49
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
Issue 116 Name and Reuse Repeating Conditions #131
Conversation
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.
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.
# Conflicts: # src/test/java/com/cognifide/aemrules/htl/HtlProfileTest.java
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.
src/main/resources/rules/HTL-5.md
Outdated
@@ -0,0 +1,25 @@ | |||
Always Place HTL Attributes After the Ones that are Part of the Markup |
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.
This description is actually for different rule!
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.
changed to: Always try to re-use existing conditions, so the code is more readable.
src/main/java/com/cognifide/aemrules/htl/checks/NamingAndReusingConditionsCheck.java
Outdated
Show resolved
Hide resolved
src/main/java/com/cognifide/aemrules/htl/checks/NamingAndReusingConditionsCheck.java
Show resolved
Hide resolved
src/main/java/com/cognifide/aemrules/htl/checks/NamingAndReusingConditionsCheck.java
Show resolved
Hide resolved
src/main/java/com/cognifide/aemrules/htl/checks/NamingAndReusingConditionsCheck.java
Outdated
Show resolved
Hide resolved
src/main/java/com/cognifide/aemrules/htl/checks/NamingAndReusingConditionsCheck.java
Outdated
Show resolved
Hide resolved
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.
Please check comments
# Conflicts: # src/main/java/com/cognifide/aemrules/htl/rules/HtlCheckClasses.java
I think that the previous implementation was better and needed a tweak. It's hard to understand current approach. |
|
||
@Override | ||
public void startHtlElement(List<Expression> expressions, TagNode node) { | ||
if (isConditionReused(expressions, node)) { |
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.
This time also, the method name is misleading since "reused" has a positive connotation whereas in your case reused means "the same condition used couple of times needlessly".
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.
Conditions inverted and method name changed to isConditionReusedCorrectly()
.
private Set<String> namedConditions = new HashSet<>(); | ||
|
||
@Override | ||
public void startHtlElement(List<Expression> expressions, TagNode node) { |
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.
This API has to be rewritten as it decouples expression from the attribute.
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.
@piotr-wilczynski You should take a look at this :)
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.
this method is giving You all of the expressions that exists in node, but if You want the have relation between attribute and expresion You can always check if there is any expression in the attribute and proceed somehow. There is a method called htlExpression default void htlExpression(Expression expression, Node node)
It is probably what You are looking for @chutch ?
private boolean isConditionReused(List<Expression> expressions, TagNode node) { | ||
String condition = expressions.stream() | ||
.map(Expression::getRawText) | ||
.map(text -> text.replaceAll("[${}]", "")) |
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.
Please extract 'getRawText' and 'text.replaceAll("[${}]", "")' to method and reuse it in '103' line.
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.
Extracted
private Set<String> clearExpressions(List<Expression> expressions){ return expressions.stream() .map(Expression::getRawText) .map(text -> text.replaceAll("[${}]", "")) .collect(Collectors.toSet()); }
.filter(text -> text.contains(SLY_TEST)) | ||
.findFirst(); | ||
if (condition.isPresent() && !condition.get().equals(SLY_TEST)) { | ||
condition = Optional.of(condition.get().substring(14)); |
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.
what is 14?
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.
fixed.
src/main/resources/rules/HTL-5.md
Outdated
<!--/* Bad - the same condition is evaluated multiple times */--> | ||
<span class="uber-mode__top-bar" data-sly-test="${uberModeHelper.uberModeEnabled || forceUberMode}"> | ||
<div class="my-component"> | ||
Blah blah blah |
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.
maybe ... ?
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.
fixed
private Set<String> namedConditions = new HashSet<>(); | ||
|
||
@Override | ||
public void startHtlElement(List<Expression> expressions, TagNode node) { |
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.
this method is giving You all of the expressions that exists in node, but if You want the have relation between attribute and expresion You can always check if there is any expression in the attribute and proceed somehow. There is a method called htlExpression default void htlExpression(Expression expression, Node node)
It is probably what You are looking for @chutch ?
@ParsingErrorRule | ||
public class NamingAndReusingConditionsCheck extends AbstractHtlCheck { | ||
|
||
static final String RULE_KEY = "HTL-4"; |
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.
You define HTL-4 rule key, but doc file is named HTL-5, why?
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.
Changed
# Conflicts: # README.md
fixes #116