{-# LANGUAGE OverloadedStrings #-}
module Tokstyle.Cimple.Analysis.LoggerNoEscapes (analyse) where

import           Control.Monad               (when)
import           Control.Monad.State.Lazy    (State)
import qualified Control.Monad.State.Lazy    as State
import           Data.Text                   (Text, isInfixOf)
import qualified Data.Text                   as Text
import           Language.Cimple             (AstActions, Lexeme (..),
                                              LiteralType (String), Node (..),
                                              defaultActions, doNode,
                                              lexemeText, traverseAst)
import qualified Language.Cimple.Diagnostics as Diagnostics


linter :: AstActions [Text]
linter :: AstActions [Text]
linter = AstActions [Text]
forall a. IdentityActions (State a) () Text
defaultActions
    { doNode :: FilePath
-> Node () (Lexeme Text)
-> State [Text] (Node () (Lexeme Text))
-> State [Text] (Node () (Lexeme Text))
doNode = \FilePath
file Node () (Lexeme Text)
node State [Text] (Node () (Lexeme Text))
act -> case Node () (Lexeme Text)
node of
        -- LOGGER_ASSERT has its format as the third parameter.
        FunctionCall (LiteralExpr LiteralType
_ (L AlexPosn
_ LexemeClass
_ Text
"LOGGER_ASSERT")) (Node () (Lexeme Text)
_ : Node () (Lexeme Text)
_ : LiteralExpr LiteralType
String Lexeme Text
fmt : [Node () (Lexeme Text)]
_)
            -> do
                FilePath -> Lexeme Text -> State [Text] ()
checkFormat FilePath
file Lexeme Text
fmt
                State [Text] (Node () (Lexeme Text))
act

        FunctionCall (LiteralExpr LiteralType
_ (L AlexPosn
_ LexemeClass
_ Text
func)) (Node () (Lexeme Text)
_ : LiteralExpr LiteralType
String Lexeme Text
fmt : [Node () (Lexeme Text)]
_)
            | Text -> Text -> Bool
Text.isPrefixOf Text
"LOGGER_" Text
func
            -> do
                FilePath -> Lexeme Text -> State [Text] ()
checkFormat FilePath
file Lexeme Text
fmt
                State [Text] (Node () (Lexeme Text))
act

        Node () (Lexeme Text)
_ -> State [Text] (Node () (Lexeme Text))
act
    }


checkFormat :: FilePath -> Lexeme Text -> State [Text] ()
checkFormat :: FilePath -> Lexeme Text -> State [Text] ()
checkFormat FilePath
file Lexeme Text
fmt =
    Bool -> State [Text] () -> State [Text] ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text
"\\" Text -> Text -> Bool
`isInfixOf` Text
text) (State [Text] () -> State [Text] ())
-> State [Text] () -> State [Text] ()
forall a b. (a -> b) -> a -> b
$
        FilePath -> Lexeme Text -> Text -> State [Text] ()
forall diags.
HasDiagnostics diags =>
FilePath -> Lexeme Text -> Text -> DiagnosticsT diags ()
Diagnostics.warn FilePath
file Lexeme Text
fmt (Text -> State [Text] ()) -> Text -> State [Text] ()
forall a b. (a -> b) -> a -> b
$
            Text
"logger format "
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
text
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" contains escape sequences (newlines, tabs, or escaped quotes)"
    where text :: Text
text = Lexeme Text -> Text
forall text. Lexeme text -> text
lexemeText Lexeme Text
fmt


analyse :: (FilePath, [Node () (Lexeme Text)]) -> [Text]
analyse :: (FilePath, [Node () (Lexeme Text)]) -> [Text]
analyse = [Text] -> [Text]
forall a. [a] -> [a]
reverse ([Text] -> [Text])
-> ((FilePath, [Node () (Lexeme Text)]) -> [Text])
-> (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State [Text] (FilePath, [Node () (Lexeme Text)])
 -> [Text] -> [Text])
-> [Text]
-> State [Text] (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall a b c. (a -> b -> c) -> b -> a -> c
flip State [Text] (FilePath, [Node () (Lexeme Text)])
-> [Text] -> [Text]
forall s a. State s a -> s -> s
State.execState [] (State [Text] (FilePath, [Node () (Lexeme Text)]) -> [Text])
-> ((FilePath, [Node () (Lexeme Text)])
    -> State [Text] (FilePath, [Node () (Lexeme Text)]))
-> (FilePath, [Node () (Lexeme Text)])
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AstActions [Text]
-> (FilePath, [Node () (Lexeme Text)])
-> State [Text] (FilePath, [Node () (Lexeme Text)])
forall iattr oattr itext otext a (f :: * -> *).
(TraverseAst iattr oattr itext otext a a, Applicative f) =>
AstActions f iattr oattr itext otext -> a -> f a
traverseAst AstActions [Text]
linter