(e, code)
| 72 | // Note: `e` (the original exception) is not used by the current implementation, |
| 73 | // but may be needed in the future. |
| 74 | function 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 | ); |
no test coverage detected
searching dependent graphs…