提交 903dd371 编写于 作者: J jnm2

Recognize existing GlobalSuppressions.cs having using directive

上级 9e275d45
......@@ -1475,7 +1475,7 @@ End Class]]>
' a specific target and scoped to a namespace, type, member, etc.
<Assembly: Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""<Pending>"", Scope:=""type"", Target:=""Class1"")>
<Assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
<Assembly: Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
"
Await TestAsync(source.ToString(), expected)
......@@ -1511,7 +1511,7 @@ End Class
' Project-level suppressions either have no target or are given
' a specific target and scoped to a namespace, type, member, etc.
<Assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
<Assembly: Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
"
Await TestAsync(source.ToString(), expected)
......@@ -1557,7 +1557,7 @@ End Class
' a specific target and scoped to a namespace, type, member, etc.
<Assembly: Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""<Pending>"", Scope:=""type"", Target:=""Class1"")>
<Assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
<Assembly: Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"", Scope:=""type"", Target:=""~T:Class2"")>
"
Await TestAsync(source.ToString(), expected)
......@@ -1678,7 +1678,7 @@ End Class
Imports System
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1701,7 +1701,7 @@ End Class
Imports System
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage("SomeOtherDiagnostic", "SomeOtherDiagnostic:Title", Justification:="<Pending>")>
<Diagnostics.CodeAnalysis.SuppressMessage("SomeOtherDiagnostic", "SomeOtherDiagnostic:Title", Justification:="<Pending>")>
[|Class C|]
Sub Method()
Dim x
......@@ -1712,8 +1712,8 @@ End Class
Imports System
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage(""SomeOtherDiagnostic"", ""SomeOtherDiagnostic:Title"", Justification:=""<Pending>"")>
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""SomeOtherDiagnostic"", ""SomeOtherDiagnostic:Title"", Justification:=""<Pending>"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1752,7 +1752,7 @@ Imports System
''' <summary>
''' My custom type
''' </summary>
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1793,7 +1793,7 @@ Imports System
''' My custom type
''' </summary>
<Diagnostics.CodeAnalysis.SuppressMessage(""SomeOtherDiagnostic"", ""SomeOtherDiagnostic:Title"", Justification:=""<Pending>"")>
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1827,7 +1827,7 @@ Imports System
Namespace N
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1861,7 +1861,7 @@ Imports System
Class Generic(Of T)
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Class C
Sub Method()
Dim x
......@@ -1896,7 +1896,7 @@ Imports System
Class Generic(Of T)
Class C
' Some Trivia
<System.Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
<Diagnostics.CodeAnalysis.SuppressMessage(""InfoDiagnostic"", ""InfoDiagnostic:InfoDiagnostic"", Justification:=""{FeaturesResources.Pending}"")>
Sub Method()
Dim x
End Sub
......
......@@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.Suppression
......@@ -70,6 +71,11 @@ protected override SyntaxTriviaList CreatePragmaRestoreDirectiveTrivia(Diagnosti
protected override string SingleLineCommentStart => "//";
protected override bool IsImportsLine(SyntaxNode node)
{
return node is UsingDirectiveSyntax;
}
protected override bool IsAttributeListWithAssemblyAttributes(SyntaxNode node)
{
return node is AttributeListSyntax attributeList &&
......@@ -124,9 +130,11 @@ protected override SyntaxNode AddLocalSuppressMessageAttribute(SyntaxNode target
SyntaxTriviaList leadingTrivia,
bool needsLeadingEndOfLine)
{
var attributeName = SyntaxFactory.ParseName(SuppressMessageAttributeName).WithAdditionalAnnotations(Simplifier.Annotation);
var attributeArguments = CreateAttributeArguments(targetSymbol, diagnostic, isAssemblyAttribute);
var attribute = SyntaxFactory.Attribute(SyntaxFactory.ParseName(SuppressMessageAttributeName), attributeArguments);
var attributes = new SeparatedSyntaxList<AttributeSyntax>().Add(attribute);
var attributes = new SeparatedSyntaxList<AttributeSyntax>()
.Add(SyntaxFactory.Attribute(attributeName, attributeArguments));
AttributeListSyntax attributeList;
if (isAssemblyAttribute)
......
......@@ -70,12 +70,13 @@ protected async Task<Document> GetOrCreateSuppressionsDocumentAsync(Cancellation
var fullPath = !string.IsNullOrEmpty(filePath) ? Path.GetFullPath(filePath) : filePath;
if (fullPath == suppressionsFilePath)
{
// Existing global suppressions file, see if this file only has global assembly attributes.
// Existing global suppressions file. See if this file only has imports and global assembly
// attributes.
hasDocWithSuppressionsName = true;
var t = await document.GetSyntaxTreeAsync(c).ConfigureAwait(false);
var r = await t.GetRootAsync(c).ConfigureAwait(false);
if (r.ChildNodes().All(n => Fixer.IsAttributeListWithAssemblyAttributes(n)))
if (r.ChildNodes().All(n => Fixer.IsImportsLine(n) || Fixer.IsAttributeListWithAssemblyAttributes(n)))
{
suppressionsDoc = document;
break;
......
......@@ -49,6 +49,7 @@ public bool IsFixableDiagnostic(Diagnostic diagnostic)
protected abstract string DefaultFileExtension { get; }
protected abstract string SingleLineCommentStart { get; }
protected abstract bool IsImportsLine(SyntaxNode node);
protected abstract bool IsAttributeListWithAssemblyAttributes(SyntaxNode node);
protected abstract bool IsEndOfLine(SyntaxTrivia trivia);
protected abstract bool IsEndOfFileToken(SyntaxToken token);
......
......@@ -6,6 +6,7 @@ Imports System.Threading
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.CodeFixes.Suppression
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.Simplification
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
......@@ -109,6 +110,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
End Get
End Property
Protected Overrides Function IsImportsLine(node As SyntaxNode) As Boolean
Return TypeOf node Is ImportsStatementSyntax
End Function
Protected Overrides Function IsAttributeListWithAssemblyAttributes(node As SyntaxNode) As Boolean
Dim attributesStatement = TryCast(node, AttributesStatementSyntax)
Return attributesStatement IsNot Nothing AndAlso
......@@ -158,7 +163,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
Private Function CreateAttributeList(targetSymbol As ISymbol, diagnostic As Diagnostic, isAssemblyAttribute As Boolean) As AttributeListSyntax
Dim attributeTarget = If(isAssemblyAttribute, SyntaxFactory.AttributeTarget(SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)), Nothing)
Dim attributeName = SyntaxFactory.ParseName(SuppressMessageAttributeName)
Dim attributeName = SyntaxFactory.ParseName(SuppressMessageAttributeName).WithAdditionalAnnotations(Simplifier.Annotation)
Dim attributeArguments = CreateAttributeArguments(targetSymbol, diagnostic, isAssemblyAttribute)
Dim attribute As AttributeSyntax = SyntaxFactory.Attribute(attributeTarget, attributeName, attributeArguments)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册