1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Linter
4-- Copyright : (c) Copyright 2015 PureScript
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 #-}
19module Language.PureScript.Linter (lint) where
21import Data.List (mapAccumL, nub)
22import Data.Maybe (mapMaybe)
25import qualified Data.Set as S
27import Control.Applicative
28import Control.Monad.Writer.Class
30import Language.PureScript.AST
31import Language.PureScript.Names
32import Language.PureScript.Errors
34-- | Lint the PureScript AST.
36-- | Right now, this pass only performs a shadowing check.
37lint :: forall m. (Applicative m, MonadWriter MultipleErrors m) => Module -> m ()
38lint (Module _ mn ds _) = censor (onErrorMessages (ErrorInModule mn)) $ mapM_ lintDeclaration ds
40 moduleNames :: S.Set Ident
41 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
43 getDeclIdent :: Declaration -> Maybe Ident
44 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
45 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
46 getDeclIdent (ExternDeclaration ident _) = Just ident
47 getDeclIdent (ExternInstanceDeclaration ident _ _ _) = Just ident
48 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
49 getDeclIdent (BindingGroupDeclaration _) = error "lint: binding groups should not be desugared yet."
50 getDeclIdent _ = Nothing
52 lintDeclaration :: Declaration -> m ()
54 let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend def stepE stepB def def
59 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
60 stepE s (Abs (Left name) _) = bind s name
62 case mapAccumL bind s (nub (mapMaybe getDeclIdent ds')) of
63 (s', es) -> (s', mconcat es)
64 stepE s _ = (s, mempty)
66 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
67 stepB s (VarBinder name) = bind s name
68 stepB s (NamedBinder name _) = bind s name
69 stepB s _ = (s, mempty)
71 bind :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
72 bind s name | name `S.member` s = (s, errorMessage (ShadowedName name))
73 | 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