-
Notifications
You must be signed in to change notification settings - Fork 830
Democratizing code fixes - part one #15373
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.Text | ||
|
|
||
| open CancellableTasks | ||
|
|
||
| type IFSharpCodeFix = | ||
| abstract member GetChangesAsync: document: Document -> span: TextSpan -> CancellableTask<string * TextChange list> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
vsintegration/tests/FSharp.Editor.Tests/CodeFixes/AddInstanceMemberParameterTests.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| module FSharp.Editor.Tests.CodeFixes.AddInstanceMemberParameterTests | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.Editor | ||
| open Xunit | ||
|
|
||
| open CodeFixTestFramework | ||
|
|
||
| let private codeFix = FSharpAddInstanceMemberParameterCodeFixProvider() | ||
| let private diagnostic = 0673 // This instance member needs a parameter to represent the object being invoked... | ||
|
|
||
| [<Fact>] | ||
| let ``Fixes FS0673`` () = | ||
| let code = | ||
| """ | ||
| type UsefulTestHarness() = | ||
| member FortyTwo = 42 | ||
| """ | ||
|
|
||
| let expected = | ||
| { | ||
| Title = "Add missing instance member parameter" | ||
| FixedCode = | ||
| """ | ||
| type UsefulTestHarness() = | ||
| member x.FortyTwo = 42 | ||
| """ | ||
| } | ||
|
|
||
| let actual = codeFix |> fix code diagnostic | ||
|
|
||
| Assert.Equal(expected, actual) |
66 changes: 66 additions & 0 deletions
66
vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ChangeToUpcastTests.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| module FSharp.Editor.Tests.CodeFixes.ChangeToUpcastTests | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.Editor | ||
| open Xunit | ||
|
|
||
| open CodeFixTestFramework | ||
|
|
||
| let private codeFix = FSharpChangeToUpcastCodeFixProvider() | ||
| let private diagnostic = 3198 // The conversion is an upcast, not a downcast... | ||
|
|
||
| // Test cases are taken from the original PR: | ||
| // https://github.com/dotnet/fsharp/pull/10463 | ||
|
|
||
| [<Fact>] | ||
| let ``Fixes FS3198 - operator`` () = | ||
| let code = | ||
| """ | ||
| type IFoo = abstract member Bar : unit -> unit | ||
| type Foo() = interface IFoo with member __.Bar () = () | ||
|
|
||
| let Thing : IFoo = Foo() :?> IFoo | ||
| """ | ||
|
|
||
| let expected = | ||
| { | ||
| Title = "Use ':>' operator" | ||
| FixedCode = | ||
| """ | ||
| type IFoo = abstract member Bar : unit -> unit | ||
| type Foo() = interface IFoo with member __.Bar () = () | ||
|
|
||
| let Thing : IFoo = Foo() :> IFoo | ||
| """ | ||
| } | ||
|
|
||
| let actual = codeFix |> fix code diagnostic | ||
|
|
||
| Assert.Equal(expected, actual) | ||
|
|
||
| [<Fact>] | ||
| let ``Fixes FS3198 - keyword`` () = | ||
| let code = | ||
| """ | ||
| type IFoo = abstract member Bar : unit -> unit | ||
| type Foo() = interface IFoo with member __.Bar () = () | ||
|
|
||
| let Thing : IFoo = downcast Foo() | ||
| """ | ||
|
|
||
| let expected = | ||
| { | ||
| Title = "Use 'upcast'" | ||
| FixedCode = | ||
| """ | ||
| type IFoo = abstract member Bar : unit -> unit | ||
| type Foo() = interface IFoo with member __.Bar () = () | ||
|
|
||
| let Thing : IFoo = upcast Foo() | ||
| """ | ||
| } | ||
|
|
||
| let actual = codeFix |> fix code diagnostic | ||
|
|
||
| Assert.Equal(expected, actual) |
46 changes: 46 additions & 0 deletions
46
vsintegration/tests/FSharp.Editor.Tests/CodeFixes/CodeFixTestFramework.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.CodeFixTestFramework | ||
|
|
||
| open System.Threading | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.Text | ||
| open Microsoft.VisualStudio.FSharp.Editor | ||
| open Microsoft.VisualStudio.FSharp.Editor.CancellableTasks | ||
|
|
||
| open FSharp.Editor.Tests.Helpers | ||
|
|
||
| type TestCodeFix = { Title: string; FixedCode: string } | ||
|
|
||
| let getRelevantDiagnostic (document: Document) errorNumber = | ||
| cancellableTask { | ||
| let! _, checkFileResults = document.GetFSharpParseAndCheckResultsAsync "test" | ||
|
|
||
| return | ||
| checkFileResults.Diagnostics | ||
| |> Seq.where (fun d -> d.ErrorNumber = errorNumber) | ||
| |> Seq.exactlyOne | ||
| } | ||
|
|
||
| let fix (code: string) diagnostic (fixProvider: IFSharpCodeFix) = | ||
| cancellableTask { | ||
| let sourceText = SourceText.From code | ||
| let document = RoslynTestHelpers.GetFsDocument code | ||
|
|
||
| let! diagnostic = getRelevantDiagnostic document diagnostic | ||
|
|
||
| let diagnosticSpan = | ||
| RoslynHelpers.FSharpRangeToTextSpan(sourceText, diagnostic.Range) | ||
|
|
||
| let! title, changes = fixProvider.GetChangesAsync document diagnosticSpan | ||
| let fixedSourceText = sourceText.WithChanges changes | ||
|
|
||
| return | ||
| { | ||
| Title = title | ||
| FixedCode = fixedSourceText.ToString() | ||
| } | ||
| } | ||
| |> CancellableTask.start CancellationToken.None | ||
| |> fun task -> task.Result |
31 changes: 31 additions & 0 deletions
31
vsintegration/tests/FSharp.Editor.Tests/CodeFixes/ConvertToAnonymousRecordTests.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| module FSharp.Editor.Tests.CodeFixes.ConvertToAnonymousRecordTests | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.Editor | ||
| open Xunit | ||
|
|
||
| open CodeFixTestFramework | ||
|
|
||
| let private codeFix = FSharpConvertToAnonymousRecordCodeFixProvider() | ||
| let private diagnostic = 0039 // The record label is not defined... | ||
|
|
||
| [<Fact>] | ||
| let ``Fixes FS0039`` () = | ||
| let code = | ||
| """ | ||
| let band = { Name = "The Velvet Underground" } | ||
| """ | ||
|
|
||
| let expected = | ||
| { | ||
| Title = "Convert to Anonymous Record" | ||
| FixedCode = | ||
| """ | ||
| let band = {| Name = "The Velvet Underground" |} | ||
| """ | ||
| } | ||
|
|
||
| let actual = codeFix |> fix code diagnostic | ||
|
|
||
| Assert.Equal(expected, actual) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.