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

Function pushMethod

packages/babel-plugin-transform-classes/src/transformClass.ts:554–611  ·  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

552 * Push a method to its respective mutatorMap.
553 */
554 function pushMethod(node: t.ClassMethod, wrapped?: t.Expression) {
555 if (assumptions.setClassMethods) {
556 if (node.kind === "method") {
557 processMethodToSet(node as Extract<typeof node, { kind: "method" }>);
558 return;
559 } else if (
560 (node.kind === "get" || node.kind === "set") &&
561 // If either 'setClassMethods' or 'noClassPrototypeAssign' is not
562 // enabled we need to emit `createClass` anyway
563 assumptions.noClassPrototypeAssign
564 ) {
565 processAccessorToDefine(
566 node as Extract<typeof node, { kind: "get" | "set" }>,
567 );
568 return;
569 }
570 }
571
572 const placement = node.static ? "static" : "instance";
573 const methods = classState.methods[placement];
574
575 const descKey = node.kind === "method" ? "value" : node.kind;
576 const key =
577 t.isNumericLiteral(node.key) || t.isBigIntLiteral(node.key)
578 ? t.stringLiteral(String(node.key.value))
579 : t.toComputedKey(node);
580 methods.hasComputed = !t.isStringLiteral(key);
581
582 const fn: t.Expression = wrapped ?? t.toExpression(node);
583
584 let descriptor: Descriptor;
585 if (
586 !methods.hasComputed &&
587 methods.map.has((key as t.StringLiteral).value)
588 ) {
589 descriptor = methods.map.get((key as t.StringLiteral).value)!;
590 descriptor[descKey] = fn;
591
592 if (descKey === "value") {
593 descriptor.get = null;
594 descriptor.set = null;
595 } else {
596 descriptor.value = null;
597 }
598 } else {
599 descriptor = {
600 key:
601 // private name has been handled in class-properties transform
602 key as t.Expression,
603 [descKey]: fn,
604 } as Descriptor;
605 methods.list.push(descriptor);
606
607 if (!methods.hasComputed) {
608 methods.map.set((key as t.StringLiteral).value, descriptor);
609 }
610 }
611 }

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