<commit_before>module TypedJavaScript.ErasedEnvTree
  ( buildErasedEnvTree
  , ErasedEnv
  ) where

import Prelude hiding (catch)
import Data.Tree
import TypedJavaScript.Prelude
import TypedJavaScript.Syntax
import qualified Data.Map as M

type ErasedEnvTree = Tree ErasedEnv

type ErasedEnv = Map SourcePos [Type]


empty :: ErasedEnvTree
empty = Node M.empty []

single :: SourcePos -> Type -> ErasedEnvTree
single loc type_ = multi loc [type_]

multi :: SourcePos -> [Type] -> ErasedEnvTree
multi loc types = Node (M.singleton loc types) []

nest :: ErasedEnvTree -> ErasedEnvTree
nest et = Node M.empty [et]


(+++) :: ErasedEnvTree -> ErasedEnvTree -> ErasedEnvTree
ee1 +++ ee2 =
  -- Using (++) is a bit of a hack.
  Node (M.union (rootLabel ee1) (rootLabel ee2))
       (subForest ee1 ++ subForest ee2)

unions :: [ErasedEnvTree] -> ErasedEnvTree
unions et = foldr (+++) empty et


buildErasedEnvTree :: [Statement SourcePos] -> ErasedEnvTree
buildErasedEnvTree stmts = complete M.empty $ unions (map stmt stmts) where
  complete enclosingEnv (Node env subtrees) =
    let completeEnv = M.union env enclosingEnv -- left-biased, shadows correctly
      in Node completeEnv (map (complete completeEnv) subtrees)

proploc :: Prop SourcePos -> SourcePos
proploc (PropId pos _) = pos
proploc (PropString pos _) = pos
proploc (PropNum pos _) = pos

prop :: (Prop SourcePos, Maybe (Type), Expression SourcePos)
     -> ErasedEnvTree
prop (prop, mtype, e) = case mtype of
  Nothing -> expr e -- If there is no type annotation, we add nothing
  Just type_ -> expr e +++ single (proploc prop) type_
<commit_msg>removed stupid comment; re last commit: erasedEnvTree did not descend into for-loops<commit_after>module TypedJavaScript.ErasedEnvTree
  ( buildErasedEnvTree
  , ErasedEnv
  ) where

import Prelude hiding (catch)
import Data.Tree
import TypedJavaScript.Prelude
import TypedJavaScript.Syntax
import qualified Data.Map as M

type ErasedEnvTree = Tree ErasedEnv

type ErasedEnv = Map SourcePos [Type]


empty :: ErasedEnvTree
empty = Node M.empty []

single :: SourcePos -> Type -> ErasedEnvTree
single loc type_ = multi loc [type_]

multi :: SourcePos -> [Type] -> ErasedEnvTree
multi loc types = Node (M.singleton loc types) []

nest :: ErasedEnvTree -> ErasedEnvTree
nest et = Node M.empty [et]


(+++) :: ErasedEnvTree -> ErasedEnvTree -> ErasedEnvTree
ee1 +++ ee2 =
  Node (M.union (rootLabel ee1) (rootLabel ee2))
       (subForest ee1 ++ subForest ee2)

unions :: [ErasedEnvTree] -> ErasedEnvTree
unions et = foldr (+++) empty et


buildErasedEnvTree :: [Statement SourcePos] -> ErasedEnvTree
buildErasedEnvTree stmts = complete M.empty $ unions (map stmt stmts) where
  complete enclosingEnv (Node env subtrees) =
    let completeEnv = M.union env enclosingEnv -- left-biased, shadows correctly
      in Node completeEnv (map (complete completeEnv) subtrees)

proploc :: Prop SourcePos -> SourcePos
proploc (PropId pos _) = pos
proploc (PropString pos _) = pos
proploc (PropNum pos _) = pos

prop :: (Prop SourcePos, Maybe (Type), Expression SourcePos)
     -> ErasedEnvTree
prop (prop, mtype, e) = case mtype of
  Nothing -> expr e -- If there is no type annotation, we add nothing
  Just type_ -> expr e +++ single (proploc prop) type_

{'fraction_non_alphanumeric': 0.09904644724700093, 'fraction_numerical': 0.003691171947093202, 'mean_word_length': 4.137440758293839, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '393260', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1042, 'n_words': 358}