提交 d2eaf843 编写于 作者: D David Poeschl

Update OOP analyzers to calculate OpenFileOnly based on options

Only Error- and Warning-level options will run on closed files.
Suggestions will only run on open files.
上级 861529fa
......@@ -23,5 +23,7 @@ internal class CSharpPreferFrameworkTypeDiagnosticAnalyzer :
protected override bool IsInMemberAccessOrCrefReferenceContext(ExpressionSyntax node) =>
node.IsInMemberAccessContext() || node.InsideCrefReference();
protected override string GetLanguageName() => LanguageNames.CSharp;
}
}
// 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 Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.QualifyMemberAccess;
......@@ -8,6 +9,11 @@ namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.QualifyMemberAccess
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CSharpQualifyMemberAccessDiagnosticAnalyzer : QualifyMemberAccessDiagnosticAnalyzerBase<SyntaxKind>
{
protected override string GetLanguageName()
{
return LanguageNames.CSharp;
}
protected override bool IsAlreadyQualifiedMemberAccess(SyntaxNode node)
{
return node.IsKind(SyntaxKind.ThisExpression);
......
......@@ -12,7 +12,7 @@
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
{
internal partial class CSharpTypeStyleDiagnosticAnalyzerBase
internal abstract partial class CSharpTypeStyleDiagnosticAnalyzerBase
{
internal class State
{
......
......@@ -10,6 +10,9 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CodeStyle;
using System;
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
{
......@@ -38,7 +41,17 @@ public CSharpTypeStyleDiagnosticAnalyzerBase(string diagnosticId, LocalizableStr
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(CreateDiagnosticDescriptor(DiagnosticSeverity.Hidden));
public DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticSpanAnalysis;
public bool OpenFileOnly(Workspace workspace) => true;
public bool OpenFileOnly(Workspace workspace)
{
var forIntrinsicTypesOption = workspace.Options.GetOption(CSharpCodeStyleOptions.UseImplicitTypeForIntrinsicTypes).Notification;
var whereApparentOption = workspace.Options.GetOption(CSharpCodeStyleOptions.UseImplicitTypeWhereApparent).Notification;
var wherePossibleOption = workspace.Options.GetOption(CSharpCodeStyleOptions.UseImplicitTypeWherePossible).Notification;
return !(forIntrinsicTypesOption == NotificationOption.Warning || forIntrinsicTypesOption == NotificationOption.Error ||
whereApparentOption == NotificationOption.Warning || whereApparentOption == NotificationOption.Error ||
wherePossibleOption == NotificationOption.Warning || wherePossibleOption == NotificationOption.Error);
}
public override void Initialize(AnalysisContext context)
{
......
......@@ -45,7 +45,18 @@ internal abstract class PreferFrameworkTypeDiagnosticAnalyzerBase<TSyntaxKind, T
private PerLanguageOption<CodeStyleOption<bool>> GetOptionForMemberAccessContext =>
CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess;
public bool OpenFileOnly(Workspace workspace) => true;
public bool OpenFileOnly(Workspace workspace)
{
var preferTypeKeywordInDeclarationOption = workspace.Options.GetOption(
CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, GetLanguageName()).Notification;
var preferTypeKeywordInMemberAccessOption = workspace.Options.GetOption(
CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, GetLanguageName()).Notification;
return !(preferTypeKeywordInDeclarationOption == NotificationOption.Warning || preferTypeKeywordInDeclarationOption == NotificationOption.Error ||
preferTypeKeywordInMemberAccessOption == NotificationOption.Warning || preferTypeKeywordInMemberAccessOption == NotificationOption.Error);
}
protected abstract string GetLanguageName();
public DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticSpanAnalysis;
......
......@@ -26,7 +26,20 @@ internal abstract class QualifyMemberAccessDiagnosticAnalyzerBase<TLanguageKindE
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_descriptorQualifyMemberAccess);
public bool OpenFileOnly(Workspace workspace) => true;
public bool OpenFileOnly(Workspace workspace)
{
var qualifyFieldAccessOption = workspace.Options.GetOption(CodeStyleOptions.QualifyFieldAccess, GetLanguageName()).Notification;
var qualifyPropertyAccessOption = workspace.Options.GetOption(CodeStyleOptions.QualifyPropertyAccess, GetLanguageName()).Notification;
var qualifyMethodAccessOption = workspace.Options.GetOption(CodeStyleOptions.QualifyMethodAccess, GetLanguageName()).Notification;
var qualifyEventAccessOption = workspace.Options.GetOption(CodeStyleOptions.QualifyEventAccess, GetLanguageName()).Notification;
return !(qualifyFieldAccessOption == NotificationOption.Warning || qualifyFieldAccessOption == NotificationOption.Error ||
qualifyPropertyAccessOption == NotificationOption.Warning || qualifyPropertyAccessOption == NotificationOption.Error ||
qualifyMethodAccessOption == NotificationOption.Warning || qualifyMethodAccessOption == NotificationOption.Error ||
qualifyEventAccessOption == NotificationOption.Warning || qualifyEventAccessOption == NotificationOption.Error);
}
protected abstract string GetLanguageName();
protected abstract bool IsAlreadyQualifiedMemberAccess(SyntaxNode node);
......
......@@ -72,7 +72,16 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
}
}
public bool OpenFileOnly(Workspace workspace) => true;
public bool OpenFileOnly(Workspace workspace)
{
var preferTypeKeywordInDeclarationOption = workspace.Options.GetOption(
CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, GetLanguageName()).Notification;
var preferTypeKeywordInMemberAccessOption = workspace.Options.GetOption(
CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, GetLanguageName()).Notification;
return !(preferTypeKeywordInDeclarationOption == NotificationOption.Warning || preferTypeKeywordInDeclarationOption == NotificationOption.Error ||
preferTypeKeywordInMemberAccessOption == NotificationOption.Warning || preferTypeKeywordInMemberAccessOption == NotificationOption.Error);
}
protected abstract void AnalyzeNode(SyntaxNodeAnalysisContext context);
......
......@@ -26,6 +26,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Diagnostics.Analyzers
Return Not KeywordMatchesTypeName(node.Keyword.Kind())
End Function
Protected Overrides Function GetLanguageName() As String
Return LanguageNames.VisualBasic
End Function
''' <summary>
''' Returns true, if the VB language keyword for predefined type matches its
''' actual framework type name.
......
......@@ -9,6 +9,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.QualifyMemberAccess
Friend NotInheritable Class VisualBasicQualifyMemberAccessDiagnosticAnalyzer
Inherits QualifyMemberAccessDiagnosticAnalyzerBase(Of SyntaxKind)
Protected Overrides Function GetLanguageName() As String
Return LanguageNames.VisualBasic
End Function
Protected Overrides Function IsAlreadyQualifiedMemberAccess(node As SyntaxNode) As Boolean
Return node.IsKind(SyntaxKind.MeExpression)
End Function
......
......@@ -9,6 +9,7 @@ namespace Microsoft.CodeAnalysis.CodeStyle
/// <remarks>
/// This also supports various properties for databinding.
/// </remarks>
/// <completionlist cref="NotificationOption"/>
public class NotificationOption
{
public string Name { get; set; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册