diff --git a/src/acorn/plugins/flow.js b/src/acorn/plugins/flow.js index 387634f84c..c15b597f64 100644 --- a/src/acorn/plugins/flow.js +++ b/src/acorn/plugins/flow.js @@ -16,6 +16,15 @@ pp.expectRelational = function (op) { } } +pp.flow_parseTypeInitialiser = function (tok) { + var oldInType = this.inType + this.inType = true + this.expect(tok || tt.colon) + var type = this.flow_parseType() + this.inType = oldInType + return type; +} + pp.flow_parseDeclareClass = function (node) { this.next() this.flow_parseInterfaceish(node, true) @@ -41,12 +50,7 @@ pp.flow_parseDeclareFunction = function (node) { typeNode.params = tmp.params typeNode.rest = tmp.rest this.expect(tt.parenR) - - var oldInType = this.inType - this.inType = true - this.expect(tt.colon) - typeNode.returnType = this.flow_parseType() - this.inType = oldInType + typeNode.returnType = this.flow_parseTypeInitialiser() typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation") id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation") @@ -157,15 +161,7 @@ pp.flow_parseTypeAlias = function (node) { node.typeParameters = null } - var oldInType = this.inType; - this.inType = true; - - this.expect(tt.eq) - - node.right = this.flow_parseType() - - this.inType = oldInType; - + node.right = this.flow_parseTypeInitialiser(tt.eq) this.semicolon() return this.finishNode(node, "TypeAlias") @@ -218,11 +214,9 @@ pp.flow_parseObjectTypeIndexer = function (node, isStatic) { this.expect(tt.bracketL) node.id = this.flow_parseObjectPropertyKey() - this.expect(tt.colon) - node.key = this.flow_parseType() + node.key = this.flow_parseTypeInitialiser() this.expect(tt.bracketR) - this.expect(tt.colon) - node.value = this.flow_parseType() + node.value = this.flow_parseTypeInitialiser() this.flow_objectTypeSemicolon() return this.finishNode(node, "ObjectTypeIndexer") @@ -249,8 +243,7 @@ pp.flow_parseObjectTypeMethodish = function (node) { node.rest = this.flow_parseFunctionTypeParam() } this.expect(tt.parenR) - this.expect(tt.colon) - node.returnType = this.flow_parseType() + node.returnType = this.flow_parseTypeInitialiser() return this.finishNode(node, "FunctionTypeAnnotation") } @@ -269,7 +262,7 @@ pp.flow_parseObjectTypeCallProperty = function (node, isStatic) { var valueNode = this.startNode() node.static = isStatic node.value = this.flow_parseObjectTypeMethodish(valueNode) - this.flow_objectTypeSemicolon() + this.flow_objectTypeSemicolon() return this.finishNode(node, "ObjectTypeCallProperty") } @@ -314,9 +307,8 @@ pp.flow_parseObjectType = function (allowStatic) { if (this.eat(tt.question)) { optional = true } - this.expect(tt.colon) node.key = propertyKey - node.value = this.flow_parseType() + node.value = this.flow_parseTypeInitialiser() node.optional = optional node.static = isStatic this.flow_objectTypeSemicolon() @@ -384,9 +376,8 @@ pp.flow_parseFunctionTypeParam = function () { if (this.eat(tt.question)) { optional = true } - this.expect(tt.colon) node.optional = optional - node.typeAnnotation = this.flow_parseType() + node.typeAnnotation = this.flow_parseTypeInitialiser() return this.finishNode(node, "FunctionTypeParam") } @@ -583,13 +574,7 @@ pp.flow_parseType = function () { pp.flow_parseTypeAnnotation = function () { var node = this.startNode() - - var oldInType = this.inType - this.inType = true - this.expect(tt.colon) - node.typeAnnotation = this.flow_parseType() - this.inType = oldInType - + node.typeAnnotation = this.flow_parseTypeInitialiser() return this.finishNode(node, "TypeAnnotation") } diff --git a/test/acorn/tests-flow.js b/test/acorn/tests-flow.js index 1690329c69..ac0ea347d5 100644 --- a/test/acorn/tests-flow.js +++ b/test/acorn/tests-flow.js @@ -9965,6 +9965,95 @@ var fbTestFixture = { end: { line: 1, column: 49 } } }, + 'declare class IViewFactory { didAnimate(view:Object, prop:string) :void; }': { + start: 0, + id: { + start: 14, + name: "IViewFactory", + type: "Identifier", + end: 26 + }, + typeParameters: null, + extends: [], + body: { + start: 27, + callProperties: [], + properties: [ + { + start: 29, + value: { + start: 29, + params: [ + { + start: 40, + name: { + start: 40, + name: "view", + type: "Identifier", + end: 44 + }, + optional: false, + typeAnnotation: { + start: 45, + typeParameters: null, + id: { + start: 45, + name: "Object", + type: "Identifier", + end: 51 + }, + type: "GenericTypeAnnotation", + end: 51 + }, + type: "FunctionTypeParam", + end: 51 + }, + { + start: 53, + name: { + start: 53, + name: "prop", + type: "Identifier", + end: 57 + }, + optional: false, + typeAnnotation: { + start: 58, + type: "StringTypeAnnotation", + end: 64 + }, + type: "FunctionTypeParam", + end: 64 + } + ], + rest: null, + typeParameters: null, + returnType: { + start: 67, + type: "VoidTypeAnnotation", + end: 71 + }, + type: "FunctionTypeAnnotation", + end: 71 + }, + key: { + start: 29, + name: "didAnimate", + type: "Identifier", + end: 39 + }, + optional: false, + type: "ObjectTypeProperty", + end: 72 + } + ], + indexers: [], + type: "ObjectTypeAnnotation", + end: 74 + }, + type: "DeclareClass", + end: 74 + }, 'declare class A {}': { type: 'DeclareClass', id: {