michaelboyles / redcr

Compile-time alternative to Immer
MIT License
6 stars 0 forks source link

[BUG] All array functions but the first one are lost when there's multiple calls on the same field #6

Closed michaelboyles closed 2 years ago

michaelboyles commented 3 years ago

Describe the bug

If multiple array functions are called on the same field, all of them except the first call are dropped on the floor. In the example below, the result is the same as if the array pop wasn't there.

This reducer should probably no-op (and maybe warn?), but currently it will push the number 123.

Sample input

interface NumberArrayState {
    arr: number[]
}
const reducer = redcr((state: NumberArrayState) => {
    state.arr.push(123);
    state.arr.pop();
});

Current output

Specify which ES version you're targeting: ES2020

const reducer = (state) => {
    state = {
        ...state,
        arr: [...state.arr, 123]
    };
    return state;
};
michaelboyles commented 2 years ago

After the fix, generated code looks like:

const reducer = (state) => {
    state = {
        ...state,
        arr: [...state.arr]
    };
    return state;
};

The spreads are unnecessary but can be improved later. Ticket to remove the unnecessary spreads is here: #7