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
use std::{rc::Rc, sync::Arc};
use swc_common::{BytePos, SourceMap, SourceMapper, SourceMapperDyn, Span, Spanned};
use crate::list::ListFormat;
pub trait SpanExt: Spanned {
#[inline]
fn is_synthesized(&self) -> bool {
false
}
fn starts_on_new_line(&self, format: ListFormat) -> bool {
format.intersects(ListFormat::PreferNewLine)
}
fn comment_range(&self) -> Span {
self.span()
}
}
impl<T: Spanned> SpanExt for T {}
pub trait SourceMapperExt {
fn get_code_map(&self) -> &dyn SourceMapper;
fn is_on_same_line(&self, _lo: BytePos, _hi: BytePos) -> bool {
false
}
fn should_write_separating_line_terminator<P: Spanned, N: Spanned>(
&self,
prev: Option<P>,
next: Option<N>,
format: ListFormat,
) -> bool {
let prev = prev.map(|s| s.span());
let next = next.map(|s| s.span());
if format.contains(ListFormat::MultiLine) {
return true;
}
if format.contains(ListFormat::PreserveLines) {
if let (Some(prev), Some(next)) = (prev, next) {
if prev.is_synthesized() || next.is_synthesized() {
return prev.starts_on_new_line(format) || next.starts_on_new_line(format);
}
return !self.is_on_same_line(prev.hi(), next.lo());
} else {
return false;
}
}
false
}
fn should_write_leading_line_terminator<N: Spanned>(
&self,
parent_node: Span,
children: &[N],
format: ListFormat,
) -> bool {
if format.contains(ListFormat::MultiLine) {
return true;
}
if format.contains(ListFormat::PreserveLines) {
if format.contains(ListFormat::PreferNewLine) {
return true;
}
if children.is_empty() {
return !self.is_on_same_line(parent_node.lo(), parent_node.hi());
}
let first_child = children[0].span();
if parent_node.is_synthesized() || first_child.is_synthesized() {
return first_child.starts_on_new_line(format);
}
!self.is_on_same_line(parent_node.lo(), first_child.lo())
} else {
false
}
}
fn should_write_closing_line_terminator<N: Spanned>(
&self,
parent_node: Span,
children: &[N],
format: ListFormat,
) -> bool {
if format.contains(ListFormat::MultiLine) {
return (format & ListFormat::NoTrailingNewLine) == ListFormat::None;
}
if format.contains(ListFormat::PreserveLines) {
if format.contains(ListFormat::PreferNewLine) {
return true;
}
if children.is_empty() {
return !self.is_on_same_line(parent_node.lo(), parent_node.hi());
}
let last_child = children[children.len() - 1].span();
if parent_node.is_synthesized() || last_child.is_synthesized() {
last_child.starts_on_new_line(format)
} else {
!self.is_on_same_line(parent_node.hi(), last_child.hi())
}
} else {
false
}
}
}
impl SourceMapperExt for SourceMap {
fn get_code_map(&self) -> &dyn SourceMapper {
self
}
}
impl SourceMapperExt for dyn SourceMapper {
fn get_code_map(&self) -> &dyn SourceMapper {
self
}
}
impl SourceMapperExt for Arc<SourceMapperDyn> {
fn get_code_map(&self) -> &dyn SourceMapper {
&**self
}
}
impl SourceMapperExt for Rc<SourceMapperDyn> {
fn get_code_map(&self) -> &dyn SourceMapper {
&**self
}
}
impl SourceMapperExt for Arc<SourceMap> {
fn get_code_map(&self) -> &dyn SourceMapper {
&**self
}
}
impl SourceMapperExt for Rc<SourceMap> {
fn get_code_map(&self) -> &dyn SourceMapper {
&**self
}
}