提交 e2fda86a 编写于 作者: M Manish Vasani

Port UseSystemHashCode analyzer/fixer/tests to shared layer

上级 f2c14f64
......@@ -70,8 +70,10 @@
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpAsAndNullCheckTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpIsAndCastCheckDiagnosticAnalyzerTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UsePatternMatching\CSharpIsAndCastCheckDiagnosticAnalyzerTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionTests_FixAllTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseVarTestExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\ValidateFormatStringTests.cs" />
</ItemGroup>
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' AND '$(BuildingInsideVisualStudio)' != 'true'">
......
......@@ -1209,7 +1209,7 @@ public override int GetHashCode()
hash.Add(i);
return hash.ToHashCode();
}
}", options: this.PreferImplicitTypeWithInfo());
}", options: UseVarTestExtensions.PreferImplicitTypeWithInfo(this));
}
[WorkItem(39916, "https://github.com/dotnet/roslyn/issues/39916")]
......
......@@ -5,7 +5,12 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
#if CODE_STYLE
using AbstractCodeActionOrUserDiagnosticTest = Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest;
#else
using static Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.AbstractCodeActionOrUserDiagnosticTest;
#endif
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions
{
......
......@@ -67,6 +67,9 @@
<Compile Include="$(MSBuildThisFileDirectory)UseNullPropagation\AbstractUseNullPropagationDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\AbstractUseObjectInitializerDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\ObjectCreationExpressionAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\Analyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\Analyzer.OperationDeconstructor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\AbstractUseThrowExpressionDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\AbstractValidateFormatStringDiagnosticAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\ValidateFormatStringOption.cs" />
......
......@@ -14,7 +14,7 @@
namespace Microsoft.CodeAnalysis.UseSystemHashCode
{
/// <summary>
/// Helper code to support both <see cref="UseSystemHashCodeCodeFixProvider"/> and
/// Helper code to support both "UseSystemHashCodeCodeFixProvider" and
/// <see cref="UseSystemHashCodeDiagnosticAnalyzer"/>.
/// </summary>
internal partial struct Analyzer
......
......@@ -16,8 +16,8 @@ internal class UseSystemHashCodeDiagnosticAnalyzer : AbstractBuiltInCodeStyleDia
public UseSystemHashCodeDiagnosticAnalyzer()
: base(IDEDiagnosticIds.UseSystemHashCode,
CodeStyleOptions2.PreferSystemHashCode,
new LocalizableResourceString(nameof(FeaturesResources.Use_System_HashCode), FeaturesResources.ResourceManager, typeof(FeaturesResources)),
new LocalizableResourceString(nameof(FeaturesResources.GetHashCode_implementation_can_be_simplified), FeaturesResources.ResourceManager, typeof(FeaturesResources)))
new LocalizableResourceString(nameof(AnalyzersResources.Use_System_HashCode), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)),
new LocalizableResourceString(nameof(AnalyzersResources.GetHashCode_implementation_can_be_simplified), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)))
{
}
......
......@@ -41,6 +41,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseExplicitTupleName\UseExplicitTupleNameCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseNullPropagation\AbstractUseNullPropagationCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\AbstractUseObjectInitializerCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeCodeFixProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UseThrowExpression\UseThrowExpressionCodeFixProvider.cs" />
</ItemGroup>
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' AND '$(BuildingInsideVisualStudio)' != 'true'">
......
......@@ -51,6 +51,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
SyntaxEditor editor, CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var generatorInternal = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var declarationService = document.GetLanguageService<ISymbolDeclarationService>();
if (declarationService == null)
{
......@@ -88,16 +89,16 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
var updatedDecl = generator.WithStatements(
methodBlock,
generator.CreateGetHashCodeStatementsUsingSystemHashCode(
analyzer.SystemHashCodeType, components));
generatorInternal, analyzer.SystemHashCodeType, components));
editor.ReplaceNode(methodBlock, updatedDecl);
}
}
}
private class MyCodeAction : CodeAction.DocumentChangeAction
private class MyCodeAction : CustomCodeActions.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument)
: base(FeaturesResources.Use_System_HashCode, createChangedDocument, FeaturesResources.Use_System_HashCode)
: base(AnalyzersResources.Use_System_HashCode, createChangedDocument, AnalyzersResources.Use_System_HashCode)
{
}
}
......
......@@ -40,6 +40,7 @@
<Compile Include="$(MSBuildThisFileDirectory)UseExplicitTupleName\UseExplicitTupleNameTests.vb" />
<Compile Include="$(MSBuildThisFileDirectory)UseNullPropagation\UseNullPropagationTests.vb" />
<Compile Include="$(MSBuildThisFileDirectory)UseObjectInitializer\UseObjectInitializerTests.vb" />
<Compile Include="$(MSBuildThisFileDirectory)UseSystemHashCode\UseSystemHashCodeTests.vb" />
<Compile Include="$(MSBuildThisFileDirectory)ValidateFormatString\ValidateFormatStringTests.vb" />
</ItemGroup>
<ItemGroup Condition="'$(DefaultLanguageSourceExtension)' != '' AND '$(BuildingInsideVisualStudio)' != 'true'">
......
......@@ -155,12 +155,13 @@ public async Task<Document> FormatDocumentAsync(Document document, CancellationT
if (components.Length > 0 && hashCodeType != null)
{
return factory.CreateGetHashCodeStatementsUsingSystemHashCode(hashCodeType, components);
return factory.CreateGetHashCodeStatementsUsingSystemHashCode(
factory.SyntaxGeneratorInternal, hashCodeType, components);
}
// Otherwise, try to just spit out a reasonable hash code for these members.
var statements = factory.CreateGetHashCodeMethodStatements(
compilation, namedType, members, useInt64: false);
factory.SyntaxGeneratorInternal, compilation, namedType, members, useInt64: false);
// Unfortunately, our 'reasonable' hash code may overflow in checked contexts.
// C# can handle this by adding 'checked{}' around the code, VB has to jump
......@@ -194,7 +195,7 @@ public async Task<Document> FormatDocumentAsync(Document document, CancellationT
//
// This does mean all hashcodes will be positive. But it will avoid the overflow problem.
return factory.CreateGetHashCodeMethodStatements(
compilation, namedType, members, useInt64: true);
factory.SyntaxGeneratorInternal, compilation, namedType, members, useInt64: true);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册