mirror of
https://github.com/hanzoai/babel.git
synced 2026-09-24 16:56:45 +00:00
Simplify AST validators (#18103)
This commit is contained in:
@@ -61,24 +61,25 @@ function addIsHelper(
|
||||
// Signature overload to avoid issues like https://github.com/babel/babel/pull/17503#discussion_r2325598609
|
||||
`export function is${type}(node: t.Node | null | undefined): ${result};`,
|
||||
`export function is${type}<Opts extends Options<t.${type}>>(node: t.Node | null | undefined, opts?: Opts | null): ${resultWithOpts};`,
|
||||
`export function is${type}<Opts extends Options<t.${type}>>(node: t.Node | null | undefined, opts?: Opts | null): boolean {
|
||||
${deprecatedWarning || ""}
|
||||
if (!node) return false;
|
||||
`export function is${type}<Opts extends Options<t.${type}>>(node: t.Node | null | undefined, opts?: Opts | null): boolean {`,
|
||||
deprecatedWarning || "",
|
||||
cases
|
||||
? `
|
||||
if (!node) return false;
|
||||
|
||||
${
|
||||
cases
|
||||
? `
|
||||
switch(node.type){
|
||||
${cases}
|
||||
default:
|
||||
return false;
|
||||
}`
|
||||
: `if (node.type !== ${targetType}) return false;`
|
||||
}
|
||||
|
||||
return opts == null || shallowEqual(node, opts);
|
||||
switch(node.type){
|
||||
${cases}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return opts == null || shallowEqual(node, opts);
|
||||
`
|
||||
: `
|
||||
return isType<t.${type}>(${targetType}, node, opts);
|
||||
`,
|
||||
"}",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
@@ -91,17 +92,9 @@ export default function generateValidators() {
|
||||
/* eslint-disable no-fallthrough */
|
||||
|
||||
import shallowEqual from "../../utils/shallowEqual.ts";
|
||||
import isType, {type Options} from "../../utils/isType.ts";
|
||||
import type * as t from "../../index.ts";
|
||||
import deprecationWarning from "../../utils/deprecationWarning.ts";
|
||||
|
||||
type Options<Obj> = Partial<{
|
||||
[Prop in Exclude<keyof Obj, "type">]: Obj[Prop] extends t.Node
|
||||
? t.Node
|
||||
: Obj[Prop] extends t.Node[]
|
||||
? t.Node[]
|
||||
: Obj[Prop];
|
||||
}>;
|
||||
|
||||
`;
|
||||
|
||||
Object.keys(VISITOR_KEYS).forEach(type => {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import shallowEqual from "./shallowEqual.ts";
|
||||
import type * as t from "../index.ts";
|
||||
|
||||
export type Options<Obj> = Partial<{
|
||||
[Prop in Exclude<keyof Obj, "type">]: Obj[Prop] extends t.Node
|
||||
? t.Node
|
||||
: Obj[Prop] extends t.Node[]
|
||||
? t.Node[]
|
||||
: Obj[Prop];
|
||||
}>;
|
||||
|
||||
export default function isType<T extends t.Node>(
|
||||
type: T["type"],
|
||||
node: t.Node | null | undefined,
|
||||
opts: Options<T> | null | undefined,
|
||||
): boolean {
|
||||
return node?.type === type && (opts == null || shallowEqual(node, opts));
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user