Skip to content

Commit 59eecf4

Browse files
committed
fixup: VecDeque
1 parent 9317f4f commit 59eecf4

File tree

1 file changed

+24
-21
lines changed
  • packages/async-rewriter3/src

1 file changed

+24
-21
lines changed

packages/async-rewriter3/src/lib.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::VecDeque;
12
use wasm_bindgen::prelude::*;
23
use rslint_parser::{ast::{ArrowExpr, CallExpr, Expr, ExprOrBlock, FnDecl, FnExpr, ReturnStmt}, parse_text, AstNode, SyntaxNode, TextSize};
34

@@ -25,15 +26,15 @@ fn is_block(body: &ExprOrBlock) -> bool {
2526
}
2627
}
2728

28-
fn fn_start_insertion(body: &ExprOrBlock) -> Vec<Insertion> {
29-
let mut ret = Vec::new();
29+
fn fn_start_insertion(body: &ExprOrBlock) -> VecDeque<Insertion> {
30+
let mut ret = VecDeque::new();
3031
let mut offset = body.syntax().text_range().start();
3132
if !is_block(body) {
32-
ret.push(Insertion::new(offset, "{"));
33+
ret.push_back(Insertion::new(offset, "{"));
3334
} else {
3435
offset = offset.checked_add(1.into()).unwrap();
3536
}
36-
ret.push(Insertion::new(offset, r#"
37+
ret.push_back(Insertion::new(offset, r#"
3738
const _syntheticPromise = Symbol.for('@@mongosh.syntheticPromise');
3839
3940
function _markSyntheticPromise(p) {
@@ -53,22 +54,22 @@ fn fn_start_insertion(body: &ExprOrBlock) -> Vec<Insertion> {
5354
"#
5455
));
5556
if !is_block(body) {
56-
ret.push(Insertion::new(
57+
ret.push_back(Insertion::new(
5758
offset,
5859
"return ("
5960
));
6061
}
6162
ret
6263
}
63-
fn fn_end_insertion(body: &ExprOrBlock) -> Vec<Insertion> {
64-
let mut ret = Vec::new();
64+
fn fn_end_insertion(body: &ExprOrBlock) -> VecDeque<Insertion> {
65+
let mut ret = VecDeque::new();
6566
let mut offset = body.syntax().text_range().end();
6667
if is_block(body) {
6768
offset = offset.checked_sub(1.into()).unwrap();
6869
} else {
69-
ret.push(Insertion::new(offset, ");"));
70+
ret.push_back(Insertion::new(offset, ");"));
7071
}
71-
ret.push(Insertion::new(
72+
ret.push_back(Insertion::new(
7273
offset,
7374
r#"
7475
} catch (err) {
@@ -98,17 +99,18 @@ fn fn_end_insertion(body: &ExprOrBlock) -> Vec<Insertion> {
9899
"#
99100
));
100101
if !is_block(body) {
101-
ret.push(Insertion::new(offset, "}"));
102+
ret.push_back(Insertion::new(offset, "}"));
102103
}
103104
ret
104105
}
105106

106-
fn collect_insertions(node: &SyntaxNode, has_function_parent: bool) -> Vec<Insertion> {
107+
fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> VecDeque<Insertion> {
107108
let is_function_node = FnExpr::can_cast(node.kind()) || FnDecl::can_cast(node.kind()) || ArrowExpr::can_cast(node.kind());
108-
let mut insertions = Vec::new();
109+
let has_function_parent = nesting_depth > 0;
110+
let mut insertions = VecDeque::new();
109111
for child in node.children() {
110112
let range = child.text_range();
111-
let child_insertions = &mut collect_insertions(&child, has_function_parent || is_function_node);
113+
let child_insertions = &mut collect_insertions(&child, nesting_depth + if is_function_node { 1 } else { 0 });
112114
if FnDecl::can_cast(child.kind()) {
113115
let as_fn = FnDecl::cast(child).unwrap();
114116
if as_fn.async_token().is_none() {
@@ -129,9 +131,9 @@ fn collect_insertions(node: &SyntaxNode, has_function_parent: bool) -> Vec<Inser
129131
let mut is_dot_call_expression = false;
130132
if has_function_parent {
131133
if is_returned_expression {
132-
insertions.push(Insertion::new(range.start(), "(_synchronousReturnValue = "));
134+
insertions.push_back(Insertion::new(range.start(), "(_synchronousReturnValue = "));
133135
}
134-
insertions.push(Insertion::new(range.start(), "(_ex = "));
136+
insertions.push_back(Insertion::new(range.start(), "(_ex = "));
135137
}
136138

137139
match as_expr {
@@ -154,7 +156,7 @@ fn collect_insertions(node: &SyntaxNode, has_function_parent: bool) -> Vec<Inser
154156
Expr::DotExpr(_) => {
155157
if is_called_expression {
156158
is_dot_call_expression = true;
157-
insertions.pop();
159+
insertions.pop_back();
158160
}
159161
insertions.append(child_insertions);
160162
}
@@ -164,10 +166,10 @@ fn collect_insertions(node: &SyntaxNode, has_function_parent: bool) -> Vec<Inser
164166
}
165167
if has_function_parent {
166168
if !is_dot_call_expression {
167-
insertions.push(Insertion::new(range.end(), ", _isp(_ex) ? await _ex : _ex)"));
169+
insertions.push_back(Insertion::new(range.end(), ", _isp(_ex) ? await _ex : _ex)"));
168170
}
169171
if is_returned_expression {
170-
insertions.push(Insertion::new(
172+
insertions.push_back(Insertion::new(
171173
range.end(),
172174
", _functionState === 'async' ? _synchronousReturnValue : null)"
173175
));
@@ -182,13 +184,13 @@ fn collect_insertions(node: &SyntaxNode, has_function_parent: bool) -> Vec<Inser
182184
#[wasm_bindgen]
183185
pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
184186
let parsed = parse_text(input.as_str(), 0);
185-
let mut insertions = collect_insertions(&parsed.syntax(), false);
187+
let mut insertions = collect_insertions(&parsed.syntax(), 0);
186188
let mut i = 0;
187189
for insertion in &mut insertions {
188190
i += 1;
189191
insertion.original_ordering = Some(i);
190192
}
191-
insertions.sort_by(|a, b| a.offset.cmp(&b.offset));
193+
insertions.make_contiguous().sort_by(|a, b| a.offset.cmp(&b.offset));
192194

193195
let mut result = input.to_string();
194196
let mut debug_tag = "".to_string();
@@ -197,7 +199,8 @@ pub fn async_rewrite(input: String, with_debug_tags: bool) -> String {
197199
if with_debug_tags {
198200
debug_tag = [
199201
"/*i", insertion.original_ordering.unwrap().to_string().as_str(), "@",
200-
u32::from(insertion.offset).to_string().as_str(), "*/"
202+
u32::from(insertion.offset).to_string().as_str(),
203+
if insertion.text.contains("/*") { "" } else { "*/" }
201204
].concat();
202205
}
203206
result = [before, debug_tag.as_str(), insertion.text, debug_tag.as_str(), after].concat();

0 commit comments

Comments
 (0)