MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / detectMoveMechanism

Function detectMoveMechanism

packages/cli/src/batch-transform.ts:1003–1026  ·  view source on GitHub ↗

* Detect which mechanism should be used to apply a move to this element. * - framer-motion: element is motion.* with animate prop that ALREADY contains x or y * (only if the animate prop already controls position — don't inject position into * opacity-only or scale-only animations) * - trans

(nodePath: any, axis: "x" | "y")

Source from the content-addressed store, hash-verified

1001 * without destroying existing values
1002 */
1003function detectMoveMechanism(nodePath: any, axis: "x" | "y"): MoveMechanism {
1004 const node = nodePath.node;
1005 const tagName = getJSXTagName(node);
1006 const attrs = node.openingElement?.attributes ?? [];
1007
1008 // Check for framer-motion: tag starts with "motion." and animate prop has x/y
1009 if (tagName?.startsWith("motion.")) {
1010 const animateProp = attrs.find(
1011 (a: any) => a.type === "JSXAttribute" && a.name?.name === "animate"
1012 );
1013 if (animateProp?.value?.type === "JSXExpressionContainer") {
1014 const expr = animateProp.value.expression;
1015 if (expr.type === "ObjectExpression") {
1016 const propName = axis === "x" ? "x" : "y";
1017 const hasAxisProp = expr.properties.some(
1018 (p: any) => p.type === "ObjectProperty" && p.key?.name === propName
1019 );
1020 if (hasAxisProp) return "framer-motion";
1021 }
1022 }
1023 }
1024
1025 return "translate-class";
1026}
1027
1028/**
1029 * Apply move to a framer-motion element by modifying the animate prop's existing x/y value.

Callers 1

applyOpFunction · 0.85

Calls 1

getJSXTagNameFunction · 0.85

Tested by

no test coverage detected