Skip to content

Commit

Permalink
Add TSSatisfiesExpression to types and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jan 27, 2022
1 parent ebf1902 commit 668f7cc
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 9 deletions.
14 changes: 11 additions & 3 deletions packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,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("<");
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-generator/src/node/parentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
isVariableDeclarator,
isWhileStatement,
isYieldExpression,
isTSSatisfiesExpression,
} from "@babel/types";
import type * as t from "@babel/types";
const PRECEDENCE = {
Expand Down Expand Up @@ -197,9 +198,10 @@ export function TSAsExpression() {
return true;
}

export function TSTypeAssertion() {
return true;
}
export {
TSAsExpression as TSSatisfiesExpression,
TSAsExpression as TSTypeAssertion,
};

export function TSUnionType(node: any, parent: any): boolean {
return (
Expand Down Expand Up @@ -302,7 +304,8 @@ export function ConditionalExpression(node: any, parent?): boolean {
isConditionalExpression(parent, { test: node }) ||
isAwaitExpression(parent) ||
isTSTypeAssertion(parent) ||
isTSAsExpression(parent)
isTSAsExpression(parent) ||
isTSSatisfiesExpression(parent)
) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions packages/babel-traverse/src/path/generated/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ export interface NodePathAssetions {
opts?: object,
): asserts this is NodePath<t.TSQualifiedName>;
assertTSRestType(opts?: object): asserts this is NodePath<t.TSRestType>;
assertTSSatisfiesExpression(
opts?: object,
): asserts this is NodePath<t.TSSatisfiesExpression>;
assertTSStringKeyword(
opts?: object,
): asserts this is NodePath<t.TSStringKeyword>;
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-traverse/src/path/generated/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ export interface NodePathValidators {
isTSPropertySignature(opts?: object): this is NodePath<t.TSPropertySignature>;
isTSQualifiedName(opts?: object): this is NodePath<t.TSQualifiedName>;
isTSRestType(opts?: object): this is NodePath<t.TSRestType>;
isTSSatisfiesExpression(
opts?: object,
): this is NodePath<t.TSSatisfiesExpression>;
isTSStringKeyword(opts?: object): this is NodePath<t.TSStringKeyword>;
isTSSymbolKeyword(opts?: object): this is NodePath<t.TSSymbolKeyword>;
isTSThisType(opts?: object): this is NodePath<t.TSThisType>;
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-types/src/asserts/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,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,
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-types/src/ast-types/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export type Node =
| TSPropertySignature
| TSQualifiedName
| TSRestType
| TSSatisfiesExpression
| TSStringKeyword
| TSSymbolKeyword
| TSThisType
Expand Down Expand Up @@ -1920,6 +1921,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;
Expand Down Expand Up @@ -2146,6 +2153,7 @@ export type Expression =
| PipelineBareFunction
| PipelinePrimaryTopicReference
| TSAsExpression
| TSSatisfiesExpression
| TSTypeAssertion
| TSNonNullExpression;
export type Binary = BinaryExpression | LogicalExpression;
Expand Down Expand Up @@ -2582,6 +2590,7 @@ export type TypeScript =
| TSInterfaceBody
| TSTypeAliasDeclaration
| TSAsExpression
| TSSatisfiesExpression
| TSTypeAssertion
| TSEnumDeclaration
| TSEnumMember
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-types/src/builders/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,13 @@ export function tsAsExpression(
return builder.apply("TSAsExpression", arguments);
}
export { tsAsExpression as tSAsExpression };
export function tsSatisfiesExpression(
expression: t.Expression,
typeAnnotation: t.TSType,
): t.TSSatisfiesExpression {
return builder.apply("TSSatisfiesExpression", arguments);
}
export { tsSatisfiesExpression as tSSatisfiesExpression };
export function tsTypeAssertion(
typeAnnotation: t.TSType,
expression: t.Expression,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/builders/generated/uppercase.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export {
tsInterfaceBody as TSInterfaceBody,
tsTypeAliasDeclaration as TSTypeAliasDeclaration,
tsAsExpression as TSAsExpression,
tsSatisfiesExpression as TSSatisfiesExpression,
tsTypeAssertion as TSTypeAssertion,
tsEnumDeclaration as TSEnumDeclaration,
tsEnumMember as TSEnumMember,
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-types/src/definitions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,17 @@ defineType("TSTypeAliasDeclaration", {
},
});

defineType("TSAsExpression", {
const TSTypeExpression = {
aliases: ["Expression"],
visitor: ["expression", "typeAnnotation"],
fields: {
expression: validateType("Expression"),
typeAnnotation: validateType("TSType"),
},
});
};

defineType("TSAsExpression", TSTypeExpression);
defineType("TSSatisfiesExpression", TSTypeExpression);

defineType("TSTypeAssertion", {
aliases: ["Expression"],
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-types/src/validators/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3932,6 +3932,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,
Expand Down Expand Up @@ -4351,6 +4368,7 @@ export function isExpression(
"PipelineBareFunction" === nodeType ||
"PipelinePrimaryTopicReference" === nodeType ||
"TSAsExpression" === nodeType ||
"TSSatisfiesExpression" === nodeType ||
"TSTypeAssertion" === nodeType ||
"TSNonNullExpression" === nodeType ||
(nodeType === "Placeholder" &&
Expand Down Expand Up @@ -5565,6 +5583,7 @@ export function isTypeScript(
"TSInterfaceBody" === nodeType ||
"TSTypeAliasDeclaration" === nodeType ||
"TSAsExpression" === nodeType ||
"TSSatisfiesExpression" === nodeType ||
"TSTypeAssertion" === nodeType ||
"TSEnumDeclaration" === nodeType ||
"TSEnumMember" === nodeType ||
Expand Down

0 comments on commit 668f7cc

Please sign in to comment.