ast-monkey-traverse 4.2.2 → 4.3.1

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,32 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 4.3.1 (2026-09-06)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **ast-monkey-traverse:** remove unnecessary test dependency ([013b228](https://github.com/codsen/codsen/commit/013b2283e7b554debb27e135726cb4241e2139cd))
11
+
12
+ ## 4.3.0 (2026-09-01)
13
+
14
+ ### Bug Fixes
15
+
16
+ - **ast-monkey:** preserve traversal data semantics ([861b9fc](https://github.com/codsen/codsen/commit/861b9fc242abb9e6a06c665a80e884970276b22d))
17
+ - enforce traversal tree inputs ([c539860](https://github.com/codsen/codsen/commit/c5398605891bca6e8074a9ab1cd8903489e0ddb0))
18
+ - isolate traversal parent snapshots ([10b3735](https://github.com/codsen/codsen/commit/10b3735f6d243dc831f6cf84e3be89094730b351))
19
+ - make ast traversal stack-safe ([fce2227](https://github.com/codsen/codsen/commit/fce22270cbec4406974b3b89e964ec3e78584374))
20
+ - publish sound traversal types ([03f3d46](https://github.com/codsen/codsen/commit/03f3d467978a27d41a16599a17d6cccf15d35634))
21
+ - refresh metadata after container callbacks ([a39d320](https://github.com/codsen/codsen/commit/a39d32002d4d46b19ebe234c7e46256546fae822))
22
+ - traverse compacted array entries ([815cb45](https://github.com/codsen/codsen/commit/815cb4511820d7cf409ea081ed30360c34105045))
23
+
24
+ ### Features
25
+
26
+ - expose exact traversal path segments ([7fad3cb](https://github.com/codsen/codsen/commit/7fad3cbe99b0bcb9eae2c7600ccdd2318d5c2d02))
27
+
28
+ ### Performance Improvements
29
+
30
+ - establish corrected traversal baseline ([fd8b705](https://github.com/codsen/codsen/commit/fd8b705aec8373b1cd1e9b8e11dbcaacd15bf202))
31
+
6
32
  ## 4.2.2 (2026-08-22)
7
33
 
8
34
  ### Performance Improvements
package/README.md CHANGED
@@ -21,6 +21,8 @@
21
21
  <img src="https://img.shields.io/badge/licence-MIT-brightgreen.svg?style=flat-square" alt="MIT Licence">
22
22
  </p>
23
23
 
24
+ **No dependencies whatsoever.** This package declares no dependencies or devDependencies.
25
+
24
26
  ## Install
25
27
 
26
28
  This package is [pure ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). If you're not ready yet, install an older version of this program, 2.1.0 (`npm i ast-monkey-traverse@2.1.0`).
@@ -55,12 +57,11 @@ const source = {
55
57
  };
56
58
 
57
59
  traverse(source, (key, val, innerObj) => {
58
- // if currently an object is traversed, you get both "key" and "val"
59
- // if it's array, only "key" is present, "val" is undefined
60
- let current = val !== undefined ? val : key;
60
+ // Objects supply a property key and value; arrays supply the element as key.
61
+ let current = innerObj.parentType === "object" ? val : key;
61
62
  if (
62
63
  // it's object (not array)
63
- val !== undefined &&
64
+ innerObj.parentType === "object" &&
64
65
  // and has the key we need
65
66
  key === "foo"
66
67
  ) {
@@ -77,7 +78,7 @@ assert.deepEqual(paths, ["a.foo", "a.foo.bar.0.foo", "a.foo.d.e.foo"]);
77
78
 
78
79
  ## Documentation
79
80
 
80
- Please [visit codsen.com](https://codsen.com/os/ast-monkey-traverse/) for a full description of the API. If you’re looking for the **Changelog**, it’s [here](https://github.com/codsen/codsen/blob/main/packages/ast-monkey-traverse/CHANGELOG.md).
81
+ Please [visit codsen.com](https://codsen.com/os/ast-monkey-traverse/) for a full description of the API. For **Changelog**, see [raw md on GitHub](https://github.com/codsen/codsen/blob/main/packages/ast-monkey-traverse/CHANGELOG.md) or [the website](https://codsen.com/os/ast-monkey-traverse/changelog).
81
82
 
82
83
  ## Contributing
83
84
 
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * @name ast-monkey-traverse
3
3
  * @fileoverview Utility library to traverse AST
4
- * @version 4.2.2
4
+ * @version 4.3.1
5
5
  * @author Roy Revelt
6
6
  * @license MIT
7
7
  * {@link https://codsen.com/os/ast-monkey-traverse/}
8
8
  */
9
9
 
10
- import{deepClone as f,isPlainObject as S}from"codsen-utils";var k="4.2.2";var I=k;function E(u){return typeof u=="object"&&u!==null?f(u):u}function N(u,h){if(typeof h!="function")throw new TypeError(`ast-monkey-traverse/traverse(): [THROW_ID_01] The second argument must be a callback function. It was ${typeof h}.`);let x={now:!1},i=0;function j(O,$,y,o,l,c){let n=O,v=o?o.slice(o.lastIndexOf(".")+1):null,s,p=-1,r,g=!1;if(Array.isArray(n))for(let t=0;t<n.length&&!c.now;t++){let a=n[t];if(a===void 0){n.splice(t,1),i+=1,r=void 0;continue}let d=o?`${o}.${t}`:`${t}`;p!==i&&(r===void 0?s=f(n):(s=s.slice(),g?s.splice(r,1):s[r]=E(n[r])),p=i),r=void 0;let b={depth:y,path:d,parent:s,parentType:"array",parentKey:v};l!==void 0&&(b.topmostKey=l);let e=$(a,void 0,b,c);typeof e=="object"&&e!==null&&(e!==a&&(e=f(e)),e=j(e,$,y+1,d,l,c));let m=!1;Number.isNaN(e)&&t<n.length?(n.splice(t,1),i+=1,m=!0):e!==a&&(n[t]=e,i+=1),p!==i&&(r=t,g=m),m&&(t-=1)}else if(S(n))for(let t in n){if(c.now)break;let a=o?`${o}.${t}`:t;y===0&&(l=t),p!==i&&(r===void 0?s=f(n):(s={...s},g?delete s[r]:s[r]=E(n[r])),p=i),r=void 0;let d=n[t],b={depth:y,path:a,parent:s,parentType:"object",parentKey:v};l!==void 0&&(b.topmostKey=l);let e=$(t,d,b,c);typeof e=="object"&&e!==null&&(e!==d&&(e=f(e)),e=j(e,$,y+1,a,l,c));let m=!1;Number.isNaN(e)?(delete n[t],i+=1,m=!0):e!==d&&(n[t]=e,i+=1),p!==i&&(r=t,g=m)}return n}return j(f(u),h,0,"",void 0,x)}export{N as traverse,I as version};
10
+ var $="4.3.1";var G=$,K=Symbol.for("ast-monkey-traverse.delete"),W=Object.prototype.hasOwnProperty;function f(e){throw new TypeError(`ast-monkey-traverse/traverse(): [THROW_ID_02] The first argument must be an acyclic, unaliased tree of arrays, plain objects, and supported primitive values; ${e}.`)}function B(e,n){let r=Number(e);return Number.isInteger(r)&&r>=0&&r<n&&`${r}`===e}function D(e){return Array.isArray(e)?new Array(e.length):{}}function P(e,n,r){n==="__proto__"?Object.defineProperty(e,n,{configurable:!0,enumerable:!0,value:r,writable:!0}):e[n]=r}function N(e){return e==null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function I(e,n=!1){if(N(e))return e;typeof e!="object"&&f(`encountered an unsupported ${typeof e} value`);let r=e,i=D(r),o=[{source:r,target:i}],u=new WeakSet([r]);for(;o.length;){let t=o.pop(),{source:l,target:d}=t,s=Array.isArray(l);Object.getPrototypeOf(l)!==(s?Array.prototype:Object.prototype)&&f(s?"encountered an array with a custom prototype":"encountered a non-plain or custom-prototype object");for(let a of Reflect.ownKeys(l)){if(s&&a==="length")continue;typeof a!="string"&&f(s?"encountered a symbol-keyed array property":"encountered a symbol-keyed object property"),s&&!B(a,l.length)&&f(`encountered the non-index array property ${JSON.stringify(a)}`);let c=Object.getOwnPropertyDescriptor(l,a);c.enumerable||f(s?`encountered a non-enumerable array index ${a}`:`encountered a non-enumerable property ${JSON.stringify(a)}`),!("value"in c)&&!n&&f(s?`encountered an accessor array index ${a}`:`encountered an accessor property ${JSON.stringify(a)}`);let p="value"in c?c.value:l[a];if(N(p))P(d,a,p);else if(typeof p=="object"){u.has(p)&&f("encountered a cycle or repeated object reference"),u.add(p);let g=D(p);P(d,a,g),o.push({source:p,target:g})}else f(`encountered an unsupported ${typeof p} value`)}}return i}var m=Symbol("deleted snapshot entry");function k(e){let n={array:Array.isArray(e),entries:new Map,...Array.isArray(e)?{lengths:[{value:e.length,version:0}]}:{}},r=[{source:e,target:n}];for(;r.length;){let i=r.pop(),{source:o,target:u}=i;for(let t of Object.keys(o)){let l=o[t];if(typeof l=="object"&&l!==null){let d={array:Array.isArray(l),entries:new Map,...Array.isArray(l)?{lengths:[{value:l.length,version:0}]}:{}};u.entries.set(t,[{value:d,version:0}]),r.push({source:l,target:d})}else u.entries.set(t,[{value:l,version:0}])}}return n}function F(e,n){let r=0,i=e.length-1;for(;r<i;){let o=Math.ceil((r+i)/2);e[o].version<=n?r=o:i=o-1}return e[r]}function T(e,n,r){let i=e.entries.get(n);return i?F(i,r).value:m}function V(e,n){return F(e.lengths,n).value}function O(e,n,r,i){let o=e.entries.get(n);o?o.push({value:r,version:i}):e.entries.set(n,[{value:m,version:i-1},{value:r,version:i}])}function H(e,n,r){let i=r-1,o=V(e,i);for(let u=n;u<o-1;u+=1)O(e,`${u}`,T(e,`${u+1}`,i),r);o&&O(e,`${o-1}`,m,r),e.lengths.push({value:Math.max(0,o-1),version:r})}function _(e,n){let r=new WeakMap;function i(o){let u=r.get(o);if(u)return u;let t=o.array?new Array(V(o,n)):{},l={defineProperty:()=>!0,deleteProperty:()=>!0,get:(s,a,c)=>{if(o.array&&a==="length")return V(o,n);if(typeof a=="string"){let p=T(o,a,n);if(p!==m)return typeof p=="object"&&p!==null?i(p):p}return Reflect.get(s,a,c)},getOwnPropertyDescriptor:(s,a)=>{if(o.array&&a==="length")return Reflect.getOwnPropertyDescriptor(s,a);if(typeof a=="string"){let c=T(o,a,n);return c===m?void 0:{configurable:!0,enumerable:!0,value:typeof c=="object"&&c!==null?i(c):c,writable:!0}}},has:(s,a)=>typeof a=="string"&&T(o,a,n)!==m||Reflect.has(s,a),ownKeys:()=>{let s=[...o.entries].filter(([a])=>T(o,a,n)!==m).map(([a])=>a);return o.array?[...s,"length"]:s},set:()=>!0,setPrototypeOf:()=>!0},d=new Proxy(t,l);return r.set(o,d),d}return i(e)}function M(e){let n=[],r=e;for(;r;)n.push(r.segment),r=r.parent;return n.reverse()}function J(e){let n="";for(let r of e)n=n?`${n}.${r}`:r;return n}function Q(e,n){if(typeof n!="function")throw new TypeError(`ast-monkey-traverse/traverse(): [THROW_ID_01] The second argument must be a callback function. It was ${typeof n}.`);let r=I(e);if(typeof r!="object"||r===null)return r;let i=0,o={now:!1},u=[{container:r,depth:0,index:0,keys:Array.isArray(r)?[]:Object.keys(r),parentKey:null}];for(;u.length&&!o.now;){let t=u[u.length-1],l=Array.isArray(t.container)?t.container:void 0,d=!!l,s;if(l){if(t.index>=l.length){u.pop();continue}if(!W.call(l,t.index)){t.index+=1;continue}s=`${t.index}`}else{if(t.index>=t.keys.length){u.pop();continue}s=t.keys[t.index]}let a=t.container[s],c={parent:t.pathNode,segment:s},p=t.depth===0&&!d?s:t.topmostKey,g,R=i,j,v,S;if(t.depth<100)j=t.path?`${t.path}.${s}`:s,v=t.pathSegments?[...t.pathSegments,s]:[s],S={depth:t.depth,path:j,pathSegments:v,get parent(){return t.snapshot||=k(t.container),g||=_(t.snapshot,R),g},parentType:d?"array":"object",parentKey:t.parentKey,...p===void 0?{}:{topmostKey:p}};else{let x,E;S={depth:t.depth,get path(){return x||=M(c),E??=J(x),E},get pathSegments(){return x||=M(c),x},get parent(){return t.snapshot||=k(t.container),g||=_(t.snapshot,R),g},parentType:d?"array":"object",parentKey:t.parentKey,...p===void 0?{}:{topmostKey:p}}}let y=n(d?a:s,d?void 0:a,S,o),A=y===K,w=typeof y=="object"&&y!==null,C=w&&(typeof a=="object"&&a!==null)&&y===a&&!!t.snapshot,h=y===K?a:y,b;!A&&!w&&!N(y)&&f(`encountered an unsupported ${typeof y} value`),!A&&w&&(y!==a||C)&&(h=I(y,!0),t.snapshot&&(b=k(h))),A?l?(l.splice(t.index,1),i+=1,t.snapshot&&H(t.snapshot,t.index,i)):(delete t.container[s],i+=1,t.snapshot&&O(t.snapshot,s,m,i),t.index+=1):((!Object.is(h,a)||C)&&(P(t.container,s,h),i+=1,t.snapshot&&O(t.snapshot,s,b===void 0?h:b,i)),t.index+=1,!o.now&&typeof h=="object"&&h!==null&&u.push({container:h,depth:t.depth+1,index:0,keys:Array.isArray(h)?[]:Object.keys(h),parentKey:s,...j===void 0?{}:{path:j},pathNode:c,...v===void 0?{}:{pathSegments:v},...b?{snapshot:b}:{},...p===void 0?{}:{topmostKey:p}}))}return r}export{K as DELETE,Q as traverse,G as version};
@@ -1,18 +1,10 @@
1
1
  /**
2
2
  * @name ast-monkey-traverse
3
3
  * @fileoverview Utility library to traverse AST
4
- * @version 4.2.2
4
+ * @version 4.3.1
5
5
  * @author Roy Revelt
6
6
  * @license MIT
7
7
  * {@link https://codsen.com/os/ast-monkey-traverse/}
8
8
  */
9
9
 
10
- "use strict";var astMonkeyTraverse=(()=>{var k=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames,D=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,V=Object.prototype.propertyIsEnumerable;var C=(e,t,r)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,I=(e,t)=>{for(var r in t||(t={}))E.call(t,r)&&C(e,r,t[r]);if(D)for(var r of D(t))V.call(t,r)&&C(e,r,t[r]);return e};var L=(e,t)=>{for(var r in t)k(e,r,{get:t[r],enumerable:!0})},U=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of N(t))!E.call(e,o)&&o!==r&&k(e,o,{get:()=>t[o],enumerable:!(i=M(t,o))||i.enumerable});return e};var F=e=>U(k({},"__esModule",{value:!0}),e);var O=(e,t,r)=>C(e,typeof t!="symbol"?t+"":t,r);var q={};L(q,{traverse:()=>Q,version:()=>K});var G=new Set([..."\u060B$\u20BC\u17DB\xA5\u20A1\u20B1\xA3\u20AC\xA2\u20B9\uFDFC\u20AA\u20A9\u20AD\u20A8\u20AE\u20A6\u20BD\u20AB\u0E3F\u20A9\u20BA\u20B4","$U","$b","B/.","BZ$","Br","Bs","C$","CHF","Ft","Gs","J$","KM","K\u010D","L","MT","NT$","P","Q","R","R$","RD$","RM","Rp","S","S/.","TT$","Z$","kn","kr","lei","z\u0142","\u0192","\u0414\u0438\u043D.","\u0434\u0435\u043D","\u043B\u0432","\u062F.\u0625","Lek"]);var _=class{constructor(){O(this,"hasRepeatedReferences",!1);O(this,"clones",[]);O(this,"index");O(this,"sources",[])}get(e){if(this.index){let r=this.index.get(e);return r!==void 0&&(this.hasRepeatedReferences=!0),r}let t=this.sources.indexOf(e);if(t!==-1)return this.hasRepeatedReferences=!0,this.clones[t]}set(e,t){if(this.index){this.index.set(e,t);return}if(this.sources.push(e),this.clones.push(t),this.sources.length===32){this.index=new WeakMap;for(let r=0;r<this.sources.length;r++)this.index.set(this.sources[r],this.clones[r]);this.sources=[],this.clones=[]}}};function T(e,t){let r=t.get(e);if(r!==void 0)return r;let i=e.slice(0);return t.set(e,i),i}function H(e,t){if(typeof Buffer<"u"&&Buffer.isBuffer(e)){let n=Buffer.from(e);return t.set(e,n),n}let r=T(e.buffer,t);if(e instanceof DataView){let n=new DataView(r,e.byteOffset,e.byteLength);return t.set(e,n),n}let i=e.constructor,o=new i(r,e.byteOffset,e.length);return t.set(e,o),o}function j(e,t,r,i){for(let o of r){let n=t[o];e[o]=typeof n!="object"||n===null?n:A(n,i)}}function A(e,t){if(typeof e!="object"||e===null)return e;let r=t.get(e);if(r!==void 0)return r;if(Array.isArray(e)){let n=e.length,a=new Array(n);t.set(e,a);let g=e,d=Object.keys(e);if(d.length===n&&(n===0||d[n-1]==="".concat(n-1)))for(let c=0;c<n;c++){let h=g[c];a[c]=typeof h!="object"||h===null?h:A(h,t)}else j(a,g,d,t);return a}let i=Object.getPrototypeOf(e);if(i===Object.prototype||i===null){let n={};return t.set(e,n),j(n,e,Object.keys(e),t),n}if(e instanceof Date){let n=new Date(e.getTime());return t.set(e,n),n}if(ArrayBuffer.isView(e))return H(e,t);if(e instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&e instanceof SharedArrayBuffer)return T(e,t);if(e instanceof Map){let n=new Map;t.set(e,n);for(let[a,g]of e)n.set(A(a,t),A(g,t));return n}if(e instanceof Set){let n=new Set;t.set(e,n);for(let a of e)n.add(A(a,t));return n}let o={};return t.set(e,o),j(o,e,Object.keys(e),t),o}function m(e){return A(e,new _)}function W(e){if(e==null||typeof e!="object")return!1;let t=Object.getPrototypeOf(e);return t!==null&&t!==Object.prototype&&Object.getPrototypeOf(t)!==null?!1:!(Symbol.iterator in e)&&!(Symbol.toStringTag in e)}var R,Z=(R=Object.getOwnPropertyDescriptor(RegExp.prototype,"source"))==null?void 0:R.get;var Y=Date.prototype.getTime;var P="4.2.2";var K=P;function B(e){return typeof e=="object"&&e!==null?m(e):e}function Q(e,t){if(typeof t!="function")throw new TypeError("ast-monkey-traverse/traverse(): [THROW_ID_01] The second argument must be a callback function. It was ".concat(typeof t,"."));let r={now:!1},i=0;function o(n,a,g,d,c,h){let u=n,x=d?d.slice(d.lastIndexOf(".")+1):null,p,b=-1,f,S=!1;if(Array.isArray(u))for(let s=0;s<u.length&&!h.now;s++){let y=u[s];if(y===void 0){u.splice(s,1),i+=1,f=void 0;continue}let v=d?"".concat(d,".").concat(s):"".concat(s);b!==i&&(f===void 0?p=m(u):(p=p.slice(),S?p.splice(f,1):p[f]=B(u[f])),b=i),f=void 0;let w={depth:g,path:v,parent:p,parentType:"array",parentKey:x};c!==void 0&&(w.topmostKey=c);let l=a(y,void 0,w,h);typeof l=="object"&&l!==null&&(l!==y&&(l=m(l)),l=o(l,a,g+1,v,c,h));let $=!1;Number.isNaN(l)&&s<u.length?(u.splice(s,1),i+=1,$=!0):l!==y&&(u[s]=l,i+=1),b!==i&&(f=s,S=$),$&&(s-=1)}else if(W(u))for(let s in u){if(h.now)break;let y=d?"".concat(d,".").concat(s):s;g===0&&(c=s),b!==i&&(f===void 0?p=m(u):(p=I({},p),S?delete p[f]:p[f]=B(u[f])),b=i),f=void 0;let v=u[s],w={depth:g,path:y,parent:p,parentType:"object",parentKey:x};c!==void 0&&(w.topmostKey=c);let l=a(s,v,w,h);typeof l=="object"&&l!==null&&(l!==v&&(l=m(l)),l=o(l,a,g+1,y,c,h));let $=!1;Number.isNaN(l)?(delete u[s],i+=1,$=!0):l!==v&&(u[s]=l,i+=1),b!==i&&(f=s,S=$)}return u}return o(m(e),t,0,"",void 0,r)}return F(q);})();
11
- /**
12
- * @name codsen-utils
13
- * @fileoverview Various utility functions
14
- * @version 1.9.0
15
- * @author Roy Revelt
16
- * @license MIT
17
- * {@link https://codsen.com/os/codsen-utils/}
18
- */
10
+ "use strict";var astMonkeyTraverse=(()=>{var S=Object.defineProperty,z=Object.defineProperties,U=Object.getOwnPropertyDescriptor,G=Object.getOwnPropertyDescriptors,Q=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var _=(e,r,t)=>r in e?S(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t,g=(e,r)=>{for(var t in r||(r={}))M.call(r,t)&&_(e,t,r[t]);if(I)for(var t of I(r))X.call(r,t)&&_(e,t,r[t]);return e},F=(e,r)=>z(e,G(r));var Y=(e,r)=>{for(var t in r)S(e,t,{get:r[t],enumerable:!0})},Z=(e,r,t,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Q(r))!M.call(e,o)&&o!==t&&S(e,o,{get:()=>r[o],enumerable:!(s=U(r,o))||s.enumerable});return e};var ee=e=>Z(S({},"__esModule",{value:!0}),e);var le={};Y(le,{DELETE:()=>V,traverse:()=>ie,version:()=>re});var L="4.3.1";var re=L,V=Symbol.for("ast-monkey-traverse.delete"),ne=Object.prototype.hasOwnProperty;function m(e){throw new TypeError("ast-monkey-traverse/traverse(): [THROW_ID_02] The first argument must be an acyclic, unaliased tree of arrays, plain objects, and supported primitive values; ".concat(e,"."))}function oe(e,r){let t=Number(e);return Number.isInteger(t)&&t>=0&&t<r&&"".concat(t)===e}function W(e){return Array.isArray(e)?new Array(e.length):{}}function C(e,r,t){r==="__proto__"?Object.defineProperty(e,r,{configurable:!0,enumerable:!0,value:t,writable:!0}):e[r]=t}function E(e){return e==null||typeof e=="string"||typeof e=="number"||typeof e=="boolean"}function B(e,r=!1){if(E(e))return e;typeof e!="object"&&m("encountered an unsupported ".concat(typeof e," value"));let t=e,s=W(t),o=[{source:t,target:s}],u=new WeakSet([t]);for(;o.length;){let n=o.pop(),{source:l,target:y}=n,i=Array.isArray(l);Object.getPrototypeOf(l)!==(i?Array.prototype:Object.prototype)&&m(i?"encountered an array with a custom prototype":"encountered a non-plain or custom-prototype object");for(let a of Reflect.ownKeys(l)){if(i&&a==="length")continue;typeof a!="string"&&m(i?"encountered a symbol-keyed array property":"encountered a symbol-keyed object property"),i&&!oe(a,l.length)&&m("encountered the non-index array property ".concat(JSON.stringify(a)));let c=Object.getOwnPropertyDescriptor(l,a);c.enumerable||m(i?"encountered a non-enumerable array index ".concat(a):"encountered a non-enumerable property ".concat(JSON.stringify(a))),!("value"in c)&&!r&&m(i?"encountered an accessor array index ".concat(a):"encountered an accessor property ".concat(JSON.stringify(a)));let p="value"in c?c.value:l[a];if(E(p))C(y,a,p);else if(typeof p=="object"){u.has(p)&&m("encountered a cycle or repeated object reference"),u.add(p);let f=W(p);C(y,a,f),o.push({source:p,target:f})}else m("encountered an unsupported ".concat(typeof p," value"))}}return s}var b=Symbol("deleted snapshot entry");function R(e){let r=g({array:Array.isArray(e),entries:new Map},Array.isArray(e)?{lengths:[{value:e.length,version:0}]}:{}),t=[{source:e,target:r}];for(;t.length;){let s=t.pop(),{source:o,target:u}=s;for(let n of Object.keys(o)){let l=o[n];if(typeof l=="object"&&l!==null){let y=g({array:Array.isArray(l),entries:new Map},Array.isArray(l)?{lengths:[{value:l.length,version:0}]}:{});u.entries.set(n,[{value:y,version:0}]),t.push({source:l,target:y})}else u.entries.set(n,[{value:l,version:0}])}}return r}function q(e,r){let t=0,s=e.length-1;for(;t<s;){let o=Math.ceil((t+s)/2);e[o].version<=r?t=o:s=o-1}return e[t]}function x(e,r,t){let s=e.entries.get(r);return s?q(s,t).value:b}function $(e,r){return q(e.lengths,r).value}function w(e,r,t,s){let o=e.entries.get(r);o?o.push({value:t,version:s}):e.entries.set(r,[{value:b,version:s-1},{value:t,version:s}])}function ae(e,r,t){let s=t-1,o=$(e,s);for(let u=r;u<o-1;u+=1)w(e,"".concat(u),x(e,"".concat(u+1),s),t);o&&w(e,"".concat(o-1),b,t),e.lengths.push({value:Math.max(0,o-1),version:t})}function H(e,r){let t=new WeakMap;function s(o){let u=t.get(o);if(u)return u;let n=o.array?new Array($(o,r)):{},l={defineProperty:()=>!0,deleteProperty:()=>!0,get:(i,a,c)=>{if(o.array&&a==="length")return $(o,r);if(typeof a=="string"){let p=x(o,a,r);if(p!==b)return typeof p=="object"&&p!==null?s(p):p}return Reflect.get(i,a,c)},getOwnPropertyDescriptor:(i,a)=>{if(o.array&&a==="length")return Reflect.getOwnPropertyDescriptor(i,a);if(typeof a=="string"){let c=x(o,a,r);return c===b?void 0:{configurable:!0,enumerable:!0,value:typeof c=="object"&&c!==null?s(c):c,writable:!0}}},has:(i,a)=>typeof a=="string"&&x(o,a,r)!==b||Reflect.has(i,a),ownKeys:()=>{let i=[...o.entries].filter(([a])=>x(o,a,r)!==b).map(([a])=>a);return o.array?[...i,"length"]:i},set:()=>!0,setPrototypeOf:()=>!0},y=new Proxy(n,l);return t.set(o,y),y}return s(e)}function J(e){let r=[],t=e;for(;t;)r.push(t.segment),t=t.parent;return r.reverse()}function se(e){let r="";for(let t of e)r=r?"".concat(r,".").concat(t):t;return r}function ie(e,r){if(typeof r!="function")throw new TypeError("ast-monkey-traverse/traverse(): [THROW_ID_01] The second argument must be a callback function. It was ".concat(typeof r,"."));let t=B(e);if(typeof t!="object"||t===null)return t;let s=0,o={now:!1},u=[{container:t,depth:0,index:0,keys:Array.isArray(t)?[]:Object.keys(t),parentKey:null}];for(;u.length&&!o.now;){let n=u[u.length-1],l=Array.isArray(n.container)?n.container:void 0,y=!!l,i;if(l){if(n.index>=l.length){u.pop();continue}if(!ne.call(l,n.index)){n.index+=1;continue}i="".concat(n.index)}else{if(n.index>=n.keys.length){u.pop();continue}i=n.keys[n.index]}let a=n.container[i],c={parent:n.pathNode,segment:i},p=n.depth===0&&!y?i:n.topmostKey,f,K=s,O,A,k;if(n.depth<100)O=n.path?"".concat(n.path,".").concat(i):i,A=n.pathSegments?[...n.pathSegments,i]:[i],k=g({depth:n.depth,path:O,pathSegments:A,get parent(){return n.snapshot||(n.snapshot=R(n.container)),f||(f=H(n.snapshot,K)),f},parentType:y?"array":"object",parentKey:n.parentKey},p===void 0?{}:{topmostKey:p});else{let T,v;k=g({depth:n.depth,get path(){return T||(T=J(c)),v!=null||(v=se(T)),v},get pathSegments(){return T||(T=J(c)),T},get parent(){return n.snapshot||(n.snapshot=R(n.container)),f||(f=H(n.snapshot,K)),f},parentType:y?"array":"object",parentKey:n.parentKey},p===void 0?{}:{topmostKey:p})}let d=r(y?a:i,y?void 0:a,k,o),N=d===V,P=typeof d=="object"&&d!==null,D=P&&(typeof a=="object"&&a!==null)&&d===a&&!!n.snapshot,h=d===V?a:d,j;!N&&!P&&!E(d)&&m("encountered an unsupported ".concat(typeof d," value")),!N&&P&&(d!==a||D)&&(h=B(d,!0),n.snapshot&&(j=R(h))),N?l?(l.splice(n.index,1),s+=1,n.snapshot&&ae(n.snapshot,n.index,s)):(delete n.container[i],s+=1,n.snapshot&&w(n.snapshot,i,b,s),n.index+=1):((!Object.is(h,a)||D)&&(C(n.container,i,h),s+=1,n.snapshot&&w(n.snapshot,i,j===void 0?h:j,s)),n.index+=1,!o.now&&typeof h=="object"&&h!==null&&u.push(g(g(g(F(g({container:h,depth:n.depth+1,index:0,keys:Array.isArray(h)?[]:Object.keys(h),parentKey:i},O===void 0?{}:{path:O}),{pathNode:c}),A===void 0?{}:{pathSegments:A}),j?{snapshot:j}:{}),p===void 0?{}:{topmostKey:p})))}return t}return ee(le);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ast-monkey-traverse",
3
- "version": "4.2.2",
3
+ "version": "4.3.1",
4
4
  "description": "Utility library to traverse AST",
5
5
  "keywords": [
6
6
  "ast",
@@ -79,12 +79,6 @@
79
79
  "extras": [""]
80
80
  }
81
81
  },
82
- "dependencies": {
83
- "codsen-utils": "^1.9.0"
84
- },
85
- "devDependencies": {
86
- "deep-equal": "^2.2.3"
87
- },
88
82
  "engines": {
89
83
  "node": ">=18.20.8"
90
84
  },
package/types/index.d.ts CHANGED
@@ -2,19 +2,51 @@ declare const version: string;
2
2
  interface Stop {
3
3
  now: boolean;
4
4
  }
5
+ type TreePrimitive = string | number | boolean | null | undefined;
6
+ type TreeValue = TreePrimitive | TreeArray | TreeObject;
7
+ interface TreeArray extends Array<TreeValue> {}
8
+ interface TreeObject {
9
+ [key: string]: TreeValue;
10
+ }
11
+ type ReadonlyTreeValue = TreePrimitive | ReadonlyTreeArray | ReadonlyTreeObject;
12
+ interface ReadonlyTreeArray extends ReadonlyArray<ReadonlyTreeValue> {}
13
+ interface ReadonlyTreeObject {
14
+ readonly [key: string]: ReadonlyTreeValue;
15
+ }
16
+ type ReadonlyTreeContainer = ReadonlyTreeArray | ReadonlyTreeObject;
17
+ /** Return this value from a traversal callback to delete the current node. */
18
+ declare const DELETE: unique symbol;
5
19
  interface InnerObj {
6
20
  depth: number;
7
21
  path: string;
22
+ pathSegments: readonly string[];
8
23
  topmostKey?: string;
9
- parent: any;
10
- parentType: string;
24
+ parent: ReadonlyTreeContainer;
25
+ parentType: "array" | "object";
11
26
  parentKey: string | null;
12
27
  }
13
- type Callback = (key: any, val: any, innerObj: InnerObj, stop: Stop) => any;
28
+ type Callback = (
29
+ key: string | TreeValue,
30
+ val: TreeValue | undefined,
31
+ innerObj: InnerObj,
32
+ stop: Stop,
33
+ ) => TreeValue | typeof DELETE;
14
34
  /**
15
35
  * Utility library to traverse AST
16
36
  */
17
- declare function traverse<T>(tree1: T, cb1: Callback): T;
37
+ declare function traverse(tree1: TreeValue, cb1: Callback): TreeValue;
18
38
 
19
- export { traverse, version };
20
- export type { Callback, InnerObj, Stop };
39
+ export { DELETE, traverse, version };
40
+ export type {
41
+ Callback,
42
+ InnerObj,
43
+ ReadonlyTreeArray,
44
+ ReadonlyTreeContainer,
45
+ ReadonlyTreeObject,
46
+ ReadonlyTreeValue,
47
+ Stop,
48
+ TreeArray,
49
+ TreeObject,
50
+ TreePrimitive,
51
+ TreeValue,
52
+ };