From ba7612b30e9190b65ba01484650fb4b89d32d520 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Sat, 5 Mar 2022 13:22:55 -0500 Subject: [PATCH] introduce dependency mechanism to recovery_codes to prevent erroneous suppression --- src/repl.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/repl.ts b/src/repl.ts index 6909f7c34..91976f903 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -671,17 +671,22 @@ function lineCount(value: string) { /** * TS diagnostic codes which are recoverable, meaning that the user likely entered and incomplete line of code * and should be prompted for the next. For example, starting a multi-line for() loop and not finishing it. + * null value means code is always recoverable. `Set` means code is only recoverable when occurring alongside at least one + * of the other codes. */ -const RECOVERY_CODES: Set = new Set([ - 1003, // "Identifier expected." - 1005, // "')' expected." - 1109, // "Expression expected." - 1126, // "Unexpected end of text." - 1160, // "Unterminated template literal." - 1161, // "Unterminated regular expression literal." - 2355, // "A function whose declared type is neither 'void' nor 'any' must return a value." - 2391, // "Function implementation is missing or not immediately following the declaration." - 7010, // "Function, which lacks return-type annotation, implicitly has an 'any' return type." +const RECOVERY_CODES: Map | null> = new Map([ + [1003, null], // "Identifier expected." + [1005, null], // "')' expected.", "'}' expected." + [1109, null], // "Expression expected." + [1126, null], // "Unexpected end of text." + [1160, null], // "Unterminated template literal." + [1161, null], // "Unterminated regular expression literal." + [2355, null], // "A function whose declared type is neither 'void' nor 'any' must return a value." + [2391, null], // "Function implementation is missing or not immediately following the declaration." + [ + 7010, // "Function, which lacks return-type annotation, implicitly has an 'any' return type." + new Set([1005]) // happens when fn signature spread across multiple lines: 'function a(\nb: any\n) {' + ] ]); /** @@ -700,7 +705,10 @@ const topLevelAwaitDiagnosticCodes = [ * Check if a function can recover gracefully. */ function isRecoverable(error: TSError) { - return error.diagnosticCodes.every((code) => RECOVERY_CODES.has(code)); + return error.diagnosticCodes.every((code) => { + const deps = RECOVERY_CODES.get(code); + return deps === null || (deps && error.diagnosticCodes.some(code => deps.has(code))); + }); } /**