Allow imports in declare module (#315)

* Allow imports in declare module {}

* Rename bodyElement to bodyNode
This commit is contained in:
Daniel Tschinder 2017-01-20 22:19:50 +01:00 committed by GitHub
parent f2df08e253
commit d5cd2c3231
7 changed files with 177 additions and 5 deletions

View File

@ -1016,7 +1016,7 @@ pp.parseExportSpecifiers = function () {
// Parses import declaration.
pp.parseImport = function (node) {
this.next();
this.eat(tt._import);
// import '...'
if (this.match(tt.string)) {

View File

@ -95,11 +95,22 @@ pp.flowParseDeclareModule = function (node) {
const body = bodyNode.body = [];
this.expect(tt.braceL);
while (!this.match(tt.braceR)) {
const node2 = this.startNode();
let bodyNode = this.startNode();
this.expectContextual("declare", "Unexpected token. Only declares are allowed inside declare module");
if (this.match(tt._import)) {
const lookahead = this.lookahead();
if (lookahead.value !== "type" && lookahead.value !== "typeof") {
this.unexpected(null, "Imports within a `declare module` body must always be `import type` or `import typeof`");
}
body.push(this.flowParseDeclare(node2));
this.parseImport(bodyNode);
} else {
this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
bodyNode = this.flowParseDeclare(bodyNode, true);
}
body.push(bodyNode);
}
this.expect(tt.braceR);

View File

@ -1,3 +1,3 @@
{
"throws": "Unexpected token. Only declares are allowed inside declare module (2:2)"
"throws": "Only declares and type imports are allowed inside declare module (2:2)"
}

View File

@ -0,0 +1 @@
declare module "M" { import type T from "TM"; }

View File

@ -0,0 +1,156 @@
{
"type": "File",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 47
}
},
"program": {
"type": "Program",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 47
}
},
"sourceType": "module",
"body": [
{
"type": "DeclareModule",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 47
}
},
"id": {
"type": "StringLiteral",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 18
}
},
"extra": {
"rawValue": "M",
"raw": "\"M\""
},
"value": "M"
},
"body": {
"type": "BlockStatement",
"start": 19,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 47
}
},
"body": [
{
"type": "ImportDeclaration",
"start": 21,
"end": 45,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 45
}
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 33
},
"end": {
"line": 1,
"column": 34
}
},
"local": {
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 33
},
"end": {
"line": 1,
"column": 34
},
"identifierName": "T"
},
"name": "T"
}
}
],
"importKind": "type",
"source": {
"type": "StringLiteral",
"start": 40,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 40
},
"end": {
"line": 1,
"column": 44
}
},
"extra": {
"rawValue": "TM",
"raw": "\"TM\""
},
"value": "TM"
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
declare module "M" { import T from "TM"; }

View File

@ -0,0 +1,3 @@
{
"throws": "Imports within a `declare module` body must always be `import type` or `import typeof` (1:21)"
}