summaryrefslogtreecommitdiff
path: root/js/repl.ts
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2019-03-10 04:30:38 +1100
committerRyan Dahl <ry@tinyclouds.org>2019-03-09 12:30:38 -0500
commit034e2cc02829c9244b32232074c7a48af827a2fb (patch)
treebade01606a1ee076c1f753ce99c97ddb1e4edf30 /js/repl.ts
parent8c7a12d1b258f0ef5ab27f49c424331d43e8d97f (diff)
Migrate from tslint to eslint for linting (#1905)
Diffstat (limited to 'js/repl.ts')
-rw-r--r--js/repl.ts84
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);
-}