MCPcopy Create free account
hub / github.com/BuilderIO/mitosis / addUpdateAfterSetInCode

Function addUpdateAfterSetInCode

packages/core/src/generators/html/generator.ts:542–590  ·  view source on GitHub ↗
(
  code = '',
  options: InternalToHtmlOptions,
  useString = options.format === 'class' ? 'this.update' : 'update',
)

Source from the content-addressed store, hash-verified

540};
541
542function addUpdateAfterSetInCode(
543 code = '',
544 options: InternalToHtmlOptions,
545 useString = options.format === 'class' ? 'this.update' : 'update',
546) {
547 let updates = 0;
548 return babelTransformExpression(code, {
549 AssignmentExpression(path: babel.NodePath<babel.types.AssignmentExpression>) {
550 const { node } = path;
551 if (types.isMemberExpression(node.left)) {
552 if (types.isIdentifier(node.left.object)) {
553 // TODO: utillity to properly trace this reference to the beginning
554 if (node.left.object.name === 'state') {
555 // TODO: ultimately do updates by property, e.g. updateName()
556 // that updates any attributes dependent on name, etcç
557 let parent: NodePath<any> | null = path;
558
559 // `_temp = ` assignments are created sometimes when we insertAfter
560 // for simple expressions. this causes us to re-process the same expression
561 // in an infinite loop
562 while ((parent = parent.parentPath)) {
563 if (
564 types.isAssignmentExpression(parent.node) &&
565 types.isIdentifier(parent.node.left) &&
566 parent.node.left.name.startsWith('_temp')
567 ) {
568 return;
569 }
570 }
571
572 // Uncomment to debug infinite loops:
573 // if (updates++ > 1000) {
574 // console.error('Infinite assignment detected');
575 // return;
576 // }
577 if (options?.experimental?.addUpdateAfterSetInCode) {
578 useString = options?.experimental?.addUpdateAfterSetInCode(useString, options, {
579 node,
580 code,
581 types,
582 });
583 }
584 path.insertAfter(types.callExpression(types.identifier(useString), []));
585 }
586 }
587 }
588 },
589 });
590}
591
592const htmlDecode = (html: string) => html.replace(/&quot;/gi, '"');
593

Callers 3

addUpdateAfterSetFunction · 0.85
componentToHtmlFunction · 0.85
componentToCustomElementFunction · 0.85

Calls 1

babelTransformExpressionFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…