UNPKG

estree-walker-ts

Version:

🥑 Simple utility for walking an ESTree-compliant AST, such as one generated by acorn.

2 lines (1 loc) • 3.57 kB
"use strict";var p=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var k=(i,e)=>{for(var t in e)p(i,t,{get:e[t],enumerable:!0})},N=(i,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of y(e))!_.call(i,n)&&n!==t&&p(i,n,{get:()=>e[n],enumerable:!(s=v(e,n))||s.enumerable});return i};var w=i=>N(p({},"__esModule",{value:!0}),i);var P={};k(P,{asyncWalk:()=>H,walk:()=>x});module.exports=w(P);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,n){e&&t&&(s!=null?e[t][s]=n:e[t]=n)}remove(e,t,s){e&&t&&(s!=null?e[t].splice(s,1):delete e[t])}};function d(i){return i!==null&&typeof i=="object"&&"type"in i&&typeof i.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,n){if(e){if(this.enter){let o=this.should_skip,r=this.should_remove,l=this.replacement;this.should_skip=!1,this.should_remove=!1,this.replacement=null,this.enter.call(this.context,e,t,s,n),this.replacement&&(e=this.replacement,this.replace(t,s,n,e)),this.should_remove&&this.remove(t,s,n);let h=this.should_skip,f=this.should_remove;if(this.should_skip=o,this.should_remove=r,this.replacement=l,h)return e;if(f)return null}let a;for(a in e){let o=e[a];if(o&&typeof o=="object")if(Array.isArray(o)){let r=o;for(let l=0;l<r.length;l+=1){let h=r[l];d(h)&&(this.visit(h,e,a,l)||l--)}}else d(o)&&this.visit(o,e,a,null)}if(this.leave){let o=this.replacement,r=this.should_remove;this.replacement=null,this.should_remove=!1,this.leave.call(this.context,e,t,s,n),this.replacement&&(e=this.replacement,this.replace(t,s,n,e)),this.should_remove&&this.remove(t,s,n);let l=this.should_remove;if(this.replacement=o,this.should_remove=r,l)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,n){if(e){if(this.enter){let o=this.should_skip,r=this.should_remove,l=this.replacement;this.should_skip=!1,this.should_remove=!1,this.replacement=null,await this.enter.call(this.context,e,t,s,n),this.replacement&&(e=this.replacement,this.replace(t,s,n,e)),this.should_remove&&this.remove(t,s,n);let h=this.should_skip,f=this.should_remove;if(this.should_skip=o,this.should_remove=r,this.replacement=l,h)return e;if(f)return null}let a;for(a in e){let o=e[a];if(o&&typeof o=="object")if(Array.isArray(o)){let r=o;for(let l=0;l<r.length;l+=1){let h=r[l];d(h)&&(await this.visit(h,e,a,l)||l--)}}else d(o)&&await this.visit(o,e,a,null)}if(this.leave){let o=this.replacement,r=this.should_remove;this.replacement=null,this.should_remove=!1,await this.leave.call(this.context,e,t,s,n),this.replacement&&(e=this.replacement,this.replace(t,s,n,e)),this.should_remove&&this.remove(t,s,n);let l=this.should_remove;if(this.replacement=o,this.should_remove=r,l)return null}}return e}};function x(i,{enter:e,leave:t}){return new c(e,t).visit(i,null)}async function H(i,{enter:e,leave:t}){return await new m(e,t).visit(i,null)}0&&(module.exports={asyncWalk,walk});