mirror of
https://github.com/got-feedBack/feedBack.git
synced 2026-07-22 12:52:29 +00:00
37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
// Shared test helpers for the brace-matching app.js extraction strategy used
|
|
// by song_restart / song_close (and friends): pull a single top-level function
|
|
// out of app.js by name so it can run in an isolated vm sandbox with stubbed
|
|
// deps, without executing the whole module.
|
|
|
|
function extractFunction(src, signature) {
|
|
const start = src.indexOf(signature);
|
|
if (start === -1) throw new Error(`extractFunction: '${signature}' not found`);
|
|
let scan = start + signature.length;
|
|
if (src[scan] === '(') {
|
|
let parenDepth = 1;
|
|
scan++;
|
|
while (scan < src.length && parenDepth > 0) {
|
|
const ch = src[scan];
|
|
if (ch === '(') parenDepth++;
|
|
else if (ch === ')') parenDepth--;
|
|
scan++;
|
|
}
|
|
}
|
|
const openBrace = src.indexOf('{', scan);
|
|
if (openBrace === -1) throw new Error(`extractFunction: no '{' after '${signature}'`);
|
|
let depth = 1;
|
|
let i = openBrace + 1;
|
|
while (i < src.length && depth > 0) {
|
|
const ch = src[i];
|
|
if (ch === '{') depth++;
|
|
else if (ch === '}') depth--;
|
|
i++;
|
|
}
|
|
if (depth !== 0) throw new Error(`extractFunction: unbalanced braces after '${signature}'`);
|
|
return src.slice(start, i);
|
|
}
|
|
|
|
module.exports = { extractFunction };
|