Skip to content

Commit 32e23ed

Browse files
authoredJun 14, 2024··
refactor(es/parser): Remove unused raw: Raw params (#9048)
1 parent e560198 commit 32e23ed

File tree

2 files changed

+12
-54
lines changed

2 files changed

+12
-54
lines changed
 

‎crates/swc_ecma_parser/src/lexer/mod.rs

+12-47
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,7 @@ impl<'a> Lexer<'a> {
429429
/// Read an escaped character for string literal.
430430
///
431431
/// In template literal, we should preserve raw string.
432-
fn read_escaped_char(
433-
&mut self,
434-
raw: &mut Raw,
435-
in_template: bool,
436-
) -> LexResult<Option<Vec<Char>>> {
432+
fn read_escaped_char(&mut self, in_template: bool) -> LexResult<Option<Vec<Char>>> {
437433
debug_assert_eq!(self.cur(), Some('\\'));
438434

439435
let start = self.cur_pos();
@@ -447,7 +443,6 @@ impl<'a> Lexer<'a> {
447443

448444
macro_rules! push_c_and_ret {
449445
($c:expr) => {{
450-
raw.push(c);
451446
$c
452447
}};
453448
}
@@ -461,35 +456,23 @@ impl<'a> Lexer<'a> {
461456
'v' => push_c_and_ret!('\u{000b}'),
462457
'f' => push_c_and_ret!('\u{000c}'),
463458
'\r' => {
464-
raw.push_str("\r");
465-
466459
self.bump(); // remove '\r'
467460

468-
if self.eat(b'\n') {
469-
raw.push_str("\n");
470-
}
461+
self.eat(b'\n');
471462

472463
return Ok(None);
473464
}
474465
'\n' | '\u{2028}' | '\u{2029}' => {
475-
match c {
476-
'\n' => raw.push_str("\n"),
477-
'\u{2028}' => raw.push_str("\u{2028}"),
478-
'\u{2029}' => raw.push_str("\u{2029}"),
479-
_ => unreachable!(),
480-
}
481466
self.bump();
482467

483468
return Ok(None);
484469
}
485470

486471
// read hexadecimal escape sequences
487472
'x' => {
488-
raw.push_str("x");
489-
490473
self.bump(); // 'x'
491474

492-
match self.read_int_u32::<16>(2, raw)? {
475+
match self.read_int_u32::<16>(2, &mut Raw(None))? {
493476
Some(val) => return Ok(Some(vec![Char::from(val)])),
494477
None => self.error(
495478
start,
@@ -501,15 +484,13 @@ impl<'a> Lexer<'a> {
501484
}
502485

503486
// read unicode escape sequences
504-
'u' => match self.read_unicode_escape(raw) {
487+
'u' => match self.read_unicode_escape() {
505488
Ok(chars) => return Ok(Some(chars)),
506489
Err(err) => self.error(start, err.into_kind())?,
507490
},
508491

509492
// octal escape sequences
510493
'0'..='7' => {
511-
raw.push(c);
512-
513494
self.bump();
514495

515496
let first_c = if c == '0' {
@@ -550,7 +531,6 @@ impl<'a> Lexer<'a> {
550531
};
551532

552533
self.bump();
553-
raw.push(cur.unwrap());
554534
}
555535
_ => return Ok(Some(vec![Char::from(value as u32)])),
556536
}
@@ -562,10 +542,7 @@ impl<'a> Lexer<'a> {
562542

563543
return Ok(Some(vec![Char::from(value as char)]));
564544
}
565-
_ => {
566-
raw.push(c);
567-
c
568-
}
545+
_ => c,
569546
};
570547

571548
unsafe {
@@ -860,7 +837,7 @@ impl<'a> Lexer<'a> {
860837

861838
has_escape = true;
862839

863-
let chars = l.read_unicode_escape(&mut Raw(None))?;
840+
let chars = l.read_unicode_escape()?;
864841

865842
if let Some(c) = chars.first() {
866843
let valid = if first {
@@ -890,24 +867,20 @@ impl<'a> Lexer<'a> {
890867
})
891868
}
892869

893-
fn read_unicode_escape(&mut self, raw: &mut Raw) -> LexResult<Vec<Char>> {
870+
fn read_unicode_escape(&mut self) -> LexResult<Vec<Char>> {
894871
debug_assert_eq!(self.cur(), Some('u'));
895872

896873
let mut chars = vec![];
897874
let mut is_curly = false;
898875

899876
self.bump(); // 'u'
900877

901-
raw.push_str("u");
902-
903878
if self.eat(b'{') {
904879
is_curly = true;
905-
906-
raw.push('{');
907880
}
908881

909882
let state = self.input.cur_pos();
910-
let c = match self.read_int_u32::<16>(if is_curly { 0 } else { 4 }, raw) {
883+
let c = match self.read_int_u32::<16>(if is_curly { 0 } else { 4 }, &mut Raw(None)) {
911884
Ok(Some(val)) => {
912885
if 0x0010_ffff >= val {
913886
char::from_u32(val)
@@ -985,12 +958,8 @@ impl<'a> Lexer<'a> {
985958
}
986959
}
987960

988-
if is_curly {
989-
if !self.eat(b'}') {
990-
self.error(state, SyntaxError::InvalidUnicodeEscape)?
991-
}
992-
993-
raw.push('}');
961+
if is_curly && !self.eat(b'}') {
962+
self.error(state, SyntaxError::InvalidUnicodeEscape)?
994963
}
995964

996965
Ok(chars)
@@ -1033,9 +1002,7 @@ impl<'a> Lexer<'a> {
10331002
});
10341003
}
10351004
'\\' => {
1036-
let mut wrapped = Raw(Some(Default::default()));
1037-
1038-
if let Some(chars) = l.read_escaped_char(&mut wrapped, false)? {
1005+
if let Some(chars) = l.read_escaped_char(false)? {
10391006
for c in chars {
10401007
out.extend(c);
10411008
}
@@ -1208,9 +1175,7 @@ impl<'a> Lexer<'a> {
12081175
if c == '\\' {
12091176
consume_cooked!();
12101177

1211-
let mut wrapped = Raw(None);
1212-
1213-
match self.read_escaped_char(&mut wrapped, true) {
1178+
match self.read_escaped_char(true) {
12141179
Ok(Some(chars)) => {
12151180
if let Ok(ref mut cooked) = cooked {
12161181
for c in chars {

‎crates/swc_ecma_parser/src/lexer/util.rs

-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ use crate::{
2828
pub(super) struct Raw(pub Option<SmartString<LazyCompact>>);
2929

3030
impl Raw {
31-
#[inline]
32-
pub fn push_str(&mut self, s: &str) {
33-
if let Some(ref mut st) = self.0 {
34-
st.push_str(s)
35-
}
36-
}
37-
3831
#[inline]
3932
pub fn push(&mut self, c: char) {
4033
if let Some(ref mut st) = self.0 {

0 commit comments

Comments
 (0)
Please sign in to comment.