Skip to content

Commit

Permalink
Gracefully deal with bad editorconfigs (#1229)
Browse files Browse the repository at this point in the history
closes #1227
  • Loading branch information
belav committed Apr 16, 2024
1 parent ea02607 commit 2d5c87f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs
Expand Up @@ -18,27 +18,33 @@ internal static class ConfigFileParser
CommentRegex = CommentRegex,
AllowDuplicateKeys = true,
AllowDuplicateSections = true,
OverrideDuplicateKeys = true
OverrideDuplicateKeys = true,
SkipInvalidLines = true,
ThrowExceptionsOnError = false
};

public static ConfigFile Parse(string filePath, IFileSystem fileSystem)
{
var directory = fileSystem.Path.GetDirectoryName(filePath);

ArgumentNullException.ThrowIfNull(directory);

var parser = new FileIniDataParser(new IniDataParser(Configuration));

using var stream = fileSystem.File.OpenRead(filePath);
using var streamReader = new StreamReader(stream);
var configData = parser.ReadData(streamReader);

var directory = fileSystem.Path.GetDirectoryName(filePath);

ArgumentNullException.ThrowIfNull(directory);

var sections = new List<Section>();
foreach (var section in configData.Sections)
if (configData is not null)
{
sections.Add(new Section(section, directory));
sections.AddRange(configData.Sections.Select(s => new Section(s, directory)));
}

return new ConfigFile { IsRoot = configData.Global["root"] == "true", Sections = sections };
return new ConfigFile
{
IsRoot = configData?.Global["root"] == "true",
Sections = sections
};
}
}
4 changes: 2 additions & 2 deletions Src/CSharpier.Tests/OptionsProviderTests.cs
Expand Up @@ -559,7 +559,7 @@ public async Task Should_Not_Prefer_Closer_EditorConfig()
}

[Test]
public async Task Should_Ignore_Invalid_EditorConfig()
public async Task Should_Ignore_Invalid_EditorConfig_Lines()
{
var context = new TestContext();
context.WhenAFileExists(
Expand All @@ -573,7 +573,7 @@ public async Task Should_Ignore_Invalid_EditorConfig()

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

result.TabWidth.Should().Be(4);
result.TabWidth.Should().Be(2);
}

[Test]
Expand Down

0 comments on commit 2d5c87f

Please sign in to comment.