-
-
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 3 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 |
|---|---|---|
|
|
@@ -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 default case for unrecognized escape sequences should emit a strict mode error and handle template literals correctly. In strict mode, unrecognized escape sequences are syntax errors. In template literals (pre-ES2018), they should always be errors. The implementation should follow the pattern used for octal escapes (lines 1524-1528). Consider adding:\n
rust\n_ => {\n if in_template {\n self.error(start, SyntaxError::InvalidStrEscape)?\n }\n \n self.emit_strict_mode_error(start, SyntaxError::InvalidStrEscape);\n \n return Ok(Some(CodePoint::from_char('\\\\')));\n}\n