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

Function pushMethod

packages/babel-plugin-transform-classes/src/transformClass.ts:526–583  ·  view source on GitHub ↗

* Push a method to its respective mutatorMap.

(node: t.ClassMethod, wrapped?: t.Expression)

Source from the content-addressed store, hash-verified

524 * Push a method to its respective mutatorMap.
525 */
526 function pushMethod(node: t.ClassMethod, wrapped?: t.Expression) {
527 if (assumptions.setClassMethods) {
528 if (node.kind === "method") {
529 processMethodToSet(node as Extract<typeof node, { kind: "method" }>);
530 return;
531 } else if (
532 (node.kind === "get" || node.kind === "set") &&
533 // If either 'setClassMethods' or 'noClassPrototypeAssign' is not
534 // enabled we need to emit `createClass` anyway
535 assumptions.noClassPrototypeAssign
536 ) {
537 processAccessorToDefine(
538 node as Extract<typeof node, { kind: "get" | "set" }>,
539 );
540 return;
541 }
542 }
543
544 const placement = node.static ? "static" : "instance";
545 const methods = classState.methods[placement];
546
547 const descKey = node.kind === "method" ? "value" : node.kind;
548 const key =
549 t.isNumericLiteral(node.key) || t.isBigIntLiteral(node.key)
550 ? t.stringLiteral(String(node.key.value))
551 : t.toComputedKey(node);
552 methods.hasComputed = !t.isStringLiteral(key);
553
554 const fn: t.Expression = wrapped ?? t.toExpression(node);
555
556 let descriptor: Descriptor;
557 if (
558 !methods.hasComputed &&
559 methods.map.has((key as t.StringLiteral).value)
560 ) {
561 descriptor = methods.map.get((key as t.StringLiteral).value)!;
562 descriptor[descKey] = fn;
563
564 if (descKey === "value") {
565 descriptor.get = null;
566 descriptor.set = null;
567 } else {
568 descriptor.value = null;
569 }
570 } else {
571 descriptor = {
572 key:
573 // private name has been handled in class-properties transform
574 key as t.Expression,
575 [descKey]: fn,
576 } as Descriptor;
577 methods.list.push(descriptor);
578
579 if (!methods.hasComputed) {
580 methods.map.set((key as t.StringLiteral).value, descriptor);
581 }
582 }
583 }

Callers 1

pushBodyFunction · 0.85

Calls 9

processMethodToSetFunction · 0.85
processAccessorToDefineFunction · 0.85
isNumericLiteralMethod · 0.80
isBigIntLiteralMethod · 0.80
isStringLiteralMethod · 0.80
getMethod · 0.65
setMethod · 0.65
hasMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected