From dfd13cf950e64e98dac869a51d62416f0b99efd6 Mon Sep 17 00:00:00 2001 From: CodeMan62 Date: Sun, 6 Jul 2025 17:49:49 +0530 Subject: [PATCH] replace from take_leading --- crates/jsdoc/tests/fixture.rs | 10 ++--- crates/swc_common/src/comments.rs | 42 +++++++------------- crates/swc_ecma_codegen/src/comments.rs | 25 ++++++------ crates/swc_estree_compat/src/babelify/mod.rs | 8 ++-- crates/swc_node_comments/src/lib.rs | 10 ++--- 5 files changed, 39 insertions(+), 56 deletions(-) diff --git a/crates/jsdoc/tests/fixture.rs b/crates/jsdoc/tests/fixture.rs index db42cec14881..d8624ff9488d 100644 --- a/crates/jsdoc/tests/fixture.rs +++ b/crates/jsdoc/tests/fixture.rs @@ -89,19 +89,17 @@ impl Comments for SwcComments { } fn move_leading(&self, from: BytePos, to: BytePos) { - let cmt = self.take_leading(from); + let mut cmt = self.take_leading(from); - if let Some(mut cmt) = cmt { if from < to && self.has_leading(to) { - cmt.extend(self.take_leading(to).unwrap()); + cmt.extend(self.take_leading(to)); } self.add_leading_comments(to, cmt); - } } - fn take_leading(&self, pos: BytePos) -> Option> { - self.leading.remove(&pos).map(|v| v.1) + fn take_leading(&self, pos: BytePos) -> Vec { + self.leading.remove(&pos).map(|v| v.1).expect("Comment should exist") } fn get_leading(&self, pos: BytePos) -> Option> { diff --git a/crates/swc_common/src/comments.rs b/crates/swc_common/src/comments.rs index 762bfa633c01..062de5fc66e1 100644 --- a/crates/swc_common/src/comments.rs +++ b/crates/swc_common/src/comments.rs @@ -33,7 +33,7 @@ pub trait Comments { fn add_leading_comments(&self, pos: BytePos, comments: Vec); fn has_leading(&self, pos: BytePos) -> bool; fn move_leading(&self, from: BytePos, to: BytePos); - fn take_leading(&self, pos: BytePos) -> Option>; + fn take_leading(&self, pos: BytePos) -> Vec; fn get_leading(&self, pos: BytePos) -> Option>; fn add_trailing(&self, pos: BytePos, cmt: Comment); @@ -50,17 +50,11 @@ pub trait Comments { Self: Sized, F: FnOnce(&[Comment]) -> Ret, { - let cmts = self.take_leading(pos); + let mut cmts = self.take_leading(pos); - let ret = if let Some(cmts) = &cmts { - f(cmts) - } else { - f(&[]) - }; + let ret = f(&cmts); - if let Some(cmts) = cmts { self.add_leading_comments(pos, cmts); - } ret } @@ -90,11 +84,11 @@ pub trait Comments { /// If `flag` is `PURE`, this method will look for `@__PURE__` and /// `#__PURE__`. fn has_flag(&self, lo: BytePos, flag: &str) -> bool { - let cmts = self.take_leading(lo); + let mut cmts = self.take_leading(lo); - let ret = if let Some(comments) = &cmts { + let ret = { (|| { - for c in comments { + for c in &cmts { if c.kind == CommentKind::Block { for line in c.text.lines() { // jsdoc @@ -115,13 +109,9 @@ pub trait Comments { false })() - } else { - false }; - if let Some(cmts) = cmts { self.add_trailing_comments(lo, cmts); - } ret } @@ -145,7 +135,7 @@ macro_rules! delegate { (**self).move_leading(from, to) } - fn take_leading(&self, pos: BytePos) -> Option> { + fn take_leading(&self, pos: BytePos) -> Vec { (**self).take_leading(pos) } @@ -235,8 +225,8 @@ impl Comments for NoopComments { fn move_leading(&self, _: BytePos, _: BytePos) {} #[cfg_attr(not(debug_assertions), inline(always))] - fn take_leading(&self, _: BytePos) -> Option> { - None + fn take_leading(&self, _: BytePos) -> Vec { + Vec::new() } #[cfg_attr(not(debug_assertions), inline(always))] @@ -308,11 +298,11 @@ where } } - fn take_leading(&self, pos: BytePos) -> Option> { + fn take_leading(&self, pos: BytePos) -> Vec { if let Some(c) = self { c.take_leading(pos) } else { - None + Vec::new() } } @@ -440,19 +430,17 @@ impl Comments for SingleThreadedComments { } fn move_leading(&self, from: BytePos, to: BytePos) { - let cmt = self.take_leading(from); + let mut cmt = self.take_leading(from); - if let Some(mut cmt) = cmt { if from < to && self.has_leading(to) { - cmt.extend(self.take_leading(to).unwrap()); + cmt.extend(self.take_leading(to)); } self.add_leading_comments(to, cmt); - } } - fn take_leading(&self, pos: BytePos) -> Option> { - self.leading.borrow_mut().remove(&pos) + fn take_leading(&self, pos: BytePos) -> Vec { + self.leading.borrow_mut().remove(&pos).expect("take_leading should not be called with a position that has no comments") } fn get_leading(&self, pos: BytePos) -> Option> { diff --git a/crates/swc_ecma_codegen/src/comments.rs b/crates/swc_ecma_codegen/src/comments.rs index 002bbc25e7bf..fc8fe1848e58 100644 --- a/crates/swc_ecma_codegen/src/comments.rs +++ b/crates/swc_ecma_codegen/src/comments.rs @@ -5,10 +5,11 @@ use super::*; macro_rules! write_comments { ($e:expr, $prefix_space:expr, $cmts:expr) => {{ - let cmts = match $cmts { - Some(v) => v, - None => return Ok(()), - }; + let cmts = $cmts; + + if cmts.is_empty() { + return Ok(()); + } for cmt in cmts.iter() { match cmt.kind { @@ -68,7 +69,7 @@ where ) -> Result { let cmts = self.take_trailing_comments_of_pos(pos); - write_comments!(self, prefix_space, &cmts) + write_comments!(self, prefix_space, cmts) } pub(super) fn emit_trailing_comments_of_pos_with( @@ -81,20 +82,20 @@ where callback(self)?; - write_comments!(self, prefix_space, &cmts) + write_comments!(self, prefix_space, cmts) } - fn take_trailing_comments_of_pos(&mut self, pos: BytePos) -> Option> { + fn take_trailing_comments_of_pos(&mut self, pos: BytePos) -> Vec { if pos.is_dummy() { - return None; + return Vec::new(); } let comments = match self.comments { Some(ref comments) => comments, - None => return None, + None => return Vec::new(), }; - comments.take_trailing(pos) + comments.take_trailing(pos).unwrap_or_default() } pub(super) fn emit_leading_comments(&mut self, mut pos: BytePos, is_hi: bool) -> Result { @@ -106,11 +107,11 @@ where write_comments!( self, false, - Some(vec![Comment { + vec![Comment { kind: CommentKind::Block, span: DUMMY_SP, text: atom!("#__PURE__"), - }]) + }] ); } diff --git a/crates/swc_estree_compat/src/babelify/mod.rs b/crates/swc_estree_compat/src/babelify/mod.rs index c234c368bd0c..d0263958bc1e 100644 --- a/crates/swc_estree_compat/src/babelify/mod.rs +++ b/crates/swc_estree_compat/src/babelify/mod.rs @@ -95,11 +95,9 @@ impl Context { /// Note that we removes comment from the comment map because `.babelify` /// takes `self`. (not reference) fn base(&self, span: Span) -> BaseNode { - let leading_comments = self - .comments - .take_leading(span.lo) - .map(|v| self.convert_comments(v)) - .unwrap_or_default(); + let leading_comments = self.convert_comments( + self.comments.take_leading(span.lo) + ); let trailing_comments = self .comments .take_trailing(span.hi) diff --git a/crates/swc_node_comments/src/lib.rs b/crates/swc_node_comments/src/lib.rs index 417f00bf161a..9c2f0cad40a7 100644 --- a/crates/swc_node_comments/src/lib.rs +++ b/crates/swc_node_comments/src/lib.rs @@ -37,19 +37,17 @@ impl Comments for SwcComments { } fn move_leading(&self, from: BytePos, to: BytePos) { - let cmt = self.take_leading(from); + let mut cmt = self.take_leading(from); - if let Some(mut cmt) = cmt { if from < to && self.has_leading(to) { - cmt.extend(self.take_leading(to).unwrap()); + cmt.extend(self.take_leading(to)); } self.add_leading_comments(to, cmt); - } } - fn take_leading(&self, pos: BytePos) -> Option> { - self.leading.remove(&pos).map(|v| v.1) + fn take_leading(&self, pos: BytePos) -> Vec { + self.leading.remove(&pos).map(|v| v.1).expect("Comment should exist") } fn get_leading(&self, pos: BytePos) -> Option> {