Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully deal with bad editorconfigs #1229

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading