parse void as an identifier when inside a type annotation to avoid setting void keyword token - cc @DmitrySoshnikov

This commit is contained in:
Sebastian McKenzie 2015-06-24 23:15:00 +01:00
parent 23ec1a455e
commit aa25903c05
3 changed files with 21 additions and 15 deletions

View File

@ -1,3 +1,4 @@
Error.stackTraceLimit = Infinity;
var acorn = require("../src/index") var acorn = require("../src/index")
var pp = acorn.Parser.prototype var pp = acorn.Parser.prototype
@ -352,12 +353,6 @@ pp.flow_parseGenericType = function (start, id) {
return this.finishNode(node, "GenericTypeAnnotation") return this.finishNode(node, "GenericTypeAnnotation")
} }
pp.flow_parseVoidType = function () {
var node = this.startNode()
this.expect(tt._void)
return this.finishNode(node, "VoidTypeAnnotation")
}
pp.flow_parseTypeofType = function () { pp.flow_parseTypeofType = function () {
var node = this.startNode() var node = this.startNode()
this.expect(tt._typeof) this.expect(tt._typeof)
@ -411,6 +406,9 @@ pp.flow_identToTypeAnnotation = function (start, node, id) {
case "any": case "any":
return this.finishNode(node, "AnyTypeAnnotation") return this.finishNode(node, "AnyTypeAnnotation")
case "void":
return this.finishNode(node, "VoidTypeAnnotation")
case "bool": case "bool":
case "boolean": case "boolean":
return this.finishNode(node, "BooleanTypeAnnotation") return this.finishNode(node, "BooleanTypeAnnotation")
@ -524,16 +522,10 @@ pp.flow_parsePrimaryType = function () {
return this.finishNode(node, "StringLiteralTypeAnnotation") return this.finishNode(node, "StringLiteralTypeAnnotation")
default: default:
if (this.type.keyword) { if (this.type.keyword === "typeof") {
switch (this.type.keyword) {
case "void":
return this.flow_parseVoidType()
case "typeof":
return this.flow_parseTypeofType() return this.flow_parseTypeofType()
} }
} }
}
this.unexpected() this.unexpected()
} }
@ -694,6 +686,14 @@ acorn.plugins.flow = function (instance) {
} }
}) })
// don't consider `void` to be a keyword as then it'll use the void token type
// and set startExpr
instance.extend("isKeyword", function (inner) {
return function(name) {
return name !== "void" && inner.call(this, name)
}
})
instance.extend("readToken", function (inner) { instance.extend("readToken", function (inner) {
return function(code) { return function(code) {
if (this.inType && (code === 62 || code === 60)) { if (this.inType && (code === 62 || code === 60)) {

View File

@ -4,11 +4,11 @@ import {lineBreak} from "./whitespace"
export function Parser(options, input, startPos) { export function Parser(options, input, startPos) {
this.options = options this.options = options
this.loadPlugins(this.options.plugins)
this.sourceFile = this.options.sourceFile || null this.sourceFile = this.options.sourceFile || null
this.isKeyword = keywords[this.options.ecmaVersion >= 6 ? 6 : 5] this.isKeyword = keywords[this.options.ecmaVersion >= 6 ? 6 : 5]
this.isReservedWord = reservedWords[this.options.ecmaVersion] this.isReservedWord = reservedWords[this.options.ecmaVersion]
this.input = input this.input = input
this.loadPlugins(this.options.plugins)
// Set up token state // Set up token state

View File

@ -11148,3 +11148,9 @@ for (var ns in fbTestFixture) {
}); });
} }
} }
test("<Foo foo={function (): void {}} />", {}, {
ecmaVersion: 6,
sourceType: "module",
plugins: { jsx: true, flow: true },
});