From 10904668cfd6ae24316366f1be7cbd6c3155ba1e Mon Sep 17 00:00:00 2001 From: Sakibul Mowla Date: Sun, 20 Oct 2019 01:15:27 +0100 Subject: [PATCH 1/5] Add test --- .../cast/destructure-and-assign/input.ts | 1 + .../cast/destructure-and-assign/output.json | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts new file mode 100644 index 000000000000..b7a83210faa3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts @@ -0,0 +1 @@ +(a as number) = 42; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json new file mode 100644 index 000000000000..f07df1678fb0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json @@ -0,0 +1,137 @@ +{ + "type": "File", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "operator": "=", + "left": { + "type": "TSAsExpression", + "start": 1, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "expression": { + "type": "Identifier", + "start": 1, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 2 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 6, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + }, + "right": { + "type": "NumericLiteral", + "start": 16, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 302deb6165531fcee5188dd8e3c2c8a5a7bcadb2 Mon Sep 17 00:00:00 2001 From: Sakibul Mowla Date: Sun, 20 Oct 2019 01:16:06 +0100 Subject: [PATCH 2/5] Add fix --- .../src/plugins/typescript/index.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 26a6246d59e6..827e43a7499e 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2424,7 +2424,13 @@ export default (superClass: Class): Class => } } - toAssignableList(exprList: N.Expression[]): $ReadOnlyArray { + toAssignableList( + exprList: N.Expression[], + isBinding: ?boolean, + contextDescription: string, + ): $ReadOnlyArray { + const isAssignmentExpression = + contextDescription === "assignment expression"; for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; if (!expr) continue; @@ -2434,10 +2440,14 @@ export default (superClass: Class): Class => break; case "TSAsExpression": case "TSTypeAssertion": - this.raise( - expr.start, - "Unexpected type cast in parameter position.", - ); + if (isAssignmentExpression) { + exprList[i] = this.typeCastToParameter(expr); + } else { + this.raise( + expr.start, + "Unexpected type cast in parameter position.", + ); + } break; } } From d05c54563f1b7317c7184807486f560b238daa0b Mon Sep 17 00:00:00 2001 From: Sakibul Mowla Date: Sat, 26 Oct 2019 12:22:39 +0100 Subject: [PATCH 3/5] Fix test, destructure with as assertion --- .../cast/destructure-and-assign/input.ts | 2 +- .../cast/destructure-and-assign/output.json | 123 ++++++++++-------- 2 files changed, 70 insertions(+), 55 deletions(-) diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts index b7a83210faa3..699cea6eb64d 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts @@ -1 +1 @@ -(a as number) = 42; +[a as number] = [42]; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json index f07df1678fb0..293db3ae845d 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 19, + "end": 21, "loc": { "start": { "line": 1, @@ -9,13 +9,13 @@ }, "end": { "line": 1, - "column": 19 + "column": 21 } }, "program": { "type": "Program", "start": 0, - "end": 19, + "end": 21, "loc": { "start": { "line": 1, @@ -23,7 +23,7 @@ }, "end": { "line": 1, - "column": 19 + "column": 21 } }, "sourceType": "module", @@ -32,7 +32,7 @@ { "type": "ExpressionStatement", "start": 0, - "end": 19, + "end": 21, "loc": { "start": { "line": 1, @@ -40,13 +40,13 @@ }, "end": { "line": 1, - "column": 19 + "column": 21 } }, "expression": { "type": "AssignmentExpression", "start": 0, - "end": 18, + "end": 20, "loc": { "start": { "line": 1, @@ -54,65 +54,63 @@ }, "end": { "line": 1, - "column": 18 + "column": 20 } }, "operator": "=", "left": { - "type": "TSAsExpression", - "start": 1, - "end": 12, + "type": "ArrayPattern", + "start": 0, + "end": 13, "loc": { "start": { "line": 1, - "column": 1 + "column": 0 }, "end": { "line": 1, - "column": 12 + "column": 13 } }, - "expression": { - "type": "Identifier", - "start": 1, - "end": 2, - "loc": { - "start": { - "line": 1, - "column": 1 + "elements": [ + { + "type": "Identifier", + "start": 1, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "a" }, - "end": { - "line": 1, - "column": 2 - }, - "identifierName": "a" - }, - "name": "a" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 6, - "end": 12, - "loc": { - "start": { - "line": 1, - "column": 6 - }, - "end": { - "line": 1, - "column": 12 + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 6, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 12 + } + } } } - }, - "extra": { - "parenthesized": true, - "parenStart": 0 - } + ] }, "right": { - "type": "NumericLiteral", + "type": "ArrayExpression", "start": 16, - "end": 18, + "end": 20, "loc": { "start": { "line": 1, @@ -120,14 +118,31 @@ }, "end": { "line": 1, - "column": 18 + "column": 20 } }, - "extra": { - "rawValue": 42, - "raw": "42" - }, - "value": 42 + "elements": [ + { + "type": "NumericLiteral", + "start": 17, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + ] } } } From a48a37a44bf9de83c422f36eba40fec049fd7146 Mon Sep 17 00:00:00 2001 From: Sakibul Mowla Date: Sat, 26 Oct 2019 12:25:29 +0100 Subject: [PATCH 4/5] Add angle-bracket assertion case --- .../cast/destructure-and-assign/input.ts | 1 + .../cast/destructure-and-assign/output.json | 129 +++++++++++++++++- 2 files changed, 124 insertions(+), 6 deletions(-) diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts index 699cea6eb64d..ece9096fb9f6 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts @@ -1 +1,2 @@ [a as number] = [42]; +[a] = [42]; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json index 293db3ae845d..d621a6cb84c1 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json +++ b/packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 21, + "end": 41, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 21 + "line": 2, + "column": 19 } }, "program": { "type": "Program", "start": 0, - "end": 21, + "end": 41, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 21 + "line": 2, + "column": 19 } }, "sourceType": "module", @@ -145,6 +145,123 @@ ] } } + }, + { + "type": "ExpressionStatement", + "start": 22, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 22, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "operator": "=", + "left": { + "type": "ArrayPattern", + "start": 22, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "elements": [ + { + "type": "Identifier", + "start": 31, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 24, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + } + ] + }, + "right": { + "type": "ArrayExpression", + "start": 36, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "elements": [ + { + "type": "NumericLiteral", + "start": 37, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + ] + } + } } ], "directives": [] From 35786c70aa0714ebdbecf2e56b9a8ee796c719ea Mon Sep 17 00:00:00 2001 From: Sakibul Mowla Date: Wed, 30 Oct 2019 21:14:58 +0000 Subject: [PATCH 5/5] Use isBinding to make typeCastToParameter decision --- packages/babel-parser/src/plugins/typescript/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 827e43a7499e..4d708dbba095 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2427,10 +2427,7 @@ export default (superClass: Class): Class => toAssignableList( exprList: N.Expression[], isBinding: ?boolean, - contextDescription: string, ): $ReadOnlyArray { - const isAssignmentExpression = - contextDescription === "assignment expression"; for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; if (!expr) continue; @@ -2440,7 +2437,7 @@ export default (superClass: Class): Class => break; case "TSAsExpression": case "TSTypeAssertion": - if (isAssignmentExpression) { + if (!isBinding) { exprList[i] = this.typeCastToParameter(expr); } else { this.raise(