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);

View File

@ -0,0 +1 @@
var a: -z

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected number (1:8)"
}

View File

@ -0,0 +1 @@
var a: +1

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:7)"
}

View File

@ -1 +1 @@
var a: 0b1111011
var a: -0b1111011

View File

@ -1,7 +1,7 @@
{
"type": "File",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -9,13 +9,13 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"program": {
"type": "Program",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -23,7 +23,7 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"sourceType": "module",
@ -31,7 +31,7 @@
{
"type": "VariableDeclaration",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -39,14 +39,14 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -54,13 +54,13 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -68,14 +68,14 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -83,13 +83,13 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
@ -97,13 +97,13 @@
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"value": 123,
"value": -123,
"extra": {
"rawValue": 123,
"raw": "0b1111011"
"rawValue": -123,
"raw": "-0b1111011"
}
}
}
@ -116,4 +116,4 @@
],
"directives": []
}
}
}

View File

@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "123.0"
"raw": "-123.0"
}
}
}
@ -116,4 +116,4 @@
],
"directives": []
}
}
}

View File

@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0o173"
"raw": "-0o173"
}
}
}
@ -116,4 +116,4 @@
],
"directives": []
}
}
}

View File

@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0x7B"
"raw": "-0x7B"
}
}
}
@ -116,4 +116,4 @@
],
"directives": []
}
}
}

View File

@ -92,7 +92,7 @@
"value": -1,
"extra": {
"rawValue": -1,
"raw": "1"
"raw": "-1"
}
},
{
@ -417,4 +417,4 @@
}
}
]
}
}