-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(lexer): Handle unrecognized escape sequences correctly #11217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1462,6 +1462,8 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized { | |||||||||||||
|
|
||||||||||||||
| let c = match c { | ||||||||||||||
| '\\' => '\\', | ||||||||||||||
| '\'' => '\'', | ||||||||||||||
| '"' => '"', | ||||||||||||||
| 'n' => '\n', | ||||||||||||||
| 'r' => '\r', | ||||||||||||||
| 't' => '\t', | ||||||||||||||
|
|
@@ -1557,7 +1559,26 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized { | |||||||||||||
|
|
||||||||||||||
| return Ok(CodePoint::from_u32(value as u32)); | ||||||||||||||
| } | ||||||||||||||
| _ => c, | ||||||||||||||
| // For unrecognized escape sequences, return the backslash and don't consume | ||||||||||||||
| // the following character. According to ECMAScript, when a backslash precedes | ||||||||||||||
| // a character that doesn't form a valid escape sequence, both the backslash | ||||||||||||||
| // and the character should be preserved in the string value. | ||||||||||||||
| // | ||||||||||||||
| // However, in strict mode, unrecognized escape sequences are syntax errors. | ||||||||||||||
| // In template literals, they should always be errors (pre-ES2018 behavior). | ||||||||||||||
| _ => { | ||||||||||||||
| // In template literals, unrecognized escape sequences are always errors | ||||||||||||||
| if in_template { | ||||||||||||||
| self.error(start, SyntaxError::InvalidStrEscape)? | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // In strict mode, unrecognized escape sequences are syntax errors | ||||||||||||||
| self.emit_strict_mode_error(start, SyntaxError::InvalidStrEscape); | ||||||||||||||
|
|
||||||||||||||
| // Don't bump - let the following character be read normally in the next | ||||||||||||||
| // iteration | ||||||||||||||
|
||||||||||||||
| // iteration | |
| // iteration | |
| if in_template { | |
| self.error(start, SyntaxError::InvalidStrEscape)?; | |
| } | |
| self.emit_strict_mode_error(start, SyntaxError::InvalidStrEscape); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2241,3 +2241,51 @@ fn issue_9106() { | |
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn issue_11214_windows_path_escape() { | ||
| // Test for Windows file paths with backslashes | ||
| // When a backslash precedes a character that doesn't form a valid escape | ||
| // sequence, the backslash should be treated as a literal backslash | ||
| assert_eq!( | ||
| lex_tokens( | ||
| Syntax::default(), | ||
| r#""C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts""# | ||
| ), | ||
| vec![Token::Str { | ||
| value: atom!("C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts") | ||
| .into(), | ||
| raw: atom!(r#""C:\\github\\swc-plugin-coverage-instrument\\spec\\util\\verifier.ts""#), | ||
| }] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn issue_11214_unrecognized_escape_sequences() { | ||
| // Test various unrecognized escape sequences | ||
| // According to ECMAScript, \s, \g, \a etc. (when not part of a valid escape) | ||
| // preserve the backslash: the value should be backslash + character | ||
| assert_eq!( | ||
| lex_tokens(Syntax::default(), r#""\s""#), | ||
| vec![Token::Str { | ||
| value: atom!(r"\s").into(), | ||
| raw: atom!(r#""\s""#), | ||
| }] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| lex_tokens(Syntax::default(), r#""\g""#), | ||
| vec![Token::Str { | ||
| value: atom!(r"\g").into(), | ||
| raw: atom!(r#""\g""#), | ||
| }] | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| lex_tokens(Syntax::default(), r#""\a""#), | ||
| vec![Token::Str { | ||
| value: atom!(r"\a").into(), | ||
| raw: atom!(r#""\a""#), | ||
| }] | ||
| ); | ||
|
Comment on lines
+2268
to
+2290
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new behavior for unrecognized escape sequences may not align with the ECMAScript specification. According to ES spec section 12.9.4, when a backslash precedes a character that forms a
NonEscapeCharacter(like\s,\g,\a), the escape sequence should produce just the character itself, not the backslash + character.For example,
"\s"should have a string value of"s"(length 1), not"\s"(length 2).This can be verified in Node.js:
The current implementation returns the backslash and preserves the following character, which would make
"\s"have a value of"\s"(length 2).Could you clarify if this behavior is intentional for a specific mode (e.g., TypeScript compatibility, source preservation), or if there's a misunderstanding of the ECMAScript specification?