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

Function applyOp

packages/cli/src/batch-transform.ts:502–635  ·  view source on GitHub ↗
(j: any, root: any, rop: ResolvedOp, source: string)

Source from the content-addressed store, hash-verified

500// ── Mutation application ─────────────────────────────────────────────────
501
502function applyOp(j: any, root: any, rop: ResolvedOp, source: string): string | undefined {
503 const { op, node } = rop;
504
505 switch (op.op) {
506 case "updateClass": {
507 const updates: ClassNameUpdate[] = op.updates.map((u: Extract<BatchOperation, { op: "updateClass" }>["updates"][number]) => ({
508 tailwindPrefix: u.tailwindPrefix,
509 tailwindToken: u.tailwindToken,
510 value: u.value,
511 relatedPrefixes: u.relatedPrefixes,
512 classPattern: u.classPattern,
513 standalone: u.standalone,
514 }));
515 mutateClassName(j, node, updates);
516 return undefined;
517 }
518
519 case "updateText": {
520 if (node) {
521 const boundFallback = replaceBoundStringLiteralInElement(j, root, node, op.originalText, op.newText);
522 if (boundFallback) {
523 return undefined;
524 }
525
526 const found = mutateTextContent(node, op.originalText, op.newText, source, op.cursorOffset, op.textAnchor);
527 if (found) {
528 return undefined;
529 }
530 }
531
532 const fallback = replaceStringLiteralInFile(j, root, op.originalText, op.newText);
533 if (fallback.replaced) {
534 return undefined;
535 }
536 if (fallback.ambiguous) {
537 return `Text "${op.originalText}" matched multiple string literals in this file; refusing ambiguous rewrite`;
538 }
539 return `No matching text "${op.originalText}" found in element at ${op.line}:${op.col}`;
540 }
541
542 case "reorder": {
543 mutateReorder(j, root, op.fromLine, op.toLine);
544 return undefined;
545 }
546
547 case "moveSpacing": {
548 // Detect the right mechanism for this element
549 const mechanism = detectMoveMechanism(node, op.axis);
550
551 if (mechanism === "framer-motion") {
552 return applyFramerMotionMove(node, op);
553 }
554
555 // Default: CSS translate class
556 const basePrefix = op.axis === "x" ? "translate-x" : "translate-y";
557 const classPattern = `^-?${basePrefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(-|$)`;
558 const classPatternRe = new RegExp(classPattern);
559

Callers 1

executeBatchFunction · 0.85

Calls 11

mutateClassNameFunction · 0.85
mutateTextContentFunction · 0.85
mutateReorderFunction · 0.85
detectMoveMechanismFunction · 0.85
applyFramerMotionMoveFunction · 0.85
getJSXStaticClassesFunction · 0.85
parseTranslateClassPxFunction · 0.85
removeClassByPatternFunction · 0.85
snapPxToSpacingTokenFunction · 0.85

Tested by

no test coverage detected