benjycui / benjycui/jsonml.js

Why does utils.appendChild allow an empty parent array?

Open
#11 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12
Forks
7
PR merge metrics
No merged PRs in 30d

Description

When writing tests for the utils methods, I came across some logic that was confusing:
https://github.com/benjycui/jsonml.js/blob/master/lib/utils.js#L132

```js
} else if (child || !parent.length) {
// append
parent.push(child);
```

This allows for some unclear behavior, particularly when allowing exception for a parent with no length.

1. append a child to an empty parent array
```js
const jml = [];
utils.appendChild(jml, '');
// jml === ['']
```
```js
const jml = [];
utils.appendChild(jml, ['p', 'hello']);
// jml === [ ['p', 'hello'] ]
```
Is this a real use case or an edge case bug? I'm guessing this would never be desired and should probably not be allowed.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at lib/utils.js:132 and inspect the surrounding appendChild logic and its existing tests, if any. Reproduce the empty-parent examples from the issue, then determine the intended behavior for empty parents and document that decision with regression coverage; done means the behavior is explicit and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.