1module Docs.Check (check) where
3import qualified Data.List as List
4import qualified Data.Map as Map
5import qualified Data.Set as Set
6import qualified Data.Traversable as T
7import Text.Parsec ((<|>), choice, many, setPosition, string)
8import Text.Parsec.Pos (newPos)
10import qualified AST.Variable as Var
11import qualified Docs.AST as Docs
12import qualified Elm.Compiler.Type as Type
13import Elm.Utils ((|>))
15 ( addLocation, anyUntil, commaSep1, iParse, parens, simpleNewline, symOp
18import qualified Reporting.Annotation as A
19import qualified Reporting.Error.Docs as Error
20import qualified Reporting.Error.Helpers as Error (nearbyNames)
21import qualified Reporting.Region as R
22import qualified Reporting.Result as R
25type Result w a = R.Result w Error.Error a
30check :: [Var.Value] -> A.Located (Maybe Docs.Centralized) -> Result w Docs.Checked
31check exports (A.A region maybeDocs) =
34 R.throw region Error.NoDocs
37 checkHelp region exports docs
40checkHelp :: R.Region -> [Var.Value] -> Docs.Centralized -> Result w Docs.Checked
41checkHelp region exports (Docs.Docs comment aliases types values) =
44 commentedNames region comment
47 Set.fromList (map A.drop docNames)
49 checkCategory checkValue dict =
51 |> Map.filterWithKey (\key _ -> Set.member key docSet)
52 |> Map.mapWithKey checkValue
56 map (\(tipe, Var.Listing tags _) -> (tipe, tags)) (Var.getUnions exports)
58 (\() -> Docs.Docs comment)
59 <$> checkModuleComment region exports docNames
60 <*> checkCategory (checkComment Docs.aliasComment) aliases
61 <*> checkCategory (checkUnion exportedUnions) types
62 <*> checkCategory checkValue values
65checkComment :: (a -> Maybe String) -> String -> A.Located a -> Result w (A.Located a)
66checkComment getComment name (A.A region value) =
67 case getComment value of
69 R.throw region (Error.NoComment name)
72 R.ok (A.A region value)
75checkUnion :: [(String, [String])] -> String -> A.Located Docs.Union -> Result w (A.Located Docs.Union)
76checkUnion exportedUnions name value =
78 <$> filterUnionTags exportedUnions name value
79 <*> checkComment Docs.unionComment name value
82filterUnionTags :: [(String, [String])] -> String -> A.Located Docs.Union -> Result w (A.Located Docs.Union)
83filterUnionTags exportedUnions name (A.A region union@(Docs.Union _ _ ctors)) =
86 maybe [] id (List.lookup name exportedUnions)
88 R.ok $ A.A region $ union
90 filter (\(tag, _) -> elem tag exportedTags) ctors
95checkValue :: String -> A.Located (Docs.Value (Maybe Type.Type)) -> Result w (A.Located (Docs.Value Type.Type))
96checkValue name value =
98 <$> hasType name value
99 <*> checkComment Docs.valueComment name value
102hasType :: String -> A.Located (Docs.Value (Maybe Type.Type)) -> Result w (A.Located (Docs.Value Type.Type))
103hasType name (A.A region value) =
104 case Docs.valueType value of
106 R.ok (A.A region (value { Docs.valueType = tipe }))
109 R.throw region (Error.NoType name)
112-- CHECK MODULE COMMENT
114checkModuleComment :: R.Region -> [Var.Value] -> [A.Located String] -> Result w ()
115checkModuleComment docRegion exports locatedDocNames =
118 map valueName exports
121 Map.fromList (map (\name -> (name, ())) exportNames)
124 Map.fromList (map (\(A.A region name) -> (name, region)) locatedDocNames)
126 extraNameError (name, region) =
127 R.throw region (Error.OnlyInDocs name (Error.nearbyNames id name exportNames))
130 Map.difference docDict exportDict
132 |> T.traverse extraNameError
135 Map.difference exportDict docDict
140 if null namesOnlyInExports then
143 R.throw docRegion (Error.OnlyInExports namesOnlyInExports)
150commentedNames :: R.Region -> String -> [A.Located String]
151commentedNames (R.Region start _) comment =
157 commaSep1 (addLocation (var <|> parens symOp))
158 , do anyUntil simpleNewline
163 do setPosition (newPos "docs" (R.line start) (R.column start + 3))
164 concat <$> many nameParser
166 case iParse docCommentParser comment of
174valueName :: Var.Value -> String
177 Var.Value name -> name
178 Var.Alias name -> name
179 Var.Union name _ -> name
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list