Have Review.Project contain the modules of the project

This commit is contained in:
Jeroen Engels 2020-01-09 22:21:01 +01:00
parent d8df52aa52
commit ca0d3bc4e6
2 changed files with 65 additions and 15 deletions

View File

@ -1,7 +1,7 @@
module Review.Project exposing
( Project, ElmJson
, elmJson, modules
, new, withElmJson, withDependency
, modules, elmJson, dependencyModules
, new, withFile, withElmJson, withDependency
)
{-| Represents project-related data, that a rule can access to get more information.
@ -21,18 +21,22 @@ ignore it if you just want to write a review rule.
# Access
@docs elmJson, modules
@docs modules, elmJson, dependencyModules
# Build
@docs new, withElmJson, withDependency
@docs new, withFile, withElmJson, withDependency
-}
import Dict exposing (Dict)
import Elm.Docs
import Elm.Parser as Parser
import Elm.Processing
import Elm.Project
import Elm.Syntax.File exposing (File)
import Review.File exposing (ParsedFile)
@ -44,8 +48,10 @@ the `elm.json` file.
-}
type Project
= Project
{ elmJson : Maybe ElmJson
, modules : Dict String Elm.Docs.Module
{ modules : List ParsedFile
, filesThatFailedToParse : List String
, elmJson : Maybe ElmJson
, dependencyModules : Dict String Elm.Docs.Module
, moduleToDependency : Dict String String
}
@ -61,6 +67,13 @@ type alias ElmJson =
-- ACCESS
{-| Get the list of modules in the project.
-}
modules : Project -> List ParsedFile
modules (Project project) =
project.modules
{-| Get the contents of the `elm.json` file, if available.
This will give you a `Project` type from the
@ -82,9 +95,9 @@ package, so you will need to install and use it to gain access to the
information inside the `elm.json` file.
-}
modules : Project -> Dict String Elm.Docs.Module
modules (Project project) =
project.modules
dependencyModules : Project -> Dict String Elm.Docs.Module
dependencyModules (Project project) =
project.dependencyModules
@ -96,12 +109,49 @@ modules (Project project) =
new : Project
new =
Project
{ elmJson = Nothing
, modules = Dict.empty
{ modules = []
, filesThatFailedToParse = []
, elmJson = Nothing
, dependencyModules = Dict.empty
, moduleToDependency = Dict.empty
}
{-| Add the content of the `elm.json` file to the project details, making it
available for rules to access using
[`Review.Rule.withElmJsonVisitor`](./Review-Rule#withElmJsonVisitor).
-}
withFile : { path : String, source : String } -> Project -> Project
withFile { path, source } (Project project) =
case parseSource source of
Ok ast ->
Project
{ project
| modules =
{ path = path
, source = source
, ast = ast
}
:: project.modules
}
Err _ ->
Project { project | filesThatFailedToParse = path :: project.filesThatFailedToParse }
{-| Parse source code into a AST
-}
parseSource : String -> Result () File
parseSource source =
source
|> Parser.parse
|> Result.mapError (always ())
-- TODO Add the dependencies to fix how files will be parsed
-- Note: If the dependencies change, we'll need to reprocess everything, just in case.
-- Also, invalidate the cache for all the files.
|> Result.map (Elm.Processing.process Elm.Processing.init)
{-| Add the content of the `elm.json` file to the project details, making it
available for rules to access using
[`Review.Rule.withElmJsonVisitor`](./Review-Rule#withElmJsonVisitor).
@ -124,11 +174,11 @@ withDependency : { r | packageName : String, modules : List Elm.Docs.Module } ->
withDependency dependency (Project project) =
Project
{ project
| modules =
| dependencyModules =
dependency.modules
|> List.map (\module_ -> ( module_.name, module_ ))
|> Dict.fromList
|> Dict.union project.modules
|> Dict.union project.dependencyModules
, moduleToDependency =
dependency.modules
|> List.map (\module_ -> ( module_.name, dependency.packageName ))

View File

@ -495,7 +495,7 @@ computeErrors (Schema schema) project =
initialContext =
schema.initialContext
|> accumulateContext schema.elmJsonVisitors (Review.Project.elmJson project)
|> accumulateContext schema.dependenciesVisitors (Review.Project.modules project)
|> accumulateContext schema.dependenciesVisitors (Review.Project.dependencyModules project)
declarationVisitors : InAndOut (DirectedVisitor Declaration context)
declarationVisitors =
@ -654,7 +654,7 @@ allFilesInParallelTraversal (MultiSchema schema) startCache project =
initialContext =
schema.context.initGlobalContext
|> accumulateContext schema.elmJsonVisitors (Review.Project.elmJson project)
|> accumulateContext schema.dependenciesVisitors (Review.Project.modules project)
|> accumulateContext schema.dependenciesVisitors (Review.Project.dependencyModules project)
in
\files ->
let