diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 007a403e28..23aa49e175 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -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); diff --git a/test/fixtures/flow/literal-types/number-negative-binary/actual.js b/test/fixtures/flow/literal-types/number-negative-binary/actual.js new file mode 100644 index 0000000000..665417b13a --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-binary/actual.js @@ -0,0 +1 @@ +var a: 0b1111011 diff --git a/test/fixtures/flow/literal-types/number-negative-binary/expected.json b/test/fixtures/flow/literal-types/number-negative-binary/expected.json new file mode 100644 index 0000000000..44d455a56d --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-binary/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/literal-types/number-negative-float/actual.js b/test/fixtures/flow/literal-types/number-negative-float/actual.js new file mode 100644 index 0000000000..a1a32f959f --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-float/actual.js @@ -0,0 +1 @@ +var a: -123.0 diff --git a/test/fixtures/flow/literal-types/number-negative-float/expected.json b/test/fixtures/flow/literal-types/number-negative-float/expected.json new file mode 100644 index 0000000000..735c2e093f --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-float/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/literal-types/number-negative-octal-2/actual.js b/test/fixtures/flow/literal-types/number-negative-octal-2/actual.js new file mode 100644 index 0000000000..e9eb626734 --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-octal-2/actual.js @@ -0,0 +1 @@ +var a: -0o173 diff --git a/test/fixtures/flow/literal-types/number-negative-octal-2/expected.json b/test/fixtures/flow/literal-types/number-negative-octal-2/expected.json new file mode 100644 index 0000000000..489e2faac9 --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-octal-2/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/literal-types/number-negative-octal/actual.js b/test/fixtures/flow/literal-types/number-negative-octal/actual.js new file mode 100644 index 0000000000..f808ab87df --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-octal/actual.js @@ -0,0 +1 @@ +var a: -0x7B diff --git a/test/fixtures/flow/literal-types/number-negative-octal/expected.json b/test/fixtures/flow/literal-types/number-negative-octal/expected.json new file mode 100644 index 0000000000..2ac2c96235 --- /dev/null +++ b/test/fixtures/flow/literal-types/number-negative-octal/expected.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/type-annotations/negative-number-literal/actual.js b/test/fixtures/flow/type-annotations/negative-number-literal/actual.js new file mode 100644 index 0000000000..fd282fd7b1 --- /dev/null +++ b/test/fixtures/flow/type-annotations/negative-number-literal/actual.js @@ -0,0 +1,12 @@ +/** + * @flow + */ + +type DirectionVector = + | -1 + | 0 + | 1; + + +var x:DirectionVector = -1; +console.log('foo'); diff --git a/test/fixtures/flow/type-annotations/negative-number-literal/expected.json b/test/fixtures/flow/type-annotations/negative-number-literal/expected.json new file mode 100644 index 0000000000..52ca7d33a2 --- /dev/null +++ b/test/fixtures/flow/type-annotations/negative-number-literal/expected.json @@ -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 + } + } + } + ] +} \ No newline at end of file