From f4e07cc5762cdf6dcd8f3d2a11ea678d2a696bc7 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 23 Dec 2022 08:39:44 +0800 Subject: [PATCH] fix: Do not throw exceptions when scope information is corrupted (#15303) fix --- .../babel-plugin-transform-block-scoping/src/index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 (