support negative numeric type literals - fixes T7450

This commit is contained in:
Sebastian McKenzie 2016-06-22 13:17:27 +01:00
parent 96a7eadbe4
commit ec0a349ec8
11 changed files with 919 additions and 0 deletions

View File

@ -559,6 +559,18 @@ pp.flowParsePrimaryType = function () {
this.next();
return this.finishNode(node, "BooleanLiteralTypeAnnotation");
case tt.plusMin:
if (this.state.value === "-") {
this.next();
if (!this.match(tt.num)) this.unexpected();
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");
}
case tt.num:
node.value = this.state.value;
this.addExtra(node, "rawValue", node.value);

View File

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

View File

@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
},
"program": {
"type": "Program",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 16
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 16
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 16
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 16
}
},
"value": 123,
"extra": {
"rawValue": 123,
"raw": "0b1111011"
}
}
}
},
"init": null
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 13
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 13
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 13
}
},
"value": -123,
"extra": {
"rawValue": -123,
"raw": "123.0"
}
}
}
},
"init": null
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 13
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 13
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 13
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 13
}
},
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0o173"
}
}
}
},
"init": null
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"sourceType": "module",
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 12
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 12
}
},
"name": "a",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 5,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
},
"typeAnnotation": {
"type": "NumericLiteralTypeAnnotation",
"start": 7,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 12
}
},
"value": -123,
"extra": {
"rawValue": -123,
"raw": "0x7B"
}
}
}
},
"init": null
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

@ -0,0 +1,12 @@
/**
* @flow
*/
type DirectionVector =
| -1
| 0
| 1;
var x:DirectionVector = -1;
console.log('foo');

View File

@ -0,0 +1,415 @@
{
"type": "File",
"start": 0,
"end": 110,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 110,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"column": 19
}
},
"sourceType": "module",
"body": [
{
"type": "TypeAlias",
"start": 18,
"end": 60,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 8,
"column": 6
}
},
"id": {
"type": "Identifier",
"start": 23,
"end": 38,
"loc": {
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 20
}
},
"name": "DirectionVector"
},
"typeParameters": null,
"right": {
"type": "UnionTypeAnnotation",
"start": 45,
"end": 59,
"loc": {
"start": {
"line": 6,
"column": 4
},
"end": {
"line": 8,
"column": 5
}
},
"types": [
{
"type": "NumericLiteralTypeAnnotation",
"start": 45,
"end": 47,
"loc": {
"start": {
"line": 6,
"column": 4
},
"end": {
"line": 6,
"column": 6
}
},
"value": -1,
"extra": {
"rawValue": -1,
"raw": "1"
}
},
{
"type": "NumericLiteralTypeAnnotation",
"start": 52,
"end": 53,
"loc": {
"start": {
"line": 7,
"column": 4
},
"end": {
"line": 7,
"column": 5
}
},
"value": 0,
"extra": {
"rawValue": 0,
"raw": "0"
}
},
{
"type": "NumericLiteralTypeAnnotation",
"start": 58,
"end": 59,
"loc": {
"start": {
"line": 8,
"column": 4
},
"end": {
"line": 8,
"column": 5
}
},
"value": 1,
"extra": {
"rawValue": 1,
"raw": "1"
}
}
]
},
"leadingComments": [
{
"type": "CommentBlock",
"value": "*\n * @flow\n ",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 3
}
}
}
]
},
{
"type": "VariableDeclaration",
"start": 63,
"end": 90,
"loc": {
"start": {
"line": 11,
"column": 0
},
"end": {
"line": 11,
"column": 27
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 67,
"end": 89,
"loc": {
"start": {
"line": 11,
"column": 4
},
"end": {
"line": 11,
"column": 26
}
},
"id": {
"type": "Identifier",
"start": 67,
"end": 84,
"loc": {
"start": {
"line": 11,
"column": 4
},
"end": {
"line": 11,
"column": 21
}
},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 68,
"end": 84,
"loc": {
"start": {
"line": 11,
"column": 5
},
"end": {
"line": 11,
"column": 21
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 69,
"end": 84,
"loc": {
"start": {
"line": 11,
"column": 6
},
"end": {
"line": 11,
"column": 21
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 69,
"end": 84,
"loc": {
"start": {
"line": 11,
"column": 6
},
"end": {
"line": 11,
"column": 21
}
},
"name": "DirectionVector"
}
}
}
},
"init": {
"type": "UnaryExpression",
"start": 87,
"end": 89,
"loc": {
"start": {
"line": 11,
"column": 24
},
"end": {
"line": 11,
"column": 26
}
},
"operator": "-",
"prefix": true,
"extra": {
"parenthesizedArgument": false
},
"argument": {
"type": "NumericLiteral",
"start": 88,
"end": 89,
"loc": {
"start": {
"line": 11,
"column": 25
},
"end": {
"line": 11,
"column": 26
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start": 91,
"end": 110,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 19
}
},
"expression": {
"type": "CallExpression",
"start": 91,
"end": 109,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 18
}
},
"callee": {
"type": "MemberExpression",
"start": 91,
"end": 102,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 11
}
},
"object": {
"type": "Identifier",
"start": 91,
"end": 98,
"loc": {
"start": {
"line": 12,
"column": 0
},
"end": {
"line": 12,
"column": 7
}
},
"name": "console"
},
"property": {
"type": "Identifier",
"start": 99,
"end": 102,
"loc": {
"start": {
"line": 12,
"column": 8
},
"end": {
"line": 12,
"column": 11
}
},
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "StringLiteral",
"start": 103,
"end": 108,
"loc": {
"start": {
"line": 12,
"column": 12
},
"end": {
"line": 12,
"column": 17
}
},
"extra": {
"rawValue": "foo",
"raw": "'foo'"
},
"value": "foo"
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": "*\n * @flow\n ",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 3
}
}
}
]
}