Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue trying to generate fields/properties from a top level program #76161

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Fix issue trying to generate fields/properties from a top level program
  • Loading branch information
CyrusNajmabadi committed Nov 29, 2024
commit 5e055ea6b477a6937bde780a33ea1e23d8fc66ce
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
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; }
}
""");
}
}
Loading