add scope to TSModuleDeclaration (#10119)
This commit is contained in:
parent
599d2ff324
commit
b0acfb24dd
@ -9,6 +9,7 @@ import type Parser from "../../parser";
|
|||||||
import {
|
import {
|
||||||
type BindingTypes,
|
type BindingTypes,
|
||||||
BIND_NONE,
|
BIND_NONE,
|
||||||
|
SCOPE_TS_MODULE,
|
||||||
SCOPE_OTHER,
|
SCOPE_OTHER,
|
||||||
BIND_TS_ENUM,
|
BIND_TS_ENUM,
|
||||||
BIND_TS_CONST_ENUM,
|
BIND_TS_CONST_ENUM,
|
||||||
@ -1167,7 +1168,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
this.tsParseModuleOrNamespaceDeclaration(inner, true);
|
this.tsParseModuleOrNamespaceDeclaration(inner, true);
|
||||||
node.body = inner;
|
node.body = inner;
|
||||||
} else {
|
} else {
|
||||||
|
this.scope.enter(SCOPE_TS_MODULE);
|
||||||
node.body = this.tsParseModuleBlock();
|
node.body = this.tsParseModuleBlock();
|
||||||
|
this.scope.exit();
|
||||||
}
|
}
|
||||||
return this.finishNode(node, "TSModuleDeclaration");
|
return this.finishNode(node, "TSModuleDeclaration");
|
||||||
}
|
}
|
||||||
@ -1183,9 +1186,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
} else {
|
} else {
|
||||||
this.unexpected();
|
this.unexpected();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.match(tt.braceL)) {
|
if (this.match(tt.braceL)) {
|
||||||
|
this.scope.enter(SCOPE_TS_MODULE);
|
||||||
node.body = this.tsParseModuleBlock();
|
node.body = this.tsParseModuleBlock();
|
||||||
|
this.scope.exit();
|
||||||
} else {
|
} else {
|
||||||
this.semicolon();
|
this.semicolon();
|
||||||
}
|
}
|
||||||
@ -1337,10 +1341,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
// `global { }` (with no `declare`) may appear inside an ambient module declaration.
|
// `global { }` (with no `declare`) may appear inside an ambient module declaration.
|
||||||
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
|
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
|
||||||
if (this.match(tt.braceL)) {
|
if (this.match(tt.braceL)) {
|
||||||
|
this.scope.enter(SCOPE_TS_MODULE);
|
||||||
const mod: N.TsModuleDeclaration = node;
|
const mod: N.TsModuleDeclaration = node;
|
||||||
mod.global = true;
|
mod.global = true;
|
||||||
mod.id = expr;
|
mod.id = expr;
|
||||||
mod.body = this.tsParseModuleBlock();
|
mod.body = this.tsParseModuleBlock();
|
||||||
|
this.scope.exit();
|
||||||
return this.finishNode(mod, "TSModuleDeclaration");
|
return this.finishNode(mod, "TSModuleDeclaration");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -2,17 +2,18 @@
|
|||||||
|
|
||||||
// Each scope gets a bitset that may contain these flags
|
// Each scope gets a bitset that may contain these flags
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
export const SCOPE_OTHER = 0b000000000,
|
export const SCOPE_OTHER = 0b0000000000,
|
||||||
SCOPE_PROGRAM = 0b000000001,
|
SCOPE_PROGRAM = 0b0000000001,
|
||||||
SCOPE_FUNCTION = 0b000000010,
|
SCOPE_FUNCTION = 0b0000000010,
|
||||||
SCOPE_ASYNC = 0b000000100,
|
SCOPE_ASYNC = 0b0000000100,
|
||||||
SCOPE_GENERATOR = 0b000001000,
|
SCOPE_GENERATOR = 0b0000001000,
|
||||||
SCOPE_ARROW = 0b000010000,
|
SCOPE_ARROW = 0b0000010000,
|
||||||
SCOPE_SIMPLE_CATCH = 0b000100000,
|
SCOPE_SIMPLE_CATCH = 0b0000100000,
|
||||||
SCOPE_SUPER = 0b001000000,
|
SCOPE_SUPER = 0b0001000000,
|
||||||
SCOPE_DIRECT_SUPER = 0b010000000,
|
SCOPE_DIRECT_SUPER = 0b0010000000,
|
||||||
SCOPE_CLASS = 0b100000000,
|
SCOPE_CLASS = 0b0100000000,
|
||||||
SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION;
|
SCOPE_TS_MODULE = 0b1000000000,
|
||||||
|
SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
|
||||||
|
|
||||||
export type ScopeFlags =
|
export type ScopeFlags =
|
||||||
| typeof SCOPE_OTHER
|
| typeof SCOPE_OTHER
|
||||||
|
|||||||
5
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var-2/input.js
vendored
Normal file
5
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var-2/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
declare class foo {
|
||||||
|
}
|
||||||
|
module bar {
|
||||||
|
export var foo: any;
|
||||||
|
}
|
||||||
232
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var-2/output.json
vendored
Normal file
232
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var-2/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
5
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var/input.js
vendored
Normal file
5
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
declare class foo {
|
||||||
|
}
|
||||||
|
declare module 'bar' {
|
||||||
|
export var foo: any;
|
||||||
|
}
|
||||||
236
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var/output.json
vendored
Normal file
236
packages/babel-parser/test/fixtures/typescript/scope/module-declaration-var/output.json
vendored
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 69,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 69,
|
||||||
|
"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": 69,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 37,
|
||||||
|
"end": 42,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 20
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "bar",
|
||||||
|
"raw": "'bar'"
|
||||||
|
},
|
||||||
|
"value": "bar"
|
||||||
|
},
|
||||||
|
"body": {
|
||||||
|
"type": "TSModuleBlock",
|
||||||
|
"start": 43,
|
||||||
|
"end": 69,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 21
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ExportNamedDeclaration",
|
||||||
|
"start": 47,
|
||||||
|
"end": 67,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 22
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"specifiers": [],
|
||||||
|
"source": null,
|
||||||
|
"declaration": {
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start": 54,
|
||||||
|
"end": 67,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 22
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start": 58,
|
||||||
|
"end": 66,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 13
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 21
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 58,
|
||||||
|
"end": 66,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 13
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 21
|
||||||
|
},
|
||||||
|
"identifierName": "foo"
|
||||||
|
},
|
||||||
|
"name": "foo",
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start": 61,
|
||||||
|
"end": 66,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 16
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 21
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSAnyKeyword",
|
||||||
|
"start": 63,
|
||||||
|
"end": 66,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 18
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 21
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"init": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "var"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"declare": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user