From 74682f33bc34ffe4c02913abd4218301e6224845 Mon Sep 17 00:00:00 2001 From: Maaz Syed Adeeb Date: Tue, 9 Jan 2018 02:59:27 +0530 Subject: [PATCH] Support 'assert and assign' TypeScript syntax (#7098) --- packages/babylon/src/plugins/typescript.js | 15 + .../cast/assert-and-assign/actual.js | 10 + .../cast/assert-and-assign/expected.json | 486 ++++++++++++++ .../cast/multiple-assert-and-assign/actual.js | 11 + .../multiple-assert-and-assign/expected.json | 616 ++++++++++++++++++ 5 files changed, 1138 insertions(+) create mode 100644 packages/babylon/test/fixtures/typescript/cast/assert-and-assign/actual.js create mode 100644 packages/babylon/test/fixtures/typescript/cast/assert-and-assign/expected.json create mode 100644 packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/actual.js create mode 100644 packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/expected.json diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index 586681e49c..664e45c6f9 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -1866,6 +1866,13 @@ export default (superClass: Class): Class => ); case "TSParameterProperty": return super.toAssignable(node, isBinding, contextDescription); + case "TSAsExpression": + node.expression = this.toAssignable( + node.expression, + isBinding, + contextDescription, + ); + return node; default: return super.toAssignable(node, isBinding, contextDescription); } @@ -1891,6 +1898,14 @@ export default (superClass: Class): Class => "parameter property", ); return; + case "TSAsExpression": + this.checkLVal( + expr.expression, + isBinding, + checkClashes, + contextDescription, + ); + return; default: super.checkLVal(expr, isBinding, checkClashes, contextDescription); return; diff --git a/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/actual.js b/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/actual.js new file mode 100644 index 0000000000..360d80c719 --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/actual.js @@ -0,0 +1,10 @@ +interface ResponseBody { + name: string + age: number +} + +let response; +(response as ResponseBody) = { + name: 'Alice', + age: 23, +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/expected.json b/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/expected.json new file mode 100644 index 0000000000..50b878eb59 --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/assert-and-assign/expected.json @@ -0,0 +1,486 @@ +{ + "type": "File", + "start": 0, + "end": 131, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 131, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "ResponseBody" + }, + "name": "ResponseBody" + }, + "body": { + "type": "TSInterfaceBody", + "start": 23, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 27, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "key": { + "type": "Identifier", + "start": 27, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "name" + }, + "name": "name" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 31, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 42, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 42, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "age" + }, + "name": "age" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 45, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + } + ] + } + }, + { + "type": "VariableDeclaration", + "start": 57, + "end": 70, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 12 + }, + "identifierName": "response" + }, + "name": "response" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start": 71, + "end": 131, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 10, + "column": 1 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 71, + "end": 131, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 10, + "column": 1 + } + }, + "operator": "=", + "left": { + "type": "TSAsExpression", + "start": 72, + "end": 96, + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 7, + "column": 25 + } + }, + "expression": { + "type": "Identifier", + "start": 72, + "end": 80, + "loc": { + "start": { + "line": 7, + "column": 1 + }, + "end": { + "line": 7, + "column": 9 + }, + "identifierName": "response" + }, + "name": "response" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 84, + "end": 96, + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 25 + } + }, + "typeName": { + "type": "Identifier", + "start": 84, + "end": 96, + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 25 + }, + "identifierName": "ResponseBody" + }, + "name": "ResponseBody" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 71 + } + }, + "right": { + "type": "ObjectExpression", + "start": 100, + "end": 131, + "loc": { + "start": { + "line": 7, + "column": 29 + }, + "end": { + "line": 10, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 104, + "end": 117, + "loc": { + "start": { + "line": 8, + "column": 2 + }, + "end": { + "line": 8, + "column": 15 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 104, + "end": 108, + "loc": { + "start": { + "line": 8, + "column": 2 + }, + "end": { + "line": 8, + "column": 6 + }, + "identifierName": "name" + }, + "name": "name" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 110, + "end": 117, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 15 + } + }, + "extra": { + "rawValue": "Alice", + "raw": "'Alice'" + }, + "value": "Alice" + } + }, + { + "type": "ObjectProperty", + "start": 121, + "end": 128, + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 9 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 121, + "end": 124, + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 5 + }, + "identifierName": "age" + }, + "name": "age" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start": 126, + "end": 128, + "loc": { + "start": { + "line": 9, + "column": 7 + }, + "end": { + "line": 9, + "column": 9 + } + }, + "extra": { + "rawValue": 23, + "raw": "23" + }, + "value": 23 + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/actual.js b/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/actual.js new file mode 100644 index 0000000000..05b1ed46f4 --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/actual.js @@ -0,0 +1,11 @@ +interface A { + name: string +} +interface B extends A { + age: number +} +let a; +(a as A as B) = { + name: 'Alice', + age: 23 +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/expected.json b/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/expected.json new file mode 100644 index 0000000000..20dd72836a --- /dev/null +++ b/packages/babylon/test/fixtures/typescript/cast/multiple-assert-and-assign/expected.json @@ -0,0 +1,616 @@ +{ + "type": "File", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 16, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "key": { + "type": "Identifier", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "name" + }, + "name": "name" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 20, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + } + ] + } + }, + { + "type": "TSInterfaceDeclaration", + "start": 31, + "end": 70, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 4, + "column": 11 + }, + "identifierName": "B" + }, + "name": "B" + }, + "extends": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 21 + } + }, + "expression": { + "type": "Identifier", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 20 + }, + "end": { + "line": 4, + "column": 21 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ], + "body": { + "type": "TSInterfaceBody", + "start": 53, + "end": 70, + "loc": { + "start": { + "line": 4, + "column": 22 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 57, + "end": 68, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "key": { + "type": "Identifier", + "start": 57, + "end": 60, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "age" + }, + "name": "age" + }, + "computed": false, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 60, + "end": 68, + "loc": { + "start": { + "line": 5, + "column": 5 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 62, + "end": 68, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + } + } + ] + } + }, + { + "type": "VariableDeclaration", + "start": 71, + "end": 77, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 75, + "end": 76, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 75, + "end": 76, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 5 + }, + "identifierName": "a" + }, + "name": "a" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start": 78, + "end": 124, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 78, + "end": 124, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "operator": "=", + "left": { + "type": "TSAsExpression", + "start": 79, + "end": 90, + "loc": { + "start": { + "line": 8, + "column": 1 + }, + "end": { + "line": 8, + "column": 12 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 8, + "column": 1 + }, + "end": { + "line": 8, + "column": 7 + } + }, + "expression": { + "type": "Identifier", + "start": 79, + "end": 80, + "loc": { + "start": { + "line": 8, + "column": 1 + }, + "end": { + "line": 8, + "column": 2 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 8, + "column": 6 + }, + "end": { + "line": 8, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 8, + "column": 6 + }, + "end": { + "line": 8, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 8, + "column": 11 + }, + "end": { + "line": 8, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "start": 89, + "end": 90, + "loc": { + "start": { + "line": 8, + "column": 11 + }, + "end": { + "line": 8, + "column": 12 + }, + "identifierName": "B" + }, + "name": "B" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 78 + } + }, + "right": { + "type": "ObjectExpression", + "start": 94, + "end": 124, + "loc": { + "start": { + "line": 8, + "column": 16 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 98, + "end": 111, + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 15 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 98, + "end": 102, + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 6 + }, + "identifierName": "name" + }, + "name": "name" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start": 104, + "end": 111, + "loc": { + "start": { + "line": 9, + "column": 8 + }, + "end": { + "line": 9, + "column": 15 + } + }, + "extra": { + "rawValue": "Alice", + "raw": "'Alice'" + }, + "value": "Alice" + } + }, + { + "type": "ObjectProperty", + "start": 115, + "end": 122, + "loc": { + "start": { + "line": 10, + "column": 2 + }, + "end": { + "line": 10, + "column": 9 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 115, + "end": 118, + "loc": { + "start": { + "line": 10, + "column": 2 + }, + "end": { + "line": 10, + "column": 5 + }, + "identifierName": "age" + }, + "name": "age" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 10, + "column": 7 + }, + "end": { + "line": 10, + "column": 9 + } + }, + "extra": { + "rawValue": 23, + "raw": "23" + }, + "value": 23 + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file