MCPcopy Create free account
hub / github.com/Snapchat/Valdi / replaceReturnValue

Method replaceReturnValue

valdi/vscode_debugger/src/adapter/evaluator.ts:165–190  ·  view source on GitHub ↗

* Replaces $returnValue in the given expression with the `hoisted` variable, * returning the modified expression if it was found.

(expr: string, hoistedVar: string)

Source from the content-addressed store, hash-verified

163 * returning the modified expression if it was found.
164 */
165 private replaceReturnValue(expr: string, hoistedVar: string): string | undefined {
166 const sourceFile = ts.createSourceFile(
167 'test.js',
168 expr,
169 ts.ScriptTarget.ESNext,
170 /*setParentNodes */ true,
171 );
172
173 let adjust = 0;
174 let found = false;
175
176 const replacement = `(typeof ${hoistedVar} !== 'undefined' ? ${hoistedVar} : undefined)`;
177 const replace = (node: ts.Node) => {
178 if (ts.isIdentifier(node) && node.text === returnValueStr) {
179 expr = expr.slice(0, node.getStart() + adjust) + replacement + expr.slice(node.getEnd());
180 adjust += node.getEnd() - node.getStart() - replacement.length;
181 found = true;
182 }
183
184 ts.forEachChild(node, replace);
185 };
186
187 replace(sourceFile);
188
189 return found ? expr : undefined;
190 }
191}

Callers 1

prepareMethod · 0.95

Calls 2

replaceFunction · 0.85
createSourceFileMethod · 0.45

Tested by

no test coverage detected