Skip to content

Commit

Permalink
Fix issue trying to generate fields/properties from a top level progr…
Browse files Browse the repository at this point in the history
…am (#76161)

Fixes #58491
  • Loading branch information
CyrusNajmabadi authored Dec 2, 2024
2 parents 6b87d0e + 5e055ea commit 8639171
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -13,7 +11,6 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeFixes.GenerateMember;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.GenerateMember.GenerateVariable;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand All @@ -40,7 +37,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
protected override bool IsCandidate(SyntaxNode node, SyntaxToken token, Diagnostic diagnostic)
=> node is SimpleNameSyntax or PropertyDeclarationSyntax or MemberBindingExpressionSyntax;

protected override SyntaxNode GetTargetNode(SyntaxNode node)
protected override SyntaxNode? GetTargetNode(SyntaxNode node)
{
if (node.IsKind(SyntaxKind.MemberBindingExpression))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ private async Task<bool> TryInitializeAsync(

internal bool CanGeneratePropertyOrField()
{
return ContainingType is { IsImplicitClass: false }
&& ContainingType.GetMembers(WellKnownMemberNames.TopLevelStatementsEntryPointMethodName).IsEmpty;
return this.TypeToGenerateIn is { IsImplicitClass: false }
&& TypeToGenerateIn.GetMembers(WellKnownMemberNames.TopLevelStatementsEntryPointMethodName).IsEmpty;
}

internal bool CanGenerateLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddParameter;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand All @@ -24,10 +21,6 @@ internal abstract partial class AbstractGenerateVariableService<TService, TSimpl
where TSimpleNameSyntax : TExpressionSyntax
where TExpressionSyntax : SyntaxNode
{
protected AbstractGenerateVariableService()
{
}

protected abstract bool IsExplicitInterfaceGeneration(SyntaxNode node);
protected abstract bool IsIdentifierNameGeneration(SyntaxNode node);

Expand All @@ -47,9 +40,7 @@ public async Task<ImmutableArray<CodeAction>> GenerateVariableAsync(

var state = await State.GenerateAsync((TService)this, semanticDocument, node, cancellationToken).ConfigureAwait(false);
if (state == null)
{
return [];
}

using var _ = ArrayBuilder<CodeAction>.GetInstance(out var actions);

Expand Down
27 changes: 21 additions & 6 deletions src/Features/CSharpTest/GenerateVariable/GenerateVariableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.GenerateVariable;

[Trait(Traits.Feature, Traits.Features.CodeActionsGenerateVariable)]
public class GenerateVariableTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor
public sealed class GenerateVariableTests(ITestOutputHelper logger)
: AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest_NoEditor(logger)
{
private const int FieldIndex = 0;
private const int ReadonlyFieldIndex = 1;
Expand All @@ -30,11 +31,6 @@ public class GenerateVariableTests : AbstractCSharpDiagnosticProviderBasedUserDi
private const int Parameter = 4;
private const int ParameterAndOverrides = 5;

public GenerateVariableTests(ITestOutputHelper logger)
: base(logger)
{
}

internal override (DiagnosticAnalyzer?, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (null, new CSharpGenerateVariableCodeFixProvider());

Expand Down Expand Up @@ -11135,4 +11131,23 @@ void M()
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/58491")]
public async Task TestGeneratePropertiesFromTopLevel()
{
await TestInRegularAndScriptAsync(
"""
var x = new Test() { [|A|] = 1, B = 1 };
class Test
{
}
""",
"""
var x = new Test() { A = 1, B = 1 };
class Test
{
public int A { get; internal set; }
}
""");
}
}

0 comments on commit 8639171

Please sign in to comment.