From af606f11d8f8ae0f871db74b8f43febd79094cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 13 Dec 2021 17:15:42 -0500 Subject: [PATCH] fix: check preceding line break before exclamation --- .../src/plugins/typescript/index.js | 6 ++- .../variable-declarator/definite-asi/input.ts | 2 + .../definite-asi/output.json | 54 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 8fb93cddda71..d07ad1759b5c 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2832,7 +2832,11 @@ export default (superClass: Class): Class => kind: "var" | "let" | "const", ): void { super.parseVarId(decl, kind); - if (decl.id.type === "Identifier" && this.eat(tt.bang)) { + if ( + decl.id.type === "Identifier" && + !this.hasPrecedingLineBreak() && + this.eat(tt.bang) + ) { decl.definite = true; } diff --git a/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/input.ts b/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/input.ts new file mode 100644 index 000000000000..d17851234900 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/input.ts @@ -0,0 +1,2 @@ +let x +!function() {}; diff --git a/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/output.json b/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/output.json new file mode 100644 index 000000000000..d777e9b8156c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/variable-declarator/definite-asi/output.json @@ -0,0 +1,54 @@ +{ + "type": "File", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":15}}, + "program": { + "type": "Program", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":15}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5}}, + "id": { + "type": "Identifier", + "start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"x"}, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start":6,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}}, + "expression": { + "type": "UnaryExpression", + "start":6,"end":20,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":14}}, + "operator": "!", + "prefix": true, + "argument": { + "type": "FunctionExpression", + "start":7,"end":20,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":14}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":18,"end":20,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":14}}, + "body": [], + "directives": [] + } + } + } + } + ], + "directives": [] + } +}