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

Disable use-expression-body for lambdas with PP directives in them #75261

Merged
merged 2 commits into from
Sep 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -112,6 +113,33 @@ internal static bool TryConvertToExpressionBody(
{
var body = declaration.Body as BlockSyntax;

return body.TryConvertToExpressionBody(languageVersion, conversionPreference, cancellationToken, out expression, out _);
if (!body.TryConvertToExpressionBody(languageVersion, conversionPreference, cancellationToken, out expression, out var semicolonToken))
return false;

// If we have directives, we have something like:
//
// X(c =>
// {
// #if DEBUG
// Y();
// #else
// Z();
// #endif
// });
//
// Converting this to an expression body is a little too complex for us to support currently. We'd have to grab
// out the parts of the #else/#elif blocks, grab out their expressions, and rewrite into a form like so:
//
// X(c =>
// #if DEBUG
// Y()
// #else
// Z()
// #endif
// );
if (semicolonToken.TrailingTrivia.Any(t => t.IsDirective))
return false;

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBody;

[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.UseExpressionBody), Shared]
[ExtensionOrder(Before = PredefinedCodeRefactoringProviderNames.ExtractClass)]
internal class UseExpressionBodyCodeRefactoringProvider : SyntaxEditorBasedCodeRefactoringProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class UseExpressionBodyCodeRefactoringProvider() : SyntaxEditorBasedCodeRefactoringProvider
{
private static readonly ImmutableArray<UseExpressionBodyHelper> _helpers = UseExpressionBodyHelper.Helpers;

private static readonly BidirectionalMap<(UseExpressionBodyHelper helper, bool useExpressionBody), string> s_equivalenceKeyMap
= CreateEquivalanceKeyMap(UseExpressionBodyHelper.Helpers);

[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public UseExpressionBodyCodeRefactoringProvider()
{
}

private static BidirectionalMap<(UseExpressionBodyHelper helper, bool useExpressionBody), string> CreateEquivalanceKeyMap(
ImmutableArray<UseExpressionBodyHelper> helpers)
{
Expand Down Expand Up @@ -127,9 +123,9 @@ private static bool TryComputeRefactoring(
context.RegisterRefactoring(
CodeAction.Create(
helper.UseExpressionBodyTitle.ToString(),
c => UpdateDocumentAsync(
cancellationToken => UpdateDocumentAsync(
document, root, declaration, helper,
useExpressionBody: true, cancellationToken: c),
useExpressionBody: true, cancellationToken),
s_equivalenceKeyMap[(helper, useExpressionBody: true)]),
declaration.Span);
succeeded = true;
Expand All @@ -140,9 +136,9 @@ private static bool TryComputeRefactoring(
context.RegisterRefactoring(
CodeAction.Create(
helper.UseBlockBodyTitle.ToString(),
c => UpdateDocumentAsync(
cancellationToken => UpdateDocumentAsync(
document, root, declaration, helper,
useExpressionBody: false, cancellationToken: c),
useExpressionBody: false, cancellationToken),
s_equivalenceKeyMap[(helper, useExpressionBody: false)]),
declaration.Span);
succeeded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
namespace Microsoft.CodeAnalysis.CSharp.UseExpressionBodyForLambda;

[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.UseExpressionBodyForLambda), Shared]
internal sealed class UseExpressionBodyForLambdaCodeRefactoringProvider : CodeRefactoringProvider
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class UseExpressionBodyForLambdaCodeRefactoringProvider() : CodeRefactoringProvider
{
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
public UseExpressionBodyForLambdaCodeRefactoringProvider()
{
}

public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
var document = context.Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseExpressionBody;

[Trait(Traits.Feature, Traits.Features.CodeActionsUseExpressionBody)]
public class UseExpressionBodyForLambdasRefactoringTests : AbstractCSharpCodeActionTest_NoEditor
public sealed class UseExpressionBodyForLambdasRefactoringTests : AbstractCSharpCodeActionTest_NoEditor
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(TestWorkspace workspace, TestParameters parameters)
=> new UseExpressionBodyForLambdaCodeRefactoringProvider();
Expand Down Expand Up @@ -208,4 +209,20 @@ void Goo()
}
""", parameters: new TestParameters(options: UseExpressionBody));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74137")]
public async Task TestNotWithPreprocessorDirectives()
{
await TestMissingAsync(
"""
app.UseSwaggerUI(c [||]=>
{
#if DEBUG
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
#else
c.SwaggerEndpoint("/api/swagger/v1/swagger.json", "API V1");
#endif
});
""", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic));
}
}
Loading