From e8038863c3869f6b3c15ee23f43cbf3de3e7d656 Mon Sep 17 00:00:00 2001 From: Armano Date: Tue, 15 Jan 2019 10:35:49 +0100 Subject: [PATCH] Fix range on TypeScript this type predicate (#9339) --- .../babel-parser/src/plugins/typescript.js | 2 +- .../typescript/class/predicate-types/input.js | 6 + .../class/predicate-types/output.json | 361 ++++++++++++++++++ 3 files changed, 368 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/class/predicate-types/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/class/predicate-types/output.json diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 02cdd54b89..0aa6211748 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -264,7 +264,7 @@ export default (superClass: Class): Class => tsParseThisTypePredicate(lhs: N.TsThisType): N.TsTypePredicate { this.next(); - const node: N.TsTypePredicate = this.startNode(); + const node: N.TsTypePredicate = this.startNodeAtNode(lhs); node.parameterName = lhs; node.typeAnnotation = this.tsParseTypeAnnotation(/* eatColon */ false); return this.finishNode(node, "TSTypePredicate"); diff --git a/packages/babel-parser/test/fixtures/typescript/class/predicate-types/input.js b/packages/babel-parser/test/fixtures/typescript/class/predicate-types/input.js new file mode 100644 index 0000000000..5a4be8de8a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/predicate-types/input.js @@ -0,0 +1,6 @@ +class Foo { + isBar(): this is string { + } + isBaz = (): this is string => { + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/predicate-types/output.json b/packages/babel-parser/test/fixtures/typescript/class/predicate-types/output.json new file mode 100644 index 0000000000..a0f7f5f5f3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/predicate-types/output.json @@ -0,0 +1,361 @@ +{ + "type": "File", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 10, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "isBar" + }, + "name": "isBar" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 21, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 23, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "parameterName": { + "type": "TSThisType", + "start": 23, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 38, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 3, + "column": 3 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassProperty", + "start": 46, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 46, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 7 + }, + "identifierName": "isBaz" + }, + "name": "isBaz" + }, + "computed": false, + "value": { + "type": "ArrowFunctionExpression", + "start": 54, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "returnType": { + "type": "TSTypeAnnotation", + "start": 56, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 58, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 14 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "parameterName": { + "type": "TSThisType", + "start": 58, + "end": 62, + "loc": { + "start": { + "line": 4, + "column": 14 + }, + "end": { + "line": 4, + "column": 18 + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 22 + }, + "end": { + "line": 4, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 22 + }, + "end": { + "line": 4, + "column": 28 + } + } + } + } + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 76, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 32 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "body": [], + "directives": [] + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file