Recently started using EcmaScript 6 / 2015 and having my code transpiled with Babel. I'm also using Redux, so writing a reducer that looks like this:
const reducer = (state, action) => {
switch (action.type) {
case ACTION1:
const myvar = ''; // do something to determine myvar
return { ...state, myvar };
case ACTION2:
const myvar = ''; // do something to determine myvar
return { ...state, myvar };
default:
return state;
}
};
Obviously the real thing does more advanced things, this just illustrates the idea. My .babelrc
contains:
{
"plugins": ["transform-object-rest-spread"],
"presets": ["es2015"]
}
Now, Babel tells me:
Duplicate declaration "myvar"
I don't understand. I thought that both case
blocks would create a different scope, and thus re-using variable names for whatever reason would be allowed. Is my expectation wrong, or is this indeed supposed to work in EcmaScript 6 / 2015 and is it some kind of bug?