From 6627deb88de4c439d5f12cf48f0dce29851412fd Mon Sep 17 00:00:00 2001 From: Fadhlan Date: Sun, 22 Nov 2026 20:40:37 +0700 Subject: [PATCH 1/2] fix: revive nested values before the parse reviver runs Since 3.4.0 the parse queue swaps every nested container for the shared `ignore` placeholder and calls the reviver on the parent before those placeholders are filled in, so a reviver that reads a nested value (or builds a new object from it) sees empty objects and the revived children are assigned to the discarded instance. Revive every queued container first, then walk the queue backwards to hand each container to the reviver. A child is always queued after its parent, so the reverse order assigns bottom-up without recursion and each reviver receives fully revived nested values, matching JSON.parse. Fixes #109 --- cjs/index.js | 14 ++++++++++---- es.js | 2 +- esm.js | 2 +- esm/index.js | 14 ++++++++++---- index.js | 14 ++++++++++---- min.js | 2 +- test/index.js | 30 ++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 15 deletions(-) diff --git a/cjs/index.js b/cjs/index.js index a4833a5..4990689 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -65,11 +65,17 @@ const parse = (text, reviver) => { const revive = resolver(input, lazy, new Set, $); value = revive(value); + // Revive all containers first so that each reviver receives nested + // values that are already revived. let i = 0; - while (i < lazy.length) { - // it could be a lazy.shift() but that's costly - const {o, k, r} = lazy[i++]; - o[k] = $.call(o, k, revive(r)); + while (i < lazy.length) + revive(lazy[i++].r); + + // Parents are always queued before their children, so walking the + // queue backwards assigns containers bottom-up without recursion. + while (i--) { + const {o, k, r} = lazy[i]; + o[k] = $.call(o, k, r); } } diff --git a/es.js b/es.js index 3e4d187..69a0cd7 100644 --- a/es.js +++ b/es.js @@ -1 +1 @@ -self.Flatted=function(t){"use strict";const{parse:e,stringify:n}=JSON,{keys:r}=Object,o=String,s="string",c={},l="object",f=(t,e)=>e,i=t=>t instanceof o?o(t):t,a=(t,e)=>typeof e===s?new o(e):e,u=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},p=(t,n)=>{const s=e(t,a).map(i),u=n||f;let p=s[0];if(typeof p===l&&p){const t=[],e=((t,e,n,s)=>f=>{for(let i=r(f),{length:a}=i,u=0;u{const o=e&&typeof e===l?(t,n)=>""===t||-1p(n(t)),t.parse=p,t.stringify=g,t.toJSON=t=>e(g(t)),t}({}); +self.Flatted=function(t){"use strict";const{parse:e,stringify:n}=JSON,{keys:r}=Object,o=String,s="string",c={},l="object",f=(t,e)=>e,i=t=>t instanceof o?o(t):t,a=(t,e)=>typeof e===s?new o(e):e,u=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},p=(t,n)=>{const s=e(t,a).map(i),u=n||f;let p=s[0];if(typeof p===l&&p){const t=[],e=((t,e,n,s)=>f=>{for(let i=r(f),{length:a}=i,u=0;u{const o=e&&typeof e===l?(t,n)=>""===t||-1p(n(t)),t.parse=p,t.stringify=g,t.toJSON=t=>e(g(t)),t}({}); diff --git a/esm.js b/esm.js index 2563a41..92acf36 100644 --- a/esm.js +++ b/esm.js @@ -1 +1 @@ -const{parse:t,stringify:e}=JSON,{keys:n}=Object,o=String,r="string",s={},c="object",l=(t,e)=>e,f=t=>t instanceof o?o(t):t,i=(t,e)=>typeof e===r?new o(e):e,a=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},u=(e,r)=>{const a=t(e,i).map(f),u=r||l;let p=a[0];if(typeof p===c&&p){const t=[],e=((t,e,r,l)=>f=>{for(let i=n(f),{length:a}=i,u=0;u{const s=n&&typeof n===c?(t,e)=>""===t||-1t(p(e)),g=t=>u(e(t));export{g as fromJSON,u as parse,p as stringify,h as toJSON}; +const{parse:t,stringify:e}=JSON,{keys:n}=Object,r=String,o="string",s={},c="object",l=(t,e)=>e,f=t=>t instanceof r?r(t):t,i=(t,e)=>typeof e===o?new r(e):e,a=(t,e,n)=>{const o=r(e.push(n)-1);return t.set(n,o),o},u=(e,o)=>{const a=t(e,i).map(f),u=o||l;let p=a[0];if(typeof p===c&&p){const t=[],e=((t,e,o,l)=>f=>{for(let i=n(f),{length:a}=i,u=0;u{const s=n&&typeof n===c?(t,e)=>""===t||-1t(p(e)),g=t=>u(e(t));export{g as fromJSON,u as parse,p as stringify,h as toJSON}; diff --git a/esm/index.js b/esm/index.js index eb2da2d..a5b8582 100644 --- a/esm/index.js +++ b/esm/index.js @@ -64,11 +64,17 @@ export const parse = (text, reviver) => { const revive = resolver(input, lazy, new Set, $); value = revive(value); + // Revive all containers first so that each reviver receives nested + // values that are already revived. let i = 0; - while (i < lazy.length) { - // it could be a lazy.shift() but that's costly - const {o, k, r} = lazy[i++]; - o[k] = $.call(o, k, revive(r)); + while (i < lazy.length) + revive(lazy[i++].r); + + // Parents are always queued before their children, so walking the + // queue backwards assigns containers bottom-up without recursion. + while (i--) { + const {o, k, r} = lazy[i]; + o[k] = $.call(o, k, r); } } diff --git a/index.js b/index.js index 9386548..5e3e933 100644 --- a/index.js +++ b/index.js @@ -73,14 +73,20 @@ self.Flatted = (function (exports) { var lazy = []; var revive = resolver(input, lazy, new Set(), $); value = revive(value); + + // Revive all containers first so that each reviver receives nested + // values that are already revived. var i = 0; - while (i < lazy.length) { - // it could be a lazy.shift() but that's costly - var _lazy$i = lazy[i++], + while (i < lazy.length) revive(lazy[i++].r); + + // Parents are always queued before their children, so walking the + // queue backwards assigns containers bottom-up without recursion. + while (i--) { + var _lazy$i = lazy[i], o = _lazy$i.o, k = _lazy$i.k, r = _lazy$i.r; - o[k] = $.call(o, k, revive(r)); + o[k] = $.call(o, k, r); } } return $.call({ diff --git a/min.js b/min.js index d3a97cb..520caec 100644 --- a/min.js +++ b/min.js @@ -1 +1 @@ -self.Flatted=function(n){"use strict";function t(n){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},t(n)}var r=JSON.parse,e=JSON.stringify,o=Object.keys,u=String,f="string",i={},c="object",a=function(n,t){return t},l=function(n){return n instanceof u?u(n):n},s=function(n,r){return t(r)===f?new u(r):r},y=function(n,t,r){var e=u(t.push(r)-1);return n.set(r,e),e},p=function(n,e){var f=r(n,s).map(l),y=e||a,p=f[0];if(t(p)===c&&p){var v=[],S=function(n,r,e,f){return function(a){for(var l=o(a),s=l.length,y=0;y Date: Sun, 22 Nov 2026 20:41:54 +0700 Subject: [PATCH 2/2] docs: state the parse queue ordering invariant precisely A container is queued before the containers it first references, which is what makes the reverse walk safe; it is not a general parent-before-child order for shared references. --- cjs/index.js | 5 +++-- esm/index.js | 5 +++-- index.js | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cjs/index.js b/cjs/index.js index 4990689..0232813 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -71,8 +71,9 @@ const parse = (text, reviver) => { while (i < lazy.length) revive(lazy[i++].r); - // Parents are always queued before their children, so walking the - // queue backwards assigns containers bottom-up without recursion. + // A container is always queued before the containers it owns, so a + // reverse walk fills nested references in before their parent is + // passed to the reviver, without recursion. while (i--) { const {o, k, r} = lazy[i]; o[k] = $.call(o, k, r); diff --git a/esm/index.js b/esm/index.js index a5b8582..2f27115 100644 --- a/esm/index.js +++ b/esm/index.js @@ -70,8 +70,9 @@ export const parse = (text, reviver) => { while (i < lazy.length) revive(lazy[i++].r); - // Parents are always queued before their children, so walking the - // queue backwards assigns containers bottom-up without recursion. + // A container is always queued before the containers it owns, so a + // reverse walk fills nested references in before their parent is + // passed to the reviver, without recursion. while (i--) { const {o, k, r} = lazy[i]; o[k] = $.call(o, k, r); diff --git a/index.js b/index.js index 5e3e933..aa52c6e 100644 --- a/index.js +++ b/index.js @@ -79,8 +79,9 @@ self.Flatted = (function (exports) { var i = 0; while (i < lazy.length) revive(lazy[i++].r); - // Parents are always queued before their children, so walking the - // queue backwards assigns containers bottom-up without recursion. + // A container is always queued before the containers it owns, so a + // reverse walk fills nested references in before their parent is + // passed to the reviver, without recursion. while (i--) { var _lazy$i = lazy[i], o = _lazy$i.o,