michaelboyles / redcr

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

[FEATURE] Don't spread array unnecessarily when using inverse array operations #7

Open michaelboyles opened 2 years ago

michaelboyles commented 2 years ago

When executing array operations which are the inverse of another such as

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

The generated code looks like this:

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

Both the ...state.arr spread and the ...state spread are pointless. It would be better if Redcr was smart enough to just remove them