提交 adfe8f0f 编写于 作者: J Jason Malinowski

Remove some duplicate helpers around removing attributes

It turns out for methods we already had
RemoveInaccessibleAttributesAndAttributesOfTypes but had accidentally
created another helper for filtering out attributes from parameters
that more correctly should have used that. This also observed that we
didn't have a helper for IParameterSymbols to do the same thing.
We did have one that filtered but not one that removed innaccessible
parameters, which was actually a bug.
上级 f5bdc0b4
......@@ -6036,6 +6036,56 @@ public void set_P(int x, object Value)
await TestWithAllCodeStyleOptionsOffAsync(initial, expected, index: 0);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestImplementationOfIndexerWithInaccessibleAttributes()
{
var initial = @"
<Workspace>
<Project Language=""C#"" AssemblyName=""Assembly1"" CommonReferences=""true"">
<Document>
using System;
internal class ShouldBeRemovedAttribute : Attribute { }
public interface I
{
string this[[ShouldBeRemovedAttribute] int i] { get; set; }
}
</Document>
</Project>
<Project Language=""C#"" AssemblyName=""Assembly2"" CommonReferences=""true"">
<ProjectReference>Assembly1</ProjectReference>
<Document>
using System;
class C : [|I|]
{
}
</Document>
</Project>
</Workspace>";
var expected = @"
using System;
class C : I
{
public string this[int i]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
";
await TestWithAllCodeStyleOptionsOffAsync(initial, expected, index: 0);
}
#if false
[WorkItem(13677)]
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
......
......@@ -60,7 +60,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
explicitInterfaceImplementations:=Nothing,
name:=methodName,
typeParameters:=Nothing,
parameters:=delegateInvokeMethod.Parameters.WithAttributesToBeCopied(destinationType),
parameters:=delegateInvokeMethod.RemoveInaccessibleAttributesAndAttributesOfTypes(destinationType).Parameters,
handlesExpressions:=ImmutableArray.Create(Of SyntaxNode)(handlesSyntax))
methodSymbol = GeneratedSymbolAnnotation.AddAnnotationToSymbol(methodSymbol)
......
......@@ -29,8 +29,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
Dim codeGenerationSymbol = GeneratedSymbolAnnotation.AddAnnotationToSymbol(
CodeGenerationSymbolFactory.CreateMethodSymbol(
methodToReplicate,
parameters:=methodToReplicate.Parameters.WithAttributesToBeCopied(destinationType)))
methodToReplicate.RemoveInaccessibleAttributesAndAttributesOfTypes(destinationType)))
Return Await CodeGenerator.AddMethodDeclarationAsync(document.Project.Solution,
destinationType,
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
......@@ -9,6 +11,7 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.ImplementInterface
{
......@@ -44,7 +47,7 @@ internal partial class ImplementInterfaceCodeAction
var updatedProperty = property.RenameParameters(parameterNames);
updatedProperty = updatedProperty.RemoveAttributeFromParameters(attributesToRemove);
updatedProperty = updatedProperty.RemoveInaccessibleAttributesAndAttributesOfTypes(compilation.Assembly, attributesToRemove);
// TODO(cyrusn): Delegate through throughMember if it's non-null.
return CodeGenerationSymbolFactory.CreatePropertySymbol(
......@@ -66,10 +69,10 @@ internal partial class ImplementInterfaceCodeAction
private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
{
return new[] { compilation.ComAliasNameAttributeType(), compilation.TupleElementNamesAttributeType(),
compilation.DynamicAttributeType() };
compilation.DynamicAttributeType() }.WhereNotNull().ToArray()!;
}
private IMethodSymbol GenerateSetAccessor(
private IMethodSymbol? GenerateSetAccessor(
Compilation compilation,
IPropertySymbol property,
Accessibility accessibility,
......@@ -103,7 +106,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
compilation, property, generateAbstractly, propertyGenerationBehavior, cancellationToken));
}
private IMethodSymbol GenerateGetAccessor(
private IMethodSymbol? GenerateGetAccessor(
Compilation compilation,
IPropertySymbol property,
Accessibility accessibility,
......
......@@ -206,7 +206,7 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
params INamedTypeSymbol[] removeAttributeTypes)
{
bool shouldRemoveAttribute(AttributeData a) =>
removeAttributeTypes.Any(attr => attr != null && attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
removeAttributeTypes.Any(attr => attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
var methodHasAttribute = method.GetAttributes().Any(shouldRemoveAttribute);
......
......@@ -53,11 +53,6 @@ public static IParameterSymbol WithAttributes(this IParameterSymbol parameter, I
parameter.HasExplicitDefaultValue ? parameter.ExplicitDefaultValue : null);
}
public static ImmutableArray<IParameterSymbol> WithAttributesToBeCopied(
this ImmutableArray<IParameterSymbol> parameters, INamedTypeSymbol containingType)
=> parameters.SelectAsArray(
p => p.WithAttributes(p.GetAttributes().WhereAsArray(a => a.ShouldKeepAttribute(containingType))));
public static ImmutableArray<IParameterSymbol> RenameParameters(this IList<IParameterSymbol> parameters, IList<string> parameterNames)
{
var result = ArrayBuilder<IParameterSymbol>.GetInstance();
......@@ -68,8 +63,5 @@ public static ImmutableArray<IParameterSymbol> RenameParameters(this IList<IPara
return result.ToImmutableAndFree();
}
private static bool ShouldKeepAttribute(this AttributeData attributeData, INamedTypeSymbol containingType)
=> attributeData.AttributeClass.IsAccessibleWithin(containingType);
}
}
......@@ -37,16 +37,11 @@ public static IPropertySymbol RenameParameters(this IPropertySymbol property, IL
property.IsIndexer);
}
public static IPropertySymbol RemoveAttributeFromParameters(
this IPropertySymbol property, INamedTypeSymbol?[]? attributesToRemove)
public static IPropertySymbol RemoveInaccessibleAttributesAndAttributesOfTypes(
this IPropertySymbol property, ISymbol accessibleWithin, params INamedTypeSymbol[] attributesToRemove)
{
if (attributesToRemove == null)
{
return property;
}
bool shouldRemoveAttribute(AttributeData a) =>
attributesToRemove.Any(attr => attr?.Equals(a.AttributeClass) ?? false);
attributesToRemove.Any(attr => attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
var someParameterHasAttribute = property.Parameters
.Any(p => p.GetAttributes().Any(shouldRemoveAttribute));
......
......@@ -426,7 +426,7 @@ private static SyntaxNode CreateNewArgumentNullException(SyntaxGenerator factory
accessibility: overriddenProperty.ComputeResultantAccessibility(containingType),
modifiers: modifiers,
name: overriddenProperty.Name,
parameters: overriddenProperty.Parameters.WithAttributesToBeCopied(containingType),
parameters: overriddenProperty.RemoveInaccessibleAttributesAndAttributesOfTypes(containingType).Parameters,
isIndexer: overriddenProperty.IsIndexer(),
getMethod: accessorGet,
setMethod: accessorSet);
......@@ -525,10 +525,9 @@ private static DeclarationModifiers GetOverrideModifiers(ISymbol symbol)
}
return CodeGenerationSymbolFactory.CreateMethodSymbol(
method: overriddenMethod,
method: overriddenMethod.RemoveInaccessibleAttributesAndAttributesOfTypes(newContainingType),
accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
modifiers: modifiers,
parameters: overriddenMethod.Parameters.WithAttributesToBeCopied(newContainingType),
statements: overriddenMethod.ReturnsVoid
? ImmutableArray.Create(codeFactory.ExpressionStatement(body))
: ImmutableArray.Create(codeFactory.ReturnStatement(body)));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册