Skip to content

Fix a bug in the Add Open code fix #15998

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
Sep 19, 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 @@ -55,21 +55,26 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr

// attribute, shouldn't be here
| line when line.StartsWith "[<" && line.EndsWith ">]" ->
let moduleDeclLineNumber =
let moduleDeclLineNumberOpt =
sourceText.Lines
|> Seq.skip insertionLineNumber
|> Seq.findIndex (fun line -> line.ToString().Contains "module")
// add back the skipped lines
|> fun i -> insertionLineNumber + i
|> Seq.tryFindIndex (fun line -> line.ToString().Contains "module")

let moduleDeclLineText = sourceText.Lines[ moduleDeclLineNumber ].ToString().Trim()
match moduleDeclLineNumberOpt with
// implicit top level module
| None -> insertionLineNumber, $"{margin}open {ns}{br}{br}"
// explicit top level module
| Some number ->
// add back the skipped lines
let moduleDeclLineNumber = insertionLineNumber + number
let moduleDeclLineText = sourceText.Lines[ moduleDeclLineNumber ].ToString().Trim()

if moduleDeclLineText.EndsWith "=" then
insertionLineNumber, $"{margin}open {ns}{br}{br}"
else
moduleDeclLineNumber + 2, $"{margin}open {ns}{br}{br}"
if moduleDeclLineText.EndsWith "=" then
insertionLineNumber, $"{margin}open {ns}{br}{br}"
else
moduleDeclLineNumber + 2, $"{margin}open {ns}{br}{br}"

// something else, shot in the dark
// implicit top level module
| _ -> insertionLineNumber, $"{margin}open {ns}{br}{br}"

| ScopeKind.Namespace -> insertionLineNumber + 3, $"{margin}open {ns}{br}{br}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ let ``Fixes FS0039 for missing opens - nested module`` () =
Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - module has attributes`` () =
let ``Fixes FS0039 for missing opens - explicit module has attributes`` () =
let code =
"""
[<AutoOpen>]
Expand Down Expand Up @@ -187,6 +187,33 @@ Console.WriteLine 42

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - implicit module has attributes`` () =
let code =
"""
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""

let expected =
Some
{
Message = "open System"
FixedCode =
"""
open System

[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""
}

let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)

// TODO: the open statement should actually be within the module
[<Fact>]
let ``Fixes FS0039 for missing opens - nested module has attributes`` () =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module Module1 =
Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - module has attributes`` () =
let ``Fixes FS0039 for missing opens - explicit module has attributes`` () =
let code =
"""
[<AutoOpen>]
Expand Down Expand Up @@ -181,6 +181,33 @@ Console.WriteLine 42

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - implicit module has attributes`` () =
let code =
"""
[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""

let expected =
Some
{
Message = "open System"
FixedCode =
"""
open System

[<Obsolete>]
type MyType() =
let now = DateTime.Now
"""
}

let actual = codeFix |> tryFix code Auto

Assert.Equal(expected, actual)

[<Fact>]
let ``Fixes FS0039 for missing opens - nested module has attributes`` () =
let code =
Expand Down