fix(ecmascript/lexer): Normalize \r\n and \r to \n in template literals (#1286)

swc_ecma_parser:
 - Normalize \r\n and \r to \n in template literals.

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
Liam Murphy 2020-12-21 19:56:17 +11:00 committed by GitHub
parent edf74fc1ec
commit 576fb6a532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 11 deletions

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_codegen"
repository = "https://github.com/swc-project/swc.git"
version = "0.41.4"
version = "0.41.5"
[dependencies]
bitflags = "1"

View File

@ -1 +1 @@
`\r\n \n`;
`\n \n`;

View File

@ -1 +1 @@
`\n\r\n\r`;
`\n\n\n`;

View File

@ -1 +1 @@
`\n\r\n`;
`\n\n`;

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.43.5"
version = "0.43.6"
[features]
default = []

View File

@ -1019,23 +1019,22 @@ impl<'a, I: Input> Lexer<'a, I> {
} else if c.is_line_break() {
self.state.had_line_break = true;
let c = if c == '\r' && self.peek() == Some('\n') {
raw.push_str("\\r\\n");
self.bump(); // '\r'
'\n'
} else {
match c {
'\n' => raw.push_str("\n"),
'\r' => raw.push_str("\r"),
'\u{2028}' => raw.push_str("\u{2028}"),
'\u{2029}' => raw.push_str("\u{2029}"),
'\n' => '\n',
'\r' => '\n',
'\u{2028}' => '\u{2028}',
'\u{2029}' => '\u{2029}',
_ => unreachable!(),
}
c
};
self.bump();
if let Some(ref mut cooked) = cooked {
cooked.push(c);
}
raw.push(c);
} else {
self.bump();
if let Some(ref mut cooked) = cooked {

View File

@ -1309,3 +1309,15 @@ fn issue_1272_2_js() {
assert_eq!(tokens.len(), 1);
assert_eq!(errors, vec![]);
}
#[test]
fn normalize_tpl_carriage_return() {
assert_eq!(
lex_tokens(Syntax::default(), "`\r\n`"),
lex_tokens(Syntax::default(), "`\n`")
);
assert_eq!(
lex_tokens(Syntax::default(), "`\r`"),
lex_tokens(Syntax::default(), "`\n`")
);
}