Fix negative number literal typeannotations (#366)

* Fix negative number literal typeannotations

Also use parseLiteral() to parser string and number literal typeannotations
so that future changes (estree) to literals are also reflected to flow.

* Instead of invalid fallthrough throw immediately

* Increase coverage and better error mesage
This commit is contained in:
Daniel Tschinder
2017-02-20 22:43:59 +01:00
committed by GitHub
parent 57aaceaae7
commit 2ef00a6631
13 changed files with 47 additions and 47 deletions

View File

@@ -545,10 +545,13 @@ pp.parseMetaProperty = function (node, meta, propertyName) {
return this.finishNode(node, "MetaProperty");
};
pp.parseLiteral = function (value, type) {
const node = this.startNode();
pp.parseLiteral = function (value, type, startPos, startLoc) {
startPos = startPos || this.state.start;
startLoc = startLoc || this.state.startLoc;
const node = this.startNodeAt(startPos, startLoc);
this.addExtra(node, "rawValue", value);
this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
node.value = value;
this.next();
return this.finishNode(node, type);

View File

@@ -7,7 +7,7 @@ const pp = Parser.prototype;
const commentKeys = ["leadingComments", "trailingComments", "innerComments"];
class Node {
constructor(pos?: number, loc?: SourceLocation, filename?: string) {
constructor(pos?: number, loc?: number, filename?: string) {
this.type = "";
this.start = pos;
this.end = 0;

View File

@@ -704,11 +704,7 @@ pp.flowParsePrimaryType = function () {
return this.finishNode(node, "FunctionTypeAnnotation");
case tt.string:
node.value = this.state.value;
this.addExtra(node, "rawValue", node.value);
this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
this.next();
return this.finishNode(node, "StringLiteralTypeAnnotation");
return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
case tt._true: case tt._false:
node.value = this.match(tt._true);
@@ -718,21 +714,14 @@ pp.flowParsePrimaryType = function () {
case tt.plusMin:
if (this.state.value === "-") {
this.next();
if (!this.match(tt.num)) this.unexpected();
if (!this.match(tt.num)) this.unexpected(null, "Unexpected token, expected number");
node.value = -this.state.value;
this.addExtra(node, "rawValue", node.value);
this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
this.next();
return this.finishNode(node, "NumericLiteralTypeAnnotation");
return this.parseLiteral(-this.state.value, "NumericLiteralTypeAnnotation", node.start, node.loc.start);
}
this.unexpected();
case tt.num:
node.value = this.state.value;
this.addExtra(node, "rawValue", node.value);
this.addExtra(node, "raw", this.input.slice(this.state.start, this.state.end));
this.next();
return this.finishNode(node, "NumericLiteralTypeAnnotation");
return this.parseLiteral(this.state.value, "NumericLiteralTypeAnnotation");
case tt._null:
node.value = this.match(tt._null);