diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index 5d5cdd283a8f..e83acac52cf7 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -3540,6 +3540,7 @@ export default (superClass: ClassWithMixin) => this.toAssignableParenthesizedExpression(node, isLHS); break; case "TSAsExpression": + case "TSSatisfiesExpression": case "TSNonNullExpression": case "TSTypeAssertion": if (isLHS) { @@ -3565,6 +3566,7 @@ export default (superClass: ClassWithMixin) => toAssignableParenthesizedExpression(node: N.Node, isLHS: boolean): void { switch (node.expression.type) { case "TSAsExpression": + case "TSSatisfiesExpression": case "TSNonNullExpression": case "TSTypeAssertion": case "ParenthesizedExpression": @@ -3578,6 +3580,7 @@ export default (superClass: ClassWithMixin) => checkToRestConversion(node: N.Node, allowPattern: boolean): void { switch (node.type) { case "TSAsExpression": + case "TSSatisfiesExpression": case "TSTypeAssertion": case "TSNonNullExpression": this.checkToRestConversion(node.expression, false); @@ -3594,6 +3597,7 @@ export default (superClass: ClassWithMixin) => | "TSParameterProperty" | "TSNonNullExpression" | "TSAsExpression" + | "TSSatisfiesExpression" | "TSTypeAssertion", isUnparenthesizedInAssign: boolean, binding: BindingTypes, @@ -3609,6 +3613,8 @@ export default (superClass: ClassWithMixin) => TSNonNullExpression: "expression", TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true], + TSSatisfiesExpression: (binding !== BIND_NONE || + !isUnparenthesizedInAssign) && ["expression", true], TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true], }, diff --git a/packages/babel-parser/test/fixtures/typescript/cast/satisfies/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/satisfies/input.ts index fe7ac9eea0aa..b3198b63b970 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/satisfies/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/cast/satisfies/input.ts @@ -1,2 +1,4 @@ x satisfies T; x < y satisfies boolean; // (x < y) satisfies boolean; +(a satisfies any) = null; +[(a satisfies any)] = b; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/satisfies/output.json b/packages/babel-parser/test/fixtures/typescript/cast/satisfies/output.json index 06cdecfe8f46..ca5f04388a76 100644 --- a/packages/babel-parser/test/fixtures/typescript/cast/satisfies/output.json +++ b/packages/babel-parser/test/fixtures/typescript/cast/satisfies/output.json @@ -1,9 +1,9 @@ { "type": "File", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":54,"index":69}}, + "start":0,"end":120,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":24,"index":120}}, "program": { "type": "Program", - "start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":54,"index":69}}, + "start":0,"end":120,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":24,"index":120}}, "sourceType": "module", "interpreter": null, "body": [ @@ -62,6 +62,80 @@ "start":40,"end":69,"loc":{"start":{"line":2,"column":25,"index":40},"end":{"line":2,"column":54,"index":69}} } ] + }, + { + "type": "ExpressionStatement", + "start":70,"end":95,"loc":{"start":{"line":3,"column":0,"index":70},"end":{"line":3,"column":25,"index":95}}, + "expression": { + "type": "AssignmentExpression", + "start":70,"end":94,"loc":{"start":{"line":3,"column":0,"index":70},"end":{"line":3,"column":24,"index":94}}, + "operator": "=", + "left": { + "type": "TSSatisfiesExpression", + "start":71,"end":86,"loc":{"start":{"line":3,"column":1,"index":71},"end":{"line":3,"column":16,"index":86}}, + "expression": { + "type": "Identifier", + "start":71,"end":72,"loc":{"start":{"line":3,"column":1,"index":71},"end":{"line":3,"column":2,"index":72},"identifierName":"a"}, + "name": "a" + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":83,"end":86,"loc":{"start":{"line":3,"column":13,"index":83},"end":{"line":3,"column":16,"index":86}} + }, + "extra": { + "parenthesized": true, + "parenStart": 70 + } + }, + "right": { + "type": "NullLiteral", + "start":90,"end":94,"loc":{"start":{"line":3,"column":20,"index":90},"end":{"line":3,"column":24,"index":94}} + } + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " (x < y) satisfies boolean;", + "start":40,"end":69,"loc":{"start":{"line":2,"column":25,"index":40},"end":{"line":2,"column":54,"index":69}} + } + ] + }, + { + "type": "ExpressionStatement", + "start":96,"end":120,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":24,"index":120}}, + "expression": { + "type": "AssignmentExpression", + "start":96,"end":119,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":23,"index":119}}, + "operator": "=", + "left": { + "type": "ArrayPattern", + "start":96,"end":115,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":19,"index":115}}, + "elements": [ + { + "type": "TSSatisfiesExpression", + "start":98,"end":113,"loc":{"start":{"line":4,"column":2,"index":98},"end":{"line":4,"column":17,"index":113}}, + "expression": { + "type": "Identifier", + "start":98,"end":99,"loc":{"start":{"line":4,"column":2,"index":98},"end":{"line":4,"column":3,"index":99},"identifierName":"a"}, + "name": "a" + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":110,"end":113,"loc":{"start":{"line":4,"column":14,"index":110},"end":{"line":4,"column":17,"index":113}} + }, + "extra": { + "parenthesized": true, + "parenStart": 97 + } + } + ] + }, + "right": { + "type": "Identifier", + "start":118,"end":119,"loc":{"start":{"line":4,"column":22,"index":118},"end":{"line":4,"column":23,"index":119},"identifierName":"b"}, + "name": "b" + } + } } ], "directives": []