1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Linter
4-- Copyright : (c) 2013-15 Phil Freeman, (c) 2014-15 Gary Burgess
5-- License : MIT (http://opensource.org/licenses/MIT)
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
11-- | This module implements a simple linting pass on the PureScript AST.
13-----------------------------------------------------------------------------
15{-# LANGUAGE GeneralizedNewtypeDeriving #-}
16{-# LANGUAGE FlexibleContexts #-}
17{-# LANGUAGE ScopedTypeVariables #-}
20module Language.PureScript.Linter (lint, module L) where
22import Data.List (mapAccumL, nub)
23import Data.Maybe (mapMaybe)
26import qualified Data.Set as S
28#if __GLASGOW_HASKELL__ < 710
29import Control.Applicative
31import Control.Monad.Writer.Class
33import Language.PureScript.AST
34import Language.PureScript.Names
35import Language.PureScript.Errors
36import Language.PureScript.Types
37import Language.PureScript.Linter.Exhaustive as L
39-- | Lint the PureScript AST.
41-- | Right now, this pass only performs a shadowing check.
42lint :: forall m. (Applicative m, MonadWriter MultipleErrors m) => Module -> m ()
43lint (Module _ _ mn ds _) = censor (onErrorMessages (ErrorInModule mn)) $ mapM_ lintDeclaration ds
45 moduleNames :: S.Set Ident
46 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
48 getDeclIdent :: Declaration -> Maybe Ident
49 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
50 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
51 getDeclIdent (ExternDeclaration ident _) = Just ident
52 getDeclIdent (ExternInstanceDeclaration ident _ _ _) = Just ident
53 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
54 getDeclIdent (BindingGroupDeclaration _) = error "lint: binding groups should not be desugared yet."
55 getDeclIdent _ = Nothing
57 lintDeclaration :: Declaration -> m ()
59 let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend stepD stepE stepB def def
61 f' :: Declaration -> MultipleErrors
62 f' (PositionedDeclaration pos _ dec) = onErrorMessages (PositionedError pos) (f' dec)
63 f' dec = f dec <> checkShadowedTypeVars dec
69 stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
70 stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
72 go :: Declaration -> MultipleErrors -> MultipleErrors
73 go (PositionedDeclaration _ _ d') errs = go d' errs
74 go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
76 stepD s _ = (s, mempty)
78 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
79 stepE s (Abs (Left name) _) = bindName s name
81 case mapAccumL bindName s (nub (mapMaybe getDeclIdent ds')) of
82 (s', es) -> (s', mconcat es)
83 stepE s _ = (s, mempty)
85 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
86 stepB s (VarBinder name) = bindName s name
87 stepB s (NamedBinder name _) = bindName s name
88 stepB s _ = (s, mempty)
90 bindName :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
91 bindName = bind ShadowedName
93 checkShadowedTypeVars :: Declaration -> MultipleErrors
94 checkShadowedTypeVars d =
95 let (f, _, _, _, _) = accumTypes go in f d
97 go :: Type -> MultipleErrors
98 go = everythingWithContextOnTypes S.empty mempty mappend step
99 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
100 step s (ForAll tv _ _) = bindVar s tv
101 step s _ = (s, mempty)
102 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
103 bindVar = bind ShadowedTypeVar
105 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
106 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
107 | 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