Skip to content

Commit

Permalink
'let' statements before using statements are now properly converted i…
Browse files Browse the repository at this point in the history
…nto 'var' statements (oven-sh#14260)
  • Loading branch information
snoglobe authored Oct 10, 2024
1 parent def454d commit ff47631
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19430,6 +19430,7 @@ fn NewParser_(
}
}

data.kind = kind;
try stmts.append(stmt.*);

if (p.options.features.react_fast_refresh and p.current_scope == p.module_scope) {
Expand Down Expand Up @@ -22155,29 +22156,37 @@ fn NewParser_(
switch (stmt.data) {
.s_empty, .s_comment, .s_directive, .s_debugger, .s_type_script => continue,
.s_local => |local| {
if (!local.is_export and local.kind == .k_const and !local.was_commonjs_export) {
if (!local.is_export and !local.was_commonjs_export) {
var decls: []Decl = local.decls.slice();
var end: usize = 0;
var any_decl_in_const_values = local.kind == .k_const;
for (decls) |decl| {
if (decl.binding.data == .b_identifier) {
const symbol = p.symbols.items[decl.binding.data.b_identifier.ref.innerIndex()];
if (p.const_values.contains(decl.binding.data.b_identifier.ref) and symbol.use_count_estimate == 0) {
continue;
if (p.const_values.contains(decl.binding.data.b_identifier.ref)) {
any_decl_in_const_values = true;
const symbol = p.symbols.items[decl.binding.data.b_identifier.ref.innerIndex()];
if (symbol.use_count_estimate == 0) {
// Skip declarations that are constants with zero usage
continue;
}
}
}
decls[end] = decl;
end += 1;
}
local.decls.len = @as(u32, @truncate(end));
if (end == 0) {
stmt.* = stmt.*.toEmpty();
if (any_decl_in_const_values) {
if (end == 0) {
stmt.* = stmt.*.toEmpty();
}
continue;
}
continue;
}
},
else => {},
}

// Break after processing relevant statements
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ describe("bundler", () => {
snapshotSourceMap: {
"entry.js.map": {
files: ["../node_modules/react/index.js", "../entry.js"],
mappingsExactMatch: "uYACA,WAAW,IAAQ,EAAE,ICDrB,eACA,QAAQ,IAAI,CAAK",
mappingsExactMatch: "qYACA,WAAW,IAAQ,EAAE,ICDrB,eACA,QAAQ,IAAI,CAAK",
},
},
});
Expand Down Expand Up @@ -1883,6 +1883,7 @@ describe("bundler", () => {
target: "browser",
run: { stdout: `123` },
});

itBundled("edgecase/UninitializedVariablesMoved", {
files: {
"/entry.ts": `
Expand Down
14 changes: 7 additions & 7 deletions test/bundler/bundler_npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ describe("bundler", () => {
"../entry.tsx",
],
mappings: [
["react.development.js:524:'getContextName'", "1:5428:Y1"],
["react.development.js:2495:'actScopeDepth'", "1:26053:GJ++"],
["react.development.js:696:''Component'", '1:7490:\'Component "%s"'],
["entry.tsx:6:'\"Content-Type\"'", '1:221655:"Content-Type"'],
["entry.tsx:11:'<html>'", "1:221911:void"],
["entry.tsx:23:'await'", "1:222013:await"],
["react.development.js:524:'getContextName'", "1:5426:Y1"],
["react.development.js:2495:'actScopeDepth'", "1:26051:GJ++"],
["react.development.js:696:''Component'", '1:7488:\'Component "%s"'],
["entry.tsx:6:'\"Content-Type\"'", '1:221651:"Content-Type"'],
["entry.tsx:11:'<html>'", "1:221905:void"],
["entry.tsx:23:'await'", "1:222005:await"],
],
},
},
expectExactFilesize: {
"out/entry.js": 222283,
"out/entry.js": 222273,
},
run: {
stdout: "<!DOCTYPE html><html><body><h1>Hello World</h1><p>This is an example.</p></body></html>",
Expand Down

0 comments on commit ff47631

Please sign in to comment.