michaelboyles / redcr

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

[BUG] Local variables erased incorrectly when used in conditions #2

Closed michaelboyles closed 3 years ago

michaelboyles commented 3 years ago

Describe the bug

When consts are used in conditions, the local variable is erased but still used within the condition.

Sample input

interface StringState {
    str: string;
}

const reducer = redcr((state: StringState) => {
    const condition = false;
    if (condition) {
        state.str = 'foo';
    }
});

Current output

ES2020

const reducer = (state) => {
    if (condition) {
        return {
            ...state,
            str: 'foo'
        };
    }
    return state;
};
michaelboyles commented 3 years ago

Output after fix:

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