NoUnusedExports: Do not report tests or ReviewConfig.config

This commit is contained in:
Jeroen Engels 2020-01-15 15:35:57 +01:00
parent 691a22e5a7
commit eee29d7963
2 changed files with 75 additions and 15 deletions

View File

@ -111,7 +111,7 @@ type alias ModuleContext =
, exposesEverything : Bool
, exposed : Dict String { range : Range, exposedElement : ExposedElement }
, used : Set ( ModuleName, String )
, typesNotToReport : Set String
, elementsNotToReport : Set String
}
@ -130,7 +130,7 @@ fromGlobalToModule fileKey moduleName globalContext =
, exposesEverything = False
, exposed = Dict.empty
, used = Set.empty
, typesNotToReport = Set.empty
, elementsNotToReport = Set.empty
}
@ -145,7 +145,7 @@ fromModuleToGlobal fileKey moduleName moduleContext =
, exposed = moduleContext.exposed
}
, used =
moduleContext.typesNotToReport
moduleContext.elementsNotToReport
|> Set.map (Tuple.pair <| Node.value moduleName)
|> Set.union moduleContext.used
}
@ -162,11 +162,12 @@ foldGlobalContexts newContext previousContext =
registerAsUsed : ( ModuleName, String ) -> ModuleContext -> ModuleContext
registerAsUsed ( moduleName, name ) moduleContext =
if moduleName /= [] then
{ moduleContext | used = Set.insert ( moduleName, name ) moduleContext.used }
{ moduleContext | used = Set.insert ( moduleName, name ) moduleContext.used }
else
moduleContext
registerMultipleAsUsed : List ( ModuleName, String ) -> ModuleContext -> ModuleContext
registerMultipleAsUsed usedElements moduleContext =
{ moduleContext | used = Set.union (Set.fromList usedElements) moduleContext.used }
@ -212,6 +213,7 @@ finalEvaluationForProject globalContext =
(\( moduleName, { fileKey, exposed } ) ->
exposed
|> removeApplicationExceptions globalContext moduleName
|> removeReviewConfig moduleName
|> Dict.filter (\name _ -> not <| Set.member ( moduleName, name ) globalContext.used)
|> Dict.toList
|> List.map
@ -258,6 +260,15 @@ removeApplicationExceptions globalContext moduleName dict =
dict
removeReviewConfig : ModuleName -> Dict String a -> Dict String a
removeReviewConfig moduleName dict =
if moduleName == [ "ReviewConfig" ] then
Dict.remove "config" dict
else
dict
-- MODULE DEFINITION VISITOR
@ -311,26 +322,31 @@ declarationListVisitor declarations moduleContext =
declarations
|> List.map (typesUsedInDeclaration moduleContext)
testFunctions : List String
testFunctions =
declarations
|> List.filterMap (testFunctionName moduleContext.scope)
allUsedTypes : List ( ModuleName, String )
allUsedTypes =
typesUsedInDeclaration_
|> List.concatMap Tuple.first
contextWithUsedTypes : ModuleContext
contextWithUsedTypes =
List.foldl registerAsUsed moduleContext allUsedTypes
contextWithUsedElements : ModuleContext
contextWithUsedElements =
registerMultipleAsUsed allUsedTypes moduleContext
in
( []
, { contextWithUsedTypes
, { contextWithUsedElements
| exposed =
contextWithUsedTypes.exposed
contextWithUsedElements.exposed
|> (if moduleContext.exposesEverything then
identity
else
Dict.filter (\name _ -> Set.member name declaredNames)
)
, typesNotToReport =
, elementsNotToReport =
typesUsedInDeclaration_
|> List.concatMap
(\( list, comesFromCustomTypeWithHiddenConstructors ) ->
@ -341,6 +357,7 @@ declarationListVisitor declarations moduleContext =
List.filter (\( moduleName, name ) -> isType name && moduleName == []) list
)
|> List.map Tuple.second
|> List.append testFunctions
|> Set.fromList
}
)
@ -382,6 +399,33 @@ declarationName declaration =
Nothing
testFunctionName : Scope.ModuleContext -> Node Declaration -> Maybe String
testFunctionName scope declaration =
case Node.value declaration of
Declaration.FunctionDeclaration function ->
case
function.signature
|> Maybe.map (Node.value >> .typeAnnotation >> Node.value)
of
Just (TypeAnnotation.Typed (Node _ ( moduleName, name )) _) ->
case Scope.realFunctionOrType moduleName name scope of
( [ "Test" ], "Test" ) ->
function.declaration
|> Node.value
|> .name
|> Node.value
|> Just
_ ->
Nothing
_ ->
Nothing
_ ->
Nothing
typesUsedInDeclaration : ModuleContext -> Node Declaration -> ( List ( ModuleName, String ), Bool )
typesUsedInDeclaration moduleContext declaration =
case Node.value declaration of

View File

@ -69,8 +69,6 @@ all =
, typeAliasesTests
-- TODO Add tests that report exposing the type's variants if they are never used.
-- TODO Add tests to add exceptions to tests
-- TODO Add tests to add exceptions for ReviewConfig.config (for applications only)
]
@ -192,6 +190,24 @@ main = text ""
"""
module A exposing (b)
a = 1
"""
|> Review.Test.runWithProjectData package_ rule
|> Review.Test.expectNoErrors
, test "should not report exposed tests" <|
\() ->
"""
module ThingTest exposing (a)
import Test exposing (Test)
a : Test
a = Test.describe "thing" []
"""
|> Review.Test.runWithProjectData package_ rule
|> Review.Test.expectNoErrors
, test "should not ReviewConfig.config" <|
\() ->
"""
module ReviewConfig exposing (config)
config = []
"""
|> Review.Test.runWithProjectData package_ rule
|> Review.Test.expectNoErrors