-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
Bug Report
This is about an internal API to detect TDZ : _guessExecutionStatusRelativeTo
Current Behavior
- When used with
preset-env, the method_guessExecutionStatusRelativeTopredicts the execution result and is the opposite of when used withoutpreset-env
Input Code
Minimal Reproduction in this repo -
https://github.com/boopathi/babel-exec-status-bug-1
const x = 1, {y} = x;Plugin Code
module.exports = function(babel) {
const { types: t } = babel;
return {
visitor: {
ReferencedIdentifier(path) {
if (path.isBindingIdentifier()) return;
const binding = path.scope.getBinding(path.node.name);
if (!binding) return;
const execStatus = binding.path._guessExecutionStatusRelativeTo(path);
if (execStatus === "before") {
const evalResult = path.evaluate();
if (evalResult.confident) {
path.replaceWith(t.valueToNode(evalResult.value));
}
} else if (execStatus === "after") {
//
// This happens when `block scoped vars` are converted to vars
// i.e. the execStatus computes to `after` even though it actually is `before`
//
path.replaceWith(t.valueToNode(undefined));
}
}
}
};
};Expected behavior/code:
With (or) without preset-env, the behavior is expected to be the same - when the preset-env does not change the meaning of the code in this context.
Babel Configuration (.babelrc, package.json, cli command)
{
"presets": [
{ plugins: ["the above plugin"] },
"@babel/preset-env"
]
}Environment
- Babel version(s): 7.0.0-beta.47
- How you are using Babel: Node API
Possible Solution
Not sure. Without the preset-env it works as expected.
I'm not sure which one to look - preset-env plugins or the _guessExecutionStatus function. Maybe preset-env misses registration of the newly created items that _guessExecutionStatus isn't able to figure it out. I'll try to look and debug at the _guessExecutionStatusRelativeTo function more.
Additional context/Screenshots
The current TDZ evaluation is based on a custom implementation in babel-minify. I'm trying to use this API in babel-minify to get TDZ.