MCPcopy
hub / github.com/BuilderIO/mitosis / mapRefs

Function mapRefs

packages/core/src/helpers/map-refs.ts:40–113  ·  view source on GitHub ↗
(component: MitosisComponent, mapper: RefMapper)

Source from the content-addressed store, hash-verified

38};
39
40export const mapRefs = (component: MitosisComponent, mapper: RefMapper): void => {
41 const refSet = getRefs(component);
42
43 // grab refs not used for bindings
44 Object.keys(component.refs).forEach((ref) => refSet.add(ref));
45 const refs = Array.from(refSet);
46
47 for (const key of Object.keys(component.state)) {
48 const stateVal = component.state[key];
49
50 if (typeof stateVal?.code === 'string') {
51 const value = stateVal.code;
52 switch (stateVal.type) {
53 case 'method':
54 case 'getter':
55 const isGet = stateVal.type === 'getter';
56 const isSet = Boolean(value.match(SETTER));
57 component.state[key] = {
58 ...stateVal,
59 code: replaceRefsInString(
60 value.replace(/^(get |set )?/, 'function '),
61 refs,
62 mapper,
63 ).replace(/^function /, isGet ? 'get ' : isSet ? 'set ' : ''),
64 };
65 break;
66 case 'function':
67 component.state[key] = {
68 ...stateVal,
69 code: replaceRefsInString(value, refs, mapper),
70 type: 'function',
71 };
72 break;
73 default:
74 break;
75 }
76 }
77 }
78
79 traverse(component).forEach(function (item) {
80 if (isMitosisNode(item)) {
81 for (const key of Object.keys(item.bindings)) {
82 const value = item.bindings[key];
83 if (typeof value === 'object' && key !== 'ref') {
84 item.bindings[key] = {
85 ...value,
86 code: replaceRefsInString(value.code as string, refs, mapper, 'bindings'),
87 };
88 }
89 }
90 }
91 });
92
93 for (const key of Object.keys(component.hooks) as (keyof typeof component.hooks)[]) {
94 const _hook = component.hooks[key];
95
96 const hooks = Array.isArray(_hook) ? _hook : [_hook];
97 for (const hook of hooks) {

Callers 8

componentToMitosisFunction · 0.90
_componentToReactFunction · 0.90
componentToMarkoFunction · 0.90
getDomRefsFunction · 0.90
componentToCustomElementFunction · 0.90
componentToStencilFunction · 0.90
componentToVueFunction · 0.90
componentToLitFunction · 0.90

Calls 4

getRefsFunction · 0.90
isMitosisNodeFunction · 0.90
replaceRefsInStringFunction · 0.85
traverseFunction · 0.85

Tested by

no test coverage detected