Skip to content

Commit f36e4b6

Browse files
authored
refactor(es/helpers): Generate inline helpers from canonical ESM sources (#12006)
**Description:** Refactor inline SWC helpers to use `packages/helpers/esm` as the canonical source and generate Rust helper artifacts from it. Key points: - Removes the duplicated hand-written inline helper JS files. - Keeps generated Rust artifacts checked in, so crate builds do not need Node, pnpm, or repo-relative helper sources. - Uses one generated Rust file per helper to keep diffs and ownership local. - Precomputes inline dependency closures during codegen, so runtime injection only ORs bitsets. - Uses `u128` for helper bitsets because the helper set is small, fixed-order, dependency-free, and cache-friendly. - Normalizes helper import/emit order to the generated table order (`packages/helpers/esm/index.js`, currently lexicographic). Inline deps used to expand during helper registration; this is safe because helpers are hoisted function declarations and codegen rejects order-sensitive preludes.
1 parent dd43ad6 commit f36e4b6

493 files changed

Lines changed: 6323 additions & 2487 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/CI.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ jobs:
4343

4444
- run: cargo fmt --all -- --check
4545

46+
helper-codegen:
47+
name: Helper codegen
48+
runs-on: ubuntu-latest
49+
permissions:
50+
contents: read
51+
steps:
52+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
53+
with:
54+
persist-credentials: false
55+
56+
- uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
57+
with:
58+
toolchain: nightly-2026-04-10
59+
components: rustfmt
60+
61+
- run: cargo codegen helpers --check
62+
4663
cargo-clippy:
4764
name: Cargo clippy
4865
if: >-

bindings/binding_core_wasm/__tests__/simple.js

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,35 @@ describe("transform", () => {
55
const output = swc.transformSync("class Foo {}", {});
66

77
expect(output).toMatchInlineSnapshot(`
8-
{
9-
"code": "function _class_call_check(instance, Constructor) {
10-
if (!(instance instanceof Constructor)) {
11-
throw new TypeError("Cannot call a class as a function");
12-
}
13-
}
14-
var Foo = function Foo() {
15-
"use strict";
16-
_class_call_check(this, Foo);
17-
};
18-
",
19-
"diagnostics": [],
20-
}
8+
{
9+
"code": "function _class_call_check(instance, Constructor) {
10+
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
11+
}
12+
var Foo = function Foo() {
13+
"use strict";
14+
_class_call_check(this, Foo);
15+
};
16+
",
17+
"diagnostics": [],
18+
}
2119
`);
2220
});
2321

2422
it("should work with async facade", async () => {
2523
const output = await swc.transform("class Foo {}", {});
2624

2725
expect(output).toMatchInlineSnapshot(`
28-
{
29-
"code": "function _class_call_check(instance, Constructor) {
30-
if (!(instance instanceof Constructor)) {
31-
throw new TypeError("Cannot call a class as a function");
32-
}
33-
}
34-
var Foo = function Foo() {
35-
"use strict";
36-
_class_call_check(this, Foo);
37-
};
38-
",
39-
"diagnostics": [],
40-
}
26+
{
27+
"code": "function _class_call_check(instance, Constructor) {
28+
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
29+
}
30+
var Foo = function Foo() {
31+
"use strict";
32+
_class_call_check(this, Foo);
33+
};
34+
",
35+
"diagnostics": [],
36+
}
4137
`);
4238
});
4339

@@ -49,19 +45,17 @@ describe("transform", () => {
4945

5046
const output = await swc.transform(input, {});
5147
expect(output).toMatchInlineSnapshot(`
52-
{
53-
"code": "function _class_call_check(instance, Constructor) {
54-
if (!(instance instanceof Constructor)) {
55-
throw new TypeError("Cannot call a class as a function");
56-
}
57-
}
58-
var Foo = function Foo() {
59-
"use strict";
60-
_class_call_check(this, Foo);
61-
};
62-
",
63-
"diagnostics": [],
64-
}
48+
{
49+
"code": "function _class_call_check(instance, Constructor) {
50+
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
51+
}
52+
var Foo = function Foo() {
53+
"use strict";
54+
_class_call_check(this, Foo);
55+
};
56+
",
57+
"diagnostics": [],
58+
}
6559
`);
6660
});
6761

crates/swc/tests/fixture/issues-11xxx/11046/es5/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { _ as _type_of } from "@swc/helpers/_/_type_of";
21
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
32
import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
3+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
44
function traverse(obj) {
55
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, key, err;
66
return _ts_generator(this, function(_state) {

crates/swc/tests/fixture/issues-1xxx/1721/case1/async-generator/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
21
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
2+
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
33
function lol() {
44
return _wrap_async_generator(function() {
55
return _ts_generator(this, function(_state) {

crates/swc/tests/fixture/issues-1xxx/1721/case1/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { _ as _async_iterator } from "@swc/helpers/_/_async_iterator";
22
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
3-
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
43
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4+
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
55
function lol() {
66
return _wrap_async_generator(function() {
77
return _ts_generator(this, function(_state) {

crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
22
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
33
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4-
import { _ as _type_of } from "@swc/helpers/_/_type_of";
54
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
65
import { _ as _ts_values } from "@swc/helpers/_/_ts_values";
6+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
77
import { Transaction } from "@solana/web3.js";
88
import { WalletNotConnectedError } from "@solana/wallet-adapter-base";
99
export var getErrorForTransaction = function getErrorForTransaction(connection, txid) {

crates/swc/tests/fixture/issues-5xxx/5859/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
22
import { _ as _await_async_generator } from "@swc/helpers/_/_await_async_generator";
3-
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
43
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4+
import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator";
55
var result = [];
66
function foo() {
77
return _wrap_async_generator(function() {

crates/swc/tests/fixture/issues-6xxx/6727/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
2-
var _type_of = require("@swc/helpers/_/_type_of");
32
var _ts_generator = require("@swc/helpers/_/_ts_generator");
3+
var _type_of = require("@swc/helpers/_/_type_of");
44
require("core-js/modules/es.object.to-string.js");
55
require("core-js/modules/es.promise.js");
66
function a() {

crates/swc/tests/fixture/issues-7xxx/7220/1/output/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { _ as _apply_decs_2203_r } from "@swc/helpers/_/_apply_decs_2203_r";
12
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
23
import { _ as _create_class } from "@swc/helpers/_/_create_class";
34
import { _ as _define_property } from "@swc/helpers/_/_define_property";
45
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5-
import { _ as _apply_decs_2203_r } from "@swc/helpers/_/_apply_decs_2203_r";
66
var _dec, _initProto;
77
var _BOUND_FUNCTIONS = {};
88
export var BackgroundJob = function BackgroundJob() {

crates/swc/tests/stacktrace/issue-622/output/stacks.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ $DIR/tests/stacktrace/issue-622/input/index.js:3
55

66
ReferenceError: call is not defined
77
at foo ($DIR/tests/stacktrace/issue-622/input/index.js:3:9)
8-
at step ($DIR/tests/stacktrace/issue-622/input/_exec.js:113:23)
9-
at Object.<anonymous> ($DIR/tests/stacktrace/issue-622/input/_exec.js:54:20)
8+
at step ($DIR/tests/stacktrace/issue-622/input/_exec.js:110:23)
9+
at Object.<anonymous> ($DIR/tests/stacktrace/issue-622/input/_exec.js:51:20)
1010
at asyncGeneratorStep ($DIR/tests/stacktrace/issue-622/input/_exec.js:4:28)
11-
at _next ($DIR/tests/stacktrace/issue-622/input/_exec.js:22:17)
12-
at $DIR/tests/stacktrace/issue-622/input/_exec.js:27:13
11+
at _next ($DIR/tests/stacktrace/issue-622/input/_exec.js:19:17)
12+
at $DIR/tests/stacktrace/issue-622/input/_exec.js:24:13
1313
at new Promise (<anonymous>)
14-
at $DIR/tests/stacktrace/issue-622/input/_exec.js:19:16
14+
at $DIR/tests/stacktrace/issue-622/input/_exec.js:16:16
1515
at foo ($DIR/tests/stacktrace/issue-622/input/index.js:6:1)
1616
at Object.<anonymous> ($DIR/tests/stacktrace/issue-622/input/index.js:9:1)

0 commit comments

Comments
 (0)