Skip to content

Commit ba7612b

Browse files
committed
introduce dependency mechanism to recovery_codes to prevent erroneous suppression
1 parent da07227 commit ba7612b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/repl.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -671,17 +671,22 @@ function lineCount(value: string) {
671671
/**
672672
* TS diagnostic codes which are recoverable, meaning that the user likely entered and incomplete line of code
673673
* and should be prompted for the next. For example, starting a multi-line for() loop and not finishing it.
674+
* null value means code is always recoverable. `Set` means code is only recoverable when occurring alongside at least one
675+
* of the other codes.
674676
*/
675-
const RECOVERY_CODES: Set<number> = new Set([
676-
1003, // "Identifier expected."
677-
1005, // "')' expected."
678-
1109, // "Expression expected."
679-
1126, // "Unexpected end of text."
680-
1160, // "Unterminated template literal."
681-
1161, // "Unterminated regular expression literal."
682-
2355, // "A function whose declared type is neither 'void' nor 'any' must return a value."
683-
2391, // "Function implementation is missing or not immediately following the declaration."
684-
7010, // "Function, which lacks return-type annotation, implicitly has an 'any' return type."
677+
const RECOVERY_CODES: Map<number, Set<number> | null> = new Map([
678+
[1003, null], // "Identifier expected."
679+
[1005, null], // "')' expected.", "'}' expected."
680+
[1109, null], // "Expression expected."
681+
[1126, null], // "Unexpected end of text."
682+
[1160, null], // "Unterminated template literal."
683+
[1161, null], // "Unterminated regular expression literal."
684+
[2355, null], // "A function whose declared type is neither 'void' nor 'any' must return a value."
685+
[2391, null], // "Function implementation is missing or not immediately following the declaration."
686+
[
687+
7010, // "Function, which lacks return-type annotation, implicitly has an 'any' return type."
688+
new Set([1005]) // happens when fn signature spread across multiple lines: 'function a(\nb: any\n) {'
689+
]
685690
]);
686691

687692
/**
@@ -700,7 +705,10 @@ const topLevelAwaitDiagnosticCodes = [
700705
* Check if a function can recover gracefully.
701706
*/
702707
function isRecoverable(error: TSError) {
703-
return error.diagnosticCodes.every((code) => RECOVERY_CODES.has(code));
708+
return error.diagnosticCodes.every((code) => {
709+
const deps = RECOVERY_CODES.get(code);
710+
return deps === null || (deps && error.diagnosticCodes.some(code => deps.has(code)));
711+
});
704712
}
705713

706714
/**

0 commit comments

Comments
 (0)