Skip to content

Commit 0991c96

Browse files
committed
chore: Some cleanup.
1 parent f9b0875 commit 0991c96

File tree

12 files changed

+388
-445
lines changed

12 files changed

+388
-445
lines changed

src/parsing/comments.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,13 @@ impl<'a> CommentTracker<'a> {
3232
iterator.extend(previous_token.hi().trailing_comments_fast(self.module));
3333
}
3434

35-
loop {
36-
if let Some(token) = self.tokens.get(self.token_index) {
37-
iterator.extend(token.lo().leading_comments_fast(self.module));
35+
while let Some(token) = self.tokens.get(self.token_index) {
36+
iterator.extend(token.lo().leading_comments_fast(self.module));
3837

39-
let token_hi = token.hi();
40-
if token_hi < pos {
41-
iterator.extend(token_hi.trailing_comments_fast(self.module));
42-
self.token_index += 1;
43-
} else {
44-
break;
45-
}
38+
let token_hi = token.hi();
39+
if token_hi < pos {
40+
iterator.extend(token_hi.trailing_comments_fast(self.module));
41+
self.token_index += 1;
4642
} else {
4743
break;
4844
}
@@ -55,22 +51,18 @@ impl<'a> CommentTracker<'a> {
5551
pub fn trailing_comments_with_previous(&mut self, end: BytePos) -> CommentsIterator<'a> {
5652
let mut iterator = CommentsIterator::new(Vec::new());
5753

58-
loop {
59-
if let Some(token) = self.tokens.get(self.token_index) {
60-
iterator.extend(token.lo().leading_comments_fast(self.module));
54+
while let Some(token) = self.tokens.get(self.token_index) {
55+
iterator.extend(token.lo().leading_comments_fast(self.module));
6156

62-
let is_comma = token.token == Token::Comma;
63-
if !is_comma && token.lo() >= end {
64-
break;
65-
}
57+
let is_comma = token.token == Token::Comma;
58+
if !is_comma && token.lo() >= end {
59+
break;
60+
}
6661

67-
let token_hi = token.hi();
68-
if is_comma || token_hi <= end {
69-
iterator.extend(token.hi().trailing_comments_fast(self.module));
70-
self.token_index += 1;
71-
} else {
72-
break;
73-
}
62+
let token_hi = token.hi();
63+
if is_comma || token_hi <= end {
64+
iterator.extend(token.hi().trailing_comments_fast(self.module));
65+
self.token_index += 1;
7466
} else {
7567
break;
7668
}

src/parsing/context.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

src/parsing/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod comments;
2+
mod context;
23
mod node_helpers;
34
mod helpers;
45
mod parser_types;
@@ -8,6 +9,7 @@ mod tokens;
89
mod swc;
910

1011
use comments::*;
12+
use context::*;
1113
use helpers::*;
1214
use parser_types::*;
1315
use tokens::*;

src/parsing/node_helpers.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ pub fn is_first_node_on_line(node: &dyn Spanned, module: &Module) -> bool {
77

88
for i in (0..start).rev() {
99
let c = source_file_text[i];
10-
if c != ' ' as u8 && c != '\t' as u8 {
11-
return c == '\n' as u8;
10+
if c != b' ' && c != b'\t' {
11+
return c == b'\n';
1212
}
1313
}
1414

15-
return true;
15+
true
1616
}
1717

1818
pub fn has_separating_blank_line(first_node: &dyn Spanned, second_node: &dyn Spanned, module: &Module) -> bool {
@@ -33,14 +33,14 @@ pub fn has_separating_blank_line(first_node: &dyn Spanned, second_node: &dyn Spa
3333
}
3434

3535
pub fn get_use_new_lines_for_nodes(first_node: &dyn Spanned, second_node: &dyn Spanned, module: &Module) -> bool {
36-
return first_node.end_line_fast(module) != second_node.start_line_fast(module);
36+
first_node.end_line_fast(module) != second_node.start_line_fast(module)
3737
}
3838

39-
pub fn has_leading_comment_on_different_line<'a>(node: &dyn Spanned, comments_to_ignore: Option<&Vec<&'a Comment>>, module: &Module<'a>) -> bool {
39+
pub fn has_leading_comment_on_different_line<'a>(node: &dyn Spanned, comments_to_ignore: Option<&[&'a Comment]>, module: &Module<'a>) -> bool {
4040
get_leading_comment_on_different_line(node, comments_to_ignore, module).is_some()
4141
}
4242

43-
pub fn get_leading_comment_on_different_line<'a>(node: &dyn Spanned, comments_to_ignore: Option<&Vec<&'a Comment>>, module: &Module<'a>) -> Option<&'a Comment> {
43+
pub fn get_leading_comment_on_different_line<'a>(node: &dyn Spanned, comments_to_ignore: Option<&[&'a Comment]>, module: &Module<'a>) -> Option<&'a Comment> {
4444
let comments_to_ignore: Option<Vec<BytePos>> = comments_to_ignore.map(|x| x.iter().map(|c| c.lo()).collect());
4545
let node_start_line = node.start_line_fast(module);
4646
for comment in node.leading_comments_fast(module) {
@@ -55,18 +55,18 @@ pub fn get_leading_comment_on_different_line<'a>(node: &dyn Spanned, comments_to
5555
}
5656
}
5757

58-
return None;
58+
None
5959
}
6060

6161
pub fn nodes_have_only_spaces_between(previous_node: &Node, next_node: &Node, module: &Module) -> bool {
6262
if let Node::JSXText(previous_node) = previous_node {
6363
let previous_node_text = previous_node.text_fast(module);
6464
crate::utils::has_no_new_lines_in_trailing_whitespace(previous_node_text)
65-
&& previous_node_text.chars().last() == Some(' ')
65+
&& previous_node_text.ends_with(' ')
6666
} else if let Node::JSXText(next_node) = next_node {
6767
let next_node_text = next_node.text_fast(module);
6868
crate::utils::has_no_new_lines_in_leading_whitespace(next_node_text)
69-
&& next_node_text.chars().next() == Some(' ')
69+
&& next_node_text.starts_with(' ')
7070
} else {
7171
let source_file = module.source_file.as_ref().unwrap();
7272
crate::utils::is_not_empty_and_only_spaces(&source_file.src[previous_node.hi().0 as usize..next_node.lo().0 as usize])
@@ -192,11 +192,11 @@ pub fn is_test_library_call_expr(node: &CallExpr, module: &Module) -> bool {
192192
}
193193
}
194194

195-
pub fn is_optional_call_expr<'a>(node: &dyn NodeTrait<'a>) -> bool {
196-
return node.parent().unwrap().kind() == NodeKind::OptChainExpr;
195+
pub fn is_optional_call_expr(node: &dyn NodeTrait) -> bool {
196+
node.parent().unwrap().kind() == NodeKind::OptChainExpr
197197
}
198198

199-
pub fn is_expr_stmt_or_body_with_single_expr_stmt<'a>(node: Node<'a>) -> bool {
199+
pub fn is_expr_stmt_or_body_with_single_expr_stmt(node: Node) -> bool {
200200
match node {
201201
Node::ExprStmt(_) => true,
202202
Node::BlockStmt(block) => block.stmts.len() == 1 && block.stmts[0].is::<ExprStmt>(),

0 commit comments

Comments
 (0)