Skip to content

Commit

Permalink
Add support for naming rule
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 17, 2024
1 parent 366a5ea commit 856a008
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public EditorConfigDocument FormatAccordingToRuleDefinitions(EditorConfigDocumen

selectedStyleRuleNodes.Add(editorConfigPropertyNode);
}

if (roslynStyleRuleGroup.Rules.Any(r => r.RuleId.Equals(RoslynNameRuleInfo.RuleId)))
{
foreach (EditorConfigPropertyNode editorConfigPropertyNode in propertyNodes)
{
IEditorConfigSetting editorConfigSetting = _settingsParser.ParseSetting(editorConfigPropertyNode);
if (editorConfigSetting is not CompositeRoslynOptionEditorConfigSetting option)
continue;

if (RoslynNameRuleInfo.IsNameRuleOption(option.ToDisplayString()))
selectedStyleRuleNodes.Add(editorConfigPropertyNode);
}
}
}

List<EditorConfigPropertyNode> selectedQualityRuleNodes = new List<EditorConfigPropertyNode>();
Expand Down
12 changes: 11 additions & 1 deletion Sources/Kysect.Configuin.MsLearn/MsLearnDocumentationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RoslynRules Parse(MsLearnDocumentationRawInfo rawInfo)
var roslynStyleRules = rawInfo.StyleRuleFileContents.Select(ParseStyleRules).ToList();

roslynStyleRules = ParseIde0055FormatOptions(roslynStyleRules, rawInfo);

roslynStyleRules = AddNamingRule(roslynStyleRules);
return new RoslynRules(roslynQualityRules, roslynStyleRules);
}

Expand Down Expand Up @@ -80,6 +80,16 @@ private List<RoslynStyleRuleGroup> ParseIde0055FormatOptions(
return roslynStyleRules;
}

private List<RoslynStyleRuleGroup> AddNamingRule(List<RoslynStyleRuleGroup> roslynStyleRules)
{
var namingRule = new RoslynStyleRule(RoslynNameRuleInfo.RuleId, "Code-style naming rules", "Style");
var roslynStyleRuleGroup = new RoslynStyleRuleGroup(namingRule, string.Empty, null);

roslynStyleRules.Add(roslynStyleRuleGroup);
return roslynStyleRules;
}


public RoslynStyleRuleGroup ParseStyleRules(string info)
{
info.ThrowIfNull();
Expand Down
18 changes: 18 additions & 0 deletions Sources/Kysect.Configuin.RoslynModels/RoslynStyleRuleInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Kysect.CommonLib.BaseTypes.Extensions;

namespace Kysect.Configuin.RoslynModels;

public static class RoslynNameRuleInfo
{
public static RoslynRuleId RuleId { get; } = new RoslynRuleId("IDE", 1006);

public static bool IsNameRuleOption(string optionName)
{
optionName.ThrowIfNull();

return optionName.StartsWith("dotnet_naming_rule")
|| optionName.StartsWith("dotnet_naming_symbols")
|| optionName.StartsWith("dotnet_naming_style");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ public void Format_QualityAndStyleRulesMashed_ReturnOrderedLinesWithHeader()
FormatAndCompare(input, expected);
}

[Fact]
public void Format_NamingRule_ReturnOrderedLinesWithHeader()
{
var input = """
dotnet_naming_style.camel_case_style.capitalization = camel_case
dotnet_diagnostic.IDE0081.severity = none
dotnet_naming_style.disallowed_style.capitalization = pascal_case
dotnet_diagnostic.IDE1006.severity = none
dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style
""";

var expected = """
# Autogenerated values
[*.cs]
### IDE ###
dotnet_diagnostic.IDE0081.severity = none
dotnet_diagnostic.IDE1006.severity = none
dotnet_naming_style.camel_case_style.capitalization = camel_case
dotnet_naming_style.disallowed_style.capitalization = pascal_case
dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style
""";

MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = _repositoryPathReader.Provide(Constants.GetPathToMsDocsRoot());
RoslynRules roslynRules = _msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);

EditorConfigDocument editorConfigDocument = _parser.Parse(input);
EditorConfigDocument formattedDocument = _formatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules);

formattedDocument.ToFullString().Should().Be(expected);
}

[Fact(Skip = "Test is not work during CI. Need to rewrite it without reading and comparing huge .ini files")]
public void FormatAccordingToRuleDefinitions_Sample_ReturnExpectedFormatterDocument()
{
Expand Down

0 comments on commit 856a008

Please sign in to comment.