{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE Strict #-} module Tokstyle.Linter.DeclaredOnce (descr) where import Control.Monad.State.Strict (State) import qualified Control.Monad.State.Strict as State import Data.Fix (Fix (..)) import Data.Map (Map) import qualified Data.Map as Map import Data.Text (Text) import qualified Data.Text as Text import Language.Cimple (Lexeme (..), LexemeClass (..), Node, NodeF (..)) import Language.Cimple.Diagnostics (HasDiagnostics (..), warn) import Language.Cimple.TraverseAst (AstActions, astActions, doNode, traverseAst) data Linter = Linter { Linter -> [Text] diags :: [Text] , Linter -> Map Text (FilePath, Lexeme Text) decls :: Map Text (FilePath, Lexeme Text) } empty :: Linter empty :: Linter empty = [Text] -> Map Text (FilePath, Lexeme Text) -> Linter Linter [] Map Text (FilePath, Lexeme Text) forall k a. Map k a Map.empty instance HasDiagnostics Linter where addDiagnostic :: Text -> Linter -> Linter addDiagnostic Text diag l :: Linter l@Linter{[Text] diags :: [Text] diags :: Linter -> [Text] diags} = Linter l{diags :: [Text] diags = Text -> [Text] -> [Text] forall a. HasDiagnostics a => Text -> a -> a addDiagnostic Text diag [Text] diags} linter :: AstActions (State Linter) Text linter :: AstActions (State Linter) Text linter = AstActions (State Linter) Text forall (f :: * -> *) text. Applicative f => AstActions f text astActions { doNode :: FilePath -> Node (Lexeme Text) -> State Linter () -> State Linter () doNode = \FilePath file Node (Lexeme Text) node State Linter () act -> case Node (Lexeme Text) -> NodeF (Lexeme Text) (Node (Lexeme Text)) forall (f :: * -> *). Fix f -> f (Fix f) unFix Node (Lexeme Text) node of FunctionDecl Scope _ (Fix (FunctionPrototype Node (Lexeme Text) _ fn :: Lexeme Text fn@(L AlexPosn _ LexemeClass IdVar Text fname) [Node (Lexeme Text)] _)) -> do l :: Linter l@Linter{Map Text (FilePath, Lexeme Text) decls :: Map Text (FilePath, Lexeme Text) decls :: Linter -> Map Text (FilePath, Lexeme Text) decls} <- State Linter Linter forall s (m :: * -> *). MonadState s m => m s State.get case Text -> Map Text (FilePath, Lexeme Text) -> Maybe (FilePath, Lexeme Text) forall k a. Ord k => k -> Map k a -> Maybe a Map.lookup Text fname Map Text (FilePath, Lexeme Text) decls of Maybe (FilePath, Lexeme Text) Nothing -> Linter -> State Linter () forall s (m :: * -> *). MonadState s m => s -> m () State.put Linter l{decls :: Map Text (FilePath, Lexeme Text) decls = Text -> (FilePath, Lexeme Text) -> Map Text (FilePath, Lexeme Text) -> Map Text (FilePath, Lexeme Text) forall k a. Ord k => k -> a -> Map k a -> Map k a Map.insert Text fname (FilePath file, Lexeme Text fn) Map Text (FilePath, Lexeme Text) decls } Just (FilePath file', Lexeme Text fn') -> do FilePath -> Lexeme Text -> Text -> State Linter () forall at diags. (HasLocation at, HasDiagnostics diags) => FilePath -> at -> Text -> DiagnosticsT diags () warn FilePath file' Lexeme Text fn' (Text -> State Linter ()) -> Text -> State Linter () forall a b. (a -> b) -> a -> b $ Text "duplicate declaration of function `" Text -> Text -> Text forall a. Semigroup a => a -> a -> a <> Text fname Text -> Text -> Text forall a. Semigroup a => a -> a -> a <> Text "`" FilePath -> Lexeme Text -> Text -> State Linter () forall at diags. (HasLocation at, HasDiagnostics diags) => FilePath -> at -> Text -> DiagnosticsT diags () warn FilePath file Lexeme Text fn (Text -> State Linter ()) -> Text -> State Linter () forall a b. (a -> b) -> a -> b $ Text "function `" Text -> Text -> Text forall a. Semigroup a => a -> a -> a <> Text fname Text -> Text -> Text forall a. Semigroup a => a -> a -> a <> Text "` also declared here" FunctionDefn{} -> () -> State Linter () forall (f :: * -> *) a. Applicative f => a -> f a pure () NodeF (Lexeme Text) (Node (Lexeme Text)) _ -> State Linter () act } analyse :: [(FilePath, [Node (Lexeme Text)])] -> [Text] analyse :: [(FilePath, [Node (Lexeme Text)])] -> [Text] analyse [(FilePath, [Node (Lexeme Text)])] tus = [Text] -> [Text] forall a. [a] -> [a] reverse ([Text] -> [Text]) -> (Linter -> [Text]) -> Linter -> [Text] forall b c a. (b -> c) -> (a -> b) -> a -> c . Linter -> [Text] diags (Linter -> [Text]) -> Linter -> [Text] forall a b. (a -> b) -> a -> b $ State Linter () -> Linter -> Linter forall s a. State s a -> s -> s State.execState (AstActions (State Linter) Text -> [(FilePath, [Node (Lexeme Text)])] -> State Linter () forall text a (f :: * -> *). (TraverseAst text a, Applicative f) => AstActions f text -> a -> f () traverseAst AstActions (State Linter) Text linter [(FilePath, [Node (Lexeme Text)])] tus) Linter empty descr :: ([(FilePath, [Node (Lexeme Text)])] -> [Text], (Text, Text)) descr :: ([(FilePath, [Node (Lexeme Text)])] -> [Text], (Text, Text)) descr = ([(FilePath, [Node (Lexeme Text)])] -> [Text] analyse, (Text "declared-once", [Text] -> Text Text.unlines [ Text "Checks that any function is declared exactly once." , Text "" , Text "**Reason:** functions should never be declared in multiple files, and within the" , Text "same file, declaring it twice is unnecessary and confusing." ]))