Skip to content

Commit

Permalink
fix and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Sep 2, 2022
1 parent 27aa464 commit 109f488
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -1862,7 +1862,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>

tsParseModuleBlock(): N.TsModuleBlock {
const node = this.startNode<N.TsModuleBlock>();
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.
Expand Down
16 changes: 6 additions & 10 deletions packages/babel-parser/src/plugins/typescript/scope.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -48,30 +47,27 @@ export default class TypeScriptScopeHandler extends ScopeHandler<TypeScriptScope
}

enter(flags: number): void {
if (flags == SCOPE_TS_MODULE || flags == SCOPE_TS_TOP_LEVEL) {
if (flags == SCOPE_TS_MODULE) {
this.importsStack.push(new Set());
}

super.enter(flags);
}

exit(): ScopeFlags {
exit() {
const flags = super.exit();

if (flags == SCOPE_TS_MODULE || flags == SCOPE_TS_TOP_LEVEL) {
if (flags == SCOPE_TS_MODULE) {
this.importsStack.pop();
}

return flags;
}

hasImport(name: string, allowShadow?: boolean) {
const len = this.importsStack.length;
if (!this.importsStack[len - 1].has(name)) {
return !allowShadow && len > 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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/util/scope.ts
Expand Up @@ -93,7 +93,7 @@ export default class ScopeHandler<IScope extends Scope = Scope> {
this.scopeStack.push(this.createScope(flags));
}

exit(): ScopeFlags {
exit(): ScopeFlags | void {
const scope = this.scopeStack.pop();
return scope.flags;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-parser/src/util/scopeflags.ts
Expand Up @@ -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
Expand Down

0 comments on commit 109f488

Please sign in to comment.