From 109f48818f51197291ea73c435b32acd7db930fa Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 2 Sep 2022 23:59:00 +0800 Subject: [PATCH] fix and improve --- .../babel-parser/src/plugins/typescript/index.ts | 4 ++-- .../babel-parser/src/plugins/typescript/scope.ts | 16 ++++++---------- packages/babel-parser/src/util/scope.ts | 2 +- packages/babel-parser/src/util/scopeflags.ts | 3 +-- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 2ddb794afe01..44418d140f7b 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -20,7 +20,7 @@ import type Parser from "../../parser"; import { type BindingTypes, SCOPE_TS_MODULE, - SCOPE_TS_TOP_LEVEL, + SCOPE_OTHER, BIND_TS_ENUM, BIND_TS_CONST_ENUM, BIND_TS_TYPE, @@ -1862,7 +1862,7 @@ export default (superClass: ClassWithMixin) => tsParseModuleBlock(): N.TsModuleBlock { const node = this.startNode(); - this.scope.enter(SCOPE_TS_TOP_LEVEL); + this.scope.enter(SCOPE_OTHER); this.expect(tt.braceL); // Inside of a module block is considered "top-level", meaning it can have imports and exports. diff --git a/packages/babel-parser/src/plugins/typescript/scope.ts b/packages/babel-parser/src/plugins/typescript/scope.ts index b7669ffe9a30..f52addec598f 100644 --- a/packages/babel-parser/src/plugins/typescript/scope.ts +++ b/packages/babel-parser/src/plugins/typescript/scope.ts @@ -11,7 +11,6 @@ import { type BindingTypes, BIND_FLAGS_TS_IMPORT, SCOPE_TS_MODULE, - SCOPE_TS_TOP_LEVEL, } from "../../util/scopeflags"; import type * as N from "../../types"; import { Errors } from "../../parse-error"; @@ -48,30 +47,27 @@ export default class TypeScriptScopeHandler extends ScopeHandler 2 && this.importsStack[0].has(name); + if (this.importsStack[len - 1].has(name)) { + return true; } - - return true; + return !allowShadow && len > 1 && this.importsStack[0].has(name); } declareName(name: string, bindingType: BindingTypes, loc: Position) { diff --git a/packages/babel-parser/src/util/scope.ts b/packages/babel-parser/src/util/scope.ts index 94277cbaf603..b90137c00ad0 100644 --- a/packages/babel-parser/src/util/scope.ts +++ b/packages/babel-parser/src/util/scope.ts @@ -93,7 +93,7 @@ export default class ScopeHandler { this.scopeStack.push(this.createScope(flags)); } - exit(): ScopeFlags { + exit(): ScopeFlags | void { const scope = this.scopeStack.pop(); return scope.flags; } diff --git a/packages/babel-parser/src/util/scopeflags.ts b/packages/babel-parser/src/util/scopeflags.ts index 52e0eba7da6d..73b8dbeb1a70 100644 --- a/packages/babel-parser/src/util/scopeflags.ts +++ b/packages/babel-parser/src/util/scopeflags.ts @@ -10,8 +10,7 @@ export const SCOPE_OTHER = 0b000000000, SCOPE_CLASS = 0b001000000, SCOPE_STATIC_BLOCK = 0b010000000, SCOPE_TS_MODULE = 0b100000000, - SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE, - SCOPE_TS_TOP_LEVEL = 0b1000000000; + SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE; export type ScopeFlags = | typeof SCOPE_OTHER