From a94cbf760a46fdda07a02566afb360582730971f Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Thu, 24 May 2018 09:31:08 -0500 Subject: [PATCH] Set exprAllowed to false when parsing TSNonNullExpression (#7968) --- .../babel-parser/src/plugins/typescript.js | 5 +- .../typescript/cast/null-assertion-2/input.js | 1 + .../cast/null-assertion-2/output.json | 117 ++++++++++++++++ .../typescript/cast/null-assertion-3/input.js | 1 + .../cast/null-assertion-3/output.json | 132 ++++++++++++++++++ .../cast/null-assertion-and-assign-2/input.js | 1 + .../null-assertion-and-assign-2/output.json | 117 ++++++++++++++++ 7 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/output.json diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 507af3d882..dbb5be43b5 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -1343,7 +1343,10 @@ export default (superClass: Class): Class => noCalls: ?boolean, state: N.ParseSubscriptState, ): N.Expression { - if (!this.hasPrecedingLineBreak() && this.eat(tt.bang)) { + if (!this.hasPrecedingLineBreak() && this.match(tt.bang)) { + this.state.exprAllowed = false; + this.next(); + const nonNullExpression: N.TsNonNullExpression = this.startNodeAt( startPos, startLoc, diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/input.js b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/input.js new file mode 100644 index 0000000000..29cb0c03d5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/input.js @@ -0,0 +1 @@ +x! / 2 diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/output.json b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/output.json new file mode 100644 index 0000000000..53eb22ec40 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-2/output.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "left": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/input.js b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/input.js new file mode 100644 index 0000000000..759fd689e3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/input.js @@ -0,0 +1 @@ +const x = foo()!; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/output.json b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/output.json new file mode 100644 index 0000000000..ac3d6465ac --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-3/output.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "TSNonNullExpression", + "start": 10, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "expression": { + "type": "CallExpression", + "start": 10, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "callee": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "arguments": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/input.js b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/input.js new file mode 100644 index 0000000000..ae793d5007 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/input.js @@ -0,0 +1 @@ +x! *= 1 diff --git a/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/output.json b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/output.json new file mode 100644 index 0000000000..ee8f64d2c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/null-assertion-and-assign-2/output.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "operator": "*=", + "left": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "right": { + "type": "NumericLiteral", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file