* Adds non-enumerable parent node reference to each node.
(obj, parent?)
| 565 | */ |
| 566 | |
| 567 | function addParent(obj, parent?) { |
| 568 | var isNode = obj && typeof obj.type === 'string'; |
| 569 | var childParent = isNode ? obj : parent; |
| 570 | |
| 571 | for (var k in obj) { |
| 572 | var value = obj[k]; |
| 573 | if (Array.isArray(value)) { |
| 574 | value.forEach(function (v) { |
| 575 | addParent(v, childParent); |
| 576 | }); |
| 577 | } else if (value && typeof value === 'object') { |
| 578 | addParent(value, childParent); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | if (isNode) { |
| 583 | Object.defineProperty(obj, 'parent', { |
| 584 | configurable: true, |
| 585 | writable: true, |
| 586 | enumerable: false, |
| 587 | value: parent || null, |
| 588 | }); |
| 589 | } |
| 590 | |
| 591 | return obj; |
| 592 | } |