Skip to content

refactor(swc_common): replace Option<Vec<Comment>> with Vec<Comment> #10785

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions crates/jsdoc/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Comment>> {
self.leading.remove(&pos).map(|v| v.1)
fn take_leading(&self, pos: BytePos) -> Vec<Comment> {
self.leading.remove(&pos).map(|v| v.1).expect("Comment should exist")
}

fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
Expand Down
42 changes: 15 additions & 27 deletions crates/swc_common/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait Comments {
fn add_leading_comments(&self, pos: BytePos, comments: Vec<Comment>);
fn has_leading(&self, pos: BytePos) -> bool;
fn move_leading(&self, from: BytePos, to: BytePos);
fn take_leading(&self, pos: BytePos) -> Option<Vec<Comment>>;
fn take_leading(&self, pos: BytePos) -> Vec<Comment>;
fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>;

fn add_trailing(&self, pos: BytePos, cmt: Comment);
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -115,13 +109,9 @@ pub trait Comments {

false
})()
} else {
false
};

if let Some(cmts) = cmts {
self.add_trailing_comments(lo, cmts);
}

ret
}
Expand All @@ -145,7 +135,7 @@ macro_rules! delegate {
(**self).move_leading(from, to)
}

fn take_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
fn take_leading(&self, pos: BytePos) -> Vec<Comment> {
(**self).take_leading(pos)
}

Expand Down Expand Up @@ -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<Vec<Comment>> {
None
fn take_leading(&self, _: BytePos) -> Vec<Comment> {
Vec::new()
}

#[cfg_attr(not(debug_assertions), inline(always))]
Expand Down Expand Up @@ -308,11 +298,11 @@ where
}
}

fn take_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
fn take_leading(&self, pos: BytePos) -> Vec<Comment> {
if let Some(c) = self {
c.take_leading(pos)
} else {
None
Vec::new()
}
}

Expand Down Expand Up @@ -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<Vec<Comment>> {
self.leading.borrow_mut().remove(&pos)
fn take_leading(&self, pos: BytePos) -> Vec<Comment> {
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<Vec<Comment>> {
Expand Down
25 changes: 13 additions & 12 deletions crates/swc_ecma_codegen/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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<Vec<Comment>> {
fn take_trailing_comments_of_pos(&mut self, pos: BytePos) -> Vec<Comment> {
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 {
Expand All @@ -106,11 +107,11 @@ where
write_comments!(
self,
false,
Some(vec![Comment {
vec![Comment {
kind: CommentKind::Block,
span: DUMMY_SP,
text: atom!("#__PURE__"),
}])
}]
);
}

Expand Down
8 changes: 3 additions & 5 deletions crates/swc_estree_compat/src/babelify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions crates/swc_node_comments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Comment>> {
self.leading.remove(&pos).map(|v| v.1)
fn take_leading(&self, pos: BytePos) -> Vec<Comment> {
self.leading.remove(&pos).map(|v| v.1).expect("Comment should exist")
}

fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
Expand Down
Loading