rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 194 forks source link

setIn with empty array causes stack overflow #157

Closed ethangclark closed 6 years ago

ethangclark commented 7 years ago

Description Calling Immutable().setIn with an empty path as the first argument causes a stack overflow.

Steps to reproduce Execute something like Immutable({foo: 'bar'}).setIn([], {foo: 'baz'})

Expected vs actual behavior A descriptive error should be thrown, but a stack overflow occurs instead.

Suggested fix

function objectSetIn(path, value) {
    if (path.length === 0) {
        throw new ImmutableError("You called Immutable().setIn with an empty array." +
            " At least one property path key must be provided in the array." );
    }

This definitely isn't a major issue, I just think it would be nice to cover this potential situation.

Thanks!

itaysabato commented 7 years ago

I would consider an empty path to mean "set the given value instead of this object" and simply return Immutable(value)