提交 e330bfa6 编写于 作者: C Carl Åstholm

Update features that depend on PreferThrowExpression

上级 0411cef7
......@@ -2,9 +2,11 @@
using System.Composition;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.InitializeParameter;
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.CSharp.InitializeParameter
{
......@@ -49,5 +51,10 @@ protected override bool CanOffer(SyntaxNode body)
return true;
}
protected override bool GetPreferThrowExpressionOptionValue(DocumentOptionSet options)
{
return options.GetOption(CSharpCodeStyleOptions.PreferThrowExpression).Value;
}
}
}
// 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 Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.UseThrowExpression;
......@@ -9,6 +10,11 @@ namespace Microsoft.CodeAnalysis.CSharp.UseThrowExpression
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpUseThrowExpressionDiagnosticAnalyzer : AbstractUseThrowExpressionDiagnosticAnalyzer
{
public CSharpUseThrowExpressionDiagnosticAnalyzer()
: base(CSharpCodeStyleOptions.PreferThrowExpression, LanguageNames.CSharp)
{
}
protected override bool IsSupported(ParseOptions options)
{
var csOptions = (CSharpParseOptions)options;
......
......@@ -7,10 +7,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
......@@ -472,7 +472,7 @@ private static TStatementSyntax CreateNullCheckStatement(SemanticModel semanticM
}
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
if (!options.GetOption(CodeStyleOptions.PreferThrowExpression).Value)
if (!GetPreferThrowExpressionOptionValue(options))
{
return null;
}
......@@ -505,6 +505,8 @@ private static TStatementSyntax CreateNullCheckStatement(SemanticModel semanticM
return null;
}
protected abstract bool GetPreferThrowExpressionOptionValue(DocumentOptionSet options);
private static SyntaxNode GetTypeNode(
Compilation compilation, SyntaxGenerator generator, Type type)
{
......
......@@ -6,8 +6,8 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.UseThrowExpression
{
......@@ -33,12 +33,16 @@ namespace Microsoft.CodeAnalysis.UseThrowExpression
internal abstract class AbstractUseThrowExpressionDiagnosticAnalyzer :
AbstractBuiltInCodeStyleDiagnosticAnalyzer
{
protected AbstractUseThrowExpressionDiagnosticAnalyzer()
private readonly Option<CodeStyleOption<bool>> _preferThrowExpressionOption;
protected AbstractUseThrowExpressionDiagnosticAnalyzer(Option<CodeStyleOption<bool>> preferThrowExpressionOption, string language)
: base(IDEDiagnosticIds.UseThrowExpressionDiagnosticId,
CodeStyleOptions.PreferThrowExpression,
preferThrowExpressionOption,
language,
new LocalizableResourceString(nameof(FeaturesResources.Use_throw_expression), FeaturesResources.ResourceManager, typeof(FeaturesResources)),
new LocalizableResourceString(nameof(FeaturesResources.Null_check_can_be_simplified), FeaturesResources.ResourceManager, typeof(FeaturesResources)))
{
_preferThrowExpressionOption = preferThrowExpressionOption;
}
public override DiagnosticAnalyzerCategory GetAnalyzerCategory()
......@@ -94,7 +98,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
return;
}
var option = optionSet.GetOption(CodeStyleOptions.PreferThrowExpression, throwStatementSyntax.Language);
var option = optionSet.GetOption(_preferThrowExpressionOption);
if (!option.Value)
{
return;
......
......@@ -4,6 +4,7 @@ Imports System.Composition
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.Editing
Imports Microsoft.CodeAnalysis.InitializeParameter
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.InitializeParameter
......@@ -40,6 +41,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InitializeParameter
Return True
End Function
Protected Overrides Function GetPreferThrowExpressionOptionValue(options As DocumentOptionSet) As Boolean
' No throw expression preference option is defined for VB because it doesn't support throw expressions.
Return False
End Function
Protected Overrides Function GetBody(functionDeclaration As SyntaxNode) As SyntaxNode
Return InitializeParameterHelpers.GetBody(functionDeclaration)
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册