From ac6d1384d934257535f28130f5587cd3e0fcdf33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 27 Jan 2022 23:24:15 +0100 Subject: [PATCH] Add `TSSatisfiesExpression` to types and generator --- .../src/generators/typescript.ts | 14 +- .../babel-generator/src/node/parentheses.ts | 11 +- .../fixtures/typescript/satisfies/input.js | 4 + .../fixtures/typescript/satisfies/output.js | 6 + .../src/path/generated/asserts.d.ts | 3 + .../src/path/generated/validators.d.ts | 4 + .../src/asserts/generated/index.ts | 6 + .../src/ast-types/generated/index.ts | 163 ++++++++++++++++++ .../src/builders/generated/index.ts | 12 ++ .../src/builders/generated/uppercase.js | 1 + packages/babel-types/src/definitions/core.ts | 6 + .../babel-types/src/definitions/typescript.ts | 7 +- .../src/validators/generated/index.ts | 21 +++ 13 files changed, 249 insertions(+), 9 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/satisfies/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/satisfies/output.js diff --git a/packages/babel-generator/src/generators/typescript.ts b/packages/babel-generator/src/generators/typescript.ts index 23ce5853561a..63981ba69db9 100644 --- a/packages/babel-generator/src/generators/typescript.ts +++ b/packages/babel-generator/src/generators/typescript.ts @@ -522,15 +522,23 @@ export function TSTypeAliasDeclaration( this.token(";"); } -export function TSAsExpression(this: Printer, node: t.TSAsExpression) { - const { expression, typeAnnotation } = node; +function TSTypeExpression( + this: Printer, + node: t.TSAsExpression | t.TSSatisfiesExpression, +) { + const { type, expression, typeAnnotation } = node; this.print(expression, node); this.space(); - this.word("as"); + this.word(type === "TSAsExpression" ? "as" : "satisfies"); this.space(); this.print(typeAnnotation, node); } +export { + TSTypeExpression as TSAsExpression, + TSTypeExpression as TSSatisfiesExpression, +}; + export function TSTypeAssertion(this: Printer, node: t.TSTypeAssertion) { const { typeAnnotation, expression } = node; this.token("<"); diff --git a/packages/babel-generator/src/node/parentheses.ts b/packages/babel-generator/src/node/parentheses.ts index 4cf704caf481..2e269d8355a6 100644 --- a/packages/babel-generator/src/node/parentheses.ts +++ b/packages/babel-generator/src/node/parentheses.ts @@ -49,6 +49,7 @@ import { isVariableDeclarator, isWhileStatement, isYieldExpression, + isTSSatisfiesExpression, } from "@babel/types"; import type * as t from "@babel/types"; const PRECEDENCE = { @@ -225,9 +226,10 @@ export function TSAsExpression() { return true; } -export function TSTypeAssertion() { - return true; -} +export { + TSAsExpression as TSSatisfiesExpression, + TSAsExpression as TSTypeAssertion, +}; export function TSUnionType(node: t.TSUnionType, parent: t.Node): boolean { return ( @@ -368,7 +370,8 @@ export function ConditionalExpression( isConditionalExpression(parent, { test: node }) || isAwaitExpression(parent) || isTSTypeAssertion(parent) || - isTSAsExpression(parent) + isTSAsExpression(parent) || + isTSSatisfiesExpression(parent) ) { return true; } diff --git a/packages/babel-generator/test/fixtures/typescript/satisfies/input.js b/packages/babel-generator/test/fixtures/typescript/satisfies/input.js new file mode 100644 index 000000000000..676f9f25adfc --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/satisfies/input.js @@ -0,0 +1,4 @@ +x satisfies T; +x < y satisfies boolean; // (x < y) satisfies boolean; +x === 1 satisfies number; // x === (1 satisfies number); +x satisfies any satisfies T; diff --git a/packages/babel-generator/test/fixtures/typescript/satisfies/output.js b/packages/babel-generator/test/fixtures/typescript/satisfies/output.js new file mode 100644 index 000000000000..9dc8213a8212 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/satisfies/output.js @@ -0,0 +1,6 @@ +(x satisfies T); +(x < y satisfies boolean); // (x < y) satisfies boolean; + +x === (1 satisfies number); // x === (1 satisfies number); + +((x satisfies any) satisfies T); \ No newline at end of file diff --git a/packages/babel-traverse/src/path/generated/asserts.d.ts b/packages/babel-traverse/src/path/generated/asserts.d.ts index 8f58d171740d..f2a0369b01be 100644 --- a/packages/babel-traverse/src/path/generated/asserts.d.ts +++ b/packages/babel-traverse/src/path/generated/asserts.d.ts @@ -577,6 +577,9 @@ export interface NodePathAssetions { opts?: object, ): asserts this is NodePath; assertTSRestType(opts?: object): asserts this is NodePath; + assertTSSatisfiesExpression( + opts?: object, + ): asserts this is NodePath; assertTSStringKeyword( opts?: object, ): asserts this is NodePath; diff --git a/packages/babel-traverse/src/path/generated/validators.d.ts b/packages/babel-traverse/src/path/generated/validators.d.ts index 908e5db8f355..7c3d14d4f2e4 100644 --- a/packages/babel-traverse/src/path/generated/validators.d.ts +++ b/packages/babel-traverse/src/path/generated/validators.d.ts @@ -995,6 +995,10 @@ interface BaseNodePathValidators { this: NodePath, opts?: object, ): this is NodePath; + isTSSatisfiesExpression( + this: NodePath, + opts?: object, + ): this is NodePath; isTSStringKeyword( this: NodePath, opts?: object, diff --git a/packages/babel-types/src/asserts/generated/index.ts b/packages/babel-types/src/asserts/generated/index.ts index b3cece7f340d..a643e8a14bfb 100644 --- a/packages/babel-types/src/asserts/generated/index.ts +++ b/packages/babel-types/src/asserts/generated/index.ts @@ -1412,6 +1412,12 @@ export function assertTSAsExpression( ): asserts node is t.TSAsExpression { assert("TSAsExpression", node, opts); } +export function assertTSSatisfiesExpression( + node: object | null | undefined, + opts?: object | null, +): asserts node is t.TSSatisfiesExpression { + assert("TSSatisfiesExpression", node, opts); +} export function assertTSTypeAssertion( node: object | null | undefined, opts?: object | null, diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index 9975a9536fb9..6beb4233a2ef 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -253,6 +253,7 @@ export type Node = | TSPropertySignature | TSQualifiedName | TSRestType + | TSSatisfiesExpression | TSStringKeyword | TSSymbolKeyword | TSThisType @@ -719,6 +720,7 @@ export interface AssignmentPattern extends BaseNode { | ArrayPattern | MemberExpression | TSAsExpression + | TSSatisfiesExpression | TSTypeAssertion | TSNonNullExpression; right: Expression; @@ -1976,6 +1978,12 @@ export interface TSAsExpression extends BaseNode { typeAnnotation: TSType; } +export interface TSSatisfiesExpression extends BaseNode { + type: "TSSatisfiesExpression"; + expression: Expression; + typeAnnotation: TSType; +} + export interface TSTypeAssertion extends BaseNode { type: "TSTypeAssertion"; typeAnnotation: TSType; @@ -2206,6 +2214,7 @@ export type Expression = | PipelinePrimaryTopicReference | TSInstantiationExpression | TSAsExpression + | TSSatisfiesExpression | TSTypeAssertion | TSNonNullExpression; export type Binary = BinaryExpression | LogicalExpression; @@ -2383,6 +2392,7 @@ export type PatternLike = | ArrayPattern | ObjectPattern | TSAsExpression + | TSSatisfiesExpression | TSTypeAssertion | TSNonNullExpression; export type LVal = @@ -2394,6 +2404,7 @@ export type LVal = | ObjectPattern | TSParameterProperty | TSAsExpression + | TSSatisfiesExpression | TSTypeAssertion | TSNonNullExpression; export type TSEntityName = Identifier | TSQualifiedName; @@ -2655,6 +2666,7 @@ export type TypeScript = | TSTypeAliasDeclaration | TSInstantiationExpression | TSAsExpression + | TSSatisfiesExpression | TSTypeAssertion | TSEnumDeclaration | TSEnumMember @@ -2865,6 +2877,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -2971,6 +2984,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3034,6 +3048,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3114,6 +3129,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3178,6 +3194,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3241,6 +3258,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3304,6 +3322,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3393,6 +3412,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3520,6 +3540,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3602,6 +3623,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3679,6 +3701,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -3770,6 +3793,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4029,6 +4053,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4341,6 +4366,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4514,6 +4540,7 @@ export interface ParentMaps { | TSParameterProperty | TSPropertySignature | TSQualifiedName + | TSSatisfiesExpression | TSTypeAliasDeclaration | TSTypeAssertion | TSTypePredicate @@ -4596,6 +4623,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4795,6 +4823,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4863,6 +4892,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -4962,6 +4992,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5027,6 +5058,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5090,6 +5122,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5178,6 +5211,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5241,6 +5275,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5323,6 +5358,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5489,6 +5525,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5552,6 +5589,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5701,6 +5739,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5789,6 +5828,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5852,6 +5892,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5915,6 +5956,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -5978,6 +6020,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6041,6 +6084,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6119,6 +6163,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6182,6 +6227,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6283,6 +6329,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6372,6 +6419,7 @@ export interface ParentMaps { | TSModuleDeclaration | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6485,6 +6533,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6548,6 +6597,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6568,6 +6618,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6630,6 +6681,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6653,6 +6705,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6673,6 +6726,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6694,6 +6748,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6715,6 +6770,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6785,6 +6841,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6806,6 +6863,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6840,6 +6898,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6862,6 +6921,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6882,6 +6942,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -6942,6 +7003,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -6981,6 +7043,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7001,6 +7064,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7021,6 +7085,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7041,6 +7106,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7094,6 +7160,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7156,6 +7223,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -7179,6 +7247,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7199,6 +7268,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7219,6 +7289,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7239,6 +7310,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7269,6 +7341,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7297,6 +7370,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7306,6 +7380,72 @@ export interface ParentMaps { | TSTypeParameterInstantiation | TSUnionType | TemplateLiteral; + TSSatisfiesExpression: + | ArrayExpression + | ArrayPattern + | ArrowFunctionExpression + | AssignmentExpression + | AssignmentPattern + | AwaitExpression + | BinaryExpression + | BindExpression + | CallExpression + | ClassAccessorProperty + | ClassDeclaration + | ClassExpression + | ClassMethod + | ClassPrivateProperty + | ClassProperty + | ConditionalExpression + | Decorator + | DoWhileStatement + | ExportDefaultDeclaration + | ExpressionStatement + | ForInStatement + | ForOfStatement + | ForStatement + | IfStatement + | JSXExpressionContainer + | JSXSpreadAttribute + | JSXSpreadChild + | LogicalExpression + | MemberExpression + | NewExpression + | ObjectMethod + | ObjectProperty + | OptionalCallExpression + | OptionalMemberExpression + | ParenthesizedExpression + | PipelineBareFunction + | PipelineTopicExpression + | RestElement + | ReturnStatement + | SequenceExpression + | SpreadElement + | SwitchCase + | SwitchStatement + | TSAsExpression + | TSDeclareMethod + | TSEnumDeclaration + | TSEnumMember + | TSExportAssignment + | TSInstantiationExpression + | TSMethodSignature + | TSNonNullExpression + | TSPropertySignature + | TSSatisfiesExpression + | TSTypeAssertion + | TaggedTemplateExpression + | TemplateLiteral + | ThrowStatement + | TupleExpression + | TypeCastExpression + | UnaryExpression + | UpdateExpression + | VariableDeclarator + | WhileStatement + | WithStatement + | YieldExpression; TSStringKeyword: | TSArrayType | TSAsExpression @@ -7317,6 +7457,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7337,6 +7478,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7357,6 +7499,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7378,6 +7521,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7480,6 +7624,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -7503,6 +7648,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7523,6 +7669,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7575,6 +7722,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7595,6 +7743,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7615,6 +7764,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7635,6 +7785,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7655,6 +7806,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7675,6 +7827,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7695,6 +7848,7 @@ export interface ParentMaps { | TSOptionalType | TSParenthesizedType | TSRestType + | TSSatisfiesExpression | TSTupleType | TSTypeAliasDeclaration | TSTypeAnnotation @@ -7755,6 +7909,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -7820,6 +7975,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -7883,6 +8039,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -7985,6 +8142,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -8062,6 +8220,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -8189,6 +8348,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -8314,6 +8474,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -8402,6 +8563,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral @@ -8544,6 +8706,7 @@ export interface ParentMaps { | TSMethodSignature | TSNonNullExpression | TSPropertySignature + | TSSatisfiesExpression | TSTypeAssertion | TaggedTemplateExpression | TemplateLiteral diff --git a/packages/babel-types/src/builders/generated/index.ts b/packages/babel-types/src/builders/generated/index.ts index acfef5336e0b..a9d696d864e7 100644 --- a/packages/babel-types/src/builders/generated/index.ts +++ b/packages/babel-types/src/builders/generated/index.ts @@ -554,6 +554,7 @@ export function assignmentPattern( | t.ArrayPattern | t.MemberExpression | t.TSAsExpression + | t.TSSatisfiesExpression | t.TSTypeAssertion | t.TSNonNullExpression, right: t.Expression, @@ -2351,6 +2352,17 @@ export function tsAsExpression( }); } export { tsAsExpression as tSAsExpression }; +export function tsSatisfiesExpression( + expression: t.Expression, + typeAnnotation: t.TSType, +): t.TSSatisfiesExpression { + return validateNode({ + type: "TSSatisfiesExpression", + expression, + typeAnnotation, + }); +} +export { tsSatisfiesExpression as tSSatisfiesExpression }; export function tsTypeAssertion( typeAnnotation: t.TSType, expression: t.Expression, diff --git a/packages/babel-types/src/builders/generated/uppercase.js b/packages/babel-types/src/builders/generated/uppercase.js index 23952d0c3530..24bdea27f963 100644 --- a/packages/babel-types/src/builders/generated/uppercase.js +++ b/packages/babel-types/src/builders/generated/uppercase.js @@ -242,6 +242,7 @@ export { tsTypeAliasDeclaration as TSTypeAliasDeclaration, tsInstantiationExpression as TSInstantiationExpression, tsAsExpression as TSAsExpression, + tsSatisfiesExpression as TSSatisfiesExpression, tsTypeAssertion as TSTypeAssertion, tsEnumDeclaration as TSEnumDeclaration, tsEnumMember as TSEnumMember, diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index 2cc13e9ed197..c9c9b580dc68 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -70,6 +70,7 @@ defineType("AssignmentExpression", { "ArrayPattern", "ObjectPattern", "TSAsExpression", + "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", ), @@ -328,6 +329,7 @@ defineType("ForInStatement", { "ArrayPattern", "ObjectPattern", "TSAsExpression", + "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", ), @@ -930,6 +932,7 @@ defineType("ObjectProperty", { "Identifier", "Pattern", "TSAsExpression", + "TSSatisfiesExpression", "TSNonNullExpression", "TSTypeAssertion", ); @@ -960,6 +963,7 @@ defineType("RestElement", { "ObjectPattern", "MemberExpression", "TSAsExpression", + "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", ), @@ -1244,6 +1248,7 @@ defineType("AssignmentPattern", { "ArrayPattern", "MemberExpression", "TSAsExpression", + "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", ), @@ -1653,6 +1658,7 @@ defineType("ForOfStatement", { "ArrayPattern", "ObjectPattern", "TSAsExpression", + "TSSatisfiesExpression", "TSTypeAssertion", "TSNonNullExpression", ); diff --git a/packages/babel-types/src/definitions/typescript.ts b/packages/babel-types/src/definitions/typescript.ts index 98a5d3ba4bf5..9475c9dd6675 100644 --- a/packages/babel-types/src/definitions/typescript.ts +++ b/packages/babel-types/src/definitions/typescript.ts @@ -463,14 +463,17 @@ defineType("TSInstantiationExpression", { }, }); -defineType("TSAsExpression", { +const TSTypeExpression = { aliases: ["Expression", "LVal", "PatternLike"], visitor: ["expression", "typeAnnotation"], fields: { expression: validateType("Expression"), typeAnnotation: validateType("TSType"), }, -}); +}; + +defineType("TSAsExpression", TSTypeExpression); +defineType("TSSatisfiesExpression", TSTypeExpression); defineType("TSTypeAssertion", { aliases: ["Expression", "LVal", "PatternLike"], diff --git a/packages/babel-types/src/validators/generated/index.ts b/packages/babel-types/src/validators/generated/index.ts index c8eb203b3f6a..3d19ea5785c7 100644 --- a/packages/babel-types/src/validators/generated/index.ts +++ b/packages/babel-types/src/validators/generated/index.ts @@ -3966,6 +3966,23 @@ export function isTSAsExpression( return false; } +export function isTSSatisfiesExpression( + node: object | null | undefined, + opts?: object | null, +): node is t.TSSatisfiesExpression { + if (!node) return false; + + const nodeType = (node as t.Node).type; + if (nodeType === "TSSatisfiesExpression") { + if (typeof opts === "undefined") { + return true; + } else { + return shallowEqual(node, opts); + } + } + + return false; +} export function isTSTypeAssertion( node: object | null | undefined, opts?: object | null, @@ -4387,6 +4404,7 @@ export function isExpression( "PipelinePrimaryTopicReference" === nodeType || "TSInstantiationExpression" === nodeType || "TSAsExpression" === nodeType || + "TSSatisfiesExpression" === nodeType || "TSTypeAssertion" === nodeType || "TSNonNullExpression" === nodeType || (nodeType === "Placeholder" && @@ -4890,6 +4908,7 @@ export function isPatternLike( "ArrayPattern" === nodeType || "ObjectPattern" === nodeType || "TSAsExpression" === nodeType || + "TSSatisfiesExpression" === nodeType || "TSTypeAssertion" === nodeType || "TSNonNullExpression" === nodeType || (nodeType === "Placeholder" && @@ -4921,6 +4940,7 @@ export function isLVal( "ObjectPattern" === nodeType || "TSParameterProperty" === nodeType || "TSAsExpression" === nodeType || + "TSSatisfiesExpression" === nodeType || "TSTypeAssertion" === nodeType || "TSNonNullExpression" === nodeType || (nodeType === "Placeholder" && @@ -5627,6 +5647,7 @@ export function isTypeScript( "TSTypeAliasDeclaration" === nodeType || "TSInstantiationExpression" === nodeType || "TSAsExpression" === nodeType || + "TSSatisfiesExpression" === nodeType || "TSTypeAssertion" === nodeType || "TSEnumDeclaration" === nodeType || "TSEnumMember" === nodeType ||