diff --git a/packages/babel-plugin-transform-block-scoping/src/index.ts b/packages/babel-plugin-transform-block-scoping/src/index.ts index 9b578fcf5409..0892eb01cf29 100644 --- a/packages/babel-plugin-transform-block-scoping/src/index.ts +++ b/packages/babel-plugin-transform-block-scoping/src/index.ts @@ -75,8 +75,13 @@ export default declare((api, opts: Options) => { for (const name of names) { if (bodyScope?.hasOwnBinding(name)) continue; // shadowed + let binding = headPath.scope.getOwnBinding(name); + if (!binding) { + headPath.scope.crawl(); + binding = headPath.scope.getOwnBinding(name); + } const { usages, capturedInClosure, hasConstantViolations } = - getUsageInBody(headPath.scope.getOwnBinding(name), path); + getUsageInBody(binding, path); if (capturedInClosure) { markNeedsBodyWrap(); @@ -169,7 +174,9 @@ function transformBlockScopedVariable( const bindingNames = Object.keys(path.getBindingIdentifiers()); for (const name of bindingNames) { - path.scope.getOwnBinding(name).kind = "var"; + const binding = path.scope.getOwnBinding(name); + if (!binding) continue; + binding.kind = "var"; } if (