MCPcopy Index your code
hub / github.com/nodejs/node / isRecoverableError

Function isRecoverableError

lib/internal/repl/utils.js:74–145  ·  view source on GitHub ↗
(e, code)

Source from the content-addressed store, hash-verified

72// Note: `e` (the original exception) is not used by the current implementation,
73// but may be needed in the future.
74function isRecoverableError(e, code) {
75 // For similar reasons as `defaultEval`, wrap expressions starting with a
76 // curly brace with parenthesis. Note: only the open parenthesis is added
77 // here as the point is to test for potentially valid but incomplete
78 // expressions.
79 if (RegExpPrototypeExec(/^\s*\{/, code) !== null &&
80 isRecoverableError(e, `(${code}`))
81 return true;
82
83 let recoverable = false;
84
85 // Determine if the point of any error raised is at the end of the input.
86 // There are two cases to consider:
87 //
88 // 1. Any error raised after we have encountered the 'eof' token.
89 // This prevents us from declaring partial tokens (like '2e') as
90 // recoverable.
91 //
92 // 2. Three cases where tokens can legally span lines. This is
93 // template, comment, and strings with a backslash at the end of
94 // the line, indicating a continuation. Note that we need to look
95 // for the specific errors of 'unterminated' kind (not, for example,
96 // a syntax error in a ${} expression in a template), and the only
97 // way to do that currently is to look at the message. Should Acorn
98 // change these messages in the future, this will lead to a test
99 // failure, indicating that this code needs to be updated.
100 //
101 const RecoverableParser = AcornParser
102 .extend(
103 (Parser) => {
104 return class extends Parser {
105 nextToken() {
106 super.nextToken();
107 if (this.type === tt.eof)
108 recoverable = true;
109 }
110 raise(pos, message) {
111 switch (message) {
112 case 'Unterminated template':
113 case 'Unterminated comment':
114 recoverable = true;
115 break;
116
117 case 'Unterminated string constant': {
118 const token = StringPrototypeSlice(this.input,
119 this.lastTokStart, this.pos);
120 // See https://www.ecma-international.org/ecma-262/#sec-line-terminators
121 if (RegExpPrototypeExec(/\\(?:\r\n?|\n|\u2028|\u2029)$/,
122 token) !== null) {
123 recoverable = true;
124 }
125 }
126 }
127 super.raise(pos, message);
128 }
129 };
130 },
131 );

Callers 1

defaultEvalMethod · 0.85

Calls 2

parseMethod · 0.65
extendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…