Skip to content

Commit

Permalink
add scope to TSModuleDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Sep 6, 2019
1 parent 8769903 commit 659621d
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 12 deletions.
8 changes: 7 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -9,6 +9,7 @@ import type Parser from "../../parser";
import {
type BindingTypes,
BIND_NONE,
SCOPE_TS_MODULE,
SCOPE_OTHER,
BIND_TS_ENUM,
BIND_TS_CONST_ENUM,
Expand Down Expand Up @@ -1163,7 +1164,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.tsParseModuleOrNamespaceDeclaration(inner, true);
node.body = inner;
} else {
this.scope.enter(SCOPE_TS_MODULE);
node.body = this.tsParseModuleBlock();
this.scope.exit();
}
return this.finishNode(node, "TSModuleDeclaration");
}
Expand All @@ -1179,9 +1182,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} else {
this.unexpected();
}

if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
node.body = this.tsParseModuleBlock();
this.scope.exit();
} else {
this.semicolon();
}
Expand Down Expand Up @@ -1332,10 +1336,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// `global { }` (with no `declare`) may appear inside an ambient module declaration.
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
const mod: N.TsModuleDeclaration = node;
mod.global = true;
mod.id = expr;
mod.body = this.tsParseModuleBlock();
this.scope.exit();
return this.finishNode(mod, "TSModuleDeclaration");
}
break;
Expand Down
23 changes: 12 additions & 11 deletions packages/babel-parser/src/util/scopeflags.js
Expand Up @@ -2,17 +2,18 @@

// Each scope gets a bitset that may contain these flags
// prettier-ignore
export const SCOPE_OTHER = 0b000000000,
SCOPE_PROGRAM = 0b000000001,
SCOPE_FUNCTION = 0b000000010,
SCOPE_ASYNC = 0b000000100,
SCOPE_GENERATOR = 0b000001000,
SCOPE_ARROW = 0b000010000,
SCOPE_SIMPLE_CATCH = 0b000100000,
SCOPE_SUPER = 0b001000000,
SCOPE_DIRECT_SUPER = 0b010000000,
SCOPE_CLASS = 0b100000000,
SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION;
export const SCOPE_OTHER = 0b0000000000,
SCOPE_PROGRAM = 0b0000000001,
SCOPE_FUNCTION = 0b0000000010,
SCOPE_ASYNC = 0b0000000100,
SCOPE_GENERATOR = 0b0000001000,
SCOPE_ARROW = 0b0000010000,
SCOPE_SIMPLE_CATCH = 0b0000100000,
SCOPE_SUPER = 0b0001000000,
SCOPE_DIRECT_SUPER = 0b0010000000,
SCOPE_CLASS = 0b0100000000,
SCOPE_TS_MODULE = 0b1000000000,
SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;

export type ScopeFlags =
| typeof SCOPE_OTHER
Expand Down
@@ -0,0 +1,5 @@
declare class foo {
}
module bar {
export var foo: any;
}
@@ -0,0 +1,232 @@
{
"type": "File",
"start": 0,
"end": 59,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 59,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "foo"
},
"name": "foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 18,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 18
},
"end": {
"line": 2,
"column": 1
}
},
"body": []
},
"declare": true
},
{
"type": "TSModuleDeclaration",
"start": 22,
"end": 59,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 10
},
"identifierName": "bar"
},
"name": "bar"
},
"body": {
"type": "TSModuleBlock",
"start": 33,
"end": 59,
"loc": {
"start": {
"line": 3,
"column": 11
},
"end": {
"line": 5,
"column": 1
}
},
"body": [
{
"type": "ExportNamedDeclaration",
"start": 37,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 2
},
"end": {
"line": 4,
"column": 22
}
},
"specifiers": [],
"source": null,
"declaration": {
"type": "VariableDeclaration",
"start": 44,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 9
},
"end": {
"line": 4,
"column": 22
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 48,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 13
},
"end": {
"line": 4,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 48,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 13
},
"end": {
"line": 4,
"column": 21
},
"identifierName": "foo"
},
"name": "foo",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 51,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 16
},
"end": {
"line": 4,
"column": 21
}
},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start": 53,
"end": 56,
"loc": {
"start": {
"line": 4,
"column": 18
},
"end": {
"line": 4,
"column": 21
}
}
}
}
},
"init": null
}
],
"kind": "var"
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,5 @@
declare class foo {
}
declare module 'bar' {
export var foo: any;
}

0 comments on commit 659621d

Please sign in to comment.