Skip to content

Commit

Permalink
Fix SonarLint support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-strzelecki-vml committed Oct 25, 2024
1 parent 191d9d0 commit 417fcdf
Show file tree
Hide file tree
Showing 133 changed files with 1,571 additions and 677 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

![Builds Status](https://github.com/wttech/AEM-Rules-for-SonarQube/actions/workflows/build.yml/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/Cognifide/AEM-Rules-for-SonarQube/badge.svg?branch=master)](https://coveralls.io/github/Cognifide/AEM-Rules-for-SonarQube?branch=master)
[<img src="https://rules.sonarsource.com/images/logos/SonarLint-black.svg" height="28" alt="Available in SonarLint">](https://www.sonarlint.org/)
[<img src="https://rules.sonarsource.com/images/logos/SonarCloud-black.svg" height="28" alt="Available in SonarCloud">](https://sonarcloud.io)
[<img src="https://rules.sonarsource.com/images/logos/SonarQube-black.svg" height="28" alt="Available in SonarQube">](https://www.sonarqube.org/)
# About AEM Rules for SonarQube
Expand Down Expand Up @@ -46,8 +47,6 @@ Use of the plugin does not differ much from regular SonarQube analysis. However,

Each rule defines supported AEM version or version range. Most of the rules are universal.

Rules are not available in [<img src="https://rules.sonarsource.com/images/logos/SonarLint-black.svg" height="28" alt="Not available in SonarLint">](https://www.sonarlint.org/)

### Running analysis

When running analysis, pass `sonarRunner.aemVersion` property with your AEM version. The format is as follows:
Expand Down
449 changes: 449 additions & 0 deletions local-docker-image/conf/sonar.properties

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion local-docker-image/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ name: sonar-local

services:
sonar-local:
image: sonarqube:10.7.0-community
image: sonarqube:9.9-community
build:
context: .
ports:
- "9000:9000"
- "5005:5005"
restart: unless-stopped
volumes:
- ./extensions/plugins:/opt/sonarqube/extensions/plugins:ro
- ./conf:/opt/sonarqube/conf:ro
36 changes: 25 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@

<properties>
<htl.version>1.1.2-1.4.0</htl.version>
<sonar.html.version>3.3.0.2534</sonar.html.version>
<sonar.html.version>3.15.0.5107</sonar.html.version>
<sonar.java.plugin>7.30.1.34514</sonar.java.plugin>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.apiVersion>10.11.0.2468</sonar.apiVersion>
<sonar.apiImplVersion>10.3.0.82913</sonar.apiImplVersion>
<sonar.pluginApiVersion>10.11.0.2468</sonar.pluginApiVersion>
<sonar.pluginApiImplVersion>10.3.0.82913</sonar.pluginApiImplVersion>
<sonar.testingHarnessVersion>10.7.0.96327</sonar.testingHarnessVersion>
<sonar.javaCheksTestkitVersion>7.30.1.34514</sonar.javaCheksTestkitVersion>
<jdk.min.version>11</jdk.min.version>
Expand All @@ -136,7 +136,7 @@
<artifactId>sonar-plugin-api</artifactId>
<groupId>org.sonarsource.api.plugin</groupId>
<scope>provided</scope>
<version>${sonar.apiVersion}</version>
<version>${sonar.pluginApiVersion}</version>
</dependency>
<dependency>
<artifactId>sonar-html-plugin</artifactId>
Expand Down Expand Up @@ -257,7 +257,7 @@
<artifactId>sonar-plugin-api-impl</artifactId>
<groupId>org.sonarsource.sonarqube</groupId>
<scope>test</scope>
<version>${sonar.apiImplVersion}</version>
<version>${sonar.pluginApiImplVersion}</version>
</dependency>
<dependency>
<artifactId>java-checks-testkit</artifactId>
Expand Down Expand Up @@ -365,9 +365,28 @@
<pluginSourcesUrl>https://github.com/wttech/AEM-Rules-for-SonarQube/tree/${tagName}
</pluginSourcesUrl>
<pluginUrl>https://github.com/wttech/AEM-Rules-for-SonarQube</pluginUrl>
<sonarLintSupported>false</sonarLintSupported>
<sonarLintSupported>true</sonarLintSupported>
<skipDependenciesPackaging>true</skipDependenciesPackaging>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<!-- <shadedArtifactAttached>false</shadedArtifactAttached>-->
<!-- <minimizeJar>true</minimizeJar>-->
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -414,11 +433,6 @@
<exclude>**.xml</exclude>
<exclude>assets/**</exclude>
<exclude>src/test/resources/htl/HtlCommentsCheck.html</exclude>
<exclude>src/main/js/**</exclude>
<exclude>conf/**</exclude>
<exclude>scripts/**</exclude>
<exclude>.cirrus.yml</exclude>
<exclude>**.yml</exclude>
</excludes>
</configuration>
<executions>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/vml/aemrules/AemRulesSonarPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.vml.aemrules.htl.Htl;
import com.vml.aemrules.htl.HtlProfile;
import com.vml.aemrules.htl.HtlSensor;
import com.vml.aemrules.java.rules.JavaCheckClasses;
import com.vml.aemrules.rules.AemRulesRulesDefinition;
import com.vml.aemrules.htl.rules.HtlRulesDefinition;
import com.vml.aemrules.java.rules.JavaCheckRegistrar;
import com.vml.aemrules.java.rules.JavaRulesDefinition;
import org.sonar.api.Plugin;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
Expand Down Expand Up @@ -57,12 +58,17 @@ private static List<PropertyDefinition> pluginProperties() {

@Override
public void define(Context context) {
context.addExtensions(
JavaRulesDefinition.class,
JavaCheckRegistrar.class
);

context.addExtensions(
Htl.class,
AemRulesRulesDefinition.class,
JavaCheckClasses.class,
HtlRulesDefinition.class,
HtlProfile.class,
HtlSensor.class);

context.addExtensions(pluginProperties());
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/vml/aemrules/htl/HtlFilesAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package com.vml.aemrules.htl;

import com.vml.aemrules.htl.api.ParsingErrorRule;
import com.vml.aemrules.htl.rules.HtlCheckClasses;
import com.vml.aemrules.htl.rules.HtlRulesList;
import com.vml.aemrules.utils.Throwables;
import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
import org.sonar.api.batch.fs.InputFile;
Expand All @@ -46,7 +46,7 @@ public abstract class HtlFilesAnalyzer {

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

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/vml/aemrules/htl/HtlProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package com.vml.aemrules.htl;

import com.vml.aemrules.htl.rules.HtlCheckClasses;
import com.vml.aemrules.htl.rules.HtlRulesList;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.check.Rule;

Expand All @@ -33,11 +33,11 @@ public class HtlProfile implements BuiltInQualityProfilesDefinition {
public void define(Context context) {
NewBuiltInQualityProfile htl = context
.createBuiltInQualityProfile(QUALITY_PROFILE_NAME, Htl.KEY);
HtlCheckClasses.getCheckClasses().stream()
.map(HtlCheckClasses::getRule)
HtlRulesList.getCheckClasses().stream()
.map(HtlRulesList::getRule)
.map(Rule::key)
.filter(Objects::nonNull)
.forEach(ruleKey -> htl.activateRule(HtlCheckClasses.REPOSITORY_KEY, ruleKey));
.forEach(ruleKey -> htl.activateRule(HtlRulesList.REPOSITORY_KEY, ruleKey));
htl.done();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.apache.commons.lang.StringUtils;

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.vml.aemrules.metadata.Metadata;
import com.vml.aemrules.tag.Tags;
import com.vml.aemrules.version.AemVersion;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.scripting.sightly.compiler.expression.Expression;
import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
import org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier;
Expand Down
36 changes: 25 additions & 11 deletions src/main/java/com/vml/aemrules/htl/lex/AbstractTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
*/
package com.vml.aemrules.htl.lex;

import org.sonar.channel.Channel;
import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;
import org.sonar.plugins.html.node.Node;
import org.sonar.sslr.channel.Channel;
import org.sonar.sslr.channel.CodeReader;
import org.sonar.sslr.channel.EndMatcher;

import java.util.Arrays;
import java.util.List;

abstract class AbstractTokenizer<T extends List<Node>> extends Channel<T> {

private final char[] endChars;

private final char[] startChars;

public AbstractTokenizer(String startChars, String endChars) {
Expand All @@ -50,6 +52,16 @@ protected static boolean equalsIgnoreCase(char[] a, char[] b) {
return true;
}

protected static String popTo(CodeReader codeReader, EndMatcher endMatcher, StringBuilder stringBuilder) {
boolean shouldContinue = codeReader.peek() != -1;
while (shouldContinue) {
stringBuilder.append((char) codeReader.pop());
shouldContinue = !endMatcher.match(codeReader.peek()) && codeReader.peek() != -1;
}

return stringBuilder.toString();
}

protected void addNode(List<Node> nodeList, Node node) {
nodeList.add(node);
}
Expand All @@ -61,7 +73,7 @@ public boolean consume(CodeReader codeReader, T nodeList) {
setStartPosition(codeReader, node);

StringBuilder stringBuilder = new StringBuilder();
codeReader.popTo(getEndMatcher(codeReader), stringBuilder);
popTo(codeReader, getEndMatcher(codeReader), stringBuilder);
for (int i = 0; i < endChars.length; i++) {
codeReader.pop(stringBuilder);
}
Expand Down Expand Up @@ -104,20 +116,22 @@ private EndTokenMatcher(CodeReader codeReader) {

@Override
public boolean match(int endFlag) {
boolean result = false;
if (endFlag == '"') {
quoting = !quoting;
}
if (!quoting) {
if (equalsIgnoreCase(codeReader.peek(startChars.length), startChars)) {
boolean started = equalsIgnoreCase(codeReader.peek(startChars.length), startChars);
if (started) {
nesting++;
} else if (equalsIgnoreCase(codeReader.peek(endChars.length), endChars)) {
nesting--;
result = nesting < 0;
} else {
boolean ended = Arrays.equals(codeReader.peek(endChars.length), endChars);
if (ended) {
nesting--;
return nesting < 0;
}
}
}

return result;
return false;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/vml/aemrules/htl/lex/CommentTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
*/
package com.vml.aemrules.htl.lex;

import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;

import org.sonar.plugins.html.node.CommentNode;
import org.sonar.plugins.html.node.Node;
import org.sonar.sslr.channel.CodeReader;
import org.sonar.sslr.channel.EndMatcher;

import java.util.List;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/vml/aemrules/htl/lex/ElementTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
package com.vml.aemrules.htl.lex;

import org.apache.commons.lang3.StringUtils;
import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;
import org.sonar.plugins.html.node.Attribute;
import org.sonar.plugins.html.node.Node;
import org.sonar.plugins.html.node.TagNode;
import org.sonar.sslr.channel.CodeReader;
import org.sonar.sslr.channel.EndMatcher;

import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -77,13 +77,13 @@ private static void handleBeforeAttributeValue(CodeReader codeReader, TagNode el
if (isQuote((char) ch)) {
codeReader.pop();
if (codeReader.peek() != ch) {
codeReader.popTo(new QuoteMatcher((char) ch), sbValue);
popTo(codeReader, new QuoteMatcher((char) ch), sbValue);
attribute.setValue(unescapeQuotes(sbValue.toString(), (char) ch));
}
codeReader.pop();
attribute.setQuoteChar((char) ch);
} else {
codeReader.popTo(END_UNQUOTED_ATTRIBUTE_MATCHER, sbValue);
popTo(codeReader, END_UNQUOTED_ATTRIBUTE_MATCHER, sbValue);
attribute.setValue(sbValue.toString().trim());
}
}
Expand All @@ -92,15 +92,15 @@ private static void handleBeforeAttributeValue(CodeReader codeReader, TagNode el
private static void handleBeforeAttributeName(CodeReader codeReader, TagNode element) {
Attribute attribute;
StringBuilder sbQName = new StringBuilder();
codeReader.popTo(END_Q_NAME_MATCHER, sbQName);
popTo(codeReader, END_Q_NAME_MATCHER, sbQName);
attribute = new Attribute(sbQName.toString().trim());
attribute.setLine(codeReader.getLinePosition() + element.getStartLinePosition() - 1);
element.getAttributes().add(attribute);
}

private static void handleBeforeNodeName(CodeReader codeReader, TagNode element) {
StringBuilder sbNodeName = new StringBuilder();
codeReader.popTo(END_TOKEN_MATCHER, sbNodeName);
popTo(codeReader, END_TOKEN_MATCHER, sbNodeName);
element.setNodeName(sbNodeName.toString());
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/vml/aemrules/htl/lex/HtlLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
package com.vml.aemrules.htl.lex;

import org.apache.sling.scripting.sightly.impl.compiler.Syntax;
import org.sonar.channel.Channel;
import org.sonar.channel.ChannelDispatcher;
import org.sonar.channel.CodeReader;

import org.sonar.plugins.html.node.Node;
import org.sonar.plugins.html.node.NodeType;
import org.sonar.plugins.html.node.TagNode;
import org.sonar.sslr.channel.Channel;
import org.sonar.sslr.channel.ChannelDispatcher;
import org.sonar.sslr.channel.CodeReader;

import java.io.Reader;
import java.util.ArrayList;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/vml/aemrules/htl/lex/TextTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
*/
package com.vml.aemrules.htl.lex;

import org.sonar.channel.CodeReader;
import org.sonar.channel.EndMatcher;

import org.sonar.plugins.html.node.Node;
import org.sonar.plugins.html.node.NodeType;
import org.sonar.plugins.html.node.TagNode;
import org.sonar.plugins.html.node.TextNode;
import org.sonar.sslr.channel.CodeReader;
import org.sonar.sslr.channel.EndMatcher;

import java.util.List;

Expand Down Expand Up @@ -53,9 +54,9 @@ public boolean consume(CodeReader codeReader, List<Node> nodeList) {

StringBuilder stringBuilder = new StringBuilder();
if (inScript(nodeList)) {
codeReader.popTo(new EndScriptMatcher(codeReader), stringBuilder);
popTo(codeReader, new EndScriptMatcher(codeReader), stringBuilder);
} else {
codeReader.popTo(new EndTokenMatcher(codeReader), stringBuilder);
popTo(codeReader, new EndTokenMatcher(codeReader), stringBuilder);
}
node.setCode(stringBuilder.toString());
setEndPosition(codeReader, node);
Expand Down Expand Up @@ -111,4 +112,4 @@ public boolean match(int endFlag) {
}
}

}
}
Loading

0 comments on commit 417fcdf

Please sign in to comment.