diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs index 986a9ff..2b851d5 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs +++ b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParsingContext.cs @@ -10,6 +10,7 @@ public class EditorConfigDocumentParsingContext private EditorConfigCategoryNode? _currentCategory; private EditorConfigDocumentSectionNode? _currentSection; private List _currentTrivia; + public EditorConfigDocumentParsingContext() { _currentDocument = new EditorConfigDocument(ImmutableList.Empty); diff --git a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs b/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs index 4b31b46..8e5a976 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs +++ b/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs @@ -19,8 +19,8 @@ public EditorConfigFormatter(IDotnetConfigSettingsParser settingsParser) public EditorConfigDocument Format(EditorConfigDocument value) { List nodesForRemoving = new List(); - IReadOnlyCollection styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleType.StyleRule).OrderBy(r => r.Key.Value).ToList(); - IReadOnlyCollection qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleType.QualityRule).OrderBy(r => r.Key.Value).ToList(); + IReadOnlyCollection styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.StyleRule).OrderBy(r => r.Key.Value).ToList(); + IReadOnlyCollection qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.QualityRule).OrderBy(r => r.Key.Value).ToList(); nodesForRemoving.AddRange(styleRuleNodesForMoving); nodesForRemoving.AddRange(qualityRuleNodesForMoving); @@ -112,7 +112,7 @@ private EditorConfigCategoryNode CreateAutoGeneratedCategory(IReadOnlyCollection return autoGeneratedSection; } - private IReadOnlyCollection SelectIdeNodes(EditorConfigDocument document, RoslynRuleType roslynRuleType) + private IReadOnlyCollection SelectIdeNodes(EditorConfigDocument document, string ruleType) { List propertyNodes = document .DescendantNodes() @@ -126,7 +126,7 @@ private IReadOnlyCollection SelectIdeNodes(EditorConfi if (editorConfigSetting is not RoslynSeverityEditorConfigSetting severityConfigSetting) continue; - if (severityConfigSetting.RuleId.Type == roslynRuleType) + if (severityConfigSetting.RuleId.RuleType == ruleType) styleRuleNodes.Add(editorConfigPropertyNode); } diff --git a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleId.cs b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleId.cs index c864b04..edf159d 100644 --- a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleId.cs +++ b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleId.cs @@ -3,47 +3,31 @@ namespace Kysect.Configuin.RoslynModels; -public readonly record struct RoslynRuleId(RoslynRuleType Type, int Id) : IComparable +public readonly record struct RoslynRuleId(string RuleType, int Id) : IComparable { public static RoslynRuleId Parse(string value) { value.ThrowIfNull(); - int ParseInt(string intAsString) - { - if (int.TryParse(intAsString, out int parsedInt)) - return parsedInt; + if (value.Length < 5) + throw new ArgumentException($"Invalid Roslyn rule ID {value}"); + string code = value.Substring(value.Length - 4, 4); + if (!int.TryParse(code, out int parsedCode)) throw new ConfiguinException($"Value {value} is not valid rule identifier."); - } - // CA1234 - string qualityRulePrefix = "CA"; - if (value.StartsWith(qualityRulePrefix, StringComparison.InvariantCultureIgnoreCase)) - { - string id = value.WithoutPrefix(qualityRulePrefix); - return new RoslynRuleId(RoslynRuleType.QualityRule, ParseInt(id)); - } - - // IDE1234 - string styleRulePrefix = "IDE"; - if (value.StartsWith(styleRulePrefix, StringComparison.InvariantCultureIgnoreCase)) - { - string id = value.WithoutPrefix(styleRulePrefix); - return new RoslynRuleId(RoslynRuleType.StyleRule, ParseInt(id)); - } - - throw new ArgumentException($"String {value} is not valid Roslyn rule id"); + string type = value.Substring(0, value.Length - 4).ToUpper(); + return new RoslynRuleId(type, parsedCode); } public override string ToString() { - return $"{Type.ToAlias()}{Id:D4}"; + return $"{RuleType}{Id:D4}"; } public int CompareTo(RoslynRuleId other) { - int typeComparison = Type.CompareTo(other.Type); + int typeComparison = string.Compare(RuleType, other.RuleType, StringComparison.Ordinal); if (typeComparison != 0) return typeComparison; return Id.CompareTo(other.Id); diff --git a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleIdRange.cs b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleIdRange.cs index 2413618..eb8a0df 100644 --- a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleIdRange.cs +++ b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleIdRange.cs @@ -29,6 +29,6 @@ public static RoslynRuleIdRange Parse(string value) public IEnumerable Enumerate() { for (int i = Start.Id; i <= End.Id; i++) - yield return new RoslynRuleId(Start.Type, i); + yield return Start with { Id = i }; } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleType.cs b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleType.cs deleted file mode 100644 index 7753380..0000000 --- a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleType.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Kysect.CommonLib.Exceptions; - -namespace Kysect.Configuin.RoslynModels; - -public enum RoslynRuleType -{ - StyleRule, - QualityRule -} - -public static class RoslynRuleTypeExtensions -{ - public static string ToAlias(this RoslynRuleType ruleType) - { - return ruleType switch - { - RoslynRuleType.StyleRule => "IDE", - RoslynRuleType.QualityRule => "CA", - _ => throw SwitchDefaultExceptions.OnUnexpectedType(ruleType) - }; - } -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.RoslynModels/RoslynRuleTypes.cs b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleTypes.cs new file mode 100644 index 0000000..eb43553 --- /dev/null +++ b/Sources/Kysect.Configuin.RoslynModels/RoslynRuleTypes.cs @@ -0,0 +1,7 @@ +namespace Kysect.Configuin.RoslynModels; + +public static class RoslynRuleTypes +{ + public const string StyleRule = "IDE"; + public const string QualityRule = "CA"; +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Tests/RoslynRuleIdTests.cs b/Sources/Kysect.Configuin.Tests/RoslynRuleIdTests.cs index 3dc472a..0d1938e 100644 --- a/Sources/Kysect.Configuin.Tests/RoslynRuleIdTests.cs +++ b/Sources/Kysect.Configuin.Tests/RoslynRuleIdTests.cs @@ -1,14 +1,16 @@ -using Kysect.Configuin.RoslynModels; +using Kysect.Configuin.Common; +using Kysect.Configuin.RoslynModels; namespace Kysect.Configuin.Tests; public class RoslynRuleIdTests { [Theory] - [InlineData("CA1000", RoslynRuleType.QualityRule, 1000)] - [InlineData("ca1001", RoslynRuleType.QualityRule, 1001)] - [InlineData("IDE1002", RoslynRuleType.StyleRule, 1002)] - public void Parse(string input, RoslynRuleType type, int id) + [InlineData("CA1000", RoslynRuleTypes.QualityRule, 1000)] + [InlineData("ca1001", RoslynRuleTypes.QualityRule, 1001)] + [InlineData("IDE1002", RoslynRuleTypes.StyleRule, 1002)] + [InlineData("CS0219", "CS", 0219)] + public void Parse(string input, string type, int id) { var expected = new RoslynRuleId(type, id); @@ -20,9 +22,9 @@ public void Parse(string input, RoslynRuleType type, int id) [Fact] public void Parse_WithIncorrectPrefix_ThrowException() { - Assert.Throws(() => + Assert.Throws(() => { - var roslynRuleId = RoslynRuleId.Parse("QWE1234"); + var roslynRuleId = RoslynRuleId.Parse("QWE234"); }); } @@ -36,8 +38,8 @@ public void Parse_Range_ReturnAllValueInRange() roslynRuleIds .Should().HaveCount(3) - .And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1865)) - .And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1866)) - .And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1867)); + .And.Contain(new RoslynRuleId("CA", 1865)) + .And.Contain(new RoslynRuleId("CA", 1866)) + .And.Contain(new RoslynRuleId("CA", 1867)); } }