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 unused open type declaration detection #10510

Merged
merged 1 commit into from
Nov 20, 2020
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
7 changes: 4 additions & 3 deletions src/fsharp/service/ServiceAnalysis.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ module UnusedOpens =
yield! getModuleAndItsAutoOpens true ent |]
{ OpenedModules = getModuleAndItsAutoOpens false modul }

/// Represents single open statement.
/// Represents a single open statement
type OpenStatement =
{ /// All namespaces and modules which this open declaration effectively opens, including the AutoOpen ones
{ /// All namespaces, modules and types which this open declaration effectively opens, including the AutoOpen ones
OpenedGroups: OpenedModuleGroup list

/// The range of open statement itself
Expand All @@ -90,7 +90,8 @@ module UnusedOpens =
if firstId.idText = MangledGlobalName then
None
else
Some { OpenedGroups = openDecl.Modules |> List.map OpenedModuleGroup.Create
let openedModulesAndTypes = List.concat [openDecl.Modules; openDecl.Types |> List.map(fun ty -> ty.TypeDefinition)]
Some { OpenedGroups = openedModulesAndTypes |> List.map OpenedModuleGroup.Create
Range = range
AppliedScope = openDecl.AppliedScope }
| _ -> None)
Expand Down
47 changes: 46 additions & 1 deletion vsintegration/tests/UnitTests/UnusedOpensTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,49 @@ open Nested

let _ = f 1
"""
=> []
=> []

[<Test>]
let ``used open C# type``() =
"""
open type System.Console

WriteLine("Hello World")
"""
=> []

[<Test>]
let ``unused open C# type``() =
"""
open type System.Console

printfn "%s" "Hello World"
"""
=> [2, (10, 24)]

[<Test>]
let ``used open type from module``() =
"""
module MyModule =
type Thingy =
static member Thing = ()

open type MyModule.Thingy

printfn "%A" Thing
"""
=> []

[<Test>]
let ``unused open type from module``() =
"""
module MyModule =
type Thingy =
static member Thing = ()

open type MyModule.Thingy

printfn "%A" MyModule.Thingy.Thing
"""
=> [6, (10, 25)]