diff options
Diffstat (limited to 'js/repl.ts')
-rw-r--r-- | js/repl.ts | 84 |
1 files changed, 42 insertions, 42 deletions
diff --git a/js/repl.ts b/js/repl.ts index 2c050bf5b..c39a79a7d 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -68,6 +68,47 @@ export async function readline(rid: number, prompt: string): Promise<string> { return line || ""; } +// Error messages that allow users to continue input +// instead of throwing an error to REPL +// ref: https://github.com/v8/v8/blob/master/src/message-template.h +// TODO(kevinkassimo): this list might not be comprehensive +const recoverableErrorMessages = [ + "Unexpected end of input", // { or [ or ( + "Missing initializer in const declaration", // const a + "Missing catch or finally after try", // try {} + "missing ) after argument list", // console.log(1 + "Unterminated template literal" // `template + // TODO(kevinkassimo): need a parser to handling errors such as: + // "Missing } in template expression" // `${ or `${ a 123 }` +]; + +function isRecoverableError(e: Error): boolean { + return recoverableErrorMessages.includes(e.message); +} + +// Evaluate code. +// Returns true if code is consumed (no error/irrecoverable error). +// Returns false if error is recoverable +function evaluate(code: string): boolean { + const [result, errInfo] = libdeno.evalContext(code); + if (!errInfo) { + console.log(result); + } else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) { + // Recoverable compiler error + return false; // don't consume code. + } else { + if (errInfo.isNativeError) { + const formattedError = formatError( + libdeno.errorToJSON(errInfo.thrown as Error) + ); + console.error(formattedError); + } else { + console.error("Thrown:", errInfo.thrown); + } + } + return true; +} + // @internal export async function replLoop(): Promise<void> { Object.defineProperties(window, replCommands); @@ -75,7 +116,7 @@ export async function replLoop(): Promise<void> { const historyFile = "deno_history.txt"; const rid = startRepl(historyFile); - const quitRepl = (exitCode: number) => { + const quitRepl = (exitCode: number): void => { // Special handling in case user calls deno.close(3). try { close(rid); // close signals Drop on REPL and saves history. @@ -129,44 +170,3 @@ export async function replLoop(): Promise<void> { } } } - -// Evaluate code. -// Returns true if code is consumed (no error/irrecoverable error). -// Returns false if error is recoverable -function evaluate(code: string): boolean { - const [result, errInfo] = libdeno.evalContext(code); - if (!errInfo) { - console.log(result); - } else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) { - // Recoverable compiler error - return false; // don't consume code. - } else { - if (errInfo.isNativeError) { - const formattedError = formatError( - libdeno.errorToJSON(errInfo.thrown as Error) - ); - console.error(formattedError); - } else { - console.error("Thrown:", errInfo.thrown); - } - } - return true; -} - -// Error messages that allow users to continue input -// instead of throwing an error to REPL -// ref: https://github.com/v8/v8/blob/master/src/message-template.h -// TODO(kevinkassimo): this list might not be comprehensive -const recoverableErrorMessages = [ - "Unexpected end of input", // { or [ or ( - "Missing initializer in const declaration", // const a - "Missing catch or finally after try", // try {} - "missing ) after argument list", // console.log(1 - "Unterminated template literal" // `template - // TODO(kevinkassimo): need a parser to handling errors such as: - // "Missing } in template expression" // `${ or `${ a 123 }` -]; - -function isRecoverableError(e: Error): boolean { - return recoverableErrorMessages.includes(e.message); -} |