Skip to content

Commit

Permalink
Update HTL rules
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-strzelecki-vml committed Oct 28, 2024
1 parent 8f80b4a commit 4b3ea13
Show file tree
Hide file tree
Showing 150 changed files with 697 additions and 1,572 deletions.
9 changes: 3 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@
<version>${htl.version}</version>
</dependency>

<!-- TODO: Check if versions are valid with target AEM version-->
<!-- unit tests -->
<dependency>
<artifactId>sonar-testing-harness</artifactId>
Expand Down Expand Up @@ -299,14 +298,12 @@
<scope>test</scope>
<version>1.12.0</version>
</dependency>
<!-- TODO: Update dependecy with new version-->
<dependency>
<artifactId>org.apache.sling.servlets.resolver</artifactId>
<groupId>org.apache.sling</groupId>
<scope>test</scope>
<version>2.3.2</version>
</dependency>
<!-- TODO: Update dependecy with new version-->
<dependency>
<artifactId>org.apache.sling.api</artifactId>
<groupId>org.apache.sling</groupId>
Expand Down Expand Up @@ -372,8 +369,8 @@
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<!-- <shadedArtifactAttached>false</shadedArtifactAttached>-->
<!-- <minimizeJar>true</minimizeJar>-->
<shadedArtifactAttached>false</shadedArtifactAttached>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
Expand Down Expand Up @@ -430,7 +427,7 @@
<exclude>**/LICENSE.txt</exclude>
<exclude>**.xml</exclude>
<exclude>assets/**</exclude>
<exclude>src/test/resources/htl/HtlCommentsCheck.html</exclude>
<exclude>src/test/files/htl/HtlCommentsCheck.html</exclude>
</excludes>
</configuration>
<executions>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/vml/aemrules/AemRulesSonarPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@

public class AemRulesSonarPlugin implements Plugin {

private static List<PropertyDefinition> pluginProperties() {
private static List<PropertyDefinition> createHtlLanguageProperties() {
return Arrays.asList(
PropertyDefinition.builder(Constants.FILE_EXTENSIONS_PROP_KEY)
.name("File suffixes")
.description("List of file suffixes that will be scanned.")
.category(Htl.NAME)
.category(Htl.LANGUAGE_NAME)
.defaultValue(Constants.FILE_EXTENSIONS_DEF_VALUE)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
.build(),
PropertyDefinition.builder(Constants.HTL_FILES_RELATIVE_PATHS_KEY)
.name("HTL files relative paths")
.description("List of relative paths that contains HTL files.")
.category(Htl.NAME)
.category(Htl.LANGUAGE_NAME)
.defaultValue(Constants.HTL_FILES_RELATIVE_PATHS_DEF_VALUE)
.onQualifiers(Qualifiers.PROJECT)
.multiValues(true)
Expand All @@ -69,6 +69,6 @@ public void define(Context context) {
HtlProfile.class,
HtlSensor.class);

context.addExtensions(pluginProperties());
context.addExtensions(createHtlLanguageProperties());
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/vml/aemrules/htl/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

public final class Constants {

public static final String REPOSITORY_KEY = "AEM-HTL";
public static final String REPOSITORY_NAME = "AEM HTL";

public static final String FILE_EXTENSIONS_PROP_KEY = "sonar.htl.file.suffixes";
public static final String FILE_EXTENSIONS_DEF_VALUE = ".html";
public static final String HTL_FILES_RELATIVE_PATHS_KEY = "sonar.htl.file.paths";
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/vml/aemrules/htl/Htl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ public class Htl extends AbstractLanguage {
/**
* Language key
*/
public static final String KEY = "htl";
public static final String LANGUAGE_KEY = "htl";

/**
* Language name
*/
public static final String NAME = "HTL";
public static final String LANGUAGE_NAME = "HTL";

/**
* Default Htl files known suffixes
*/
public static final String[] DEFAULT_FILE_SUFFIXES = new String[]{};

public Htl() {
super(KEY, NAME);
super(LANGUAGE_KEY, LANGUAGE_NAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static FilePredicate createFilePredicate(Configuration configuration, Sen
return predicates.and(
predicates.hasType(InputFile.Type.MAIN),
predicates.or(
predicates.hasLanguages(Htl.KEY),
predicates.hasLanguages(Htl.LANGUAGE_KEY),
predicates.or(fileExtensions)
),
predicates.or(relativePaths)
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/vml/aemrules/htl/HtlFilesAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,33 @@
import com.vml.aemrules.htl.rules.HtlRulesList;
import com.vml.aemrules.utils.Throwables;
import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.squidbridge.ProgressReport;
import org.sonar.squidbridge.api.AnalysisException;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.CancellationException;

import static com.vml.aemrules.htl.Constants.REPOSITORY_KEY;


public abstract class HtlFilesAnalyzer {

private static final Logger LOGGER = Loggers.get(HtlFilesAnalyzer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(HtlFilesAnalyzer.class);
protected final HtlChecks checks;
private final RuleKey parsingErrorRuleKey;

public HtlFilesAnalyzer(CheckFactory checkFactory) {
protected HtlFilesAnalyzer(CheckFactory checkFactory) {
this.checks = HtlChecks.createHtlCheck(checkFactory)
.addChecks(HtlRulesList.REPOSITORY_KEY, HtlRulesList.getCheckClasses());
.addChecks(REPOSITORY_KEY, HtlRulesList.getChecks());
this.parsingErrorRuleKey = setupParsingErrorRuleKey(checks);
}

Expand Down Expand Up @@ -107,7 +110,7 @@ private void analyse(SensorContext sensorContext, InputFile inputFile) {
scanFile(sensorContext, inputFile);
} catch (SightlyCompilerException e) {
checkInterrupted(e);
LOGGER.error("Unable to parse file: " + inputFile.uri());
LOGGER.error("Unable to parse file: {}", inputFile.uri());
LOGGER.error(e.getMessage());
processRecognitionException(e, sensorContext, inputFile);
} catch (Exception e) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/vml/aemrules/htl/HtlProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@

import java.util.Objects;

import static com.vml.aemrules.htl.Constants.REPOSITORY_KEY;

public class HtlProfile implements BuiltInQualityProfilesDefinition {

private static final String QUALITY_PROFILE_NAME = "AEM HTL Profile";
private static final String QUALITY_PROFILE_NAME = "HTL";

@Override
public void define(Context context) {
NewBuiltInQualityProfile htl = context
.createBuiltInQualityProfile(QUALITY_PROFILE_NAME, Htl.KEY);
HtlRulesList.getCheckClasses().stream()
.createBuiltInQualityProfile(QUALITY_PROFILE_NAME, Htl.LANGUAGE_KEY);
HtlRulesList.getChecks().stream()
.map(HtlRulesList::getRule)
.map(Rule::key)
.filter(Objects::nonNull)
.forEach(ruleKey -> htl.activateRule(HtlRulesList.REPOSITORY_KEY, ruleKey));
.forEach(ruleKey -> htl.activateRule(REPOSITORY_KEY, ruleKey));
htl.done();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/vml/aemrules/htl/HtlSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public HtlSensor(FileLinesContextFactory fileLinesContextFactory, Configuration
@Override
public void describe(SensorDescriptor descriptor) {
descriptor
.name(Htl.NAME)
.name(Htl.LANGUAGE_NAME)
.onlyOnFileType(Type.MAIN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
*/
package com.vml.aemrules.htl.checks;

import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.apache.commons.lang3.StringUtils;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.html.node.Attribute;
import org.sonar.plugins.html.node.TagNode;
Expand All @@ -32,18 +29,8 @@
import java.util.List;
import java.util.Optional;

@Rule(
key = AvoidExtraSlyTagsCheck.RULE_KEY,
name = AvoidExtraSlyTagsCheck.RULE_MESSAGE,
priority = Priority.MINOR,
tags = Tags.AEM
)
@AemVersion(
from = "6.0"
)
@Metadata(
technicalDebt = "5min"
)
@Rule(key = AvoidExtraSlyTagsCheck.RULE_KEY)
@AemVersion(from = "6.0")
public class AvoidExtraSlyTagsCheck extends AbstractHtlCheck {

public static final String RULE_KEY = "HTL-11";
Expand Down Expand Up @@ -73,4 +60,3 @@ public void startElement(TagNode node) {
});
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,16 @@
package com.vml.aemrules.htl.checks;

import com.vml.aemrules.htl.visitors.HtlStringOptionVisitor;
import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.scripting.sightly.compiler.expression.Expression;
import org.apache.sling.scripting.sightly.compiler.expression.MarkupContext;
import org.apache.sling.scripting.sightly.impl.compiler.Syntax;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.html.node.Node;

@Rule(
key = AvoidUsingUnsafeDisplayContextCheck.RULE_KEY,
name = AvoidUsingUnsafeDisplayContextCheck.RULE_MESSAGE,
priority = Priority.CRITICAL,
tags = Tags.AEM
)
@AemVersion(
from = "6.0"
)
@Metadata(
technicalDebt = "5min"
)
@Rule(key = AvoidUsingUnsafeDisplayContextCheck.RULE_KEY)
@AemVersion(from = "6.0")
public class AvoidUsingUnsafeDisplayContextCheck extends AbstractHtlCheck {

public static final String RULE_KEY = "HTL-13";
Expand All @@ -57,6 +44,5 @@ public void htlExpression(Expression expression, Node node) {
createViolation(node.getStartLinePosition(), RULE_MESSAGE);
}
}

}
}
18 changes: 2 additions & 16 deletions src/main/java/com/vml/aemrules/htl/checks/CamelCaseCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@
package com.vml.aemrules.htl.checks;


import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.scripting.sightly.compiler.expression.Expression;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.html.node.Attribute;
import org.sonar.plugins.html.node.TagNode;
Expand All @@ -35,18 +31,8 @@
import java.util.List;
import java.util.regex.Pattern;

@Rule(
key = CamelCaseCheck.RULE_KEY,
name = CamelCaseCheck.RULE_MESSAGE,
priority = Priority.MINOR,
tags = Tags.AEM
)
@AemVersion(
from = "6.0"
)
@Metadata(
technicalDebt = "5min"
)
@Rule(key = CamelCaseCheck.RULE_KEY)
@AemVersion(from = "6.0")
public class CamelCaseCheck extends AbstractHtlCheck {

public static final String RULE_KEY = "HTL-15";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
*/
package com.vml.aemrules.htl.checks;

import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.html.node.TagNode;

Expand All @@ -35,18 +32,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Rule(
key = DefaultDisplayContextCheck.RULE_KEY,
name = DefaultDisplayContextCheck.RULE_MESSAGE,
priority = Priority.MINOR,
tags = Tags.AEM
)
@AemVersion(
from = "6.0"
)
@Metadata(
technicalDebt = "5min"
)
@Rule(key = DefaultDisplayContextCheck.RULE_KEY)
@AemVersion(from = "6.0")
public class DefaultDisplayContextCheck extends AbstractHtlCheck {

public static final String RULE_KEY = "HTL-6";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,16 @@
*/
package com.vml.aemrules.htl.checks;

import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.scripting.sightly.impl.compiler.Syntax;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.html.node.CommentNode;

import java.util.regex.Pattern;

@Rule(
key = DisplayContextInConditionalCommentsCheck.RULE_KEY,
name = DisplayContextInConditionalCommentsCheck.RULE_MESSAGE,
priority = Priority.MINOR,
tags = Tags.AEM
)
@AemVersion(
from = "6.0"
)
@Metadata(
technicalDebt = "5min"
)
@Rule(key = DisplayContextInConditionalCommentsCheck.RULE_KEY)
@AemVersion(from = "6.0")
public class DisplayContextInConditionalCommentsCheck extends AbstractHtlCheck {

public static final String RULE_KEY = "HTL-14";
Expand Down
Loading

0 comments on commit 4b3ea13

Please sign in to comment.