Skip to content

Commit d03ae7a

Browse files
Add strict mode and template literal error handling for unrecognized escape sequences
This commit addresses the PR review feedback to properly handle unrecognized escape sequences according to ECMAScript specification: - In template literals: unrecognized escape sequences are always syntax errors - In strict mode: unrecognized escape sequences are syntax errors - In non-strict mode: unrecognized escape sequences preserve the backslash The implementation follows the same pattern used for octal escape sequences, ensuring consistent error handling behavior across the lexer. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 7d83958 commit d03ae7a

File tree

1 file changed

+11
-0
lines changed
  • crates/swc_ecma_lexer/src/common/lexer

1 file changed

+11
-0
lines changed

crates/swc_ecma_lexer/src/common/lexer/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,18 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
15631563
// the following character. According to ECMAScript, when a backslash precedes
15641564
// a character that doesn't form a valid escape sequence, both the backslash
15651565
// and the character should be preserved in the string value.
1566+
//
1567+
// However, in strict mode, unrecognized escape sequences are syntax errors.
1568+
// In template literals, they should always be errors (pre-ES2018 behavior).
15661569
_ => {
1570+
// In template literals, unrecognized escape sequences are always errors
1571+
if in_template {
1572+
self.error(start, SyntaxError::InvalidStrEscape)?
1573+
}
1574+
1575+
// In strict mode, unrecognized escape sequences are syntax errors
1576+
self.emit_strict_mode_error(start, SyntaxError::InvalidStrEscape);
1577+
15671578
// Don't bump - let the following character be read normally in the next
15681579
// iteration
15691580
return Ok(Some(CodePoint::from_char('\\')));

0 commit comments

Comments
 (0)