dprint_swc_ext/common/
types.rs

1use crate::swc::parser::token::TokenAndSpan;
2
3use super::comments::CommentContainer;
4use super::text_info::*;
5use super::tokens::TokenContainer;
6
7/// A Module or Script node.
8pub trait RootNode<'a> {
9  fn maybe_text_info(&self) -> Option<&'a SourceTextInfo>;
10  fn maybe_token_container(&self) -> Option<&'a TokenContainer<'a>>;
11  fn maybe_comment_container(&self) -> Option<&'a CommentContainer<'a>>;
12
13  fn token_at_index(&self, index: usize) -> Option<&'a TokenAndSpan> {
14    self.token_container().get_token_at_index(index)
15  }
16
17  fn token_container(&self) -> &'a TokenContainer<'a> {
18    self
19      .maybe_token_container()
20      .as_ref()
21      .expect("The tokens must be provided to `with_view` in order to use this method.")
22  }
23
24  fn comment_container(&self) -> &'a CommentContainer<'a> {
25    self
26      .maybe_comment_container()
27      .as_ref()
28      .expect("The comments must be provided to `with_view` in order to use this method.")
29  }
30}
31
32impl<'a, T> SourceTextInfoProvider<'a> for T
33where
34  T: RootNode<'a>,
35{
36  fn text_info(&self) -> &'a SourceTextInfo {
37    self
38      .maybe_text_info()
39      .expect("The source file must be provided to `with_view` in order to use this method.")
40  }
41}