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:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user