From 2ef00a663127d2899b07fb37aaa2256178676d78 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Mon, 20 Feb 2017 22:43:59 +0100 Subject: [PATCH] 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 --- src/parser/expression.js | 9 +++-- src/parser/node.js | 2 +- src/plugins/flow.js | 21 +++-------- .../invalid-number-negative/actual.js | 1 + .../invalid-number-negative/options.json | 3 ++ .../invalid-number-positive/actual.js | 1 + .../invalid-number-positive/options.json | 3 ++ .../number-negative-binary/actual.js | 2 +- .../number-negative-binary/expected.json | 36 +++++++++---------- .../number-negative-float/expected.json | 4 +-- .../number-negative-octal-2/expected.json | 4 +-- .../number-negative-octal/expected.json | 4 +-- .../negative-number-literal/expected.json | 4 +-- 13 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 test/fixtures/flow/literal-types/invalid-number-negative/actual.js create mode 100644 test/fixtures/flow/literal-types/invalid-number-negative/options.json create mode 100644 test/fixtures/flow/literal-types/invalid-number-positive/actual.js create mode 100644 test/fixtures/flow/literal-types/invalid-number-positive/options.json diff --git a/src/parser/expression.js b/src/parser/expression.js index dae8408552..54d48e01ab 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -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); diff --git a/src/parser/node.js b/src/parser/node.js index 7af6c29687..86f697c130 100644 --- a/src/parser/node.js +++ b/src/parser/node.js @@ -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; diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 14f5b48562..04579ae047 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -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); diff --git a/test/fixtures/flow/literal-types/invalid-number-negative/actual.js b/test/fixtures/flow/literal-types/invalid-number-negative/actual.js new file mode 100644 index 0000000000..3c92505c9c --- /dev/null +++ b/test/fixtures/flow/literal-types/invalid-number-negative/actual.js @@ -0,0 +1 @@ +var a: -z diff --git a/test/fixtures/flow/literal-types/invalid-number-negative/options.json b/test/fixtures/flow/literal-types/invalid-number-negative/options.json new file mode 100644 index 0000000000..47910828b1 --- /dev/null +++ b/test/fixtures/flow/literal-types/invalid-number-negative/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected number (1:8)" +} diff --git a/test/fixtures/flow/literal-types/invalid-number-positive/actual.js b/test/fixtures/flow/literal-types/invalid-number-positive/actual.js new file mode 100644 index 0000000000..5632b2f804 --- /dev/null +++ b/test/fixtures/flow/literal-types/invalid-number-positive/actual.js @@ -0,0 +1 @@ +var a: +1 diff --git a/test/fixtures/flow/literal-types/invalid-number-positive/options.json b/test/fixtures/flow/literal-types/invalid-number-positive/options.json new file mode 100644 index 0000000000..c958665c03 --- /dev/null +++ b/test/fixtures/flow/literal-types/invalid-number-positive/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:7)" +} diff --git a/test/fixtures/flow/literal-types/number-negative-binary/actual.js b/test/fixtures/flow/literal-types/number-negative-binary/actual.js index 665417b13a..c5d3c1217e 100644 --- a/test/fixtures/flow/literal-types/number-negative-binary/actual.js +++ b/test/fixtures/flow/literal-types/number-negative-binary/actual.js @@ -1 +1 @@ -var a: 0b1111011 +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 index 44d455a56d..a97406933e 100644 --- a/test/fixtures/flow/literal-types/number-negative-binary/expected.json +++ b/test/fixtures/flow/literal-types/number-negative-binary/expected.json @@ -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": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/literal-types/number-negative-float/expected.json b/test/fixtures/flow/literal-types/number-negative-float/expected.json index 735c2e093f..25ac6b0013 100644 --- a/test/fixtures/flow/literal-types/number-negative-float/expected.json +++ b/test/fixtures/flow/literal-types/number-negative-float/expected.json @@ -103,7 +103,7 @@ "value": -123, "extra": { "rawValue": -123, - "raw": "123.0" + "raw": "-123.0" } } } @@ -116,4 +116,4 @@ ], "directives": [] } -} \ No newline at end of file +} 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 index 489e2faac9..136224babb 100644 --- a/test/fixtures/flow/literal-types/number-negative-octal-2/expected.json +++ b/test/fixtures/flow/literal-types/number-negative-octal-2/expected.json @@ -103,7 +103,7 @@ "value": -123, "extra": { "rawValue": -123, - "raw": "0o173" + "raw": "-0o173" } } } @@ -116,4 +116,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/literal-types/number-negative-octal/expected.json b/test/fixtures/flow/literal-types/number-negative-octal/expected.json index 2ac2c96235..aaa9ab4cce 100644 --- a/test/fixtures/flow/literal-types/number-negative-octal/expected.json +++ b/test/fixtures/flow/literal-types/number-negative-octal/expected.json @@ -103,7 +103,7 @@ "value": -123, "extra": { "rawValue": -123, - "raw": "0x7B" + "raw": "-0x7B" } } } @@ -116,4 +116,4 @@ ], "directives": [] } -} \ No newline at end of file +} diff --git a/test/fixtures/flow/type-annotations/negative-number-literal/expected.json b/test/fixtures/flow/type-annotations/negative-number-literal/expected.json index e4b1cc8f02..21f2b225e8 100644 --- a/test/fixtures/flow/type-annotations/negative-number-literal/expected.json +++ b/test/fixtures/flow/type-annotations/negative-number-literal/expected.json @@ -92,7 +92,7 @@ "value": -1, "extra": { "rawValue": -1, - "raw": "1" + "raw": "-1" } }, { @@ -417,4 +417,4 @@ } } ] -} \ No newline at end of file +}