Skip to content

Commit

Permalink
Merge pull request #20 from green-code-initiative/feature/rule-annota…
Browse files Browse the repository at this point in the history
…tion

Use same mechanism to manage/load check rules
  • Loading branch information
zippy1978 authored Dec 19, 2023
2 parents 81b8f52 + 18da3c6 commit 1a7d7da
Show file tree
Hide file tree
Showing 53 changed files with 526 additions and 691 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ In order to implement a check for the rule, create a Check class inherited from

Have a look at `swift-lang/src/main/java/io/ecocode/ios/swift/checks/idleness/IdleTimerDisabledCheck` to learn more about the implementation.

Don't forget to add the `@RegisterRule` annotation to the check in order to register it to the AST visitor.
Don't forget to add the `@org.sonar.check.Rule` annotation to the check in order to register it to the AST visitor.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ ecoCode iOS SonarQube plugin is an "eco-responsibility" static code analyzer for

Ready to use binaries are available [from GitHub](https://github.com/green-code-initiative/ecoCode-ios/releases).

🚀 Quickstart
NB: To work, `ecocode-ios` needs `Swift` language support in SonarQube. For *SonarQube Community Edition* (which does not support Swift language), you need to install an additional plugin like [sonar-apple](https://github.com/insideapp-oss/sonar-apple).

🚀 Development Quickstart
-------------

### Requirements
Expand Down
2 changes: 1 addition & 1 deletion commons-ios/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@

</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.rules;
package io.ecocode.ios;

public final class RepositoryRuleDebt {
public final class Const {
public static final String SWIFT_REPOSITORY_KEY = "ecoCode-swift";

private final String function;
private final String offset;
private Const() {

public RepositoryRuleDebt(final String function, final String offset) {
this.function = function;
this.offset = offset;
}

public String getFunction() {
return function;
}

public String getOffset() {
return offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ public class ReportIssue {

public ReportIssue(String ruleId, String message, @Nullable String filePath, @Nullable Integer lineNumber) {
this.ruleId = ruleId;
this.message = message;
this.message = Objects.requireNonNull(message);
this.filePath = filePath;
this.lineNumber = lineNumber;
}

public ReportIssue(String ruleId, String message) {
this.ruleId = ruleId;
this.message = message;
this.filePath = null;
this.lineNumber = null;
}

public String getRuleId() {
return ruleId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public void recordIssues(List<ReportIssue> issues, String repository) {
// Associating the location to the issue and saving it.
sonarIssue.at(sonarIssueLocation).save();
}

}
}
}
69 changes: 20 additions & 49 deletions commons-ios/src/main/java/io/ecocode/ios/checks/RuleCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,49 @@
*/
package io.ecocode.ios.checks;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.ecocode.ios.antlr.AntlrContext;
import io.ecocode.ios.antlr.ParseTreeItemVisitor;
import io.ecocode.ios.rules.RepositoryRule;
import io.ecocode.ios.rules.RepositoryRuleParser;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Rule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Optional.ofNullable;

public abstract class RuleCheck implements ParseTreeItemVisitor {

private static final Logger LOGGER = Loggers.get(RuleCheck.class);

protected String ruleId;
protected String rulesPath;
protected String repositoryKey;

private class Issue {
protected Issue(String ruleId, int startIndex) {

protected Issue(String ruleId, int startIndex, String message) {
this.ruleId = ruleId;
this.startIndex = startIndex;
this.message = Objects.requireNonNull(message);
}
protected String ruleId;
protected int startIndex;
final String ruleId;
final int startIndex;
final String message;
}

private List<Issue> issues = new ArrayList<>();
private final List<Issue> issues = new ArrayList<>();

private static List<RepositoryRule> rules = new ArrayList<>();
public static RepositoryRule getRepositoryRule(String ruleId, String rulesPath) throws IOException {
if (rules.isEmpty()) {
RepositoryRuleParser repositoryRuleParser = new RepositoryRuleParser();
rules = repositoryRuleParser.parse(rulesPath);
}
Optional<RepositoryRule> rule = rules.stream().filter(r -> r.getKey().equals(ruleId)).findFirst();
if (rule.isPresent()) {
return rule.get();
} else {
return null;
}
protected RuleCheck() {
this.ruleId = ofNullable(this.getClass().getAnnotation(Rule.class))
.orElseThrow(() -> new IllegalArgumentException("Please add @org.sonar.check.Rule to: " + this.getClass()))
.key();
}

protected RuleCheck(String ruleId, String rulesPath, String repositoryKey) {
this.ruleId = ruleId;
this.rulesPath = rulesPath;
this.repositoryKey = repositoryKey;
}

protected void recordIssue(String ruleId, int startIndex) {
issues.add(new Issue(ruleId, startIndex));
protected void recordIssue(int startIndex, String message) {
issues.add(new Issue(this.ruleId, startIndex, message));
}

@Override
public void fillContext(SensorContext context, AntlrContext antlrContext) {

final InputFile file = antlrContext.getFile();
if (file == null) {
return;
Expand All @@ -90,17 +71,7 @@ public void fillContext(SensorContext context, AntlrContext antlrContext) {
// Compute location
int[] loc = antlrContext.getLineAndColumn(i.startIndex);
// Retrieve rule data
try {
RepositoryRule rule = RuleCheck.getRepositoryRule(i.ruleId, rulesPath);
if (rule != null) {
reportIssues.add(new ReportIssue(i.ruleId, rule.getDescription(), file.toString(), loc[0]));
} else {
LOGGER.warn(format("Failed to identify rule %s", i.ruleId));
}

} catch (IOException e) {
LOGGER.warn(format("Failed to identify rule %s", i.ruleId),e);
}
reportIssues.add(new ReportIssue(i.ruleId, i.message, file.toString(), loc[0]));
}

// Record
Expand Down

This file was deleted.

68 changes: 0 additions & 68 deletions commons-ios/src/main/java/io/ecocode/ios/rules/RepositoryRule.java

This file was deleted.

Loading

0 comments on commit 1a7d7da

Please sign in to comment.