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

kysect / Configuin / 9129764175

17 May 2024 02:22PM UTC coverage: 81.651% (+0.1%) from 81.509%
9129764175

push

github

FrediKats
Add support of comment after content in .editorconfig

246 of 366 branches covered (67.21%)

Branch coverage included in aggregate %.

25 of 26 new or added lines in 3 files covered. (96.15%)

1792 of 2130 relevant lines covered (84.13%)

528.81 hits per line

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

91.04
/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs
1
using Kysect.CommonLib.BaseTypes.Extensions;
2
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes;
3

4
namespace Kysect.Configuin.EditorConfig.DocumentModel;
5

6
public class EditorConfigDocumentParser
7
{
8
    public EditorConfigDocument Parse(string content)
9
    {
10
        content.ThrowIfNull();
21✔
11

12
        // KB: sometimes Environment.NewLine is not the same as in the file
13
        string[] lines = content.Split(Environment.NewLine);
21✔
14
        return Parse(lines);
21✔
15
    }
16

17
    public EditorConfigDocument Parse(string[] lines)
18
    {
19
        lines.ThrowIfNull();
21✔
20

21
        var context = new EditorConfigDocumentParsingContext();
21✔
22

23
        foreach (string line in lines)
3,464✔
24
        {
25
            var trimmedLine = line.Trim();
1,711✔
26
            bool isCategory = trimmedLine.StartsWith("[");
1,711✔
27
            if (isCategory)
1,711✔
28
            {
29
                (string? lineWithoutComment, string? comment) = ExtractComment(trimmedLine);
12✔
30
                lineWithoutComment = lineWithoutComment.Trim();
12✔
31
                if (!lineWithoutComment.EndsWith("]"))
12!
NEW
32
                    throw new ArgumentException($"Line is not valid category definition: {lineWithoutComment}");
×
33

34
                string categoryName = lineWithoutComment.Substring(1, lineWithoutComment.Length - 2);
12✔
35

36
                EditorConfigCategoryNode categoryNode = comment is not null
12✔
37
                    ? new EditorConfigCategoryNode(categoryName) { TrailingTrivia = comment }
12✔
38
                    : new EditorConfigCategoryNode(categoryName);
12✔
39

40
                context.AddCategory(categoryNode);
12✔
41
                continue;
12✔
42
            }
43

44
            bool isSection = trimmedLine.StartsWith(EditorConfigDocumentSectionNode.NodeIndicator);
1,699✔
45
            if (isSection)
1,699✔
46
            {
47
                context.AddSection(trimmedLine);
3✔
48
                continue;
3✔
49
            }
50

51
            // TODO: add test for commented properties
52
            bool isTrivia = trimmedLine.StartsWith("#") || string.IsNullOrWhiteSpace(line);
1,696✔
53
            if (isTrivia)
1,696✔
54
            {
55
                context.AddTrivia(line);
494✔
56
                continue;
494✔
57
            }
58

59
            bool isProperty = trimmedLine.Contains('=');
1,202✔
60
            if (isProperty)
1,202!
61
            {
62
                (string? lineWithoutComment, string? comment) = ExtractComment(trimmedLine);
1,202✔
63
                lineWithoutComment = lineWithoutComment.Trim();
1,202✔
64

65
                string[] parts = lineWithoutComment.Split('=');
1,202✔
66
                if (parts.Length != 2)
1,202!
67
                    throw new ArgumentException($"Line {line} contains unexpected count of '='");
×
68

69
                var propertyNode = new EditorConfigPropertyNode(EditorConfigStringNode.Create(parts[0]), EditorConfigStringNode.Create(parts[1]));
1,202✔
70
                if (comment is not null)
1,202✔
71
                    propertyNode = propertyNode with { TrailingTrivia = comment };
1✔
72

73
                context.AddProperty(propertyNode);
1,202✔
74
                continue;
1,202✔
75
            }
76

77
            throw new NotSupportedException($"Not supported line: {line}");
×
78
        }
79

80
        return context.Build();
21✔
81
    }
82

83
    private (string lineWithoutComment, string? comment) ExtractComment(string originalString)
84
    {
85
        if (!originalString.Contains('#'))
1,214✔
86
            return (originalString, null);
1,212✔
87

88
        string[] parts = originalString.Split('#');
2✔
89
        return (parts[0], parts[1]);
2✔
90
    }
91
}
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