|
| 1 | +use dprint_core::formatting::{Info, ConditionReference}; |
| 2 | +use swc_common::SourceFile; |
| 3 | +use swc_ast_view::*; |
| 4 | +use fnv::{FnvHashMap, FnvHashSet}; |
| 5 | + |
| 6 | +use super::*; |
| 7 | +use crate::configuration::*; |
| 8 | +use crate::utils::Stack; |
| 9 | + |
| 10 | +pub struct Context<'a> { |
| 11 | + pub module: &'a Module<'a>, |
| 12 | + pub config: &'a Configuration, |
| 13 | + pub comments: CommentTracker<'a>, |
| 14 | + pub token_finder: TokenFinder<'a>, |
| 15 | + pub current_node: Node<'a>, |
| 16 | + pub parent_stack: Stack<Node<'a>>, |
| 17 | + handled_comments: FnvHashSet<BytePos>, |
| 18 | + pub info: &'a SourceFile, |
| 19 | + stored_infos: FnvHashMap<(BytePos, BytePos), Info>, |
| 20 | + stored_info_ranges: FnvHashMap<(BytePos, BytePos), (Info, Info)>, |
| 21 | + pub end_statement_or_member_infos: Stack<Info>, |
| 22 | + before_comments_start_info_stack: Stack<(Span, Info)>, |
| 23 | + if_stmt_last_brace_condition_ref: Option<ConditionReference>, |
| 24 | + expr_stmt_single_line_parent_brace_ref: Option<ConditionReference>, |
| 25 | + /// Used for ensuring nodes are parsed in order. |
| 26 | + #[cfg(debug_assertions)] |
| 27 | + pub last_parsed_node_pos: u32, |
| 28 | +} |
| 29 | + |
| 30 | +impl<'a> Context<'a> { |
| 31 | + pub fn new( |
| 32 | + config: &'a Configuration, |
| 33 | + tokens: &'a [TokenAndSpan], |
| 34 | + current_node: Node<'a>, |
| 35 | + info: &'a SourceFile, |
| 36 | + module: &'a Module, |
| 37 | + ) -> Context<'a> { |
| 38 | + Context { |
| 39 | + module, |
| 40 | + config, |
| 41 | + comments: CommentTracker::new(module, tokens), |
| 42 | + token_finder: TokenFinder::new(module), |
| 43 | + current_node, |
| 44 | + parent_stack: Stack::new(), |
| 45 | + handled_comments: FnvHashSet::default(), |
| 46 | + info, |
| 47 | + stored_infos: FnvHashMap::default(), |
| 48 | + stored_info_ranges: FnvHashMap::default(), |
| 49 | + end_statement_or_member_infos: Stack::new(), |
| 50 | + before_comments_start_info_stack: Stack::new(), |
| 51 | + if_stmt_last_brace_condition_ref: None, |
| 52 | + expr_stmt_single_line_parent_brace_ref: None, |
| 53 | + #[cfg(debug_assertions)] |
| 54 | + last_parsed_node_pos: 0, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn parent(&self) -> &Node<'a> { |
| 59 | + self.parent_stack.peek().unwrap() |
| 60 | + } |
| 61 | + |
| 62 | + pub fn has_handled_comment(&self, comment: &Comment) -> bool { |
| 63 | + self.handled_comments.contains(&comment.lo()) |
| 64 | + } |
| 65 | + |
| 66 | + pub fn mark_comment_handled(&mut self, comment: &Comment) { |
| 67 | + self.handled_comments.insert(comment.lo()); |
| 68 | + } |
| 69 | + |
| 70 | + pub fn store_info_for_node(&mut self, node: &dyn Spanned, info: Info) { |
| 71 | + self.stored_infos.insert((node.lo(), node.hi()), info); |
| 72 | + } |
| 73 | + |
| 74 | + pub fn get_info_for_node(&self, node: &dyn Spanned) -> Option<Info> { |
| 75 | + self.stored_infos.get(&(node.lo(), node.hi())).map(|x| x.to_owned()) |
| 76 | + } |
| 77 | + |
| 78 | + pub fn store_info_range_for_node(&mut self, node: &dyn Spanned, infos: (Info, Info)) { |
| 79 | + self.stored_info_ranges.insert((node.lo(), node.hi()), infos); |
| 80 | + } |
| 81 | + |
| 82 | + pub fn get_info_range_for_node(&self, node: &dyn Spanned) -> Option<(Info, Info)> { |
| 83 | + self.stored_info_ranges.get(&(node.lo(), node.hi())).map(|x| x.to_owned()) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn store_if_stmt_last_brace_condition_ref(&mut self, condition_reference: ConditionReference) { |
| 87 | + self.if_stmt_last_brace_condition_ref = Some(condition_reference); |
| 88 | + } |
| 89 | + |
| 90 | + pub fn take_if_stmt_last_brace_condition_ref(&mut self) -> Option<ConditionReference> { |
| 91 | + self.if_stmt_last_brace_condition_ref.take() |
| 92 | + } |
| 93 | + |
| 94 | + pub fn store_expr_stmt_single_line_parent_brace_ref(&mut self, condition_reference: ConditionReference) { |
| 95 | + self.expr_stmt_single_line_parent_brace_ref = Some(condition_reference); |
| 96 | + } |
| 97 | + |
| 98 | + pub fn take_expr_stmt_single_line_parent_brace_ref(&mut self) -> Option<ConditionReference> { |
| 99 | + self.expr_stmt_single_line_parent_brace_ref.take() |
| 100 | + } |
| 101 | + |
| 102 | + pub fn get_or_create_current_before_comments_start_info(&mut self) -> Info { |
| 103 | + let current_span = self.current_node.span(); |
| 104 | + if let Some((span, info)) = self.before_comments_start_info_stack.peek() { |
| 105 | + if *span == current_span { |
| 106 | + return *info; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + let new_info = Info::new("beforeComments"); |
| 111 | + self.before_comments_start_info_stack.push((current_span, new_info)); |
| 112 | + new_info |
| 113 | + } |
| 114 | + |
| 115 | + pub fn take_current_before_comments_start_info(&mut self) -> Option<Info> { |
| 116 | + let mut had_span = false; |
| 117 | + if let Some((span, _)) = self.before_comments_start_info_stack.peek() { |
| 118 | + if *span == self.current_node.span() { |
| 119 | + had_span = true; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if had_span { |
| 124 | + Some(self.before_comments_start_info_stack.pop().1) |
| 125 | + } else { |
| 126 | + None |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // do any assertions for how the state of this context should be at the end of the file |
| 131 | + #[cfg(debug_assertions)] |
| 132 | + pub fn assert_end_of_file_state(&self) { |
| 133 | + if self.before_comments_start_info_stack.iter().next().is_some() { |
| 134 | + panic!("Debug panic! There were infos in the before comments start info stack."); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + #[cfg(debug_assertions)] |
| 139 | + pub fn assert_text(&self, span: Span, expected_text: &str) { |
| 140 | + let actual_text = span.text_fast(self.module); |
| 141 | + if actual_text != expected_text { |
| 142 | + panic!("Debug Panic Expected text `{}`, but found `{}`", expected_text, actual_text) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments