提交 b0b8066e 编写于 作者: J Jonathon Marolf 提交者: GitHub

Properly handle lack of editorconfig file for naming styles (#16413)

We now return false if we get an empty set of naming styles.
上级 fe1c69b0
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using static Microsoft.CodeAnalysis.CodeStyle.CodeStyleHelpers;
......@@ -17,7 +18,7 @@ internal sealed class EditorConfigStorageLocation : OptionStorageLocation
private Func<string, Type, object> _parseValue;
private Func<IReadOnlyDictionary<string, object>, Type, object> _parseDictionary;
private Func<IReadOnlyDictionary<string, object>, Type, (object result, bool succeeded)> _tryParseDictionary;
public bool TryParseReadonlyDictionary(IReadOnlyDictionary<string, object> allRawConventions, Type type, out object result)
{
......@@ -29,10 +30,11 @@ public bool TryParseReadonlyDictionary(IReadOnlyDictionary<string, object> allRa
return true;
}
}
else if (_parseDictionary != null)
else if (_tryParseDictionary != null)
{
result = _parseDictionary(allRawConventions, type);
return true;
var tuple = _tryParseDictionary(allRawConventions, type);
result = tuple.result;
return tuple.succeeded;
}
result = null;
......@@ -75,11 +77,19 @@ public EditorConfigStorageLocation(string keyName, Func<string, object> parseVal
public EditorConfigStorageLocation()
{
// If the user didn't pass a keyName assume we need to parse the entire dictionary
_parseDictionary = (dictionary, type) =>
_tryParseDictionary = (dictionary, type) =>
{
if (type == typeof(NamingStylePreferences))
{
return EditorConfigNamingStyleParser.GetNamingStylesFromDictionary(dictionary);
var result = EditorConfigNamingStyleParser.GetNamingStylesFromDictionary(dictionary);
if (!result.NamingRules.Any() &&
!result.NamingStyles.Any() &&
!result.SymbolSpecifications.Any())
{
return (result: result, succeeded: false);
}
return (result: result, succeeded: true);
}
else
{
......@@ -88,10 +98,10 @@ public EditorConfigStorageLocation()
};
}
public EditorConfigStorageLocation(Func<IReadOnlyDictionary<string, object>, object> parseDictionary)
public EditorConfigStorageLocation(Func<IReadOnlyDictionary<string, object>, (object result, bool succeeded)> tryParseDictionary)
{
// If we're explicitly given a parsing function we can throw away the type when parsing
_parseDictionary = (dictionary, type) => parseDictionary(dictionary);
_tryParseDictionary = (dictionary, type) => tryParseDictionary(dictionary);
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Options;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests.EditorConfig.StorageLocation
{
public class EditorConfigStorageLocationTests
{
[Fact]
public static void TestEmptyDictionaryReturnFalse()
{
var editorConfigStorageLocation = new EditorConfigStorageLocation();
var result = editorConfigStorageLocation.TryParseReadonlyDictionary(new Dictionary<string, object>(), typeof(NamingStylePreferences), out var @object);
Assert.False(result, "Expected TryParseReadonlyDictionary to return 'false' for empty dictionary");
}
[Fact]
public static void TestObjectTypeThrowsNotSupportedException()
{
var editorConfigStorageLocation = new EditorConfigStorageLocation();
Assert.Throws<NotSupportedException>(() =>
{
editorConfigStorageLocation.TryParseReadonlyDictionary(new Dictionary<string, object>(), typeof(object), out var @object);
});
}
}
}
......@@ -72,6 +72,7 @@
<ItemGroup>
<Compile Include="CodeStyle\EditorConfigCodeStyleParserTests.cs" />
<Compile Include="DependentTypeFinderTests.cs" />
<Compile Include="EditorConfigStorageLocation\EditorConfigStorageLocationTests.cs" />
<Compile Include="Editting\SyntaxEditorTests.cs" />
<Compile Include="Execution\Extensions.cs" />
<Compile Include="Execution\SnapshotSerializationTestBase.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册