swc_core::common::comments

Trait Comments

pub trait Comments {
Show 16 methods // Required methods fn add_leading(&self, pos: BytePos, cmt: Comment); 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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>; fn add_trailing(&self, pos: BytePos, cmt: Comment); fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>); fn has_trailing(&self, pos: BytePos) -> bool; fn move_trailing(&self, from: BytePos, to: BytePos); fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>; fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>; fn add_pure_comment(&self, pos: BytePos); // Provided methods fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret where Self: Sized, F: FnOnce(&[Comment]) -> Ret { ... } fn with_trailing<F, Ret>(&self, pos: BytePos, f: F) -> Ret where Self: Sized, F: FnOnce(&[Comment]) -> Ret { ... } fn has_flag(&self, lo: BytePos, flag: &str) -> bool { ... }
}
Available on crate feature __common only.
Expand description

Stores comment.

§Implementation notes

Methods uses (&self) instead of (&mut self) for some reasons. Firstly, this is similar to the previous api. Secondly, typescript parser requires backtracking, which requires Clone. To avoid cloning large vectors, we must use Rc<RefCell>. We have two option. We may implement it in the parser or in the implementation. If we decide to go with first option, we should pass Comments to parser, and as a result we need another method to take comments back. If we decide to go with second way, we can just pass [&Comments] to the parser. Thirdly, (&self) allows multi-threaded use-cases such as swc itself.

We use Option instead of no-op Comments implementation to avoid allocation unless required.

Required Methods§

fn add_leading(&self, pos: BytePos, cmt: Comment)

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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>

fn add_trailing(&self, pos: BytePos, cmt: Comment)

fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>)

fn has_trailing(&self, pos: BytePos) -> bool

fn move_trailing(&self, from: BytePos, to: BytePos)

fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

fn add_pure_comment(&self, pos: BytePos)

Provided Methods§

fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where Self: Sized, F: FnOnce(&[Comment]) -> Ret,

fn with_trailing<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where Self: Sized, F: FnOnce(&[Comment]) -> Ret,

fn has_flag(&self, lo: BytePos, flag: &str) -> bool

This method is used to check if a comment with the given flag exist.

If flag is PURE, this method will look for @__PURE__ and #__PURE__.

Implementations on Foreign Types§

§

impl<C> Comments for Option<C>
where C: Comments,

This implementation behaves like NoopComments if it’s None.

§

fn add_leading(&self, pos: BytePos, cmt: Comment)

§

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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_trailing(&self, pos: BytePos, cmt: Comment)

§

fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>)

§

fn has_trailing(&self, pos: BytePos) -> bool

§

fn move_trailing(&self, from: BytePos, to: BytePos)

§

fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_pure_comment(&self, pos: BytePos)

§

fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where Option<C>: Sized, F: FnOnce(&[Comment]) -> Ret,

§

fn with_trailing<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where Option<C>: Sized, F: FnOnce(&[Comment]) -> Ret,

§

fn has_flag(&self, lo: BytePos, flag: &str) -> bool

§

impl<T> Comments for &T
where T: Comments + ?Sized,

§

fn add_leading(&self, pos: BytePos, cmt: Comment)

§

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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_trailing(&self, pos: BytePos, cmt: Comment)

§

fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>)

§

fn has_trailing(&self, pos: BytePos) -> bool

§

fn move_trailing(&self, from: BytePos, to: BytePos)

§

fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_pure_comment(&self, pos: BytePos)

§

fn has_flag(&self, lo: BytePos, flag: &str) -> bool

§

impl<T> Comments for Box<T>
where T: Comments + ?Sized,

§

fn add_leading(&self, pos: BytePos, cmt: Comment)

§

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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_trailing(&self, pos: BytePos, cmt: Comment)

§

fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>)

§

fn has_trailing(&self, pos: BytePos) -> bool

§

fn move_trailing(&self, from: BytePos, to: BytePos)

§

fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_pure_comment(&self, pos: BytePos)

§

fn has_flag(&self, lo: BytePos, flag: &str) -> bool

§

impl<T> Comments for Rc<T>
where T: Comments + ?Sized,

§

fn add_leading(&self, pos: BytePos, cmt: Comment)

§

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 get_leading(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_trailing(&self, pos: BytePos, cmt: Comment)

§

fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>)

§

fn has_trailing(&self, pos: BytePos) -> bool

§

fn move_trailing(&self, from: BytePos, to: BytePos)

§

fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>>

§

fn add_pure_comment(&self, pos: BytePos)

§

fn has_flag(&self, lo: BytePos, flag: &str) -> bool

Implementors§