From 903dd3717ee346b1e4147bb4e4dfa357d760b642 Mon Sep 17 00:00:00 2001 From: jnm2 Date: Wed, 31 Jul 2019 12:35:16 -0400 Subject: [PATCH] Recognize existing GlobalSuppressions.cs having using directive --- .../Suppression/SuppressionTests.vb | 24 +++++++++---------- .../CSharpSuppressionCodeFixProvider.cs | 12 ++++++++-- ...AbstractGlobalSuppressMessageCodeAction.cs | 5 ++-- .../AbstractSuppressionCodeFixProvider.cs | 1 + .../VisualBasicSuppressionCodeFixProvider.vb | 7 +++++- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/EditorFeatures/VisualBasicTest/Diagnostics/Suppression/SuppressionTests.vb b/src/EditorFeatures/VisualBasicTest/Diagnostics/Suppression/SuppressionTests.vb index 7ad04dc022f..afd3409c02c 100644 --- a/src/EditorFeatures/VisualBasicTest/Diagnostics/Suppression/SuppressionTests.vb +++ b/src/EditorFeatures/VisualBasicTest/Diagnostics/Suppression/SuppressionTests.vb @@ -1475,7 +1475,7 @@ End Class]]> ' a specific target and scoped to a namespace, type, member, etc. "", Scope:=""type"", Target:=""Class1"")> - + " 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. - + " Await TestAsync(source.ToString(), expected) @@ -1557,7 +1557,7 @@ End Class ' a specific target and scoped to a namespace, type, member, etc. "", Scope:=""type"", Target:=""Class1"")> - + " Await TestAsync(source.ToString(), expected) @@ -1678,7 +1678,7 @@ End Class Imports System ' Some Trivia - + Class C Sub Method() Dim x @@ -1701,7 +1701,7 @@ End Class Imports System ' Some Trivia - + [|Class C|] Sub Method() Dim x @@ -1712,8 +1712,8 @@ End Class Imports System ' Some Trivia -"")> - +"")> + Class C Sub Method() Dim x @@ -1752,7 +1752,7 @@ Imports System ''' ''' My custom type ''' - + Class C Sub Method() Dim x @@ -1793,7 +1793,7 @@ Imports System ''' My custom type ''' "")> - + Class C Sub Method() Dim x @@ -1827,7 +1827,7 @@ Imports System Namespace N ' Some Trivia - + Class C Sub Method() Dim x @@ -1861,7 +1861,7 @@ Imports System Class Generic(Of T) ' Some Trivia - + Class C Sub Method() Dim x @@ -1896,7 +1896,7 @@ Imports System Class Generic(Of T) Class C ' Some Trivia - + Sub Method() Dim x End Sub diff --git a/src/Features/CSharp/Portable/CodeFixes/Suppression/CSharpSuppressionCodeFixProvider.cs b/src/Features/CSharp/Portable/CodeFixes/Suppression/CSharpSuppressionCodeFixProvider.cs index b9f7e6f9087..bbc92d1b3af 100644 --- a/src/Features/CSharp/Portable/CodeFixes/Suppression/CSharpSuppressionCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/CodeFixes/Suppression/CSharpSuppressionCodeFixProvider.cs @@ -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().Add(attribute); + + var attributes = new SeparatedSyntaxList() + .Add(SyntaxFactory.Attribute(attributeName, attributeArguments)); AttributeListSyntax attributeList; if (isAssemblyAttribute) diff --git a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.AbstractGlobalSuppressMessageCodeAction.cs b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.AbstractGlobalSuppressMessageCodeAction.cs index e416a728af0..6562d919a34 100644 --- a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.AbstractGlobalSuppressMessageCodeAction.cs +++ b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.AbstractGlobalSuppressMessageCodeAction.cs @@ -70,12 +70,13 @@ protected async Task 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; diff --git a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.cs b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.cs index f2f59d33319..ddc9d6c972d 100644 --- a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.cs +++ b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.cs @@ -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); diff --git a/src/Features/VisualBasic/Portable/CodeFixes/Suppression/VisualBasicSuppressionCodeFixProvider.vb b/src/Features/VisualBasic/Portable/CodeFixes/Suppression/VisualBasicSuppressionCodeFixProvider.vb index 6a6afa1f440..d0449133ac9 100644 --- a/src/Features/VisualBasic/Portable/CodeFixes/Suppression/VisualBasicSuppressionCodeFixProvider.vb +++ b/src/Features/VisualBasic/Portable/CodeFixes/Suppression/VisualBasicSuppressionCodeFixProvider.vb @@ -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) -- GitLab