From da79c90f2af2627e8ee383569416749fa8338fde Mon Sep 17 00:00:00 2001 From: JuniorTour Date: Thu, 16 Sep 2021 16:24:11 -0500 Subject: [PATCH] fix(babel-parser): Allow line break before `assert` return type (#13771) --- .../src/plugins/typescript/index.js | 6 +- .../input.ts | 2 + .../output.json | 184 ++++++++++++++++++ 3 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 71ed89592c..b2f42b5835 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1316,11 +1316,7 @@ export default (superClass: Class): Class => } tsParseTypePredicateAsserts(): boolean { - if ( - !this.match(tt.name) || - this.state.value !== "asserts" || - this.hasPrecedingLineBreak() - ) { + if (!this.match(tt.name) || this.state.value !== "asserts") { return false; } const containsEsc = this.state.containsEsc; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/input.ts new file mode 100644 index 0000000000..404da1ba13 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/input.ts @@ -0,0 +1,2 @@ +function assert(condition: any): +asserts condition {} diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/output.json new file mode 100644 index 0000000000..c86ab9a36f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration-with-line-break/output.json @@ -0,0 +1,184 @@ +{ + "type": "File", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "assert" + }, + "name": "assert" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 16, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "condition" + }, + "name": "condition", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 25, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 31, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 33, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "parameterName": { + "type": "Identifier", + "start": 41, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "condition" + }, + "name": "condition" + }, + "asserts": true, + "typeAnnotation": null + } + }, + "body": { + "type": "BlockStatement", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +}