Skip to content

Commit

Permalink
Support in @babel/types
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 29, 2022
1 parent c341cda commit 24d147b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/babel-types/src/definitions/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ defineType("AssignmentExpression", {
"MemberExpression",
"ArrayPattern",
"ObjectPattern",
"TSAsExpresion",
"TSTypeAssertion",
"TSNonNullExpression",
),
},
right: {
Expand Down Expand Up @@ -319,6 +322,9 @@ defineType("ForInStatement", {
"MemberExpression",
"ArrayPattern",
"ObjectPattern",
"TSAsExpresion",
"TSTypeAssertion",
"TSNonNullExpression",
),
},
right: {
Expand Down Expand Up @@ -906,6 +912,9 @@ defineType("RestElement", {
"ArrayPattern",
"ObjectPattern",
"MemberExpression",
"TSAsExpresion",
"TSTypeAssertion",
"TSNonNullExpression",
),
},
// For Flow
Expand Down Expand Up @@ -1183,6 +1192,9 @@ defineType("AssignmentPattern", {
"ObjectPattern",
"ArrayPattern",
"MemberExpression",
"TSAsExpression",
"TSTypeAssertion",
"TSNonNullExpression",
),
},
right: {
Expand Down Expand Up @@ -1583,6 +1595,9 @@ defineType("ForOfStatement", {
"MemberExpression",
"ArrayPattern",
"ObjectPattern",
"TSAsExpresion",
"TSTypeAssertion",
"TSNonNullExpression",
);

return function (node, key, val) {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-types/src/definitions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ defineType("TSTypeAliasDeclaration", {
});

defineType("TSAsExpression", {
aliases: ["Expression"],
aliases: ["Expression", "LVal", "PatternLike"],
visitor: ["expression", "typeAnnotation"],
fields: {
expression: validateType("Expression"),
Expand All @@ -459,7 +459,7 @@ defineType("TSAsExpression", {
});

defineType("TSTypeAssertion", {
aliases: ["Expression"],
aliases: ["Expression", "LVal", "PatternLike"],
visitor: ["typeAnnotation", "expression"],
fields: {
typeAnnotation: validateType("TSType"),
Expand Down Expand Up @@ -542,7 +542,7 @@ defineType("TSExternalModuleReference", {
});

defineType("TSNonNullExpression", {
aliases: ["Expression"],
aliases: ["Expression", "LVal", "PatternLike"],
visitor: ["expression"],
fields: {
expression: validateType("Expression"),
Expand Down
30 changes: 30 additions & 0 deletions packages/babel-types/test/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as t from "../lib/index.js";

describe("validate", () => {
describe("TS", () => {
const id1 = t.identifier("foo");
const id2 = t.identifier("bar");
const lhs = [
t.tsAsExpression(id1, t.tsAnyKeyword()),
t.tsTypeAssertion(t.tsAnyKeyword(), id1),
t.tsNonNullExpression(id1),
];
const validNodes = {
"AssignmentExpression(=)": lhs => t.assignmentExpression("=", lhs, id2),
"AssignmentExpression(+=)": lhs => t.assignmentExpression("+=", lhs, id2),
ForOfStatement: lhs => t.forOfStatement(lhs, id2, t.emptyStatement()),
ForInStatement: lhs => t.forInStatement(lhs, id2, t.emptyStatement()),
"ObjectPattern > ObjectProperty": lhs =>
t.objectPattern([t.objectProperty(t.stringLiteral("x"), lhs)]),
ArrayPattern: lhs => t.arrayPattern([lhs]),
AssignmentPattern: lhs => t.assignmentPattern(lhs, id2),
};
Object.keys(validNodes).forEach(name => {
lhs.forEach(lhs => {
it(`${lhs.type} is allowed in ${name}`, () => {
expect(() => t.validate(validNodes[name](lhs))).not.toThrow();
});
});
});
});
});

0 comments on commit 24d147b

Please sign in to comment.