MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / collapse

Function collapse

packages/vite/hmr/server/websocket.ts:1441–1461  ·  view source on GitHub ↗
(code: string, prefix: string)

Source from the content-addressed store, hash-verified

1439 // Collapse duplicate destructuring from the same temp namespace var (e.g., multiple const { x } = __ns_rt_ns1)
1440 try {
1441 const collapse = (code: string, prefix: string) => {
1442 const re = new RegExp(`(^|\\n)\\s*const\\s*\\{([^}]+)\\}\\s*=\\s*(${prefix}\\d+)\\s*;?\\s*`, 'gm');
1443 const byVar: Record<string, Set<string>> = {};
1444 code.replace(re, (_full, _pfx, specList, varName) => {
1445 const set = (byVar[varName] ||= new Set());
1446 String(specList)
1447 .split(',')
1448 .map((s) => s.trim())
1449 .filter(Boolean)
1450 .forEach((seg) => set.add(seg));
1451 return '';
1452 });
1453 if (Object.keys(byVar).length) {
1454 // Remove all existing destructures first
1455 code = code.replace(re, (full, pfx) => pfx || '');
1456 // Re-emit one per var
1457 const blocks = Object.entries(byVar).map(([varName, set]) => `const { ${Array.from(set).join(', ')} } = ${varName};`);
1458 code = blocks.join('\n') + '\n' + code;
1459 }
1460 return code;
1461 };
1462 result = collapse(result, '__ns_rt_ns');
1463 result = collapse(result, '__ns_core_ns');
1464 } catch {}

Callers 1

processCodeForDeviceFunction · 0.85

Calls 8

replaceMethod · 0.80
forEachMethod · 0.80
joinMethod · 0.80
mapMethod · 0.65
addMethod · 0.65
keysMethod · 0.65
StringClass · 0.50
filterMethod · 0.45

Tested by

no test coverage detected