Skip to content

Navigation Menu

Sign in
Sign up

Commit bf12160

Browse files
resolve rope before pushing to previous string (oven-sh#2909)
1 parent 2f4162e commit bf12160

3 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎src/js_ast.zig‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ pub const E = struct {
21762176
return @ptrCast([*]const u16, @alignCast(@alignOf(u16), this.data.ptr))[0..this.data.len];
21772177
}
21782178

2179-
pub fn resovleRopeIfNeeded(this: *String, allocator: std.mem.Allocator) void {
2179+
pub fn resolveRopeIfNeeded(this: *String, allocator: std.mem.Allocator) void {
21802180
if (this.next == null or !this.isUTF8()) return;
21812181
var str = this.next;
21822182
var bytes = std.ArrayList(u8).initCapacity(allocator, this.rope_len) catch unreachable;
@@ -2191,7 +2191,7 @@ pub const E = struct {
21912191
}
21922192

21932193
pub fn slice(this: *String, allocator: std.mem.Allocator) []const u8 {
2194-
this.resovleRopeIfNeeded(allocator);
2194+
this.resolveRopeIfNeeded(allocator);
21952195
return this.string(allocator) catch unreachable;
21962196
}
21972197

@@ -2419,6 +2419,7 @@ pub const E = struct {
24192419
}
24202420

24212421
if (part.tail.len() > 0) {
2422+
head.data.e_string.resolveRopeIfNeeded(allocator);
24222423
head.data.e_string.push(Expr.init(E.String, part.tail, part.tail_loc).data.e_string);
24232424
}
24242425

@@ -2445,7 +2446,7 @@ pub const E = struct {
24452446

24462447
if (parts.items.len == 0) {
24472448
parts.deinit();
2448-
head.data.e_string.resovleRopeIfNeeded(allocator);
2449+
head.data.e_string.resolveRopeIfNeeded(allocator);
24492450
return head;
24502451
}
24512452

@@ -5157,8 +5158,8 @@ pub const Expr = struct {
51575158
switch (right) {
51585159
.e_string => |r| {
51595160
equality.ok = true;
5160-
r.resovleRopeIfNeeded(allocator);
5161-
l.resovleRopeIfNeeded(allocator);
5161+
r.resolveRopeIfNeeded(allocator);
5162+
l.resolveRopeIfNeeded(allocator);
51625163
equality.equal = r.eql(E.String, l);
51635164
},
51645165
.e_null, .e_undefined => {

‎src/js_printer.zig‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ fn NewPrinter(
25072507
}
25082508
},
25092509
.e_string => |e| {
2510-
e.resovleRopeIfNeeded(p.options.allocator);
2510+
e.resolveRopeIfNeeded(p.options.allocator);
25112511
p.addSourceMapping(expr.loc);
25122512

25132513
// If this was originally a template literal, print it as one as long as we're not minifying
@@ -2541,7 +2541,7 @@ fn NewPrinter(
25412541

25422542
p.print("`");
25432543
if (e.head.isPresent()) {
2544-
e.head.resovleRopeIfNeeded(p.options.allocator);
2544+
e.head.resolveRopeIfNeeded(p.options.allocator);
25452545

25462546
p.printStringContent(&e.head, '`');
25472547
}
@@ -2551,7 +2551,7 @@ fn NewPrinter(
25512551
p.printExpr(part.value, .lowest, ExprFlag.None());
25522552
p.print("}");
25532553
if (part.tail.isPresent()) {
2554-
part.tail.resovleRopeIfNeeded(p.options.allocator);
2554+
part.tail.resolveRopeIfNeeded(p.options.allocator);
25552555
p.printStringContent(&part.tail, '`');
25562556
}
25572557
}
@@ -3205,7 +3205,7 @@ fn NewPrinter(
32053205
.e_string => |key| {
32063206
p.addSourceMapping(_key.loc);
32073207
if (key.isUTF8()) {
3208-
key.resovleRopeIfNeeded(p.options.allocator);
3208+
key.resolveRopeIfNeeded(p.options.allocator);
32093209
p.printSpaceBeforeIdentifier();
32103210
var allow_shorthand: bool = true;
32113211
// In react/cjs/react.development.js, there's part of a function like this:
@@ -3453,7 +3453,7 @@ fn NewPrinter(
34533453

34543454
switch (property.key.data) {
34553455
.e_string => |str| {
3456-
str.resovleRopeIfNeeded(p.options.allocator);
3456+
str.resolveRopeIfNeeded(p.options.allocator);
34573457
p.addSourceMapping(property.key.loc);
34583458

34593459
if (str.isUTF8()) {

‎test/bundler/bundler_string.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,56 @@ describe("bundler", () => {
134134
},
135135
);
136136
}
137+
138+
itBundled("string/TemplateFolding", {
139+
files: {
140+
"entry.js": /* js */ `
141+
const s1 = "hello";
142+
\`\${s1} world\`;
143+
console.log(s1);
144+
145+
const s2 = \`hello\`;
146+
console.log(s2);
147+
const s3 = \`\${s2} world \${s1}\`;
148+
console.log(s3);
149+
150+
const s4 = \`\${s1}\${s2}\${s3}\`;
151+
console.log(s4);
152+
153+
const s5 = \`👋🌎\`;
154+
console.log(s5);
155+
const s6 = \`\${s5} 🌍 \${s1}\`;
156+
console.log(s6);
157+
158+
const s7 = \`\${s1}\${s2}\${s3}\${s4}\${s5}\${s6}\`;
159+
console.log(s7);
160+
161+
const hexCharacters = "a-f\\d";
162+
console.log(hexCharacters);
163+
const match3or4Hex = \`#?[\${hexCharacters}]{3}[\${hexCharacters}]?\`;
164+
console.log(match3or4Hex);
165+
const match6or8Hex = \`#?[\${hexCharacters}]{6}([\${hexCharacters}]{2})?\`;
166+
console.log(match6or8Hex);
167+
const nonHexChars = new RegExp(\`[^#\${hexCharacters}]\`, "gi");
168+
console.log(nonHexChars);
169+
const validHexSize = new RegExp(\`^\${match3or4Hex}\$|^\${match6or8Hex}$\`, "i");
170+
console.log(validHexSize);
171+
`,
172+
},
173+
bundling: false,
174+
run: {
175+
stdout: `hello
176+
hello
177+
hello world hello
178+
hellohellohello world hello
179+
👋🌎
180+
👋🌎 🌍 hello
181+
hellohellohello world hellohellohellohello world hello👋🌎👋🌎 🌍 hello
182+
a-f\d
183+
#?[a-f\d]{3}[a-f\d]?
184+
#?[a-f\d]{6}([a-f\d]{2})?
185+
/[^#a-f\d]/gi
186+
/^#?[a-f\d]{3}[a-f\d]?$|^#?[a-f\d]{6}([a-f\d]{2})?$/i`,
187+
},
188+
});
137189
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /