MCPcopy Create free account
hub / github.com/babel/babel / assertNodeOrValueType

Function assertNodeOrValueType

packages/babel-types/src/definitions/utils.ts:245–269  ·  view source on GitHub ↗
(
  ...types: (NodeTypes | PrimitiveTypes)[]
)

Source from the content-addressed store, hash-verified

243}
244
245export function assertNodeOrValueType<T extends t.Node>(
246 ...types: (NodeTypes | PrimitiveTypes)[]
247): Validator<T> {
248 function validate(node: T, key: string | { toString(): string }, val: any) {
249 const primitiveType = getType(val);
250 for (const type of types) {
251 if (primitiveType === type || is(type, val)) {
252 validateChild(node, key, val);
253 return;
254 }
255 }
256
257 throw new TypeError(
258 `Property ${key} of ${
259 node.type
260 } expected node to be of a type ${JSON.stringify(
261 types,
262 )} but instead got ${JSON.stringify(val?.type)}`,
263 );
264 }
265
266 validate.oneOfNodeOrValueTypes = types;
267
268 return validate;
269}
270
271export function assertValueType<T extends t.Node>(
272 type: PrimitiveTypes,

Callers 2

experimental.tsFile · 0.90
core.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected