Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Allow imports in declare module #315

Merged
merged 2 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
17 changes: 14 additions & 3 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`");
}

this.parseImport(bodyNode);
} else {
this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");

bodyNode = this.flowParseDeclare(bodyNode, true);
}

body.push(this.flowParseDeclare(node2));
body.push(bodyNode);
}
this.expect(tt.braceR);

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/flow/declare-module/8/options.json
Original file line number Diff line number Diff line change
@@ -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)"
}
1 change: 1 addition & 0 deletions test/fixtures/flow/declare-module/import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "M" { import type T from "TM"; }
156 changes: 156 additions & 0 deletions test/fixtures/flow/declare-module/import/expected.json
Original file line number Diff line number Diff line change
@@ -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": []
}
}
1 change: 1 addition & 0 deletions test/fixtures/flow/declare-module/invalid-import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "M" { import T from "TM"; }
3 changes: 3 additions & 0 deletions test/fixtures/flow/declare-module/invalid-import/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Imports within a `declare module` body must always be `import type` or `import typeof` (1:21)"
}