@speclynx/apidom-traverse 1.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.apidomTraverse=e():t.apidomTraverse=e()}(self,()=>(()=>{"use strict";var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{Path:()=>n,cloneNode:()=>ye,filter:()=>Oe,find:()=>Ee,findAtOffset:()=>Ce,forEach:()=>Me,getNodeKeys:()=>ve,getNodePrimitiveType:()=>pe,getNodeType:()=>he,getVisitFn:()=>be,isNode:()=>de,mergeVisitors:()=>we,mergeVisitorsAsync:()=>ke,mutateNode:()=>me,parents:()=>Fe,reject:()=>Pe,some:()=>xe,traverse:()=>Se,traverseAsync:()=>Ae});class n{node;key;index;parent;parentPath;inList;#t=!1;#e=!1;#n=!1;#r=!1;#o;#s=!1;constructor(t,e,n,r,o){this.node=t,this.parent=e,this.parentPath=n,this.key=r,this.index=o&&"number"==typeof r?r:void 0,this.inList=o}get shouldSkip(){return this.#t}get shouldStop(){return this.#e}get removed(){return this.#n}isRoot(){return null===this.parentPath}get depth(){let t=0,e=this.parentPath;for(;null!==e;)t+=1,e=e.parentPath;return t}getAncestry(){const t=[];let e=this.parentPath;for(;null!==e;)t.push(e),e=e.parentPath;return t}getAncestorNodes(){return this.getAncestry().map(t=>t.node)}getPathKeys(){const t=[];let e=this;for(;null!==e&&void 0!==e.key;)t.unshift(e.key),e=e.parentPath;return t}findParent(t){let e=this.parentPath;for(;null!==e;){if(t(e))return e;e=e.parentPath}return null}find(t){return t(this)?this:this.findParent(t)}skip(){this.#t=!0}stop(){this.#e=!0}replaceWith(t){this.#s&&console.warn("Warning: replaceWith() called on a stale Path. This path belongs to a node whose visit has already completed. The replacement will have no effect. To replace a parent node, do so from the parent's own visitor."),this.#r=!0,this.#o=t,this.node=t}remove(){this.#s&&console.warn("Warning: remove() called on a stale Path. This path belongs to a node whose visit has already completed. The removal will have no effect. To remove a parent node, do so from the parent's own visitor."),this.#n=!0}_getReplacementNode(){return this.#o}_wasReplaced(){return this.#r}_reset(){this.#t=!1,this.#e=!1,this.#n=!1,this.#r=!1,this.#o=void 0}_markStale(){this.#s=!0}}class r extends AggregateError{constructor(t,e,n){super(t,e,n),this.name=this.constructor.name,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}const o=r;class s extends Error{static[Symbol.hasInstance](t){return super[Symbol.hasInstance](t)||Function.prototype[Symbol.hasInstance].call(o,t)}constructor(t,e){super(t,e),this.name=this.constructor.name,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}const i=s;const c=class extends i{constructor(t,e){if(super(t,e),null!=e&&"object"==typeof e){const{cause:t,...n}=e;Object.assign(this,n)}}};function u(t,e){switch(t){case 0:return function(){return e.apply(this,arguments)};case 1:return function(t){return e.apply(this,arguments)};case 2:return function(t,n){return e.apply(this,arguments)};case 3:return function(t,n,r){return e.apply(this,arguments)};case 4:return function(t,n,r,o){return e.apply(this,arguments)};case 5:return function(t,n,r,o,s){return e.apply(this,arguments)};case 6:return function(t,n,r,o,s,i){return e.apply(this,arguments)};case 7:return function(t,n,r,o,s,i,c){return e.apply(this,arguments)};case 8:return function(t,n,r,o,s,i,c,u){return e.apply(this,arguments)};case 9:return function(t,n,r,o,s,i,c,u,a){return e.apply(this,arguments)};case 10:return function(t,n,r,o,s,i,c,u,a,l){return e.apply(this,arguments)};default:throw new Error("First argument to _arity must be a non-negative integer no greater than ten")}}function a(t){return null!=t&&"object"==typeof t&&!0===t["@@functional/placeholder"]}function l(t){return function e(n){return 0===arguments.length||a(n)?e:t.apply(this,arguments)}}function f(t){return function e(n,r){switch(arguments.length){case 0:return e;case 1:return a(n)?e:l(function(e){return t(n,e)});default:return a(n)&&a(r)?e:a(n)?l(function(e){return t(e,r)}):a(r)?l(function(e){return t(n,e)}):t(n,r)}}}function h(t,e,n){return function(){for(var r=[],o=0,s=t,i=0,c=!1;i<e.length||o<arguments.length;){var l;i<e.length&&(!a(e[i])||o>=arguments.length)?l=e[i]:(l=arguments[o],o+=1),r[i]=l,a(l)?c=!0:s-=1,i+=1}return!c&&s<=0?n.apply(this,r):u(Math.max(0,s),h(t,r,n))}}const p=f(function(t,e){return 1===t?l(e):u(t,h(t,[],e))});function d(t){var e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e||"[object GeneratorFunction]"===e||"[object AsyncGeneratorFunction]"===e}const y=f(function(t,e){return t&&e});function m(t,e,n){for(var r=0,o=n.length;r<o;)e=t(e,n[r]),r+=1;return e}const v=Array.isArray||function(t){return null!=t&&t.length>=0&&"[object Array]"===Object.prototype.toString.call(t)};const g=l(function(t){return!!v(t)||!!t&&("object"==typeof t&&(!function(t){return"[object String]"===Object.prototype.toString.call(t)}(t)&&(0===t.length||t.length>0&&(t.hasOwnProperty(0)&&t.hasOwnProperty(t.length-1)))))});var b="undefined"!=typeof Symbol?Symbol.iterator:"@@iterator";function w(t,e,n){return function(r,o,s){if(g(s))return t(r,o,s);if(null==s)return o;if("function"==typeof s["fantasy-land/reduce"])return e(r,o,s,"fantasy-land/reduce");if(null!=s[b])return n(r,o,s[b]());if("function"==typeof s.next)return n(r,o,s);if("function"==typeof s.reduce)return e(r,o,s,"reduce");throw new TypeError("reduce: list must be array or iterable")}}function k(t,e,n){for(var r=n.next();!r.done;)e=t(e,r.value),r=n.next();return e}function _(t,e,n,r){return n[r](t,e)}const j=w(m,_,k);function S(t,e,n){return function(){if(0===arguments.length)return n();var r=arguments[arguments.length-1];if(!v(r)){for(var o=0;o<t.length;){if("function"==typeof r[t[o]])return r[t[o]].apply(r,Array.prototype.slice.call(arguments,0,-1));o+=1}if(function(t){return null!=t&&"function"==typeof t["@@transducer/step"]}(r))return e.apply(null,Array.prototype.slice.call(arguments,0,-1))(r)}return n.apply(this,arguments)}}function A(t,e){for(var n=0,r=e.length,o=Array(r);n<r;)o[n]=t(e[n]),n+=1;return o}const O=function(){return this.xf["@@transducer/init"]()},E=function(t){return this.xf["@@transducer/result"](t)};var x=function(){function t(t,e){this.xf=e,this.f=t}return t.prototype["@@transducer/init"]=O,t.prototype["@@transducer/result"]=E,t.prototype["@@transducer/step"]=function(t,e){return this.xf["@@transducer/step"](t,this.f(e))},t}();const P=function(t){return function(e){return new x(t,e)}};function M(t,e){return Object.prototype.hasOwnProperty.call(e,t)}var F=Object.prototype.toString;const C=function(){return"[object Arguments]"===F.call(arguments)?function(t){return"[object Arguments]"===F.call(t)}:function(t){return M("callee",t)}}();var T=!{toString:null}.propertyIsEnumerable("toString"),V=["constructor","valueOf","isPrototypeOf","toString","propertyIsEnumerable","hasOwnProperty","toLocaleString"],N=function(){return arguments.propertyIsEnumerable("length")}(),z=function(t,e){for(var n=0;n<t.length;){if(t[n]===e)return!0;n+=1}return!1},L="function"!=typeof Object.keys||N?l(function(t){if(Object(t)!==t)return[];var e,n,r=[],o=N&&C(t);for(e in t)!M(e,t)||o&&"length"===e||(r[r.length]=e);if(T)for(n=V.length-1;n>=0;)M(e=V[n],t)&&!z(r,e)&&(r[r.length]=e),n-=1;return r}):l(function(t){return Object(t)!==t?[]:Object.keys(t)});const W=L;const R=f(S(["fantasy-land/map","map"],P,function(t,e){switch(Object.prototype.toString.call(e)){case"[object Function]":return p(e.length,function(){return t.call(this,e.apply(this,arguments))});case"[object Object]":return m(function(n,r){return n[r]=t(e[r]),n},{},W(e));default:return A(t,e)}}));const q=f(function(t,e){return"function"==typeof e["fantasy-land/ap"]?e["fantasy-land/ap"](t):"function"==typeof t.ap?t.ap(e):"function"==typeof t?function(n){return t(n)(e(n))}:j(function(t,n){return function(t,e){var n;e=e||[];var r=(t=t||[]).length,o=e.length,s=[];for(n=0;n<r;)s[s.length]=t[n],n+=1;for(n=0;n<o;)s[s.length]=e[n],n+=1;return s}(t,R(n,e))},[],t)});const I=f(function(t,e){var n=p(t,e);return p(t,function(){return m(q,R(n,arguments[0]),Array.prototype.slice.call(arguments,1))})});const U=l(function(t){return I(t.length,t)});const G=f(function(t,e){return d(t)?function(){return t.apply(this,arguments)&&e.apply(this,arguments)}:U(y)(t,e)});function B(t,e){return function(){return e.call(this,t.apply(this,arguments))}}function D(t){return function e(n,r,o){switch(arguments.length){case 0:return e;case 1:return a(n)?e:f(function(e,r){return t(n,e,r)});case 2:return a(n)&&a(r)?e:a(n)?f(function(e,n){return t(e,r,n)}):a(r)?f(function(e,r){return t(n,e,r)}):l(function(e){return t(n,r,e)});default:return a(n)&&a(r)&&a(o)?e:a(n)&&a(r)?f(function(e,n){return t(e,n,o)}):a(n)&&a(o)?f(function(e,n){return t(e,r,n)}):a(r)&&a(o)?f(function(e,r){return t(n,e,r)}):a(n)?l(function(e){return t(e,r,o)}):a(r)?l(function(e){return t(n,e,o)}):a(o)?l(function(e){return t(n,r,e)}):t(n,r,o)}}}function K(t,e,n){for(var r=0,o=n.length;r<o;){if((e=t["@@transducer/step"](e,n[r]))&&e["@@transducer/reduced"]){e=e["@@transducer/value"];break}r+=1}return t["@@transducer/result"](e)}const $=f(function(t,e){return u(t.length,function(){return t.apply(e,arguments)})});function H(t,e,n){for(var r=n.next();!r.done;){if((e=t["@@transducer/step"](e,r.value))&&e["@@transducer/reduced"]){e=e["@@transducer/value"];break}r=n.next()}return t["@@transducer/result"](e)}function Q(t,e,n,r){return t["@@transducer/result"](n[r]($(t["@@transducer/step"],t),e))}const X=w(K,Q,H);var Y=function(){function t(t){this.f=t}return t.prototype["@@transducer/init"]=function(){throw new Error("init not implemented on XWrap")},t.prototype["@@transducer/result"]=function(t){return t},t.prototype["@@transducer/step"]=function(t,e){return this.f(t,e)},t}();const Z=D(function(t,e,n){return X("function"==typeof t?new Y(t):t,e,n)});function J(t,e){return function(){var n=arguments.length;if(0===n)return e();var r=arguments[n-1];return v(r)||"function"!=typeof r[t]?e.apply(this,arguments):r[t].apply(r,Array.prototype.slice.call(arguments,0,n-1))}}const tt=l(J("tail",D(J("slice",function(t,e,n){return Array.prototype.slice.call(n,t,e)}))(1,1/0)));function et(){if(0===arguments.length)throw new Error("pipe requires at least one argument");return u(arguments[0].length,Z(B,arguments[0],tt(arguments)))}function nt(t){for(var e,n=[];!(e=t.next()).done;)n.push(e.value);return n}function rt(t,e,n){for(var r=0,o=n.length;r<o;){if(t(e,n[r]))return!0;r+=1}return!1}const ot="function"==typeof Object.is?Object.is:function(t,e){return t===e?0!==t||1/t==1/e:t!=t&&e!=e};const st=l(function(t){return null===t?"Null":void 0===t?"Undefined":Object.prototype.toString.call(t).slice(8,-1)});function it(t,e,n,r){var o=nt(t);function s(t,e){return ct(t,e,n.slice(),r.slice())}return!rt(function(t,e){return!rt(s,e,t)},nt(e),o)}function ct(t,e,n,r){if(ot(t,e))return!0;var o,s,i=st(t);if(i!==st(e))return!1;if("function"==typeof t["fantasy-land/equals"]||"function"==typeof e["fantasy-land/equals"])return"function"==typeof t["fantasy-land/equals"]&&t["fantasy-land/equals"](e)&&"function"==typeof e["fantasy-land/equals"]&&e["fantasy-land/equals"](t);if("function"==typeof t.equals||"function"==typeof e.equals)return"function"==typeof t.equals&&t.equals(e)&&"function"==typeof e.equals&&e.equals(t);switch(i){case"Arguments":case"Array":case"Object":if("function"==typeof t.constructor&&"Promise"===(o=t.constructor,null==(s=String(o).match(/^function (\w*)/))?"":s[1]))return t===e;break;case"Boolean":case"Number":case"String":if(typeof t!=typeof e||!ot(t.valueOf(),e.valueOf()))return!1;break;case"Date":if(!ot(t.valueOf(),e.valueOf()))return!1;break;case"Error":return t.name===e.name&&t.message===e.message;case"RegExp":if(t.source!==e.source||t.global!==e.global||t.ignoreCase!==e.ignoreCase||t.multiline!==e.multiline||t.sticky!==e.sticky||t.unicode!==e.unicode)return!1}for(var c=n.length-1;c>=0;){if(n[c]===t)return r[c]===e;c-=1}switch(i){case"Map":return t.size===e.size&&it(t.entries(),e.entries(),n.concat([t]),r.concat([e]));case"Set":return t.size===e.size&&it(t.values(),e.values(),n.concat([t]),r.concat([e]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var u=W(t);if(u.length!==W(e).length)return!1;var a=n.concat([t]),l=r.concat([e]);for(c=u.length-1;c>=0;){var f=u[c];if(!M(f,e)||!ct(e[f],t[f],a,l))return!1;c-=1}return!0}const ut=f(function(t,e){return ct(t,e,[],[])});function at(t,e){return function(t,e,n){var r,o;if("function"==typeof t.indexOf)switch(typeof e){case"number":if(0===e){for(r=1/e;n<t.length;){if(0===(o=t[n])&&1/o===r)return n;n+=1}return-1}if(e!=e){for(;n<t.length;){if("number"==typeof(o=t[n])&&o!=o)return n;n+=1}return-1}return t.indexOf(e,n);case"string":case"boolean":case"function":case"undefined":return t.indexOf(e,n);case"object":if(null===e)return t.indexOf(e,n)}for(;n<t.length;){if(ut(t[n],e))return n;n+=1}return-1}(e,t,0)>=0}function lt(t){return'"'+t.replace(/\\/g,"\\\\").replace(/[\b]/g,"\\b").replace(/\f/g,"\\f").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t").replace(/\v/g,"\\v").replace(/\0/g,"\\0").replace(/"/g,'\\"')+'"'}var ft=function(t){return(t<10?"0":"")+t};const ht="function"==typeof Date.prototype.toISOString?function(t){return t.toISOString()}:function(t){return t.getUTCFullYear()+"-"+ft(t.getUTCMonth()+1)+"-"+ft(t.getUTCDate())+"T"+ft(t.getUTCHours())+":"+ft(t.getUTCMinutes())+":"+ft(t.getUTCSeconds())+"."+(t.getUTCMilliseconds()/1e3).toFixed(3).slice(2,5)+"Z"};var pt=function(){function t(t,e){this.xf=e,this.f=t}return t.prototype["@@transducer/init"]=O,t.prototype["@@transducer/result"]=E,t.prototype["@@transducer/step"]=function(t,e){return this.f(e)?this.xf["@@transducer/step"](t,e):t},t}();function dt(t){return function(e){return new pt(t,e)}}var yt=f(S(["fantasy-land/filter","filter"],dt,function(t,e){return n=e,"[object Object]"===Object.prototype.toString.call(n)?m(function(n,r){return t(e[r])&&(n[r]=e[r]),n},{},W(e)):function(t){return"[object Map]"===Object.prototype.toString.call(t)}(e)?function(t,e){for(var n=new Map,r=e.entries(),o=r.next();!o.done;)t(o.value[1])&&n.set(o.value[0],o.value[1]),o=r.next();return n}(t,e):function(t,e){for(var n=0,r=e.length,o=[];n<r;)t(e[n])&&(o[o.length]=e[n]),n+=1;return o}(t,e);var n}));const mt=yt;const vt=f(function(t,e){return mt((n=t,function(){return!n.apply(this,arguments)}),e);var n});function gt(t,e){var n=function(n){var r=e.concat([t]);return at(n,r)?"<Circular>":gt(n,r)},r=function(t,e){return A(function(e){return lt(e)+": "+n(t[e])},e.slice().sort())};switch(Object.prototype.toString.call(t)){case"[object Arguments]":return"(function() { return arguments; }("+A(n,t).join(", ")+"))";case"[object Array]":return"["+A(n,t).concat(r(t,vt(function(t){return/^\d+$/.test(t)},W(t)))).join(", ")+"]";case"[object Boolean]":return"object"==typeof t?"new Boolean("+n(t.valueOf())+")":t.toString();case"[object Date]":return"new Date("+(isNaN(t.valueOf())?n(NaN):lt(ht(t)))+")";case"[object Map]":return"new Map("+n(Array.from(t))+")";case"[object Null]":return"null";case"[object Number]":return"object"==typeof t?"new Number("+n(t.valueOf())+")":1/t==-1/0?"-0":t.toString(10);case"[object Set]":return"new Set("+n(Array.from(t).sort())+")";case"[object String]":return"object"==typeof t?"new String("+n(t.valueOf())+")":lt(t);case"[object Undefined]":return"undefined";default:if("function"==typeof t.toString){var o=t.toString();if("[object Object]"!==o)return o}return"{"+r(t,W(t)).join(", ")+"}"}}const bt=l(function(t){return gt(t,[])});const wt=f(function(t,e){return t||e});const kt=f(function(t,e){return d(t)?function(){return t.apply(this,arguments)||e.apply(this,arguments)}:U(wt)(t,e)});const _t=U(l(function(t){return!t}))(ut(null));const jt=f(function(t,e){if(t===e)return e;function n(t,e){if(t>e!=e>t)return e>t?e:t}var r=n(t,e);if(void 0!==r)return r;var o=n(typeof t,typeof e);if(void 0!==o)return o===typeof t?t:e;var s=bt(t),i=n(s,bt(e));return void 0!==i&&i===s?t:e}),St=Number.isInteger||function(t){return(t|0)===t};const At=f(function(t,e){var n,r;if(null!=e)return St(t)?(r=e)[(n=t)<0?r.length+n:n]:e[t]});const Ot=f(function(t,e){return R(At(t),e)});const Et=l(function(t){return p(Z(jt,0,Ot("length",t)),function(){for(var e=0,n=t.length;e<n;){if(t[e].apply(this,arguments))return!0;e+=1}return!1})});var xt=function(t,e){switch(arguments.length){case 0:return xt;case 1:return function e(n){return 0===arguments.length?e:ot(t,n)};default:return ot(t,e)}};const Pt=xt;const Mt=p(1,et(st,Pt("GeneratorFunction")));const Ft=p(1,et(st,Pt("AsyncFunction")));var Ct=Et([et(st,Pt("Function")),Mt,Ft]);function Tt(t){return Tt="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},Tt(t)}var Vt=p(1,G(_t,kt(function(t){return"object"===Tt(t)},Ct)));const Nt=p(1,G(Vt,et(bt,ut("[object Promise]"))));const zt=class{key;value;constructor(t,e){this.key=t,this.value=e}toValue(){return{key:this.key?.toValue(),value:this.value?.toValue()}}};class Lt{elements;constructor(t){this.elements=t??[]}toValue(){return this.elements.map(t=>({key:t.key?.toValue(),value:t.value?.toValue()}))}map(t,e){return this.elements.map(n=>{const r=n.value,o=n.key;if(void 0===r||void 0===o)throw new Error("MemberElement must have both key and value");return void 0!==e?t.call(e,r,o,n):t(r,o,n)})}filter(t,e){const n=this.elements.filter(n=>{const r=n.value,o=n.key;return void 0!==r&&void 0!==o&&(void 0!==e?t.call(e,r,o,n):t(r,o,n))});return new Lt(n)}reject(t,e){const n=[];for(const r of this.elements){const o=r.value,s=r.key;void 0!==o&&void 0!==s&&(t.call(e,o,s,r)||n.push(r))}return new Lt(n)}forEach(t,e){this.elements.forEach((n,r)=>{const o=n.value,s=n.key;void 0!==o&&void 0!==s&&(void 0!==e?t.call(e,o,s,n,r):t(o,s,n,r))})}find(t,e){return this.elements.find(n=>{const r=n.value,o=n.key;return void 0!==r&&void 0!==o&&(void 0!==e?t.call(e,r,o,n):t(r,o,n))})}keys(){return this.elements.map(t=>t.key?.toValue()).filter(t=>void 0!==t)}values(){return this.elements.map(t=>t.value?.toValue()).filter(t=>void 0!==t)}get length(){return this.elements.length}get isEmpty(){return 0===this.length}get first(){return this.elements[0]}get(t){return this.elements[t]}push(t){return this.elements.push(t),this}includesKey(t){return this.elements.some(e=>e.key?.equals(t))}[Symbol.iterator](){return this.elements[Symbol.iterator]()}}const Wt=Lt;class Rt{parent;startLine;startCharacter;startOffset;endLine;endCharacter;endOffset;_storedElement="element";_content;_meta;_attributes;constructor(t,e,n){void 0!==e&&(this.meta=e),void 0!==n&&(this.attributes=n),void 0!==t&&(this.content=t)}get element(){return this._storedElement}set element(t){this._storedElement=t}get content(){return this._content}set content(t){if(t instanceof Rt)this._content=t;else if(null!=t&&"string"!=typeof t&&"number"!=typeof t&&"boolean"!=typeof t&&"bigint"!=typeof t&&"symbol"!=typeof t)if(t instanceof zt)this._content=t;else if(t instanceof Wt)this._content=t.elements;else if(Array.isArray(t))this._content=t.map(t=>this.refract(t));else{if("object"!=typeof t)throw new Error("Cannot set content to value of type "+typeof t);this._content=Object.entries(t).map(([t,e])=>new this.MemberElement(t,e))}else this._content=t}get meta(){if(!this._meta){if(this.isFrozen){const t=new this.ObjectElement;return t.freeze(),t}this._meta=new this.ObjectElement}return this._meta}set meta(t){t instanceof Rt?this._meta=t:this.meta.set(t??{})}get attributes(){if(!this._attributes){if(this.isFrozen){const t=new this.ObjectElement;return t.freeze(),t}this._attributes=new this.ObjectElement}return this._attributes}set attributes(t){t instanceof Rt?this._attributes=t:this.attributes.set(t??{})}get id(){return this.getMetaProperty("id","")}set id(t){this.setMetaProperty("id",t)}get classes(){return this.getMetaProperty("classes",[])}set classes(t){this.setMetaProperty("classes",t)}get links(){return this.getMetaProperty("links",[])}set links(t){this.setMetaProperty("links",t)}get children(){const{_content:t}=this;if(Array.isArray(t))return t;if(t instanceof zt){const e=[];return t.key&&e.push(t.key),t.value&&e.push(t.value),e}return t instanceof Rt?[t]:[]}get isFrozen(){return Object.isFrozen(this)}freeze(){if(!this.isFrozen){this._meta&&(this._meta.parent=this,this._meta.freeze()),this._attributes&&(this._attributes.parent=this,this._attributes.freeze());for(const t of this.children)t.parent=this,t.freeze();Array.isArray(this._content)&&Object.freeze(this._content),Object.freeze(this)}}toValue(){const{_content:t}=this;return t instanceof Rt||t instanceof zt?t.toValue():Array.isArray(t)?t.map(t=>t.toValue()):t}equals(t){return ut(this.toValue(),t)}primitive(){}set(t){return this.content=t,this}toRef(t){const e=this.id.toValue();if(""===e)throw new Error("Cannot create reference to an element without an ID");const n=new this.RefElement(e);return"string"==typeof t&&(n.path=this.refract(t)),n}getMetaProperty(t,e){if(!this.meta.hasKey(t)){if(this.isFrozen){const t=this.refract(e);return t.freeze(),t}this.meta.set(t,e)}return this.meta.get(t)}setMetaProperty(t,e){this.meta.set(t,e)}hasMetaProperty(t){return!this.isMetaEmpty&&this.meta.hasKey(t)}get isMetaEmpty(){return void 0===this._meta||this.meta.isEmpty}getAttributesProperty(t,e){if(!this.hasAttributesProperty(t)){if(this.isFrozen){const t=this.refract(e);return t.freeze(),t}this.attributes.set(t,e)}return this.attributes.get(t)}setAttributesProperty(t,e){this.attributes.set(t,e)}hasAttributesProperty(t){return!this.isAttributesEmpty&&this.attributes.hasKey(t)}get isAttributesEmpty(){return void 0===this._attributes||this.attributes.isEmpty}}const qt=Rt;class It extends qt{constructor(t,e,n){super(t||[],e,n)}get length(){return this._content.length}get isEmpty(){return 0===this.length}get first(){return this._content[0]}get second(){return this._content[1]}get last(){return this._content.at(-1)}push(...t){for(const e of t)this._content.push(this.refract(e));return this}shift(){return this._content.shift()}unshift(t){this._content.unshift(this.refract(t))}includes(t){return this._content.some(e=>e.equals(t))}findElements(t,e){const n=e||{},r=!!n.recursive,o=void 0===n.results?[]:n.results;for(let e=0;e<this._content.length;e+=1){const n=this._content[e],s=n;r&&"function"==typeof s.findElements&&s.findElements(t,{results:o,recursive:r}),t(n,e,void 0)&&o.push(n)}return o}find(t){const e=this.findElements(t,{recursive:!0});return new this.ArrayElement(e)}findByElement(t){return this.find(e=>e.element===t)}findByClass(t){return this.find(e=>{const n=e.classes;return"function"==typeof n.includes&&n.includes(t)})}getById(t){return this.find(e=>e.id.toValue()===t).first}concat(t){return new(0,this.constructor)(this._content.concat(t._content))}[Symbol.iterator](){return this._content[Symbol.iterator]()}}const Ut=It;const Gt=class extends Ut{constructor(t,e,n){super(t||[],e,n),this.element="array"}primitive(){return"array"}get(t){return this._content[t]}getValue(t){const e=this.get(t);if(e)return e.toValue()}set(t,e){return"number"==typeof t&&void 0!==e?this._content[t]=this.refract(e):this.content=t,this}remove(t){return this._content.splice(t,1)[0]??null}map(t,e){return this._content.map(t,e)}flatMap(t,e){return this._content.flatMap(t,e)}compactMap(t,e){const n=[];for(const r of this._content){const o=t.call(e,r);o&&n.push(o)}return n}filter(t,e){const n=this._content.filter(t,e);return new this.constructor(n)}reject(t,e){const n=[];for(const r of this._content)t.call(e,r)||n.push(r);return new this.constructor(n)}reduce(t,e){let n,r;void 0!==e?(n=0,r=this.refract(e)):(n=1,r=this.first);for(let e=n;e<this.length;e+=1){const n=t(r,this._content[e],e,this);r=void 0===n?n:this.refract(n)}return r}forEach(t,e){this._content.forEach((n,r)=>{t.call(e,n,r)})}empty(){return new this.constructor([])}};const Bt=class extends qt{constructor(t,e,n,r){super(new zt,n,r),this.element="member",void 0!==t&&(this.key=t),arguments.length>=2&&(this.value=e)}primitive(){return"member"}get key(){return this._content.key}set key(t){this._content.key=this.refract(t)}get value(){return this._content.value}set value(t){this._content.value=void 0===t?void 0:this.refract(t)}};const Dt=class extends Ut{constructor(t,e,n){super(t||[],e,n),this.element="object"}primitive(){return"object"}toValue(){return this._content.reduce((t,e)=>(t[e.key.toValue()]=e.value?e.value.toValue():void 0,t),{})}get(t){const e=this.getMember(t);if(e)return e.value}getValue(t){const e=this.get(t);if(e)return e.toValue()}getMember(t){if(void 0!==t)return this._content.find(e=>e.key.toValue()===t)}remove(t){let e=null;return this.content=this._content.filter(n=>n.key.toValue()!==t||(e=n,!1)),e}getKey(t){const e=this.getMember(t);if(e)return e.key}set(t,e){if("string"==typeof t){const n=this.getMember(t);n?n.value=e:this._content.push(new Bt(t,e))}else if("object"==typeof t&&!Array.isArray(t))for(const e of Object.keys(t))this.set(e,t[e]);return this}keys(){return this._content.map(t=>t.key.toValue())}values(){return this._content.map(t=>t.value.toValue())}hasKey(t){return this._content.some(e=>e.key.equals(t))}items(){return this._content.map(t=>[t.key.toValue(),t.value.toValue()])}map(t,e){return this._content.map(n=>t.call(e,n.value,n.key,n))}compactMap(t,e){const n=[];return this.forEach((r,o,s)=>{const i=t.call(e,r,o,s);i&&n.push(i)}),n}filter(t,e){return new Wt(this._content).filter(t,e)}reject(t,e){const n=[];for(const r of this._content)t.call(e,r.value,r.key,r)||n.push(r);return new Wt(n)}forEach(t,e){this._content.forEach(n=>t.call(e,n.value,n.key,n))}reduce(t,e){let n,r;void 0!==e?(n=0,r=this.refract(e)):(n=1,r=this._content[0]?.value);for(let e=n;e<this._content.length;e+=1){const n=this._content[e],o=t(r,n.value,n.key,n,this);r=void 0===o?o:this.refract(o)}return r}empty(){return new this.constructor([])}},Kt=t=>t instanceof qt,$t=t=>t instanceof Gt,Ht=t=>t instanceof Dt,Qt=t=>t instanceof Bt,Xt=t=>"number"==typeof t.startLine&&"number"==typeof t.startCharacter&&"number"==typeof t.startOffset&&"number"==typeof t.endLine&&"number"==typeof t.endCharacter&&"number"==typeof t.endOffset;const Yt=class extends qt{constructor(t,e,n){super(t,e,n),this.element="string"}primitive(){return"string"}get length(){return this.content?.length??0}};class Zt extends Yt{constructor(t,e,n){super(t,e,n),this.element="sourceMap"}static transfer(t,e){e.startLine=t.startLine,e.startCharacter=t.startCharacter,e.startOffset=t.startOffset,e.endLine=t.endLine,e.endCharacter=t.endCharacter,e.endOffset=t.endOffset}static from(t){const{startLine:e,startCharacter:n,startOffset:r,endLine:o,endCharacter:s,endOffset:i}=t;if("number"!=typeof e||"number"!=typeof n||"number"!=typeof r||"number"!=typeof o||"number"!=typeof s||"number"!=typeof i)return;const c="sm1:"+[e,n,r,o,s,i].map(te).join("");const u=new Zt(c);return u.startLine=e,u.startCharacter=n,u.startOffset=r,u.endLine=o,u.endCharacter=s,u.endOffset=i,u}applyTo(t){this.content&&([t.startLine,t.startCharacter,t.startOffset,t.endLine,t.endCharacter,t.endOffset]=function(t){const e=t.startsWith("sm1:")?t.slice(4):t,n=[];let r=0;for(let t=0;t<6;t++){const t=ee(e,r);n.push(t.value),r=t.next}return n}(this.content))}}const Jt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";function te(t){let e=t>>>0,n="";do{let t=31&e;e>>>=5,0!==e&&(t|=32),n+=Jt[t]}while(0!==e);return n}function ee(t,e=0){let n=0,r=0,o=e;for(;;){const e=t[o++],s=Jt.indexOf(e);if(-1===s)throw new Error(`Invalid Base64 VLQ char: ${e}`);if(n|=(31&s)<<r,r+=5,!!!(32&s))break}return{value:n>>>0,next:o}}const ne=Zt;const re=class extends c{value;constructor(t,e){super(t,e),void 0!==e&&(this.value=e.value)}};const oe=class extends re{};const se=class extends re{},ie=(t,e)=>{const{visited:n=new WeakMap}=e,r={...e,visited:n};if(n.has(t))return n.get(t);const o=le(t);n.set(t,o);const{content:s}=t;return Array.isArray(s)?o.content=s.map(t=>ie(t,r)):Kt(s)?o.content=ie(s,r):o.content=s instanceof zt?ce(s,r):s,o},ce=(t,e)=>{const{visited:n=new WeakMap}=e,r={...e,visited:n};if(n.has(t))return n.get(t);const{key:o,value:s}=t,i=void 0!==o?ie(o,r):void 0,c=void 0!==s?ie(s,r):void 0,u=new zt(i,c);return n.set(t,u),u},ue=(t,e={})=>{if(t instanceof zt)return ce(t,e);if(t instanceof Wt)return((t,e)=>{const{visited:n=new WeakMap}=e,r={...e,visited:n};if(n.has(t))return n.get(t);const o=[...t].map(t=>ie(t,r)),s=new Wt(o);return n.set(t,s),s})(t,e);if(Kt(t))return ie(t,e);throw new oe("Value provided to cloneDeep function couldn't be cloned",{value:t})};ue.safe=t=>{try{return ue(t)}catch{return t}};const ae=t=>{const{key:e,value:n}=t;return new zt(e,n)},le=t=>{const e=new(0,t.constructor);e.element=t.element,t.isMetaEmpty||(e.meta=ue(t.meta)),t.isAttributesEmpty||(e.attributes=ue(t.attributes)),Xt(t)&&ne.transfer(t,e);const{content:n}=t;return Kt(n)?e.content=le(n):Array.isArray(n)?e.content=[...n]:e.content=n instanceof zt?ae(n):n,e},fe=t=>{if(t instanceof zt)return ae(t);if(t instanceof Wt)return(t=>{const e=[...t];return new Wt(e)})(t);if(Kt(t))return le(t);throw new se("Value provided to cloneShallow function couldn't be cloned",{value:t})};fe.safe=t=>{try{return fe(t)}catch{return t}};const he=t=>{const e=t?.element;return void 0===e||"element"===e?"Element":`${e.charAt(0).toUpperCase()}${e.slice(1)}Element`},pe=t=>{const e=t.primitive();return void 0===e||"element"===e?"Element":`${e.charAt(0).toUpperCase()}${e.slice(1)}Element`},de=t=>Kt(t),ye=t=>fe(t),me=(t,e,n)=>{Qt(t)?t.value=n:Array.isArray(t)?t[e]=null===n?void 0:n:null===n?delete t[e]:t[e]=n},ve=t=>Qt(t)?["key","value"]:$t(t)||Ht(t)?["content"]:[],ge=(t,e)=>{if(void 0!==t[e])return t[e];const n=e.length;for(const r in t){if(!r.includes("|"))continue;const o=r.indexOf(e);if(-1===o)continue;const s=0===o||"|"===r[o-1],i=o+n===r.length||"|"===r[o+n];if(s&&i)return t[r]}},be=(t,e,n)=>{if(void 0===e)return null;const r=t,o=n?"leave":"enter",s=ge(r,e);if(!n&&"function"==typeof s)return s;if(null!=s){const t=s[o];if("function"==typeof t)return t}const i=r[o];if("function"==typeof i)return i;if(null!=i){const t=ge(i,e);if("function"==typeof t)return t}return null},we=(t,e={})=>{const{visitFnGetter:n=be,nodeTypeGetter:r=he,exposeEdits:o=!1}=e,s=Symbol("internal-skip"),i=Symbol("break"),u=new Array(t.length).fill(s);return{enter(e){let a=e.node,l=!1;for(let f=0;f<t.length;f+=1)if(u[f]===s){const s=n(t[f],r(a),!1);if("function"==typeof s){const n=_e(e,a),r=s.call(t[f],n);if(Nt(r))throw new c("Async visitor not supported in sync mode",{visitor:t[f],visitFn:s});if(n.shouldStop){u[f]=i;break}if(n.shouldSkip&&(u[f]=a),n.removed)return void e.remove();if(n._wasReplaced()){const t=n._getReplacementNode();if(!o)return e.replaceWith(t),t;a=t,l=!0}else if(void 0!==r){if(!o)return e.replaceWith(r),r;a=r,l=!0}}}if(l)return e.replaceWith(a),a},leave(e){const o=e.node;for(let a=0;a<t.length;a+=1)if(u[a]===s){const s=n(t[a],r(o),!0);if("function"==typeof s){const n=_e(e,o),r=s.call(t[a],n);if(Nt(r))throw new c("Async visitor not supported in sync mode",{visitor:t[a],visitFn:s});if(n.shouldStop){u[a]=i;break}if(n.removed)return void e.remove();if(n._wasReplaced()){const t=n._getReplacementNode();return e.replaceWith(t),t}if(void 0!==r)return e.replaceWith(r),r}}else u[a]===o&&(u[a]=s)}}},ke=(t,e={})=>{const{visitFnGetter:n=be,nodeTypeGetter:r=he,exposeEdits:o=!1}=e,s=Symbol("internal-skip"),i=Symbol("break"),c=new Array(t.length).fill(s);return{async enter(e){let u=e.node,a=!1;for(let l=0;l<t.length;l+=1)if(c[l]===s){const s=n(t[l],r(u),!1);if("function"==typeof s){const n=_e(e,u),r=await s.call(t[l],n);if(n.shouldStop){c[l]=i;break}if(n.shouldSkip&&(c[l]=u),n.removed)return void e.remove();if(n._wasReplaced()){const t=n._getReplacementNode();if(!o)return e.replaceWith(t),t;u=t,a=!0}else if(void 0!==r){if(!o)return e.replaceWith(r),r;u=r,a=!0}}}if(a)return e.replaceWith(u),u},async leave(e){const o=e.node;for(let u=0;u<t.length;u+=1)if(c[u]===s){const s=n(t[u],r(o),!0);if("function"==typeof s){const n=_e(e,o),r=await s.call(t[u],n);if(n.shouldStop){c[u]=i;break}if(n.removed)return void e.remove();if(n._wasReplaced()){const t=n._getReplacementNode();return e.replaceWith(t),t}if(void 0!==r)return e.replaceWith(r),r}}else c[u]===o&&(c[u]=s)}}};function _e(t,e){return new n(e,t.parent,t.parentPath,t.key,t.inList)}function*je(t,e,r){const{keyMap:o,state:s,nodeTypeGetter:i,nodePredicate:u,nodeCloneFn:a,detectCycles:l,mutable:f,mutationFn:h}=r,p="function"==typeof o;let d,y,m=Array.isArray(t),v=[t],g=-1,b=[],w=t,k=null,_=null;const j=[];do{g+=1;const t=g===v.length;let r;const S=t&&0!==b.length;if(t){if(r=0===j.length?void 0:k?.key,w=y,y=j.pop(),_=k?.parentPath?.parentPath??null,S)if(f)for(const[t,e]of b)h(w,t,e);else if(m){w=w.slice();let t=0;for(const[e,n]of b){const r=e-t;null===n?(w.splice(r,1),t+=1):w[r]=n}}else{w=a(w);for(const[t,e]of b)w[t]=e}if(void 0!==d){g=d.index,v=d.keys,b=d.edits;const t=d.inArray;if(_=d.parentPath,d=d.prev,S&&!f){const e=t?g:v[g];b.push([e,w])}m=t}}else if(void 0!==y&&(r=m?g:v[g],w=y[r],void 0===w))continue;if(!Array.isArray(w)){if(!u(w))throw new c(`Invalid AST Node: ${String(w)}`,{node:w});if(l&&j.includes(w))continue;k=new n(w,y,_,r,m);const o=be(e,i(w),t);if(o){for(const[t,n]of Object.entries(s))e[t]=n;const n=yield{visitFn:o,path:k,isLeaving:t};if(k.shouldStop)break;if(k.shouldSkip&&!t)continue;if(k.removed){if(b.push([r,null]),!t)continue}else if(k._wasReplaced()){const e=k._getReplacementNode();if(b.push([r,e]),!t){if(!u(e))continue;w=e}}else if(void 0!==n&&(b.push([r,n]),!t)){if(!u(n))continue;w=n}k._markStale()}}if(!t){if(d={inArray:m,index:g,keys:v,edits:b,parentPath:_,prev:d},m=Array.isArray(w),m)v=w;else if(p)v=o(w);else{const t=i(w);v=void 0!==t?o[t]??[]:[]}g=-1,b=[],void 0!==y&&j.push(y),y=w,_=k}}while(void 0!==d);return 0!==b.length?b.at(-1)[1]:t}we[Symbol.for("nodejs.util.promisify.custom")]=ke;const Se=(t,e,n={})=>{const r=je(t,e,{keyMap:n.keyMap??ve,state:n.state??{},nodeTypeGetter:n.nodeTypeGetter??he,nodePredicate:n.nodePredicate??de,nodeCloneFn:n.nodeCloneFn??ye,detectCycles:n.detectCycles??!0,mutable:n.mutable??!1,mutationFn:n.mutationFn??me});let o=r.next();for(;!o.done;){const t=o.value,n=t.visitFn.call(e,t.path);if(Nt(n))throw new c("Async visitor not supported in sync mode",{visitor:e,visitFn:t.visitFn});o=r.next(n)}return o.value},Ae=async(t,e,n={})=>{const r=je(t,e,{keyMap:n.keyMap??ve,state:n.state??{},nodeTypeGetter:n.nodeTypeGetter??he,nodePredicate:n.nodePredicate??de,nodeCloneFn:n.nodeCloneFn??ye,detectCycles:n.detectCycles??!0,mutable:n.mutable??!1,mutationFn:n.mutationFn??me});let o=r.next();for(;!o.done;){const t=o.value,n=await t.visitFn.call(e,t.path);o=r.next(n)}return o.value};Se[Symbol.for("nodejs.util.promisify.custom")]=Ae;const Oe=(t,e)=>{const n=[];return Se(t,{enter(t){e(t.node)&&n.push(t.node)}}),n},Ee=(t,e)=>{let n;return Se(t,{enter(t){e(t.node)&&(n=t.node,t.stop())}}),n},xe=(t,e)=>void 0!==Ee(t,e),Pe=(t,e)=>Oe(t,t=>!e(t)),Me=(t,e)=>{let n,r;"function"==typeof e?(n=e,r=Kt):(n=e.callback??(()=>{}),r=e.predicate??Kt),Se(t,{enter(t){r(t.node)&&n(t.node)}})},Fe=t=>{const e=new WeakMap;return Se(t,{enter(t){e.set(t.node,t.parentPath?.node)}}),e},Ce=(t,e)=>{let n,r;"number"==typeof e?(n=e,r=!1):(n=e.offset??0,r=e.includeRightBound??!1);const o=[];return Se(t,{enter(t){const e=t.node;if(!Xt(e))return;const s=e.startOffset,i=e.endOffset;n>=s&&(n<i||r&&n<=i)?o.push(e):t.skip()}}),o.at(-1)};return n.prototype.traverse=function(t,e){return Se(this.node,t,e)},n.prototype.traverseAsync=function(t,e){return Ae(this.node,t,e)},e})());
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@speclynx/apidom-traverse",
3
+ "version": "1.12.2",
4
+ "description": "Traversal and visitor utilities for walking and transforming ApiDOM structures.",
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org",
8
+ "provenance": false
9
+ },
10
+ "type": "module",
11
+ "sideEffects": [
12
+ "./src/index.mjs",
13
+ "./src/index.cjs"
14
+ ],
15
+ "unpkg": "./dist/apidom-traverse.browser.min.js",
16
+ "main": "./src/index.cjs",
17
+ "exports": {
18
+ "types": "./types/apidom-traverse.d.ts",
19
+ "import": "./src/index.mjs",
20
+ "require": "./src/index.cjs"
21
+ },
22
+ "types": "./types/apidom-traverse.d.ts",
23
+ "scripts": {
24
+ "build": "npm run clean && run-p --max-parallel ${CPU_CORES:-2} typescript:declaration build:es build:cjs build:umd:browser",
25
+ "build:es": "cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'",
26
+ "build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'",
27
+ "build:umd:browser": "cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress",
28
+ "lint": "eslint ./",
29
+ "lint:fix": "eslint ./ --fix",
30
+ "clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types",
31
+ "typescript:check-types": "tsc --noEmit",
32
+ "typescript:declaration": "tsc -p tsconfig.declaration.json && api-extractor run -l -c ./config/api-extractor/api-extractor.json",
33
+ "test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha",
34
+ "prepack": "copyfiles -u 3 ../../LICENSES/* LICENSES && copyfiles -u 2 ../../NOTICE .",
35
+ "postpack": "rimraf NOTICE LICENSES"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/speclynx/apidom.git"
40
+ },
41
+ "author": "Vladimir Gorej",
42
+ "license": "Apache-2.0",
43
+ "dependencies": {
44
+ "@babel/runtime-corejs3": "^7.28.4",
45
+ "@speclynx/apidom-datamodel": "^1.12.2",
46
+ "@speclynx/apidom-error": "^1.12.2",
47
+ "ramda-adjunct": "^6.0.0"
48
+ },
49
+ "files": [
50
+ "src/**/*.mjs",
51
+ "src/**/*.cjs",
52
+ "dist/",
53
+ "types/apidom-traverse.d.ts",
54
+ "LICENSES",
55
+ "NOTICE",
56
+ "README.md",
57
+ "CHANGELOG.md"
58
+ ]
59
+ }
package/src/Path.cjs ADDED
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.Path = void 0;
5
+ /**
6
+ * Possible return values from a visitor function.
7
+ * @public
8
+ */
9
+
10
+ /**
11
+ * Visitor function signature - receives a Path object.
12
+ * @public
13
+ */
14
+
15
+ /**
16
+ * Path represents a node's position in the tree during traversal.
17
+ * Inspired by Babel's NodePath API.
18
+ * @public
19
+ */
20
+ class Path {
21
+ /**
22
+ * The current AST node.
23
+ */
24
+ node;
25
+
26
+ /**
27
+ * The key of this node in its parent.
28
+ * `undefined` for the root node.
29
+ */
30
+ key;
31
+
32
+ /**
33
+ * The index if this node is in an array.
34
+ * Same as `key` when parent property is an array, `undefined` otherwise.
35
+ */
36
+ index;
37
+
38
+ /**
39
+ * The parent node.
40
+ * `undefined` for the root node.
41
+ */
42
+ parent;
43
+
44
+ /**
45
+ * The parent Path.
46
+ * `null` for the root node.
47
+ */
48
+ parentPath;
49
+
50
+ /**
51
+ * Whether this node is inside an array in the parent.
52
+ */
53
+ inList;
54
+
55
+ /**
56
+ * Internal state for traversal control.
57
+ */
58
+ #shouldSkip = false;
59
+ #shouldStop = false;
60
+ #removed = false;
61
+ #replaced = false;
62
+ #replacementNode;
63
+ #stale = false;
64
+ constructor(node, parent, parentPath, key, inList) {
65
+ this.node = node;
66
+ this.parent = parent;
67
+ this.parentPath = parentPath;
68
+ this.key = key;
69
+ this.index = inList && typeof key === 'number' ? key : undefined;
70
+ this.inList = inList;
71
+ }
72
+
73
+ // ===========================================================================
74
+ // Traversal state
75
+ // ===========================================================================
76
+
77
+ /**
78
+ * Whether skip() was called on this path.
79
+ */
80
+ get shouldSkip() {
81
+ return this.#shouldSkip;
82
+ }
83
+
84
+ /**
85
+ * Whether stop() was called on this path.
86
+ */
87
+ get shouldStop() {
88
+ return this.#shouldStop;
89
+ }
90
+
91
+ /**
92
+ * Whether this node was removed.
93
+ */
94
+ get removed() {
95
+ return this.#removed;
96
+ }
97
+
98
+ // ===========================================================================
99
+ // Ancestry
100
+ // ===========================================================================
101
+
102
+ /**
103
+ * Returns true if this is the root path.
104
+ */
105
+ isRoot() {
106
+ return this.parentPath === null;
107
+ }
108
+
109
+ /**
110
+ * Get the depth of this path (0 for root).
111
+ */
112
+ get depth() {
113
+ let depth = 0;
114
+ let current = this.parentPath;
115
+ while (current !== null) {
116
+ depth += 1;
117
+ current = current.parentPath;
118
+ }
119
+ return depth;
120
+ }
121
+
122
+ /**
123
+ * Get all ancestor paths from immediate parent to root.
124
+ */
125
+ getAncestry() {
126
+ const ancestry = [];
127
+ let current = this.parentPath;
128
+ while (current !== null) {
129
+ ancestry.push(current);
130
+ current = current.parentPath;
131
+ }
132
+ return ancestry;
133
+ }
134
+
135
+ /**
136
+ * Get all ancestor nodes from immediate parent to root.
137
+ */
138
+ getAncestorNodes() {
139
+ return this.getAncestry().map(p => p.node);
140
+ }
141
+
142
+ /**
143
+ * Get the path from root as an array of keys.
144
+ */
145
+ getPathKeys() {
146
+ const keys = [];
147
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
148
+ let current = this;
149
+ while (current !== null && current.key !== undefined) {
150
+ keys.unshift(current.key);
151
+ current = current.parentPath;
152
+ }
153
+ return keys;
154
+ }
155
+
156
+ /**
157
+ * Find the closest ancestor path that satisfies the predicate.
158
+ */
159
+ findParent(predicate) {
160
+ let current = this.parentPath;
161
+ while (current !== null) {
162
+ if (predicate(current)) {
163
+ return current;
164
+ }
165
+ current = current.parentPath;
166
+ }
167
+ return null;
168
+ }
169
+
170
+ /**
171
+ * Find the closest path (including this one) that satisfies the predicate.
172
+ */
173
+ find(predicate) {
174
+ if (predicate(this)) {
175
+ return this;
176
+ }
177
+ return this.findParent(predicate);
178
+ }
179
+
180
+ // ===========================================================================
181
+ // Nested traversal
182
+ // ===========================================================================
183
+
184
+ /**
185
+ * Traverse into the current node with a new visitor.
186
+ * Populated by the traversal module to avoid circular imports.
187
+ */
188
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
189
+
190
+ /**
191
+ * Async version of traverse.
192
+ */
193
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
194
+
195
+ // ===========================================================================
196
+ // Traversal control
197
+ // ===========================================================================
198
+
199
+ /**
200
+ * Skip traversing the children of this node.
201
+ */
202
+ skip() {
203
+ this.#shouldSkip = true;
204
+ }
205
+
206
+ /**
207
+ * Stop all traversal completely.
208
+ */
209
+ stop() {
210
+ this.#shouldStop = true;
211
+ }
212
+
213
+ // ===========================================================================
214
+ // Modification
215
+ // ===========================================================================
216
+
217
+ /**
218
+ * Replace this node with a new node.
219
+ */
220
+ replaceWith(replacement) {
221
+ if (this.#stale) {
222
+ console.warn('Warning: replaceWith() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The replacement will have no effect. ' + "To replace a parent node, do so from the parent's own visitor.");
223
+ }
224
+ this.#replaced = true;
225
+ this.#replacementNode = replacement;
226
+ this.node = replacement;
227
+ }
228
+
229
+ /**
230
+ * Remove this node from the tree.
231
+ */
232
+ remove() {
233
+ if (this.#stale) {
234
+ console.warn('Warning: remove() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The removal will have no effect. ' + "To remove a parent node, do so from the parent's own visitor.");
235
+ }
236
+ this.#removed = true;
237
+ }
238
+
239
+ // ===========================================================================
240
+ // Internal methods for traversal engine
241
+ // ===========================================================================
242
+
243
+ /**
244
+ * @internal
245
+ */
246
+ _getReplacementNode() {
247
+ return this.#replacementNode;
248
+ }
249
+
250
+ /**
251
+ * @internal
252
+ */
253
+ _wasReplaced() {
254
+ return this.#replaced;
255
+ }
256
+
257
+ /**
258
+ * @internal
259
+ */
260
+ _reset() {
261
+ this.#shouldSkip = false;
262
+ this.#shouldStop = false;
263
+ this.#removed = false;
264
+ this.#replaced = false;
265
+ this.#replacementNode = undefined;
266
+ }
267
+
268
+ /**
269
+ * Mark this path as stale (visit completed).
270
+ * @internal
271
+ */
272
+ _markStale() {
273
+ this.#stale = true;
274
+ }
275
+ }
276
+ exports.Path = Path;
package/src/Path.mjs ADDED
@@ -0,0 +1,271 @@
1
+ /**
2
+ * Possible return values from a visitor function.
3
+ * @public
4
+ */
5
+
6
+ /**
7
+ * Visitor function signature - receives a Path object.
8
+ * @public
9
+ */
10
+
11
+ /**
12
+ * Path represents a node's position in the tree during traversal.
13
+ * Inspired by Babel's NodePath API.
14
+ * @public
15
+ */
16
+ export class Path {
17
+ /**
18
+ * The current AST node.
19
+ */
20
+ node;
21
+
22
+ /**
23
+ * The key of this node in its parent.
24
+ * `undefined` for the root node.
25
+ */
26
+ key;
27
+
28
+ /**
29
+ * The index if this node is in an array.
30
+ * Same as `key` when parent property is an array, `undefined` otherwise.
31
+ */
32
+ index;
33
+
34
+ /**
35
+ * The parent node.
36
+ * `undefined` for the root node.
37
+ */
38
+ parent;
39
+
40
+ /**
41
+ * The parent Path.
42
+ * `null` for the root node.
43
+ */
44
+ parentPath;
45
+
46
+ /**
47
+ * Whether this node is inside an array in the parent.
48
+ */
49
+ inList;
50
+
51
+ /**
52
+ * Internal state for traversal control.
53
+ */
54
+ #shouldSkip = false;
55
+ #shouldStop = false;
56
+ #removed = false;
57
+ #replaced = false;
58
+ #replacementNode;
59
+ #stale = false;
60
+ constructor(node, parent, parentPath, key, inList) {
61
+ this.node = node;
62
+ this.parent = parent;
63
+ this.parentPath = parentPath;
64
+ this.key = key;
65
+ this.index = inList && typeof key === 'number' ? key : undefined;
66
+ this.inList = inList;
67
+ }
68
+
69
+ // ===========================================================================
70
+ // Traversal state
71
+ // ===========================================================================
72
+
73
+ /**
74
+ * Whether skip() was called on this path.
75
+ */
76
+ get shouldSkip() {
77
+ return this.#shouldSkip;
78
+ }
79
+
80
+ /**
81
+ * Whether stop() was called on this path.
82
+ */
83
+ get shouldStop() {
84
+ return this.#shouldStop;
85
+ }
86
+
87
+ /**
88
+ * Whether this node was removed.
89
+ */
90
+ get removed() {
91
+ return this.#removed;
92
+ }
93
+
94
+ // ===========================================================================
95
+ // Ancestry
96
+ // ===========================================================================
97
+
98
+ /**
99
+ * Returns true if this is the root path.
100
+ */
101
+ isRoot() {
102
+ return this.parentPath === null;
103
+ }
104
+
105
+ /**
106
+ * Get the depth of this path (0 for root).
107
+ */
108
+ get depth() {
109
+ let depth = 0;
110
+ let current = this.parentPath;
111
+ while (current !== null) {
112
+ depth += 1;
113
+ current = current.parentPath;
114
+ }
115
+ return depth;
116
+ }
117
+
118
+ /**
119
+ * Get all ancestor paths from immediate parent to root.
120
+ */
121
+ getAncestry() {
122
+ const ancestry = [];
123
+ let current = this.parentPath;
124
+ while (current !== null) {
125
+ ancestry.push(current);
126
+ current = current.parentPath;
127
+ }
128
+ return ancestry;
129
+ }
130
+
131
+ /**
132
+ * Get all ancestor nodes from immediate parent to root.
133
+ */
134
+ getAncestorNodes() {
135
+ return this.getAncestry().map(p => p.node);
136
+ }
137
+
138
+ /**
139
+ * Get the path from root as an array of keys.
140
+ */
141
+ getPathKeys() {
142
+ const keys = [];
143
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
144
+ let current = this;
145
+ while (current !== null && current.key !== undefined) {
146
+ keys.unshift(current.key);
147
+ current = current.parentPath;
148
+ }
149
+ return keys;
150
+ }
151
+
152
+ /**
153
+ * Find the closest ancestor path that satisfies the predicate.
154
+ */
155
+ findParent(predicate) {
156
+ let current = this.parentPath;
157
+ while (current !== null) {
158
+ if (predicate(current)) {
159
+ return current;
160
+ }
161
+ current = current.parentPath;
162
+ }
163
+ return null;
164
+ }
165
+
166
+ /**
167
+ * Find the closest path (including this one) that satisfies the predicate.
168
+ */
169
+ find(predicate) {
170
+ if (predicate(this)) {
171
+ return this;
172
+ }
173
+ return this.findParent(predicate);
174
+ }
175
+
176
+ // ===========================================================================
177
+ // Nested traversal
178
+ // ===========================================================================
179
+
180
+ /**
181
+ * Traverse into the current node with a new visitor.
182
+ * Populated by the traversal module to avoid circular imports.
183
+ */
184
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
185
+
186
+ /**
187
+ * Async version of traverse.
188
+ */
189
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
190
+
191
+ // ===========================================================================
192
+ // Traversal control
193
+ // ===========================================================================
194
+
195
+ /**
196
+ * Skip traversing the children of this node.
197
+ */
198
+ skip() {
199
+ this.#shouldSkip = true;
200
+ }
201
+
202
+ /**
203
+ * Stop all traversal completely.
204
+ */
205
+ stop() {
206
+ this.#shouldStop = true;
207
+ }
208
+
209
+ // ===========================================================================
210
+ // Modification
211
+ // ===========================================================================
212
+
213
+ /**
214
+ * Replace this node with a new node.
215
+ */
216
+ replaceWith(replacement) {
217
+ if (this.#stale) {
218
+ console.warn('Warning: replaceWith() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The replacement will have no effect. ' + "To replace a parent node, do so from the parent's own visitor.");
219
+ }
220
+ this.#replaced = true;
221
+ this.#replacementNode = replacement;
222
+ this.node = replacement;
223
+ }
224
+
225
+ /**
226
+ * Remove this node from the tree.
227
+ */
228
+ remove() {
229
+ if (this.#stale) {
230
+ console.warn('Warning: remove() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The removal will have no effect. ' + "To remove a parent node, do so from the parent's own visitor.");
231
+ }
232
+ this.#removed = true;
233
+ }
234
+
235
+ // ===========================================================================
236
+ // Internal methods for traversal engine
237
+ // ===========================================================================
238
+
239
+ /**
240
+ * @internal
241
+ */
242
+ _getReplacementNode() {
243
+ return this.#replacementNode;
244
+ }
245
+
246
+ /**
247
+ * @internal
248
+ */
249
+ _wasReplaced() {
250
+ return this.#replaced;
251
+ }
252
+
253
+ /**
254
+ * @internal
255
+ */
256
+ _reset() {
257
+ this.#shouldSkip = false;
258
+ this.#shouldStop = false;
259
+ this.#removed = false;
260
+ this.#replaced = false;
261
+ this.#replacementNode = undefined;
262
+ }
263
+
264
+ /**
265
+ * Mark this path as stale (visit completed).
266
+ * @internal
267
+ */
268
+ _markStale() {
269
+ this.#stale = true;
270
+ }
271
+ }