michaelboyles / redcr

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

[FEATURE] Don't reassign reducer parameter when there are no conditions #8

Open michaelboyles opened 2 years ago

michaelboyles commented 2 years ago

As a consequence of the code which handles code paths (if conditions etc.), Redcr always ends up reassigning the parameter, regardless of whether it actually needs to. For example

interface StringState {
    str: String
}
const reducer = redcr((state: StringState) => {
    state.str = 'foo';
});

The generated code looks like this

const reducer = (state) => {
    state = {
        ...state,
        str: 'foo'
    };
    return state;
};

It would be better if it returned this

const reducer = (state) => {
    return {
        ...state,
        str: 'foo'
    };
};