Skip to content

Commit

Permalink
Merge pull request #22 from green-code-initiative/feature/ecocode-rul…
Browse files Browse the repository at this point in the history
…es-specifications-migration

Migration of rules metadata to common rules specification
  • Loading branch information
zippy1978 authored May 28, 2024
2 parents 6322d83 + e5392e1 commit 4e19075
Show file tree
Hide file tree
Showing 50 changed files with 56 additions and 395 deletions.
11 changes: 4 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ When adding a new rule, the following steps are required:

- Declare the rule
- Implement a check
- Add the @RegisterRule to the class
- Add the @Rule to the class

### Declaring the rule

The new rule must be declared into 2 files :
The new rule must be declared in `swift-lang/src/resources/ecocode_swift_profile.json`

- `swift-lang/src/resources/ecocode-swift_profile.json`
- `swift-lang/src/resources/ecocode-swift-rules.json`

> Note: if a rich HTML description of the rule is required. Use a separated .html file put aside `swift-lang/src/resources/ecocode-swift-rules.json` and name it after the rule key. For example: `RULE_KEY.html`. When an specific HTML description file is provided for a rule, it overrides the description provided into the `ecocode-swift-rules.json`.
> Note: in case the new rule is not available in https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications yet create a matching <RULE_ID>.json ans <RULE_ID>.html in `swift-lang/src/resources/io/ecocode/rules/swift/` to provide rule metadata.
### Implementing a check

In order to implement a check for the rule, create a Check class inherited from `RuleCheck` in `src/main/java/io/ecocode/ios/swift/checks`.
In order to implement a check for the rule, create a Check class inherited from `SwiftRuleCheck` in `src/main/java/io/ecocode/ios/swift/checks`.

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

Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@
<assertJ.version>3.23.1</assertJ.version>
<jacoco.version>0.8.11</jacoco.version>

<!-- Temporary version waiting for real automatic release in ecocode repository -->
<ecocode-rules-specifications.version>1.5.4</ecocode-rules-specifications.version>

</properties>

<dependencies>

<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sonar-ios-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<dependencies>
<dependency>
<groupId>io.ecocode</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>ecocode-swift-lang</artifactId>
<version>${project.version}</version>
</dependency>
Expand Down
9 changes: 8 additions & 1 deletion swift-lang/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
<dependencies>

<dependency>
<groupId>io.ecocode</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>ecocode-rules-specifications</artifactId>
<version>${ecocode-rules-specifications.version}</version>
<classifier>swift</classifier>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ecocode-commons-ios</artifactId>
<version>${project.version}</version>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@

import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;


/**
* Loads local rules definition.
* This should be removed once all rules are defined in the common rules repository (<a href="https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications">...</a>)
*/
public class EcoCodeSwiftRulesDefinition implements RulesDefinition {
private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/swift";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package io.ecocode.ios.swift.antlr.generated;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package io.ecocode.ios.swift.checks.geolocalisation;

import io.ecocode.ios.checks.RuleCheck;
import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import org.antlr.v4.runtime.tree.ParseTree;
Expand All @@ -26,7 +25,7 @@

import static io.ecocode.ios.swift.checks.CheckHelper.isImportExisting;

@Rule(key = "ESOB002")
@Rule(key = "EC524")
public class ThriftyGeolocation extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Adapt location accuracy and type to applications needs.";
private Swift5Parser.Import_declarationContext importTree = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import io.ecocode.ios.checks.RuleCheck;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

/**
* Check the use of "UIApplication.shared.isIdleTimerDisabled" and triggers when set to true.
*/
@Rule(key = "EIDL001")
@Rule(key = "EC505")
public class IdleTimerDisabledCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Do not disable idle timer, unless absolutely necessary.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import io.ecocode.ios.checks.RuleCheck;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

/**
* Check the presence of the class "Timer".
*/
@Rule(key = "EIDL002")
@Rule(key = "EC509")
public class RigidAlarmCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Setting a tolerance for timers will allow them to fire later than the scheduled fire date.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package io.ecocode.ios.swift.checks.motionsensor;

import io.ecocode.ios.checks.RuleCheck;
import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import org.antlr.v4.runtime.tree.ParseTree;
Expand All @@ -29,7 +28,7 @@

import static io.ecocode.ios.swift.checks.CheckHelper.isImportExisting;

@Rule(key="ESOB003")
@Rule(key="EC534")
public class MotionSensorUpdateRateCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Set appropriate motion sensor update rates for the application's needs";
private Swift5Parser.Import_declarationContext importTree = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import io.ecocode.ios.checks.RuleCheck;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

Expand All @@ -30,7 +29,7 @@
* or `UIDevice.batteryLevelDidChangeNotification` or `UIDevice.batteryStateDidChangeNotification`.
* If found, reports a (positive) issue.
*/
@Rule(key = "EPOW001")
@Rule(key = "EC519")
public class ChargeAwarenessCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Monitoring power changes and customizing behavior depending on battery level is a good practice";
private static final String PROPERTY_BATTERY_LEVEL = "UIDevice.current.batteryLevel";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
*/
package io.ecocode.ios.swift.checks.power;

import io.ecocode.ios.checks.RuleCheck;
import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

import java.util.List;

@Rule(key = "EPOW002")
@Rule(key = "EC520")
public class SaveModeAwarenessCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Taking into account when the device is entering or exiting the power save mode is a good practice";
private static final String PROCESS_INFO = "ProcessInfo.processInfo.isLowPowerModeEnabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* Check the use of "UIScreen.main.brightness" and triggers when set.
*/
@Rule(key = "ESOB007")
@Rule(key = "EC603")
public class AnimationFreeCheck extends SwiftRuleCheck {

private static final String DEFAULT_ISSUE_MESSAGE = "Usage of Animations must absolutely be avoided";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import io.ecocode.ios.checks.RuleCheck;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

/**
* Check the use of "UIScreen.main.brightness" and triggers when set.
*/
@Rule(key = "ESOB005")
@Rule(key = "EC522")
public class BrightnessOverrideCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Do not force Brightness in your code, unless absolutely necessary";
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package io.ecocode.ios.swift.checks.sobriety;

import io.ecocode.ios.checks.RuleCheck;
import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import org.antlr.v4.runtime.tree.ParseTree;
Expand All @@ -27,7 +26,7 @@
/**
* Check the use of "CLLocationManager#pausesLocationUpdatesAutomatically" and triggers when set to false.
*/
@Rule(key = "ESOB001")
@Rule(key = "EC533")
public class LocationUpdatesDisabledCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Do not disable location updates pause, unless absolutely necessary";
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import io.ecocode.ios.checks.RuleCheck;
import org.antlr.v4.runtime.tree.ParseTree;
import org.sonar.check.Rule;

/**
* Check the use of "AVCaptureTorchMode.on", "setTorchModeOn(level: Float)", or "torchMode = .on" and triggers when set to true.
*/
@Rule(key = "ESOB006")
@Rule(key = "EC530")
public class TorchFreeCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Usage of `AVCaptureDevice#torchMode` or `AVCaptureDevice#setTorchModeOn(level:)` must absolutely be avoided";
@Override
Expand Down
20 changes: 10 additions & 10 deletions swift-lang/src/main/resources/ecocode_swift_profile.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "ecoCode",
"ruleKeys": [
"EIDL001",
"EIDL002",
"EPOW001",
"EPOW002",
"ESOB001",
"ESOB002",
"ESOB003",
"ESOB005",
"ESOB006",
"ESOB007"
"EC505",
"EC506",
"EC602",
"EC543",
"EC521",
"EC522",
"EC547",
"EC523",
"EC503",
"EC603"
]
}

This file was deleted.

18 changes: 0 additions & 18 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EIDL001.json

This file was deleted.

17 changes: 0 additions & 17 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EIDL002.html

This file was deleted.

18 changes: 0 additions & 18 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EIDL002.json

This file was deleted.

24 changes: 0 additions & 24 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EPOW001.html

This file was deleted.

18 changes: 0 additions & 18 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EPOW001.json

This file was deleted.

Loading

0 comments on commit 4e19075

Please sign in to comment.