Skip to content
Myr

fix(jsx/dom): handle empty arrays in render children loop (#4729)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx/dom): handle empty arrays in render children loop When an empty array is spliced into the children list, the loop index still increments, causing the next child to be skipped. This leads to a TypeError when accessing properties of undefined nodes. Add i-- and continue after splice so the loop re-examines the same index after flattening, correctly handling empty arrays. * ci: apply automated fixes ---------

authored by mixelburg · committed by GitHub · parent a40d210 · Browse files · 2 files+20 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Maks Pikov <mixelburg@users.noreply.github.com>
  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

2 files+20 added−0 removed

  • tests:+18 added−0 removed
  • other:+2 added−0 removed

2 files+20 added−0 removed

modified:src/jsx/dom/index.test.tsx+18 added
117 unmodified lines
118 render(<App />, root)119 expect(root.innerHTML).toBe('Hello')120 })121122 it('render with empty array followed by non-empty array', () => {123 const tags: string[] = []124 const terms: string[] = ['hello']125 const App = () => (126 <div>127 {tags.map((x) => (128 <span>{x}</span>129 ))}130 {terms.map((x) => (131 <span>{x}</span>132 ))}133 <input type='text' />134 </div>135 )136 render(<App />, root)137 expect(root.innerHTML).toBe('<div><span>hello</span><input type="text"></div>')138 })139140 describe('performance', () => {141 it('should be O(N) for each additional element', () => {142 const App = () => (143 <>
2535 unmodified lines
modified:src/jsx/dom/render.ts+2 added
485 unmodified lines
486 let prevNode: Node | undefined487 for (let i = 0; i < children.length; i++) {488 if (Array.isArray(children[i])) {489 children.splice(i, 1, ...(children[i] as Child[]).flat())490 i--491 continue492 }493 let child = buildNode(children[i])494 if (child) {495 if (
297 unmodified lines