WIP Mark custom type constructors from other modules as used.

This commit is contained in:
Jeroen Engels 2020-03-12 19:19:06 +01:00
parent 06f46e4690
commit 36b71557da
2 changed files with 81 additions and 15 deletions

View File

@ -86,16 +86,32 @@ rule =
}
|> Scope.addProjectVisitors
|> Rule.withElmJsonProjectVisitor elmJsonVisitor
|> Rule.withFinalProjectEvaluation finalProjectEvaluation
|> Rule.fromProjectRuleSchema
-- MODULE VISITOR
moduleVisitor : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext
moduleVisitor schema =
schema
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withDeclarationListVisitor declarationListVisitor
|> Rule.withDeclarationVisitor declarationVisitor
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.withFinalModuleEvaluation finalModuleEvaluation
-- CONTEXT
type alias ProjectContext =
{ scope : Scope.ProjectContext
, exposedModules : Set String
, exposedConstructors : Dict String { moduleKey : Rule.ModuleKey, constructors : Dict String (Node String) }
}
@ -114,6 +130,7 @@ initProjectContext : ProjectContext
initProjectContext =
{ scope = Scope.initProjectContext
, exposedModules = Set.empty
, exposedConstructors = Dict.empty
}
@ -133,6 +150,9 @@ fromModuleToProject : Rule.ModuleKey -> Node ModuleName -> ModuleContext -> Proj
fromModuleToProject _ moduleName moduleContext =
{ scope = Scope.fromModuleToProject moduleName moduleContext.scope
, exposedModules = Set.empty
-- TODO
, exposedConstructors = Dict.empty
}
@ -140,6 +160,9 @@ foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts newContext previousContext =
{ scope = Scope.foldProjectContexts previousContext.scope newContext.scope
, exposedModules = previousContext.exposedModules
-- TODO
, exposedConstructors = previousContext.exposedConstructors
}
@ -190,21 +213,6 @@ elmJsonVisitor maybeElmJson projectContext =
-- MODULE VISITOR
moduleVisitor : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ModuleContext
moduleVisitor schema =
schema
|> Rule.withModuleDefinitionVisitor (\_ context -> ( [], context ))
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withDeclarationListVisitor declarationListVisitor
|> Rule.withDeclarationVisitor declarationVisitor
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.withFinalModuleEvaluation finalModuleEvaluation
-- MODULE DEFINITION VISITOR
@ -358,6 +366,7 @@ expressionVisitor node direction context =
finalModuleEvaluation : ModuleContext -> List Error
finalModuleEvaluation context =
-- TODO Turn this into a finalProjectEvaluation
if context.exposesEverything && context.isExposed then
[]
@ -374,6 +383,43 @@ finalModuleEvaluation context =
)
constructorsToWarnAbout : ModuleContext -> Dict String (Dict String (Node String))
constructorsToWarnAbout context =
Dict.empty
a = )
-- FINAL PROJECT EVALUATION
finalProjectEvaluation : ProjectContext -> List Error
finalProjectEvaluation context =
-- let
-- _ =
-- context.exposedConstructors
-- |> Dict.filter (\moduleName { moduleKey, constructors, exposedConstructors } -> True)
-- in
-- TODO Turn this into a finalProjectEvaluation
-- if context.exposesEverything && context.isExposed then
-- []
--
-- else
-- context.declaredTypesWithConstructors
-- |> Dict.filter (\customTypeName _ -> not (context.isExposed && Set.member customTypeName context.exposedCustomTypesWithConstructors))
-- |> Dict.values
-- |> List.concatMap
-- (\dict ->
-- dict
-- |> Dict.filter (\name _ -> not (Set.member name context.usedFunctionOrValues || (context.isExposed && Set.member name context.exposedCustomTypesWithConstructors)))
-- |> Dict.toList
-- |> List.map (\( _, node ) -> error node)
-- )
[]
-- TYPE ANNOTATION UTILITARY FUNCTIONS

View File

@ -95,6 +95,7 @@ all =
, phantomTypeTests "package project" packageProject
, phantomTypeTests "application project" applicationProject
, crossModuleTests
, usingConstructorsFromOtherModules "package project" packageProject
]
@ -402,3 +403,22 @@ type Foo = Bar | Baz
}
]
]
usingConstructorsFromOtherModules : String -> Project -> Test
usingConstructorsFromOtherModules typeOfProject project =
Test.skip <|
describe ("Using constructors from others modules (" ++ typeOfProject ++ ")")
[ test "should not report type constructors used in other files when module is exposing the constructors of that type (qualifed import)" <|
\() ->
[ """
module MyModule exposing (Foo(..))
type Foo = Bar | Baz
""", """
module OtherModule exposing (a)
import MyModule
a = [ MyModule.Bar, MyModule.Baz ]
""" ]
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
]