From 62265d50e9d807ea6d171f3234c643cd3eb0c19b Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 10 Jul 2016 00:57:32 +0200 Subject: [PATCH 1/3] Add support for declare module.exports --- src/plugins/flow.js | 14 +- test/fixtures/flow/declare-module/6/actual.js | 1 + .../flow/declare-module/6/expected.json | 196 ++++++++++++++++++ test/fixtures/flow/declare-module/7/actual.js | 1 + .../flow/declare-module/7/options.json | 3 + 5 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/flow/declare-module/6/actual.js create mode 100644 test/fixtures/flow/declare-module/6/expected.json create mode 100644 test/fixtures/flow/declare-module/7/actual.js create mode 100644 test/fixtures/flow/declare-module/7/options.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 7c85da6255..f7acea3002 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -66,7 +66,11 @@ pp.flowParseDeclare = function (node) { } else if (this.match(tt._var)) { return this.flowParseDeclareVariable(node); } else if (this.isContextual("module")) { - return this.flowParseDeclareModule(node); + if (this.lookahead().type === tt.dot) { + return this.flowParseDeclareModuleExports(node); + } else { + return this.flowParseDeclareModule(node); + } } else if (this.isContextual("type")) { return this.flowParseDeclareTypeAlias(node); } else if (this.isContextual("interface")) { @@ -109,6 +113,14 @@ pp.flowParseDeclareModule = function (node) { return this.finishNode(node, "DeclareModule"); }; +pp.flowParseDeclareModuleExports = function (node) { + this.next(); // module + this.next(); // . + this.eatContextual('exports'); + node.typeAnnotation = this.flowParseTypeAnnotation(); + return this.finishNode(node, "DeclareModuleExports"); +}; + pp.flowParseDeclareTypeAlias = function (node) { this.next(); this.flowParseTypeAlias(node); diff --git a/test/fixtures/flow/declare-module/6/actual.js b/test/fixtures/flow/declare-module/6/actual.js new file mode 100644 index 0000000000..7d2db89418 --- /dev/null +++ b/test/fixtures/flow/declare-module/6/actual.js @@ -0,0 +1 @@ +declare module A { declare module.exports: { foo(): number; } } diff --git a/test/fixtures/flow/declare-module/6/expected.json b/test/fixtures/flow/declare-module/6/expected.json new file mode 100644 index 0000000000..713a1d71c1 --- /dev/null +++ b/test/fixtures/flow/declare-module/6/expected.json @@ -0,0 +1,196 @@ +{ + "type": "File", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "sourceType": "module", + "body": [ + { + "type": "DeclareModule", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "id": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "name": "A" + }, + "body": { + "type": "BlockStatement", + "start": 17, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "body": [ + { + "type": "DeclareModuleExports", + "start": 19, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 61 + } + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 41, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 61 + } + }, + "typeAnnotation": { + "type": "ObjectTypeAnnotation", + "start": 43, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 61 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 45, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "value": { + "type": "FunctionTypeAnnotation", + "start": 45, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "params": [], + "rest": null, + "typeParameters": null, + "returnType": { + "type": "NumberTypeAnnotation", + "start": 52, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 52 + }, + "end": { + "line": 1, + "column": 58 + } + } + } + }, + "key": { + "type": "Identifier", + "start": 45, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "name": "foo" + }, + "optional": false + } + ], + "indexers": [] + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/declare-module/7/actual.js b/test/fixtures/flow/declare-module/7/actual.js new file mode 100644 index 0000000000..e1dd0b0c55 --- /dev/null +++ b/test/fixtures/flow/declare-module/7/actual.js @@ -0,0 +1 @@ +declare module A { declare module.foo: { foo(): number; } } diff --git a/test/fixtures/flow/declare-module/7/options.json b/test/fixtures/flow/declare-module/7/options.json new file mode 100644 index 0000000000..a97faa10f2 --- /dev/null +++ b/test/fixtures/flow/declare-module/7/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:34)" +} From ca8142d6397c9b1b6435aee38825ba2d0399fab4 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 10 Jul 2016 10:09:07 +0200 Subject: [PATCH 2/3] Use doublequotes --- src/plugins/flow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/flow.js b/src/plugins/flow.js index f7acea3002..897ebd94f2 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -116,7 +116,7 @@ pp.flowParseDeclareModule = function (node) { pp.flowParseDeclareModuleExports = function (node) { this.next(); // module this.next(); // . - this.eatContextual('exports'); + this.eatContextual("exports"); node.typeAnnotation = this.flowParseTypeAnnotation(); return this.finishNode(node, "DeclareModuleExports"); }; From 871f4a30c07612cdb60e309f9801f0761aa57116 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 10 Jul 2016 10:17:53 +0200 Subject: [PATCH 3/3] Use expect instead of eat --- src/plugins/flow.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 897ebd94f2..e491d20e42 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -114,9 +114,9 @@ pp.flowParseDeclareModule = function (node) { }; pp.flowParseDeclareModuleExports = function (node) { - this.next(); // module - this.next(); // . - this.eatContextual("exports"); + this.expectContextual("module"); + this.expect(tt.dot); + this.expectContextual("exports"); node.typeAnnotation = this.flowParseTypeAnnotation(); return this.finishNode(node, "DeclareModuleExports"); };