From c6df367082875cb22216fcc53f27ac71ef56ee66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Slupik?= Date: Thu, 19 Nov 2020 19:25:20 -0500 Subject: [PATCH] Fix unused open type declaration detection --- src/fsharp/service/ServiceAnalysis.fs | 7 +-- .../tests/UnitTests/UnusedOpensTests.fs | 47 ++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/fsharp/service/ServiceAnalysis.fs b/src/fsharp/service/ServiceAnalysis.fs index f549065fc1c..71dbdce532a 100644 --- a/src/fsharp/service/ServiceAnalysis.fs +++ b/src/fsharp/service/ServiceAnalysis.fs @@ -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 @@ -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) diff --git a/vsintegration/tests/UnitTests/UnusedOpensTests.fs b/vsintegration/tests/UnitTests/UnusedOpensTests.fs index 28738bfe1cb..58ba173e4ca 100644 --- a/vsintegration/tests/UnitTests/UnusedOpensTests.fs +++ b/vsintegration/tests/UnitTests/UnusedOpensTests.fs @@ -808,4 +808,49 @@ open Nested let _ = f 1 """ - => [] \ No newline at end of file + => [] + +[] +let ``used open C# type``() = + """ +open type System.Console + +WriteLine("Hello World") + """ + => [] + +[] +let ``unused open C# type``() = + """ +open type System.Console + +printfn "%s" "Hello World" + """ + => [2, (10, 24)] + +[] +let ``used open type from module``() = + """ +module MyModule = + type Thingy = + static member Thing = () + +open type MyModule.Thingy + +printfn "%A" Thing + """ + => [] + +[] +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)] +