提交 3cc38f0b 编写于 作者: J Jb Evain

Disable Use Object Initializer diagnostic prior to C# 3

上级 7dd78ae8
......@@ -3,6 +3,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.UseObjectInitializer;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
......@@ -193,6 +194,23 @@ void M()
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseObjectInitializer)]
public async Task TestMissingBeforeCSharp3()
{
await TestMissingAsync(
@"
class C
{
int i;
int j;
void M()
{
var c = [||]new C();
c.j = 1;
}
}", parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp2));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseObjectInitializer)]
public async Task TestFixAllInDocument()
{
......
......@@ -21,6 +21,13 @@ internal class CSharpUseObjectInitializerDiagnosticAnalyzer :
{
protected override bool FadeOutOperatorToken => true;
protected override bool AreObjectInitializersSupported(SyntaxNodeAnalysisContext context)
{
// object initializers are only available in C# 3.0 and above. Don't offer this refactoring
// in projects targeting a lesser version.
return ((CSharpParseOptions)context.Node.SyntaxTree.Options).LanguageVersion >= LanguageVersion.CSharp3;
}
protected override SyntaxKind GetObjectCreationSyntaxKind() => SyntaxKind.ObjectCreationExpression;
protected override ISyntaxFactsService GetSyntaxFactsService() => CSharpSyntaxFactsService.Instance;
......
......@@ -44,8 +44,15 @@ public override void Initialize(AnalysisContext context)
protected abstract TSyntaxKind GetObjectCreationSyntaxKind();
protected abstract bool AreObjectInitializersSupported(SyntaxNodeAnalysisContext context);
private void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
if (!AreObjectInitializersSupported(context))
{
return;
}
var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
var language = objectCreationExpression.Language;
......
......@@ -23,6 +23,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseObjectInitializer
End Get
End Property
Protected Overrides Function AreObjectInitializersSupported(context As SyntaxNodeAnalysisContext) As Boolean
'Object Initializers are supported in all the versions of Visual Basic we support
Return True
End Function
Protected Overrides Function GetObjectCreationSyntaxKind() As SyntaxKind
Return SyntaxKind.ObjectCreationExpression
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册