2-- This module implements a simple linting pass on the PureScript AST.
4module Language.PureScript.Linter (lint, module L) where
8import Control.Monad.Writer.Class
10import Data.List (nub, (\\))
11import Data.Maybe (mapMaybe)
13import qualified Data.Set as S
15import Language.PureScript.AST
16import Language.PureScript.Crash
17import Language.PureScript.Errors
18import Language.PureScript.Linter.Exhaustive as L
19import Language.PureScript.Linter.Imports as L
20import Language.PureScript.Names
21import Language.PureScript.Types
23-- | Lint the PureScript AST.
25-- | Right now, this pass only performs a shadowing check.
26lint :: forall m. (MonadWriter MultipleErrors m) => Module -> m ()
27lint (Module _ _ mn ds _) = censor (addHint (ErrorInModule mn)) $ mapM_ lintDeclaration ds
29 moduleNames :: S.Set Ident
30 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
32 getDeclIdent :: Declaration -> Maybe Ident
33 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
34 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
35 getDeclIdent (ExternDeclaration ident _) = Just ident
36 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
37 getDeclIdent (BindingGroupDeclaration _) = internalError "lint: binding groups should not be desugared yet."
38 getDeclIdent _ = Nothing
40 lintDeclaration :: Declaration -> m ()
41 lintDeclaration = tell . f
43 (warningsInDecl, _, _, _, _) = everythingWithScope (\_ _ -> mempty) stepE stepB (\_ _ -> mempty) stepDo
45 f :: Declaration -> MultipleErrors
46 f (PositionedDeclaration pos _ dec) = addHint (PositionedError pos) (f dec)
47 f dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec)
48 f (TypeDeclaration name ty) = addHint (ErrorInTypeDeclaration name) (checkTypeVars ty)
49 f dec = warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec
51 stepE :: S.Set Ident -> Expr -> MultipleErrors
52 stepE s (Abs (Left name) _) | name `S.member` s = errorMessage (ShadowedName name)
53 stepE s (Let ds' _) = foldMap go ds'
55 go d | Just i <- getDeclIdent d
56 , i `S.member` s = errorMessage (ShadowedName i)
60 stepB :: S.Set Ident -> Binder -> MultipleErrors
61 stepB s (VarBinder name) | name `S.member` s = errorMessage (ShadowedName name)
62 stepB s (NamedBinder name _) | name `S.member` s = errorMessage (ShadowedName name)
65 stepDo :: S.Set Ident -> DoNotationElement -> MultipleErrors
66 stepDo s (DoNotationLet ds') = foldMap go ds'
68 go d | Just i <- getDeclIdent d
69 , i `S.member` s = errorMessage (ShadowedName i)
73 checkTypeVarsInDecl :: Declaration -> MultipleErrors
74 checkTypeVarsInDecl d = let (f, _, _, _, _) = accumTypes checkTypeVars in f d
76 checkTypeVars :: Type -> MultipleErrors
77 checkTypeVars ty = everythingWithContextOnTypes S.empty mempty mappend step ty <> findUnused ty
79 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
80 step s (ForAll tv _ _) = bindVar s tv
81 step s _ = (s, mempty)
82 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
83 bindVar = bind ShadowedTypeVar
84 findUnused :: Type -> MultipleErrors
86 let used = usedTypeVariables ty'
87 declared = everythingOnTypes (++) go ty'
88 unused = nub declared \\ nub used
89 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
91 go :: Type -> [String]
92 go (ForAll tv _ _) = [tv]
95 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
96 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
97 | otherwise = (S.insert name s, mempty)
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