git.iko.soy/rowtype-yoga/purescript-native
on 77a6451 help sign in
91 lines 3.4 kB history raw
1-----------------------------------------------------------------------------
2--
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)
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- | This module implements a simple linting pass on the PureScript AST.
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE GeneralizedNewtypeDeriving #-}
16{-# LANGUAGE FlexibleContexts #-}
17{-# LANGUAGE ScopedTypeVariables #-}
18{-# LANGUAGE CPP #-}
20module Language.PureScript.Linter (lint, module L) where
22import Data.List (mapAccumL, nub)
23import Data.Maybe (mapMaybe)
24import Data.Monoid
26import qualified Data.Set as S
28#if __GLASGOW_HASKELL__ < 710
29import Control.Applicative
30#endif
31import Control.Monad.Writer.Class
33import Language.PureScript.AST
34import Language.PureScript.Names
35import Language.PureScript.Errors
36import Language.PureScript.Linter.Exhaustive as L
38-- | Lint the PureScript AST.
39-- |
40-- | Right now, this pass only performs a shadowing check.
41lint :: forall m. (Applicative m, MonadWriter MultipleErrors m) => Module -> m ()
42lint (Module _ _ mn ds _) = censor (onErrorMessages (ErrorInModule mn)) $ mapM_ lintDeclaration ds
43 where
44 moduleNames :: S.Set Ident
45 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
47 getDeclIdent :: Declaration -> Maybe Ident
48 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
49 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
50 getDeclIdent (ExternDeclaration ident _) = Just ident
51 getDeclIdent (ExternInstanceDeclaration ident _ _ _) = Just ident
52 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
53 getDeclIdent (BindingGroupDeclaration _) = error "lint: binding groups should not be desugared yet."
54 getDeclIdent _ = Nothing
56 lintDeclaration :: Declaration -> m ()
57 lintDeclaration d =
58 let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend stepD stepE stepB def def
60 f' :: Declaration -> MultipleErrors
61 f' (PositionedDeclaration pos _ dec) = onErrorMessages (PositionedError pos) (f' dec)
62 f' dec = f dec
64 in tell (f' d)
65 where
66 def s _ = (s, mempty)
68 stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
69 stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
70 where
71 go :: Declaration -> MultipleErrors -> MultipleErrors
72 go (PositionedDeclaration _ _ d') errs = go d' errs
73 go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
74 go _ errs = errs
75 stepD s _ = (s, mempty)
77 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
78 stepE s (Abs (Left name) _) = bind s name
79 stepE s (Let ds' _) =
80 case mapAccumL bind s (nub (mapMaybe getDeclIdent ds')) of
81 (s', es) -> (s', mconcat es)
82 stepE s _ = (s, mempty)
84 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
85 stepB s (VarBinder name) = bind s name
86 stepB s (NamedBinder name _) = bind s name
87 stepB s _ = (s, mempty)
89 bind :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
90 bind s name | name `S.member` s = (s, errorMessage (ShadowedName name))
91 | otherwise = (S.insert name s, mempty)