Skip to content

Commit be72ea0

Browse files
committed
Gracefully deal with bad editorconfigs
closes #1227
1 parent ea02607 commit be72ea0

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,33 @@ internal static class ConfigFileParser
1818
CommentRegex = CommentRegex,
1919
AllowDuplicateKeys = true,
2020
AllowDuplicateSections = true,
21-
OverrideDuplicateKeys = true
21+
OverrideDuplicateKeys = true,
22+
SkipInvalidLines = true,
23+
ThrowExceptionsOnError = false
2224
};
2325

2426
public static ConfigFile Parse(string filePath, IFileSystem fileSystem)
2527
{
28+
var directory = fileSystem.Path.GetDirectoryName(filePath);
29+
30+
ArgumentNullException.ThrowIfNull(directory);
31+
2632
var parser = new FileIniDataParser(new IniDataParser(Configuration));
2733

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

32-
var directory = fileSystem.Path.GetDirectoryName(filePath);
33-
34-
ArgumentNullException.ThrowIfNull(directory);
35-
3638
var sections = new List<Section>();
37-
foreach (var section in configData.Sections)
39+
if (configData is not null)
3840
{
39-
sections.Add(new Section(section, directory));
41+
sections.AddRange(configData.Sections.Select(s => new Section(s, directory)));
4042
}
4143

42-
return new ConfigFile { IsRoot = configData.Global["root"] == "true", Sections = sections };
44+
return new ConfigFile
45+
{
46+
IsRoot = configData?.Global["root"] == "true",
47+
Sections = sections
48+
};
4349
}
4450
}

Src/CSharpier.Tests/OptionsProviderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public async Task Should_Not_Prefer_Closer_EditorConfig()
559559
}
560560

561561
[Test]
562-
public async Task Should_Ignore_Invalid_EditorConfig()
562+
public async Task Should_Ignore_Invalid_EditorConfig_Lines()
563563
{
564564
var context = new TestContext();
565565
context.WhenAFileExists(
@@ -573,7 +573,7 @@ public async Task Should_Ignore_Invalid_EditorConfig()
573573

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

576-
result.TabWidth.Should().Be(4);
576+
result.TabWidth.Should().Be(2);
577577
}
578578

579579
[Test]

0 commit comments

Comments
 (0)