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
use crate::expr::{Data, PrimitiveType, data};
use crate::{E, Expr, e};
use bun_alloc::Arena; // bumpalo::Bump re-export
/// Links both ropes into the result (see `EString::push`); sound because shared strings are never ropes.
// upstream 14a4f45c54: inlined enum members are stored flat (s_enum resolves
// the rope before storing the EnumString), so `join_strings` no longer needs
// `has_inlined_enum_poison` / `clone_rope_nodes` — linking in O(1) is sound.
fn join_strings(left: &E::EString, right: &E::EString) -> E::EString {
let mut new = left.shallow_clone();
let mut rhs = data::Store::append(right.shallow_clone());
new.push(&mut *rhs);
new.prefer_template = new.prefer_template || right.prefer_template;
new
}
/// `std.mem.concat(arena, E.TemplatePart, &.{a, b})` — concat into the bump
/// arena. `TemplatePart` is POD-shaped (no Drop) but not `Copy` because
/// `EString` opted out; mirror `Template::fold`'s field-wise copy via
/// `shallow_clone` instead of raw `copy_nonoverlapping`.
fn concat_parts(
bump: &Arena,
a: &[e::TemplatePart],
b: &[e::TemplatePart],
) -> crate::StoreSlice<e::TemplatePart> {
let mut v = bun_alloc::ArenaVec::<e::TemplatePart>::with_capacity_in(a.len() + b.len(), bump);
for p in a.iter().chain(b.iter()) {
// Zig `var part = part.*` — field-wise copy (all fields structurally `Copy`).
v.push(e::TemplatePart {
value: p.value,
tail_loc: p.tail_loc,
tail: p.tail.shallow_clone(),
});
}
crate::StoreSlice::from_bump(v)
}
/// Transforming the left operand into a string is not safe if it comes from a
/// nested AST node.
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum FoldStringAdditionKind {
/// "x" + "y" -> "xy"
/// 1 + "y" -> "1y"
Normal,
/// a + "x" + "y" -> a + "xy"
/// a + 1 + "y" -> a + 1 + y
NestedLeft,
}
/// NOTE: unlike esbuild's js_ast_helpers.FoldStringAddition, this does mutate
/// the input AST in the case of rope strings
pub fn fold_string_addition(
l: Expr,
r: Expr,
bump: &Arena,
kind: FoldStringAdditionKind,
) -> Option<Expr> {
// "See through" inline enum constants
// TODO: implement foldAdditionPreProcess to fold some more things :)
let mut lhs = l.unwrap_inlined();
let mut rhs = r.unwrap_inlined();
if kind != FoldStringAdditionKind::NestedLeft {
// See comment on `FoldStringAdditionKind` for examples
match rhs.data {
Data::EString(_) | Data::ETemplate(_) => {
if let Some(str) = lhs.to_string_expr_without_side_effects(bump) {
lhs = str;
}
}
_ => {}
}
}
match lhs.data {
Data::EString(left) => {
if let Some(str) = rhs.to_string_expr_without_side_effects(bump) {
rhs = str;
}
if left.is_utf8() {
match rhs.data {
// "bar" + "baz" => "barbaz"
Data::EString(right) => {
if right.is_utf8() {
return Some(Expr::init(
join_strings(left.get(), right.get()),
lhs.loc,
));
}
}
// "bar" + `baz${bar}` => `barbaz${bar}`
Data::ETemplate(right) => {
if right.head.is_utf8() {
return Some(Expr::init(
E::Template {
tag: None,
parts: right.parts,
head: e::TemplateContents::Cooked(join_strings(
left.get(),
right.head.cooked(),
)),
},
l.loc,
));
}
}
_ => {
// other constant-foldable ast nodes would have been converted to .e_string
}
}
// "'x' + `y${z}`" => "`xy${z}`"
if let Data::ETemplate(t) = rhs.data {
if t.tag.is_none() {
// (intentionally empty — matches Zig)
}
}
}
if left.len() == 0 && rhs.known_primitive() == PrimitiveType::String {
return Some(rhs);
}
return None;
}
Data::ETemplate(mut left) => {
// "`${x}` + 0" => "`${x}` + '0'"
if let Some(str) = rhs.to_string_expr_without_side_effects(bump) {
rhs = str;
}
if left.tag.is_none() {
match rhs.data {
// `foo${bar}` + "baz" => `foo${bar}baz`
Data::EString(right) => {
if right.is_utf8() {
// Mutation of this node is fine because it will be not
// be shared by other places. Note that e_template will
// be treated by enums as strings, but will not be
// inlined unless they could be converted into
// .e_string.
// `parts` is `StoreSlice<T>` (arena-owned, mutable
// provenance) — write through `parts_mut()`.
if !left.parts().is_empty() {
let i = left.parts().len() - 1;
let last_tail = &left.parts()[i].tail;
if last_tail.is_utf8() {
let new_tail = e::TemplateContents::Cooked(join_strings(
last_tail.cooked(),
right.get(),
));
// Zig wrote `left.parts[i].tail = ...` in place.
left.parts_mut()[i].tail = new_tail;
return Some(lhs);
}
} else if left.head.is_utf8() {
let new_head = join_strings(left.head.cooked(), right.get());
left.head = e::TemplateContents::Cooked(new_head);
return Some(lhs);
}
}
}
// `foo${bar}` + `a${hi}b` => `foo${bar}a${hi}b`
Data::ETemplate(right) => {
if right.tag.is_none() && right.head.is_utf8() {
if !left.parts().is_empty() {
let i = left.parts().len() - 1;
let last_tail = &left.parts()[i].tail;
if last_tail.is_utf8() && right.head.is_utf8() {
let new_tail = e::TemplateContents::Cooked(join_strings(
last_tail.cooked(),
right.head.cooked(),
));
left.parts_mut()[i].tail = new_tail;
let new_parts = if right.parts().is_empty() {
left.parts
} else {
// std.mem.concat → bump-allocated concat
concat_parts(bump, left.parts(), right.parts())
};
left.parts = new_parts;
return Some(lhs);
}
} else if left.head.is_utf8() && right.head.is_utf8() {
let new_head =
join_strings(left.head.cooked(), right.head.cooked());
left.head = e::TemplateContents::Cooked(new_head);
left.parts = right.parts;
return Some(lhs);
}
}
}
_ => {
// other constant-foldable ast nodes would have been converted to .e_string
}
}
}
}
_ => {
// other constant-foldable ast nodes would have been converted to .e_string
}
}
if let Some(right) = rhs.data.as_e_string() {
if right.len() == 0 && lhs.known_primitive() == PrimitiveType::String {
return Some(lhs);
}
}
None
}
// ported from: src/js_parser/ast/foldStringAddition.zig