Handle imports from other modules

This commit is contained in:
Jeroen Engels 2020-03-14 23:41:12 +01:00
parent abcd73f0e4
commit 68eda525b5
4 changed files with 58 additions and 38 deletions

View File

@ -403,16 +403,16 @@ registerUsedFunctionOrValue moduleName name moduleContext =
else
let
realModuleName : ModuleName
realModuleName : ModuleNameAsString
realModuleName =
Scope.realFunctionOrType moduleName name moduleContext.scope
|> Tuple.first
|> String.join "."
usedFunctionsOrValues : Dict ModuleNameAsString (Set ConstructorName)
usedFunctionsOrValues =
Dict.update
-- TODO Use Scope.reaflFunctionOrName
(String.join "." realModuleName)
realModuleName
(\maybeSet ->
case maybeSet of
Just set ->

View File

@ -589,7 +589,7 @@ runModuleRule ((ModuleRuleSchema schema) as moduleRuleSchema) previousCache exce
computeErrors_ : ProjectModule -> List Error
computeErrors_ =
computeErrors moduleRuleSchema project initialContext
computeErrors moduleRuleSchema initialContext
modulesToAnalyze : List ProjectModule
modulesToAnalyze =
@ -630,8 +630,8 @@ runModuleRule ((ModuleRuleSchema schema) as moduleRuleSchema) previousCache exce
)
computeErrors : ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } moduleContext -> Project -> moduleContext -> ProjectModule -> List Error
computeErrors (ModuleRuleSchema schema) project initialContext =
computeErrors : ModuleRuleSchema { anything | hasAtLeastOneVisitor : () } moduleContext -> moduleContext -> ProjectModule -> List Error
computeErrors (ModuleRuleSchema schema) initialContext =
let
declarationVisitors : InAndOut (DirectedVisitor Declaration moduleContext)
declarationVisitors =
@ -1016,24 +1016,22 @@ runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptio
let
graph : Graph ModuleName ()
graph =
project
|> Review.Project.moduleGraph
in
let
Review.Project.moduleGraph project
elmJsonData : Maybe { elmJsonKey : ElmJsonKey, project : Elm.Project.Project }
elmJsonData =
Review.Project.elmJson project
|> Maybe.map
(\elmJson ->
{ elmJsonKey = ElmJsonKey { path = elmJson.path, raw = elmJson.raw }
, project = elmJson.project
}
)
initialContext : projectContext
initialContext =
schema.context.initProjectContext
|> accumulateContext schema.elmJsonVisitors
(case Review.Project.elmJson project of
Just elmJson ->
Just
{ elmJsonKey = ElmJsonKey { path = elmJson.path, raw = elmJson.raw }
, project = elmJson.project
}
Nothing ->
Nothing
)
|> accumulateContext schema.elmJsonVisitors elmJsonData
|> accumulateContext schema.dependenciesVisitors (Review.Project.dependencies project)
modules : Dict ModuleName ProjectModule

View File

@ -88,7 +88,7 @@ type alias InnerModuleContext =
, dependenciesModules : Dict String Elm.Docs.Module
, modules : Dict ModuleName Elm.Docs.Module
, exposesEverything : Bool
, exposedNames : Dict String { range : Range, exposedElement : ExposedElement }
, exposedNames : Dict String Range
, exposedUnions : List Elm.Docs.Union
, exposedAliases : List Elm.Docs.Alias
, exposedValues : List Elm.Docs.Value
@ -96,10 +96,6 @@ type alias InnerModuleContext =
}
type ExposedElement
= Function
type alias Scope =
{ names : Dict String VariableInfo
, cases : List ( Node Expression, Dict String VariableInfo )
@ -468,9 +464,12 @@ registerExposed declaration innerContext =
| exposedUnions =
{ name = Node.value type_.name
, comment = ""
-- TODO
, args = []
, tags =
type_.constructors
-- TODO Constructor args?
|> List.map (\constructor -> ( Node.value (Node.value constructor).name, [] ))
}
:: innerContext.exposedUnions
@ -566,21 +565,20 @@ moduleDefinitionVisitor node innerContext =
{ innerContext | exposedNames = exposedElements list }
exposedElements : List (Node Exposing.TopLevelExpose) -> Dict String { range : Range, exposedElement : ExposedElement }
exposedElements : List (Node Exposing.TopLevelExpose) -> Dict String Range
exposedElements nodes =
nodes
|> List.filterMap
(\node ->
case Node.value node of
Exposing.FunctionExpose name ->
Just <| ( name, { range = Node.range node, exposedElement = Function } )
Just ( name, Node.range node )
Exposing.TypeOrAliasExpose name ->
Just <| ( name, { range = Node.range node, exposedElement = Function } )
Just ( name, Node.range node )
Exposing.TypeExpose { name } ->
-- TODO
Nothing
Just ( name, Node.range node )
Exposing.InfixExpose name ->
Nothing
@ -677,6 +675,7 @@ registerImportExposed import_ innerContext =
exposedValues : Dict String (List String)
exposedValues =
topLevelExposeList
-- TODO HERE We notice that the module has no tags or union, even though it should
|> List.concatMap (namesFromExposingList module_)
|> List.map (\name -> ( name, moduleName ))
|> Dict.fromList

View File

@ -420,19 +420,42 @@ a = [ MyModule.Bar, MyModule.Baz ]
""" ]
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
, Test.skip <|
test "should not report type constructors used in other files when module is exposing the constructors of that type (exposing the constructors)" <|
\() ->
[ """
, test "should not report type constructors used in other files when module is exposing the constructors of that type (qualified import, aliasing the module name)" <|
\() ->
[ """
module MyModule exposing (Foo(..))
type Foo = Bar | Baz
""", """
module OtherModule exposing (a)
import MyModule as M
a = [ M.Bar, M.Baz ]
""" ]
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
, test "should not report type constructors used in other files when module is exposing the constructors of that type (exposing the constructors)" <|
\() ->
[ """
module MyModule exposing (Foo(..))
type Foo = Bar | Baz
""", """
module OtherModule exposing (a)
import MyModule exposing (Foo(..))
a = [ MyModule.Bar, MyModule.Baz ]
a = [ Bar, Baz ]
""" ]
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
, test "should not report type constructors used in other files when module is exposing the constructors of that type (nested module name)" <|
\() ->
[ """
module My.Module exposing (Foo(..))
type Foo = Bar | Baz
""", """
module OtherModule exposing (a)
import My.Module
a = [ My.Module.Bar, My.Module.Baz ]
""" ]
|> Review.Test.runOnModulesWithProjectData project rule
|> Review.Test.expectNoErrors
-- TODO Handle aliasing of module (will need to use Scope)
-- TODO Handle phantom types use in other files