1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards #-}
6-- This module implements a simple linting pass on the PureScript AST.
8module Language.PureScript.Linter (lint, module L) where
13import Data.List (nub, (\\))
14import Data.Maybe (mapMaybe)
17import qualified Data.Set as S
19import Control.Monad.Writer.Class
21import Language.PureScript.Crash
22import Language.PureScript.AST
23import Language.PureScript.Names
24import Language.PureScript.Errors
25import Language.PureScript.Types
26import Language.PureScript.Linter.Exhaustive as L
27import Language.PureScript.Linter.Imports as L
29-- | Lint the PureScript AST.
31-- | Right now, this pass only performs a shadowing check.
32lint :: forall m. (MonadWriter MultipleErrors m) => Module -> m ()
33lint (Module _ _ mn ds _) = censor (addHint (ErrorInModule mn)) $ mapM_ lintDeclaration ds
35 moduleNames :: S.Set Ident
36 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
38 getDeclIdent :: Declaration -> Maybe Ident
39 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
40 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
41 getDeclIdent (ExternDeclaration ident _) = Just ident
42 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
43 getDeclIdent (BindingGroupDeclaration _) = internalError "lint: binding groups should not be desugared yet."
44 getDeclIdent _ = Nothing
46 lintDeclaration :: Declaration -> m ()
47 lintDeclaration = tell . f
49 (warningsInDecl, _, _, _, _) = everythingWithScope stepD stepE stepB (\_ _ -> mempty) stepDo
51 f :: Declaration -> MultipleErrors
52 f (PositionedDeclaration pos _ dec) = addHint (PositionedError pos) (f dec)
53 f dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec)
54 f (TypeDeclaration name ty) = addHint (ErrorInTypeDeclaration name) (checkTypeVars ty)
55 f dec = warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec
57 stepD :: S.Set Ident -> Declaration -> MultipleErrors
58 stepD _ (ValueDeclaration (Op name) _ _ _) = errorMessage (DeprecatedOperatorDecl name)
59 stepD _ (TypeClassDeclaration _ _ _ decls) = foldMap go decls
61 go :: Declaration -> MultipleErrors
62 go (PositionedDeclaration _ _ d') = go d'
63 go (TypeDeclaration (Op name) _) = errorMessage (DeprecatedOperatorDecl name)
67 stepE :: S.Set Ident -> Expr -> MultipleErrors
68 stepE s (Abs (Left name) _) | name `S.member` s = errorMessage (ShadowedName name)
69 stepE s (Let ds' _) = foldMap go ds'
71 go d | Just i <- getDeclIdent d
72 , i `S.member` s = errorMessage (ShadowedName i)
76 stepB :: S.Set Ident -> Binder -> MultipleErrors
77 stepB s (VarBinder name) | name `S.member` s = errorMessage (ShadowedName name)
78 stepB s (NamedBinder name _) | name `S.member` s = errorMessage (ShadowedName name)
81 stepDo :: S.Set Ident -> DoNotationElement -> MultipleErrors
82 stepDo s (DoNotationLet ds') = foldMap go ds'
84 go d | Just i <- getDeclIdent d
85 , i `S.member` s = errorMessage (ShadowedName i)
89 checkTypeVarsInDecl :: Declaration -> MultipleErrors
90 checkTypeVarsInDecl d = let (f, _, _, _, _) = accumTypes checkTypeVars in f d
92 checkTypeVars :: Type -> MultipleErrors
93 checkTypeVars ty = everythingWithContextOnTypes S.empty mempty mappend step ty <> findUnused ty
95 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
96 step s (ForAll tv _ _) = bindVar s tv
97 step s _ = (s, mempty)
98 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
99 bindVar = bind ShadowedTypeVar
100 findUnused :: Type -> MultipleErrors
102 let used = usedTypeVariables ty'
103 declared = everythingOnTypes (++) go ty'
104 unused = nub declared \\ nub used
105 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
107 go :: Type -> [String]
108 go (ForAll tv _ _) = [tv]
111 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
112 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
113 | 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