Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client/source/DelphiLint.Data.pas
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ TRule = class(TObject)
FSeverity: TRuleSeverity;
FType: TRuleType;
FCleanCode: TRuleCleanCode;
FDefaultEnabled: Boolean;

public
constructor Create(
Expand All @@ -272,7 +273,8 @@ TRule = class(TObject)
Description: TRuleDescription;
Severity: TRuleSeverity;
RuleType: TRuleType;
CleanCode: TRuleCleanCode = nil
CleanCode: TRuleCleanCode = nil;
DefaultEnabled: Boolean = False
);
constructor CreateFromJson(Json: TJSONObject);
destructor Destroy; override;
Expand All @@ -283,6 +285,7 @@ TRule = class(TObject)
property Severity: TRuleSeverity read FSeverity;
property RuleType: TRuleType read FType;
property CleanCode: TRuleCleanCode read FCleanCode;
property DefaultEnabled: Boolean read FDefaultEnabled;
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -472,7 +475,8 @@ constructor TRule.Create(
Description: TRuleDescription;
Severity: TRuleSeverity;
RuleType: TRuleType;
CleanCode: TRuleCleanCode
CleanCode: TRuleCleanCode;
DefaultEnabled: Boolean
);
begin
inherited Create;
Expand All @@ -483,6 +487,7 @@ constructor TRule.Create(
FSeverity := Severity;
FType := RuleType;
FCleanCode := CleanCode;
FDefaultEnabled := DefaultEnabled;
end;

//______________________________________________________________________________________________________________________
Expand All @@ -499,6 +504,7 @@ constructor TRule.CreateFromJson(Json: TJSONObject);
FDescription := TRuleDescription.CreateFromJson(Json.GetValue<TJSONObject>('description'));
FSeverity := TRuleSeverity(IndexStr(Json.GetValue<string>('severity'), CSeverities));
FType := TRuleType(IndexStr(Json.GetValue<string>('type'), CRuleTypes));
FDefaultEnabled := Json.GetValue<Boolean>('defaultEnabled', False);
FCleanCode := nil;

if Json.TryGetValue<TJSONObject>('cleanCode', CleanCodeJson) and Assigned(CleanCodeJson) then begin
Expand Down
15 changes: 13 additions & 2 deletions client/source/DelphiLint.SettingsFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ procedure TLintSettingsFrame.PopulateStandaloneRulesBox;
SortedRules: TArray<TRule>;
Rule: TRule;
DisabledRules: TArray<string>;
UseDefaultSelection: Boolean;
begin
if not Assigned(FStandaloneRules) then begin
FStandaloneRules := LintContext.Analyzer.GetStandaloneRules;
Expand All @@ -337,6 +338,11 @@ procedure TLintSettingsFrame.PopulateStandaloneRulesBox;

DisabledRules := SplitString(LintContext.Settings.StandaloneDisabledRules, ',');

// When no custom ruleset has been configured before, the default ruleset is a better starting point
// for fine-tuning than having every rule selected
UseDefaultSelection := LintContext.Settings.StandaloneUseDefaultRules
and (LintContext.Settings.StandaloneDisabledRules = '');

SortedRules := FStandaloneRules.Values.ToArray;
TArray.Sort<TRule>(SortedRules, TComparer<TRule>.Construct(
function(const Left: TRule; const Right: TRule): Integer
Expand All @@ -347,8 +353,13 @@ procedure TLintSettingsFrame.PopulateStandaloneRulesBox;
StandaloneRulesListBox.Items.Clear;
for Rule in SortedRules do begin
StandaloneRulesListBox.Items.AddObject(Format('%s (%s)', [Rule.Name, StripRepository(Rule.RuleKey)]), Rule);
StandaloneRulesListBox.Checked[StandaloneRulesListBox.Items.Count - 1] :=
(IndexStr(Rule.RuleKey, DisabledRules) = -1);
if UseDefaultSelection then begin
StandaloneRulesListBox.Checked[StandaloneRulesListBox.Items.Count - 1] := Rule.DefaultEnabled;
end
else begin
StandaloneRulesListBox.Checked[StandaloneRulesListBox.Items.Count - 1] :=
(IndexStr(Rule.RuleKey, DisabledRules) = -1);
end;
end;
UpdateStandaloneRulesEnabled;
end;
Expand Down
25 changes: 25 additions & 0 deletions client/test/DelphiLintTest.Data.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ TDataJsonParseTest = class(TObject)
[Test]
procedure TestCreateRuleCleanCode;
[Test]
procedure TestCreateRuleDefaultEnabled;
[Test]
procedure TestParseRuleTypes;
[Test]
procedure TestParseRuleSeverities;
Expand Down Expand Up @@ -84,6 +86,29 @@ procedure TDataJsonParseTest.TestCreateRuleNoCleanCode;
Assert.AreEqual(rsMajor, Rule.Severity);
Assert.AreEqual(rtCodeSmell, Rule.RuleType);
Assert.IsNull(Rule.CleanCode);
Assert.IsFalse(Rule.DefaultEnabled);
finally
FreeAndNil(Rule);
FreeAndNil(JsonObject);
end;
end;

//______________________________________________________________________________________________________________________

procedure TDataJsonParseTest.TestCreateRuleDefaultEnabled;
const
CRuleJsonStr: string = '{"key":"myrulekey","name":"My Rule",'
+ '"description":{"introduction":"foo intro","rootCause":"","howToFix":"bar howToFix","resources":"flarp res"},'
+ '"severity":"MAJOR","type":"CODE_SMELL","defaultEnabled":true}';
var
JsonObject: TJSONObject;
Rule: TRule;
begin
JsonObject := Parse<TJSONObject>(CRuleJsonStr);
try
Rule := TRule.CreateFromJson(JsonObject);
Assert.AreEqual('myrulekey', Rule.RuleKey);
Assert.IsTrue(Rule.DefaultEnabled);
finally
FreeAndNil(Rule);
FreeAndNil(JsonObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@ public class RemoteRule {
private final RuleType type;
private final RemoteCleanCode defaultCleanCode;
private final RemoteRuleDescription ruleDescription;
private final boolean defaultEnabled;

public RemoteRule(
String key,
String name,
RemoteRuleDescription ruleDescription,
RuleSeverity severity,
RuleType type,
RemoteCleanCode cleanCode) {
RemoteCleanCode cleanCode,
boolean defaultEnabled) {
this.key = key;
this.name = name;
this.ruleDescription = ruleDescription;
this.severity = severity;
this.type = type;
this.defaultCleanCode = cleanCode;
this.defaultEnabled = defaultEnabled;
}

public String getKey() {
Expand All @@ -63,4 +66,8 @@ public RuleType getType() {
public RemoteCleanCode getCleanCode() {
return defaultCleanCode;
}

public boolean isDefaultEnabled() {
return defaultEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ private static RemoteRule convertSonarQubeRuleToRemoteRule(
SonarQubeDescriptionSection::getContent),
RuleSeverity.fromSonarLintIssueSeverity(rule.getSeverity()),
RuleType.fromSonarLintRuleType(rule.getType()),
cleanCode);
cleanCode,
// Rules retrieved from a SonarQube server all belong to its active quality profile
true);
}

private static List<String> joinStringsWithLimit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ protected AbstractStandaloneSonarHost(LoadedPlugins loadedPlugins) {
e ->
ImpactSeverity.fromSonarLintImpactSeverity(
e.getValue()))))
: null))
: null,
ruleDef.isActiveByDefault()))
.collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public class RuleData {
@JsonProperty private RuleSeverity severity;
@JsonProperty private RuleType type;
@JsonProperty private CleanCodeData cleanCode;
@JsonProperty private boolean defaultEnabled;

public RuleData(RemoteRule rule) {
key = rule.getKey();
name = rule.getName();
description = new RuleDescriptionData(rule.getRuleDescription());
severity = rule.getSeverity();
type = rule.getType();
defaultEnabled = rule.isDefaultEnabled();

if (rule.getCleanCode() != null) {
cleanCode = new CleanCodeData(rule.getCleanCode());
Expand All @@ -61,4 +63,8 @@ public RuleSeverity getSeverity() {
public RuleType getType() {
return type;
}

public boolean isDefaultEnabled() {
return defaultEnabled;
}
}