* Replaces $returnValue in the given expression with the `hoisted` variable, * returning the modified expression if it was found.
(expr: string, hoistedVar: string)
| 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 | } |
no test coverage detected