Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -21,7 +22,7 @@ public class AddGeneratedComClassFixer : ConvertToSourceGeneratedInteropFixer

protected override string BaseEquivalenceKey => nameof(AddGeneratedComClassFixer);

private static Task AddGeneratedComClassAsync(DocumentEditor editor, SyntaxNode node)
private static async Task AddGeneratedComClassAsync(DocumentEditor editor, SyntaxNode node, CancellationToken ct)
{
editor.ReplaceNode(node, (node, gen) =>
{
Expand All @@ -37,12 +38,29 @@ private static Task AddGeneratedComClassAsync(DocumentEditor editor, SyntaxNode

MakeNodeParentsPartial(editor, node);

return Task.CompletedTask;
var declaringType = editor.SemanticModel.GetDeclaredSymbol(node, ct) as INamedTypeSymbol;
if (declaringType is not null)
{
var comVisibleAttributeType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.System_Runtime_InteropServices_ComVisibleAttribute);
if (comVisibleAttributeType is not null)
{
var comVisibleAttr = declaringType.GetAttributes().FirstOrDefault(attr =>
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, comVisibleAttributeType)
&& attr.ConstructorArguments.Length == 1
&& attr.ConstructorArguments[0].Value is true);

if (comVisibleAttr?.ApplicationSyntaxReference is { } syntaxRef)
{
var comVisibleAttrSyntax = await syntaxRef.GetSyntaxAsync(ct).ConfigureAwait(false);
editor.RemoveNode(comVisibleAttrSyntax);
Comment thread
jkoritzinsky marked this conversation as resolved.
Outdated
Comment thread
jkoritzinsky marked this conversation as resolved.
Outdated
}
}
}
}

protected override Func<DocumentEditor, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
{
return (editor, _) => AddGeneratedComClassAsync(editor, node);
return (editor, ct) => AddGeneratedComClassAsync(editor, node, ct);
}

protected override string GetDiagnosticTitle(ImmutableDictionary<string, Option> selectedOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,129 @@ partial class C : J
await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
}

[Fact]
public async Task TypeWithComVisibleTrue_RemovesComVisibleAttribute()
{
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

[ComVisible(true)]
class [|C|] : I
{
}
""";

string fixedSource = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

[GeneratedComClass]
partial class C : I
{
}
""";

await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
}

[Fact]
public async Task TypeWithComVisibleFalse_PreservesComVisibleAttribute()
{
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

[ComVisible(false)]
class [|C|] : I
{
}
""";

string fixedSource = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

[ComVisible(false)]
[GeneratedComClass]
partial class C : I
{
}
""";

await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
}

[Fact]
public async Task TypeWithComVisibleTrueOnSecondPartialDeclaration_RemovesComVisibleAttribute()
{
string source = """
using System.Runtime.InteropServices;
Comment thread
jkoritzinsky marked this conversation as resolved.
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

partial class [|C|] : I
{
}

[ComVisible(true)]
partial class C
{
}
""";

string fixedSource = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public partial interface I
{
}

[GeneratedComClass]
partial class C : I
{
}

partial class C
{
}
""";

await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
}

[Fact]
public async Task TypeThatInheritsFromGeneratedComClassType_ReportsDiagnostic()
{
Expand Down
Loading