-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathlib.rs
274 lines (232 loc) · 7.76 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Prettier
//!
//! A port of <https://github.com/prettier/prettier>
#![allow(clippy::wildcard_imports)]
mod binaryish;
mod comments;
mod doc;
mod format;
mod macros;
mod needs_parens;
mod options;
mod printer;
mod utils;
use std::{iter::Peekable, vec};
use oxc_allocator::Allocator;
use oxc_ast::{ast::Program, AstKind, CommentKind, Trivias};
use oxc_span::Span;
use oxc_syntax::identifier::is_line_terminator;
use crate::{doc::Doc, doc::DocBuilder, format::Format, printer::Printer};
pub use crate::options::{ArrowParens, EndOfLine, PrettierOptions, QuoteProps, TrailingComma};
type GroupId = u32;
#[derive(Default)]
struct GroupIdBuilder {
id: GroupId,
}
impl GroupIdBuilder {
pub fn next_id(&mut self) -> GroupId {
self.id += 1;
self.id
}
}
#[derive(Debug, Default)]
pub struct PrettierArgs {
expand_first_arg: bool,
expand_last_arg: bool,
}
pub struct Prettier<'a> {
allocator: &'a Allocator,
source_text: &'a str,
options: PrettierOptions,
/// A stack of comments that will be carefully placed in the right places.
trivias: Peekable<vec::IntoIter<(CommentKind, Span)>>,
/// The stack of AST Nodes
/// See <https://github.com/prettier/prettier/blob/main/src/common/ast-path.js>
stack: Vec<AstKind<'a>>,
group_id_builder: GroupIdBuilder,
args: PrettierArgs,
}
impl<'a> DocBuilder<'a> for Prettier<'a> {
#[inline]
fn allocator(&self) -> &'a Allocator {
self.allocator
}
}
impl<'a> Prettier<'a> {
#[allow(clippy::needless_pass_by_value)]
pub fn new(
allocator: &'a Allocator,
source_text: &'a str,
trivias: Trivias,
options: PrettierOptions,
) -> Self {
Self {
allocator,
source_text,
options,
trivias: trivias.comments().collect::<Vec<_>>().into_iter().peekable(),
stack: vec![],
group_id_builder: GroupIdBuilder::default(),
args: PrettierArgs::default(),
}
}
pub fn build(mut self, program: &Program<'a>) -> String {
let doc = program.format(&mut self);
Printer::new(doc, self.source_text, self.options, self.allocator).build()
}
pub fn doc(mut self, program: &Program<'a>) -> Doc<'a> {
program.format(&mut self)
}
fn enter_node(&mut self, kind: AstKind<'a>) {
self.stack.push(kind);
}
fn leave_node(&mut self) {
self.stack.pop();
}
fn current_kind(&self) -> AstKind<'a> {
self.stack[self.stack.len() - 1]
}
fn parent_kind(&self) -> AstKind<'a> {
self.stack[self.stack.len() - 2]
}
fn parent_parent_kind(&self) -> Option<AstKind<'a>> {
let len = self.stack.len();
(len >= 3).then(|| self.stack[len - 3])
}
#[allow(unused)]
fn nth_parent_kind(&self, n: usize) -> Option<AstKind<'a>> {
let len = self.stack.len();
(len > n).then(|| self.stack[len - n - 1])
}
/// A hack for erasing the lifetime requirement.
#[allow(clippy::unused_self)]
fn alloc<T>(&self, t: &T) -> &'a T {
// SAFETY:
// This should be safe as long as `src` is an reference from the allocator.
// But honestly, I'm not really sure if this is safe.
#[allow(unsafe_code)]
unsafe {
std::mem::transmute(t)
}
}
pub fn semi(&self) -> Option<Doc<'a>> {
self.options.semi.then(|| Doc::Str(";"))
}
pub fn should_print_es5_comma(&self) -> bool {
self.should_print_comma_impl(false)
}
#[allow(unused)]
fn should_print_all_comma(&self) -> bool {
self.should_print_comma_impl(true)
}
fn should_print_comma_impl(&self, level_all: bool) -> bool {
let trailing_comma = self.options.trailing_comma;
trailing_comma.is_all() || (trailing_comma.is_es5() && !level_all)
}
fn is_next_line_empty(&self, span: Span) -> bool {
self.is_next_line_empty_after_index(span.end)
}
fn is_next_line_empty_after_index(&self, start_index: u32) -> bool {
let mut old_idx = None;
let mut idx = Some(start_index);
while idx != old_idx {
old_idx = idx;
idx = self.skip_to_line_end(idx);
idx = self.skip_inline_comment(idx);
idx = self.skip_spaces(idx, /* backwards */ false);
}
idx = self.skip_trailing_comment(idx);
idx = self.skip_newline(idx, /* backwards */ false);
idx.is_some_and(|idx| self.has_newline(idx, /* backwards */ false))
}
fn skip_trailing_comment(&self, start_index: Option<u32>) -> Option<u32> {
let start_index = start_index?;
let mut chars = self.source_text[start_index as usize..].chars();
let c = chars.next()?;
if c != '/' {
return Some(start_index);
}
let c = chars.next()?;
if c != '/' {
return Some(start_index);
}
self.skip_everything_but_new_line(Some(start_index), /* backwards */ false)
}
#[allow(clippy::unused_self)]
fn skip_inline_comment(&self, start_index: Option<u32>) -> Option<u32> {
let start_index = start_index?;
Some(start_index)
}
fn skip_to_line_end(&self, start_index: Option<u32>) -> Option<u32> {
self.skip(start_index, false, |c| matches!(c, ' ' | '\t' | ',' | ';'))
}
fn skip_spaces(&self, start_index: Option<u32>, backwards: bool) -> Option<u32> {
self.skip(start_index, backwards, |c| matches!(c, ' ' | '\t'))
}
fn skip_everything_but_new_line(
&self,
start_index: Option<u32>,
backwards: bool,
) -> Option<u32> {
self.skip(start_index, backwards, |c| !is_line_terminator(c))
}
fn skip<F>(&self, start_index: Option<u32>, backwards: bool, f: F) -> Option<u32>
where
F: Fn(char) -> bool,
{
let start_index = start_index?;
let mut index = start_index;
if backwards {
for c in self.source_text[..=start_index as usize].chars().rev() {
if !f(c) {
return Some(index);
}
index -= 1_u32;
}
} else {
for c in self.source_text[start_index as usize..].chars() {
if !f(c) {
return Some(index);
}
index += 1_u32;
}
}
None
}
#[allow(clippy::cast_possible_truncation)]
fn skip_newline(&self, start_index: Option<u32>, backwards: bool) -> Option<u32> {
let start_index = start_index?;
let c = if backwards {
self.source_text[..=start_index as usize].chars().next_back()
} else {
self.source_text[start_index as usize..].chars().next()
}?;
if is_line_terminator(c) {
let len = c.len_utf8() as u32;
return Some(if backwards { start_index - len } else { start_index + len });
}
Some(start_index)
}
fn has_newline(&self, start_index: u32, backwards: bool) -> bool {
if (backwards && start_index == 0)
|| (!backwards && start_index as usize == self.source_text.len())
{
return false;
}
let start_index = if backwards { start_index - 1 } else { start_index };
let idx = self.skip_spaces(Some(start_index), backwards);
let idx2 = self.skip_newline(idx, backwards);
idx != idx2
}
fn is_previous_line_empty(&self, start_index: u32) -> bool {
let idx = start_index - 1;
let idx = self.skip_spaces(Some(idx), true);
let idx = self.skip_newline(idx, true);
let idx = self.skip_spaces(idx, true);
let idx2 = self.skip_newline(idx, true);
idx != idx2
}
fn next_id(&mut self) -> GroupId {
self.group_id_builder.next_id()
}
}