MCPcopy
hub / github.com/TypeStrong/ts-node / nodeEval

Function nodeEval

src/repl.ts:231–317  ·  view source on GitHub ↗
(
    code: string,
    context: any,
    _filename: string,
    callback: (err: Error | null, result?: any) => any
  )

Source from the content-addressed store, hash-verified

229 }
230
231 function nodeEval(
232 code: string,
233 context: any,
234 _filename: string,
235 callback: (err: Error | null, result?: any) => any
236 ) {
237 // TODO: Figure out how to handle completion here.
238 if (code === '.scope') {
239 callback(null);
240 return;
241 }
242
243 try {
244 const evalResult = evalCodeInternal({
245 code,
246 enableTopLevelAwait: true,
247 context,
248 });
249
250 if (evalResult.containsTopLevelAwait) {
251 (async () => {
252 try {
253 callback(null, await evalResult.valuePromise);
254 } catch (promiseError) {
255 handleError(promiseError);
256 }
257 })();
258 } else {
259 callback(null, evalResult.value);
260 }
261 } catch (error) {
262 handleError(error);
263 }
264
265 // Log TSErrors, check if they're recoverable, log helpful hints for certain
266 // well-known errors, and invoke `callback()`
267 // TODO should evalCode API get the same error-handling benefits?
268 function handleError(error: unknown) {
269 // Don't show TLA hint if the user explicitly disabled repl top level await
270 const canLogTopLevelAwaitHint =
271 service!.options.experimentalReplAwait !== false &&
272 !service!.shouldReplAwait;
273 if (error instanceof TSError) {
274 // Support recoverable compilations using >= node 6.
275 if (Recoverable && isRecoverable(error)) {
276 callback(new Recoverable(error));
277 return;
278 } else {
279 _console.error(error);
280
281 if (
282 canLogTopLevelAwaitHint &&
283 error.diagnosticCodes.some((dC) =>
284 topLevelAwaitDiagnosticCodes.includes(dC)
285 )
286 ) {
287 _console.error(getTopLevelAwaitHint());
288 }

Callers

nothing calls this directly

Calls 2

evalCodeInternalFunction · 0.85
handleErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…