Add borrow_all() to SingleThreadedComments (#1262)

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
David Sherret 2020-12-08 20:45:33 -05:00 committed by GitHub
parent 718f47803b
commit 01b6a0a90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_common"
repository = "https://github.com/swc-project/swc.git"
version = "0.10.6"
version = "0.10.7"
[features]
concurrent = ["parking_lot"]

View File

@ -3,7 +3,11 @@ use crate::{
syntax_pos::{BytePos, Span},
};
use fxhash::FxHashMap;
use std::{cell::RefCell, rc::Rc, sync::Arc};
use std::{
cell::{Ref, RefCell},
rc::Rc,
sync::Arc,
};
/// Stores comment.
///
@ -107,7 +111,8 @@ where
delegate!();
}
pub type SingleThreadedCommentsMap = Rc<RefCell<FxHashMap<BytePos, Vec<Comment>>>>;
pub type SingleThreadedCommentsMapInner = FxHashMap<BytePos, Vec<Comment>>;
pub type SingleThreadedCommentsMap = Rc<RefCell<SingleThreadedCommentsMapInner>>;
/// Single-threaded storage for comments.
#[derive(Debug, Clone, Default)]
@ -184,6 +189,16 @@ impl SingleThreadedComments {
(self.leading, self.trailing)
}
/// Borrows all the comments as (leading, trailing).
pub fn borrow_all<'a>(
&'a self,
) -> (
Ref<'a, SingleThreadedCommentsMapInner>,
Ref<'a, SingleThreadedCommentsMapInner>,
) {
(self.leading.borrow(), self.trailing.borrow())
}
pub fn with_leading<F, Ret>(&self, pos: BytePos, op: F) -> Ret
where
F: FnOnce(&[Comment]) -> Ret,
@ -206,6 +221,7 @@ impl SingleThreadedComments {
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Comment {
pub kind: CommentKind,