From a6c64e600302bc58856ed93419fe90517c3efd22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 11 Jun 2024 20:41:14 +0900 Subject: [PATCH 1/3] almost --- crates/swc_ecma_parser/src/lexer/mod.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 61eb37df48d1..20239feb7b82 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -1160,6 +1160,7 @@ impl<'a> Lexer<'a> { let mut cooked = Ok(String::new()); let mut raw = SmartString::new(); + let mut raw_slice_start = start; while let Some(c) = self.cur() { if c == '`' || (c == '$' && self.peek() == Some('{')) { @@ -1174,6 +1175,13 @@ impl<'a> Lexer<'a> { } } + let last_pos = self.cur_pos(); + raw.push_str(unsafe { + // Safety: Both of start and last_pos are valid position because we got them + // from `self.input` + self.input.slice(raw_slice_start, last_pos) + }); + // TODO: Handle error return Ok(Token::Template { cooked: cooked.map(Atom::from), @@ -1184,6 +1192,13 @@ impl<'a> Lexer<'a> { if c == '\\' { raw.push('\\'); + let last_pos = self.cur_pos(); + raw.push_str(unsafe { + // Safety: Both of start and last_pos are valid position because we got them + // from `self.input` + self.input.slice(raw_slice_start, last_pos) + }); + let mut wrapped = Raw(Some(raw)); match self.read_escaped_char(&mut wrapped, true) { @@ -1201,11 +1216,12 @@ impl<'a> Lexer<'a> { } raw = wrapped.0.unwrap(); + raw_slice_start = self.cur_pos(); } else if c.is_line_terminator() { self.state.had_line_break = true; let c = if c == '\r' && self.peek() == Some('\n') { - raw.push('\r'); + // raw.push('\r'); self.bump(); // '\r' '\n' } else { @@ -1223,16 +1239,12 @@ impl<'a> Lexer<'a> { if let Ok(ref mut cooked) = cooked { cooked.push(c); } - - raw.push(c); } else { self.bump(); if let Ok(ref mut cooked) = cooked { cooked.push(c); } - - raw.push(c); } } From 7e52db7f11674e1df4aab36ed82b1e7076037965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 11 Jun 2024 20:42:21 +0900 Subject: [PATCH 2/3] done --- crates/swc_ecma_parser/src/lexer/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 20239feb7b82..394e75613828 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -1190,8 +1190,6 @@ impl<'a> Lexer<'a> { } if c == '\\' { - raw.push('\\'); - let last_pos = self.cur_pos(); raw.push_str(unsafe { // Safety: Both of start and last_pos are valid position because we got them @@ -1199,6 +1197,7 @@ impl<'a> Lexer<'a> { self.input.slice(raw_slice_start, last_pos) }); + raw.push('\\'); let mut wrapped = Raw(Some(raw)); match self.read_escaped_char(&mut wrapped, true) { From 5ac0b83f26617ad3f15c8772335484e463375aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 11 Jun 2024 20:54:56 +0900 Subject: [PATCH 3/3] fix --- crates/swc_ecma_parser/src/lexer/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 394e75613828..509084c1c740 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -1219,8 +1219,17 @@ impl<'a> Lexer<'a> { } else if c.is_line_terminator() { self.state.had_line_break = true; + { + let last_pos = self.cur_pos(); + raw.push_str(unsafe { + // Safety: Both of start and last_pos are valid position because we got them + // from `self.input` + self.input.slice(raw_slice_start, last_pos) + }); + } + let c = if c == '\r' && self.peek() == Some('\n') { - // raw.push('\r'); + raw.push('\r'); self.bump(); // '\r' '\n' } else { @@ -1238,6 +1247,8 @@ impl<'a> Lexer<'a> { if let Ok(ref mut cooked) = cooked { cooked.push(c); } + raw.push(c); + raw_slice_start = self.cur_pos(); } else { self.bump();