Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Fix negative number literal typeannotations (#366)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danez authored Feb 20, 2017
1 parent 57aacea commit 2ef00a6
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 47 deletions.
9 changes: 6 additions & 3 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 5 additions & 16 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a: -z
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected number (1:8)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a: +1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:7)"
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var a: 0b1111011
var a: -0b1111011
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
{
"type": "File",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"program": {
"type": "Program",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 16,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 16
"column": 17
}
},
"value": 123,
"value": -123,
"extra": {
"rawValue": 123,
"raw": "0b1111011"
"rawValue": -123,
"raw": "-0b1111011"
}
}
}
Expand All @@ -116,4 +116,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "123.0"
"raw": "-123.0"
}
}
}
Expand All @@ -116,4 +116,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0o173"
"raw": "-0o173"
}
}
}
Expand All @@ -116,4 +116,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0x7B"
"raw": "-0x7B"
}
}
}
Expand All @@ -116,4 +116,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"value": -1,
"extra": {
"rawValue": -1,
"raw": "1"
"raw": "-1"
}
},
{
Expand Down Expand Up @@ -417,4 +417,4 @@
}
}
]
}
}

0 comments on commit 2ef00a6

Please sign in to comment.