diff --git a/crates/swc_ecma_parser/src/lexer/jsx.rs b/crates/swc_ecma_parser/src/lexer/jsx.rs index 9f30e1d2daa..384a66d0e4f 100644 --- a/crates/swc_ecma_parser/src/lexer/jsx.rs +++ b/crates/swc_ecma_parser/src/lexer/jsx.rs @@ -148,7 +148,8 @@ impl<'a, I: Input> Lexer<'a, I> { Some(c) => c, None => { let start = self.state.start; - self.error(start, SyntaxError::UnterminatedStrLit)? + self.emit_error(start, SyntaxError::UnterminatedStrLit); + break; } }; @@ -184,7 +185,13 @@ impl<'a, I: Input> Lexer<'a, I> { } let cur_pos = self.input.cur_pos(); out.push_str(self.input.slice(chunk_start, cur_pos)); - self.input.bump(); + + // it might be at the end of the file when + // the string literal is unterminated + if self.input.peek_ahead().is_some() { + self.input.bump(); + } + Ok(Token::Str { value: out.into(), has_escape, diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 6b9cea705b3..1f0245b91f8 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -883,7 +883,7 @@ impl<'a, I: Input> Lexer<'a, I> { } has_escape = true } - c if c.is_line_break() => l.error(start, SyntaxError::UnterminatedStrLit)?, + c if c.is_line_break() => break, _ => { out.push(c); l.bump(); @@ -891,7 +891,11 @@ impl<'a, I: Input> Lexer<'a, I> { } } - l.error(start, SyntaxError::UnterminatedStrLit)? + l.emit_error(start, SyntaxError::UnterminatedStrLit); + return Ok(Token::Str { + value: (&**out).into(), + has_escape, + }); }) } diff --git a/crates/swc_ecma_parser/tests/jsx/errors/unterminated-string/input.js.stderr b/crates/swc_ecma_parser/tests/jsx/errors/unterminated-string/input.js.stderr index 74c709fb147..232832094e1 100644 --- a/crates/swc_ecma_parser/tests/jsx/errors/unterminated-string/input.js.stderr +++ b/crates/swc_ecma_parser/tests/jsx/errors/unterminated-string/input.js.stderr @@ -4,3 +4,9 @@ error: Unexpected eof 1 | + --> $DIR/tests/test262-parser/fail/0ffb1c3ecf85660e.js:2:6 + | +2 | World" + | ^ + | +note: This is the expression part of an expression statement + --> $DIR/tests/test262-parser/fail/0ffb1c3ecf85660e.js:2:1 + | +2 | World" + | ^^^^^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/0ffb1c3ecf85660e.js:1:1 | 1 | "Hello | ^^^^^^ +error: Unterminated string constant + --> $DIR/tests/test262-parser/fail/0ffb1c3ecf85660e.js:2:6 + | +2 | World" + | ^ + diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/5427bdf48f3eb6d9.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/5427bdf48f3eb6d9.js.stderr index cc279e01a18..f2fb7103b31 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/5427bdf48f3eb6d9.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/5427bdf48f3eb6d9.js.stderr @@ -1,3 +1,9 @@ +error: Invalid unicode escape + --> $DIR/tests/test262-parser/fail/5427bdf48f3eb6d9.js:1:3 + | +1 | ('\u{2028') + | ^^^^^^^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/5427bdf48f3eb6d9.js:1:10 | diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/575367951ac8635d.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/575367951ac8635d.js.stderr index 4c2872a2c41..c8ef3c3259f 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/575367951ac8635d.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/575367951ac8635d.js.stderr @@ -1,3 +1,9 @@ +error: Expected 4 hex characters + --> $DIR/tests/test262-parser/fail/575367951ac8635d.js:1:3 + | +1 | ('\u') + | ^^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/575367951ac8635d.js:1:5 | diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/89036b2edb64c00c.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/89036b2edb64c00c.js.stderr index 28b2a2d5271..6fc64df3338 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/89036b2edb64c00c.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/89036b2edb64c00c.js.stderr @@ -1,3 +1,15 @@ +error: Expected ',', got 'string literal ())' + --> $DIR/tests/test262-parser/fail/89036b2edb64c00c.js:2:1 + | +2 | ') + | ^^ + +error: Unterminated string constant + --> $DIR/tests/test262-parser/fail/89036b2edb64c00c.js:1:2 + | +1 | (' + | ^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/89036b2edb64c00c.js:2:1 | diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/b3fc8ced7ce28c35.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/b3fc8ced7ce28c35.js.stderr index b0461874fbb..649b7c77206 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/b3fc8ced7ce28c35.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/b3fc8ced7ce28c35.js.stderr @@ -1,3 +1,9 @@ +error: Expected 2 hex characters + --> $DIR/tests/test262-parser/fail/b3fc8ced7ce28c35.js:1:3 + | +1 | ('\x') + | ^^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/b3fc8ced7ce28c35.js:1:5 | diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/b61406dafcaab4b7.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/b61406dafcaab4b7.js.stderr index 6f6298bc1f1..7c7afb2c680 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/b61406dafcaab4b7.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/b61406dafcaab4b7.js.stderr @@ -1,3 +1,9 @@ +error: Expected 2 hex characters + --> $DIR/tests/test262-parser/fail/b61406dafcaab4b7.js:1:3 + | +1 | ('\x0') + | ^^^ + error: Unterminated string constant --> $DIR/tests/test262-parser/fail/b61406dafcaab4b7.js:1:6 | diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/dc431bcf293513a0.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/dc431bcf293513a0.js.stderr index 8d912ad35ed..65108c70ac5 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/dc431bcf293513a0.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/dc431bcf293513a0.js.stderr @@ -4,3 +4,9 @@ error: Unexpected eof 1 | (') | ^ +error: Unterminated string constant + --> $DIR/tests/test262-parser/fail/dc431bcf293513a0.js:1:2 + | +1 | (') + | ^^ +