<commit_before>module Checker where
import HJS.Parser
import HJS.Parser.JavaScript
import Text.ParserCombinators.Parsec.Error
import Data.List
import Extractor
import System.Environment
-- import Data.Typeable (Typeable, cast)
-- import Data.Maybe (fromMaybe)



-- toString :: (Show a, Typeable a) => a -> String
-- toString x = fromMaybe (show x) (cast x)



-- result data type
data Result = Warning String | Good String | Error String deriving Show

                    
typecheck :: String -> String -> [XType] -> Bool 
typecheck k b [] = True
typecheck k b (e:es) = if (k == (typeName e))
                       then elem b (typeFields e)
                       else typecheck k b es


statementCheck a2 (v:vs) (y:ys) en = case y of
    Stmt (StmtPos p s) -> case s of 
        ExprStmt (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallMember (MemberCall me e) [])))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
        ExprStmt (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallPrim (MemberCall me e))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys 
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
            _ -> True 
        ReturnStmt (Just (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallPrim (MemberCall me e)))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
        ReturnStmt (Just (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallMember (MemberCall me e)[]))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True           
        _ -> True
     


annotationCheck :: String -> String -> [TypeAnnotation] -> [XType] -> Bool
annotationCheck id b (x:xs) env = case x of
        TypeAnnotation typ arg -> if arg==id 
                                  then typecheck typ b env
                                  else if null xs
                                       then True
 
                                       else annotationCheck id b xs env

                                       
                                       
-- promptLocation ans location = do
--     let out = if ans then "Ok: " ++ toString (location) else "Type Error: " ++ toString(location)
--     print out
    

class CheckC t where
  check :: t -> [XType] -> Bool

instance CheckC SourceElement where
  check (Stmt s) env = check s env 
  check _ _ = True

instance CheckC Stmt where
  check (StmtPos p s) env = check s env

instance CheckC Stmt' where
  check (VarStmt (v:vs)) env = check v env 
  check (ExprStmt p) env = check p env<commit_msg>- added the code to track type error position - commented the code out.<commit_after>module Checker where
import HJS.Parser
import HJS.Parser.JavaScript
import Text.ParserCombinators.Parsec.Error
import Data.List
import Extractor
import System.Environment
-- import Data.Typeable (Typeable, cast)
-- import Data.Maybe (fromMaybe)



-- toString :: (Show a, Typeable a) => a -> String
-- toString x = fromMaybe (show x) (cast x)



-- result data type
data Result = Warning String | Good String | Error String deriving Show

                    
typecheck :: String -> String -> [XType] -> Bool 
typecheck k b [] = True
typecheck k b (e:es) = if (k == (typeName e))
                       then elem b (typeFields e)
                       else typecheck k b es


statementCheck a2 (v:vs) (y:ys) en = case y of
    Stmt (StmtPos p s) -> case s of 
        ExprStmt (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallMember (MemberCall me e) [])))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
        ExprStmt (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallPrim (MemberCall me e))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys -- promptLocation (annotationCheck id e a2 en && if null ys...)  ... means that is the whole if-then-else block
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
            _ -> True 
        ReturnStmt (Just (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallPrim (MemberCall me e)))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys -- promptLocation (annotationCheck id e a2 en && if null ys...)  ... means that is the whole if-then-else block
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True
        ReturnStmt (Just (AssignE (CondE (AExpr (AEUExpr (PostFix (LeftExpr (CallExpr (CallMember (MemberCall me e)[]))))))))) -> case me of
            MemPrimExpr (Ident id) -> if v==id
                                      then annotationCheck id e a2 en && if null ys -- promptLocation (annotationCheck id e a2 en && if null ys...)  ... means that is the whole if-then-else block
                                                                         then True
                                                                         else if null vs 
                                                                              then statementCheck a2 [v] ys en
                                                                              else statementCheck a2 vs ys en
                                      else True           
        _ -> True
     


annotationCheck :: String -> String -> [TypeAnnotation] -> [XType] -> Bool
annotationCheck id b (x:xs) env = case x of
        TypeAnnotation typ arg -> if arg==id 
                                  then typecheck typ b env
                                  else if null xs
                                       then True
 
                                       else annotationCheck id b xs env

                                       
-- get the checkers' returned value, print it, then return the value back to the caller.
-- this way, every check will be recorded, and then printed. 
-- promptLocation ans location = do
--     let out = if ans then "Ok: " ++ toString (location) else "Type Error: " ++ toString(location)
--     print out
--     return ans
    

class CheckC t where
  check :: t -> [XType] -> Bool

instance CheckC SourceElement where
  check (Stmt s) env = check s env 
  check _ _ = True

instance CheckC Stmt where
  check (StmtPos p s) env = check s env

instance CheckC Stmt' where
  check (VarStmt (v:vs)) env = check v env 
  check (ExprStmt p) env = check p env
{'fraction_non_alphanumeric': 0.06343055272282276, 'fraction_numerical': 0.002957373036916174, 'mean_word_length': 0.9225642030974319, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 143, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679770', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1974, 'n_words': 878}