title: How to modify the javascript code with AST using babel?
body: <p>I've a javascript function like this</p>
  <pre><code>function testFunction(datamap){
      if(datamap.has(&quot;key&quot;)){
  
          console.log(datamap.get('key'));
      }
      else{
          console.log(&quot;Key is not present&quot;)
      }
  
      console.log(&quot;Hi This  is check statement.&quot;);
  
  
      let key2=datamap.get('key2');
  }
  </code></pre>
  <p>datamap is a parameter of type Map.</p>
  <p>I want to convert the above function to like below</p>
  <pre><code>function testFunction(datamap){
   
     if(datamap.has(&quot;key&quot;)){
  
          if(!datamap.has('key')){
          console.log('key is not present in datamap')
           }
      }
      
  // console.log(&quot;Hi This  is check statement.&quot;);-- This statement is deleted
      if(!datamap.has(&quot;key2&quot;)){ //Get statement is converted to check
         console.log('key2 is not present in datamap')
      }
  }
  
  </code></pre>
  <p>I want to replace all statements apart from the IfSTATEMENT with dataMap.has callExpression and datamap.get with empty strings. and I want to add a check whereever datamap.get is used</p>
  <p>I've read about AST and transforming the AST with babel but I could not figure out how to write this in visitor pattern.
  I've tried something in astexplorer but was no way near what I wanted.</p>
  <p>Idea is that we transform the function in a way that we add a check if the key is supposed to be present in the datamap instead of nullpointer or undifined wherever the value from datamap.get is accessed</p>
  <p>My aim is to instrument the function and then if I run the instrumented function with a Map with some keys , It should give me the info of what keys are missing in the provided datamap argument</p>
  <p>Could someone provide any insight on how this could be done? or can this be done without instrumenting the function just by traversing the AST?</p>