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

Recognize existing GlobalSuppressions.cs having using directive

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