Skip to content

Refactoring and tests for the ConvertCSharpLambdaToFSharpLambda code fix #15472

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

Merged
merged 3 commits into from
Jun 27, 2023
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 @@ -8,38 +8,57 @@ open System.Collections.Immutable
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes

open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text

open CancellableTasks

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = CodeFix.ConvertCSharpLambdaToFSharpLambda); Shared>]
type internal ConvertCSharpLambdaToFSharpLambdaCodeFixProvider [<ImportingConstructor>] () =
inherit CodeFixProvider()

static let title = SR.UseFSharpLambda()

override _.FixableDiagnosticIds = ImmutableArray.Create("FS0039", "FS0043")
let tryGetSpans (parseResults: FSharpParseFileResults) (range: range) sourceText =
let flatten3 options =
match options with
| Some (Some a, Some b, Some c) -> Some(a, b, c)
| _ -> None

parseResults.TryRangeOfParenEnclosingOpEqualsGreaterUsage range.Start
|> Option.map (fun (fullParenRange, lambdaArgRange, lambdaBodyRange) ->
RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, fullParenRange),
RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, lambdaArgRange),
RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, lambdaBodyRange))
|> flatten3

override _.FixableDiagnosticIds = ImmutableArray.Create("FS0039")

override _.RegisterCodeFixesAsync context =
asyncMaybe {
let! parseResults =
context.Document.GetFSharpParseResultsAsync(nameof (ConvertCSharpLambdaToFSharpLambdaCodeFixProvider))
|> liftAsync
override this.RegisterCodeFixesAsync context = context.RegisterFsharpFix(this)

let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
interface IFSharpCodeFixProvider with
member _.GetCodeFixIfAppliesAsync document span =
cancellableTask {
let! cancellationToken = CancellableTask.getCurrentCancellationToken ()

let errorRange =
RoslynHelpers.TextSpanToFSharpRange(context.Document.FilePath, context.Span, sourceText)
let! parseResults = document.GetFSharpParseResultsAsync(nameof (ConvertCSharpLambdaToFSharpLambdaCodeFixProvider))

let! fullParenRange, lambdaArgRange, lambdaBodyRange =
parseResults.TryRangeOfParenEnclosingOpEqualsGreaterUsage errorRange.Start
let! sourceText = document.GetTextAsync(cancellationToken)

let! fullParenSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, fullParenRange)
let! lambdaArgSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, lambdaArgRange)
let! lambdaBodySpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, lambdaBodyRange)
let errorRange =
RoslynHelpers.TextSpanToFSharpRange(document.FilePath, span, sourceText)

let replacement =
let argText = sourceText.GetSubText(lambdaArgSpan).ToString()
let bodyText = sourceText.GetSubText(lambdaBodySpan).ToString()
TextChange(fullParenSpan, "fun " + argText + " -> " + bodyText)
return
tryGetSpans parseResults errorRange sourceText
|> Option.map (fun (fullParenSpan, lambdaArgSpan, lambdaBodySpan) ->
let replacement =
let argText = sourceText.GetSubText(lambdaArgSpan).ToString()
let bodyText = sourceText.GetSubText(lambdaBodySpan).ToString()
TextChange(fullParenSpan, "fun " + argText + " -> " + bodyText)

do context.RegisterFsharpFix(CodeFix.ConvertCSharpLambdaToFSharpLambda, title, [| replacement |])
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
{
Name = CodeFix.ConvertCSharpLambdaToFSharpLambda
Message = title
Changes = [ replacement ]
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let getRelevantDiagnostic (document: Document) errorNumber =
return
checkFileResults.Diagnostics
|> Seq.where (fun d -> d.ErrorNumber = errorNumber)
|> Seq.exactlyOne
|> Seq.head
}

let tryFix (code: string) diagnostic (fixProvider: IFSharpCodeFixProvider) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

module FSharp.Editor.Tests.CodeFixes.ConvertCSharpLambdaToFSharpLambdaTests

open Microsoft.VisualStudio.FSharp.Editor
open Xunit

open CodeFixTestFramework

let private codeFix = ConvertCSharpLambdaToFSharpLambdaCodeFixProvider()

let private diagnostic = 0039 // Something is not defined

[<Fact>]
let ``Fixes FS0039 for lambdas`` () =
let code =
"""
let incAll = List.map (n => n + 1)
"""

let expected =
Some
{
Message = "Use F# lambda syntax"
FixedCode =
"""
let incAll = List.map (fun n -> n + 1)
"""
}

let actual = codeFix |> tryFix code diagnostic

Assert.Equal(expected, actual)

[<Fact>]
let ``Doesn't fix FS0039 for random undefined stuff`` () =
let code =
"""
let f = g
"""

let expected = None

let actual = codeFix |> tryFix code diagnostic

Assert.Equal(expected, actual)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="CodeFixes\ChangeToUpcastTests.fs" />
<Compile Include="CodeFixes\AddMissingRecToMutuallyRecFunctionsTests.fs" />
<Compile Include="CodeFixes\WrapExpressionInParenthesesTests.fs" />
<Compile Include="CodeFixes\ConvertCSharpLambdaToFSharpLambdaTests.fs" />
<Compile Include="CodeFixes\RemoveReturnOrYieldTests.fs" />
<Compile Include="Hints\HintTestFramework.fs" />
<Compile Include="Hints\OptionParserTests.fs" />
Expand Down