estree-walker-ts
Version:
🥑 Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.
2 lines (1 loc) • 3.08 kB
JavaScript
var u=class{should_skip;should_remove;replacement;context;constructor(){this.should_skip=!1,this.should_remove=!1,this.replacement=null,this.context={skip:()=>this.should_skip=!0,remove:()=>this.should_remove=!0,replace:e=>this.replacement=e}}replace(e,t,s,i){e&&t&&(s!=null?e[t][s]=i:e[t]=i)}remove(e,t,s){e&&t&&(s!=null?e[t].splice(s,1):delete e[t])}};function d(r){return r!==null&&typeof r=="object"&&"type"in r&&typeof r.type=="string"}var c=class extends u{enter;leave;constructor(e,t){super(),this.should_skip=!1,this.should_remove=!1,this.replacement=null,this.context={skip:()=>this.should_skip=!0,remove:()=>this.should_remove=!0,replace:s=>this.replacement=s},this.enter=e,this.leave=t}visit(e,t,s,i){if(e){if(this.enter){let n=this.should_skip,l=this.should_remove,o=this.replacement;this.should_skip=!1,this.should_remove=!1,this.replacement=null,this.enter.call(this.context,e,t,s,i),this.replacement&&(e=this.replacement,this.replace(t,s,i,e)),this.should_remove&&this.remove(t,s,i);let h=this.should_skip,f=this.should_remove;if(this.should_skip=n,this.should_remove=l,this.replacement=o,h)return e;if(f)return null}let a;for(a in e){let n=e[a];if(n&&typeof n=="object")if(Array.isArray(n)){let l=n;for(let o=0;o<l.length;o+=1){let h=l[o];d(h)&&(this.visit(h,e,a,o)||o--)}}else d(n)&&this.visit(n,e,a,null)}if(this.leave){let n=this.replacement,l=this.should_remove;this.replacement=null,this.should_remove=!1,this.leave.call(this.context,e,t,s,i),this.replacement&&(e=this.replacement,this.replace(t,s,i,e)),this.should_remove&&this.remove(t,s,i);let o=this.should_remove;if(this.replacement=n,this.should_remove=l,o)return null}}return e}};var m=class extends u{enter;leave;constructor(e,t){super(),this.should_skip=!1,this.should_remove=!1,this.replacement=null,this.context={skip:()=>this.should_skip=!0,remove:()=>this.should_remove=!0,replace:s=>this.replacement=s},this.enter=e,this.leave=t}async visit(e,t,s,i){if(e){if(this.enter){let n=this.should_skip,l=this.should_remove,o=this.replacement;this.should_skip=!1,this.should_remove=!1,this.replacement=null,await this.enter.call(this.context,e,t,s,i),this.replacement&&(e=this.replacement,this.replace(t,s,i,e)),this.should_remove&&this.remove(t,s,i);let h=this.should_skip,f=this.should_remove;if(this.should_skip=n,this.should_remove=l,this.replacement=o,h)return e;if(f)return null}let a;for(a in e){let n=e[a];if(n&&typeof n=="object")if(Array.isArray(n)){let l=n;for(let o=0;o<l.length;o+=1){let h=l[o];d(h)&&(await this.visit(h,e,a,o)||o--)}}else d(n)&&await this.visit(n,e,a,null)}if(this.leave){let n=this.replacement,l=this.should_remove;this.replacement=null,this.should_remove=!1,await this.leave.call(this.context,e,t,s,i),this.replacement&&(e=this.replacement,this.replace(t,s,i,e)),this.should_remove&&this.remove(t,s,i);let o=this.should_remove;if(this.replacement=n,this.should_remove=l,o)return null}}return e}};function A(r,{enter:e,leave:t}){return new c(e,t).visit(r,null)}async function b(r,{enter:e,leave:t}){return await new m(e,t).visit(r,null)}export{b as asyncWalk,A as walk};