• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kysect / Configuin / 9090953468

15 May 2024 06:20AM UTC coverage: 80.585% (+1.7%) from 78.934%
9090953468

push

github

FrediKats
Implement .editorconfig model serialization

196 of 298 branches covered (65.77%)

Branch coverage included in aggregate %.

74 of 82 new or added lines in 11 files covered. (90.24%)

1 existing line in 1 file now uncovered.

1568 of 1891 relevant lines covered (82.92%)

591.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/Sources/Kysect.Configuin.EditorConfig/EditorConfigSettingsParser.cs
1
using Kysect.Configuin.EditorConfig.DocumentModel;
2
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes;
3
using Kysect.Configuin.EditorConfig.Settings;
4
using Kysect.Configuin.RoslynModels;
5
using Microsoft.Extensions.Logging;
6

7
namespace Kysect.Configuin.EditorConfig;
8

9
public class EditorConfigSettingsParser : IEditorConfigSettingsParser
10
{
11
    private readonly ILogger _logger;
12

13
    private readonly HashSet<string> _generalRuleKeys;
14
    private readonly EditorConfigDocumentParser _editorConfigDocumentParser;
15

16
    public EditorConfigSettingsParser(ILogger logger)
17
    {
18
        _logger = logger;
7✔
19

20
        // TODO: Investigate other rules
21
        _generalRuleKeys = new HashSet<string>
7✔
22
        {
7✔
23
            "tab_width",
7✔
24
            "indent_size",
7✔
25
            "end_of_line"
7✔
26
        };
7✔
27
        _editorConfigDocumentParser = new EditorConfigDocumentParser();
7✔
28
    }
7✔
29

30
    public EditorConfigSettings Parse(string content)
31
    {
32
        _logger.LogInformation("Parse .editorconfig file");
7✔
33

34
        ArgumentNullException.ThrowIfNull(content);
7✔
35

36
        EditorConfigDocument editorConfigDocument = _editorConfigDocumentParser.Parse(content);
7✔
37
        List<IEditorConfigSetting> settings = editorConfigDocument
7✔
38
            .DescendantNodes()
7✔
39
            .OfType<EditorConfigPropertyNode>()
7✔
40
            .Select(ParseSetting)
7✔
41
            .ToList();
7✔
42

43
        return new EditorConfigSettings(settings);
7✔
44
    }
45

46
    private IEditorConfigSetting ParseSetting(EditorConfigPropertyNode line)
47
    {
48
        if (_generalRuleKeys.Contains(line.Key.Value))
791✔
49
            return new GeneralEditorConfigSetting(line.Key.Value, line.Value.Value);
7✔
50

51
        bool isSeveritySetting = line.Key.Value.StartsWith("dotnet_diagnostic.");
784✔
52
        if (isSeveritySetting)
784✔
53
            return ParseSeveritySetting(line);
507✔
54

55
        bool isCompositeKeyRule = line.Key.Value.Contains('.');
277✔
56
        if (isCompositeKeyRule)
277✔
57
            return ParseCompositeKeySetting(line);
61✔
58

59
        return ParseOptionSetting(line);
216✔
60
    }
61

62
    private static RoslynSeverityEditorConfigSetting ParseSeveritySetting(EditorConfigPropertyNode line)
63
    {
64
        string[] keyParts = line.Key.Value.Split('.');
507✔
65

66
        if (keyParts.Length != 3)
507!
NEW
67
            throw new ArgumentException($"Incorrect rule key: {line.Key.Value}");
×
68

69
        if (!string.Equals(keyParts[2], "severity", StringComparison.InvariantCultureIgnoreCase))
507!
UNCOV
70
            throw new ArgumentException($"Expect postfix .severity for diagnostic rule but was {keyParts[2]}");
×
71

72
        if (!Enum.TryParse(line.Value.Value, true, out RoslynRuleSeverity severity))
507!
NEW
73
            throw new ArgumentException($"Cannot parse severity from {line.Value.Value}");
×
74

75
        var ruleId = RoslynRuleId.Parse(keyParts[1]);
507✔
76
        return new RoslynSeverityEditorConfigSetting(ruleId, severity);
507✔
77
    }
78

79
    private static CompositeRoslynOptionEditorConfigSetting ParseCompositeKeySetting(EditorConfigPropertyNode line)
80
    {
81
        string[] keyParts = line.Key.Value.Split('.');
61✔
82
        return new CompositeRoslynOptionEditorConfigSetting(keyParts, line.Value.Value, Severity: null);
61✔
83
    }
84

85
    private static RoslynOptionEditorConfigSetting ParseOptionSetting(EditorConfigPropertyNode line)
86
    {
87
        return new RoslynOptionEditorConfigSetting(line.Key.Value, line.Value.Value);
216✔
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc