diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index c3b4735341..73b185ecb5 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -1280,7 +1280,7 @@ export default (superClass: Class): Class => noCalls: ?boolean, state: { stop: boolean }, ): N.Expression { - if (this.eat(tt.bang)) { + if (!this.hasPrecedingLineBreak() && this.eat(tt.bang)) { const nonNullExpression: N.TsNonNullExpression = this.startNodeAt( startPos, startLoc, diff --git a/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/actual.js b/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/actual.js new file mode 100644 index 0000000000..0fce68865a --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/actual.js @@ -0,0 +1,2 @@ +a +!b diff --git a/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/expected.json b/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/expected.json new file mode 100644 index 0000000000..042ce23ffc --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/null-assertion-false-positive/expected.json @@ -0,0 +1,118 @@ +{ + "type": "File", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "a" + }, + "name": "a" + } + }, + { + "type": "ExpressionStatement", + "start": 2, + "end": 4, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "expression": { + "type": "UnaryExpression", + "start": 2, + "end": 4, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "operator": "!", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + }, + "identifierName": "b" + }, + "name": "b" + }, + "extra": { + "parenthesizedArgument": false + } + } + } + ], + "directives": [] + } +} \ No newline at end of file