Skip to content

Commit dfa4cc6

Browse files
fix: systemjs re-traverses helpers (#16225)
Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
1 parent eccbd20 commit dfa4cc6

13 files changed

Lines changed: 174 additions & 14 deletions

File tree

‎Makefile.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/babel-helper-transform-fixture-test-runner/src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const cachedScripts = new LruCache<
6868
{ code: string; cachedData?: Buffer }
6969
>({ max: 10 });
7070
const contextModuleCache = new WeakMap();
71-
const sharedTestContext = createContext();
71+
const sharedTestContext = createTestContext();
7272

7373
// We never want our tests to accidentally load the root
7474
// babel.config.js file, so we disable config loading by
@@ -89,7 +89,7 @@ function transformAsyncWithoutConfigFile(code: string, opts: InputOptions) {
8989
});
9090
}
9191

92-
function createContext() {
92+
export function createTestContext() {
9393
const context = vm.createContext({
9494
...helpers,
9595
process: process,
@@ -302,7 +302,7 @@ async function run(task: Test) {
302302
let resultExec;
303303

304304
if (execCode) {
305-
const context = createContext();
305+
const context = createTestContext();
306306
const execOpts = getOpts(exec);
307307

308308
// Ignore Babel logs of exec.js files.

‎packages/babel-plugin-transform-function-name/test/fixtures/function-name/export-default-arrow-renaming-module-system/output.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ System.register([], function (_export, _context) {
33

44
return {
55
setters: [],
6-
execute: function () {
6+
execute: function execute() {
77
_export("default", function (_a) {
88
return {
99
a: function a() {

‎packages/babel-plugin-transform-modules-systemjs/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
},
2727
"devDependencies": {
2828
"@babel/core": "workspace:^",
29-
"@babel/helper-plugin-test-runner": "workspace:^"
29+
"@babel/helper-plugin-test-runner": "workspace:^",
30+
"@babel/helper-transform-fixture-test-runner": "workspace:^",
31+
"core-js": "^3.35.0"
3032
},
3133
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-systemjs",
3234
"engines": {

‎packages/babel-plugin-transform-modules-systemjs/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ export default declare<PluginState>((api, options: Options) => {
709709
CONTEXT_IDENTIFIER: t.identifier(contextIdent),
710710
}),
711711
];
712+
path.requeue(path.get("body.0"));
712713
},
713714
},
714715
},
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Ref: https://github.com/babel/babel/issues/16219
2+
3+
// delete global.Symbol doesn't work with jest in node 6
4+
Object.defineProperty(global, "Symbol", {
5+
configurable: true,
6+
writable: true,
7+
value: undefined
8+
});
9+
10+
require("core-js/modules/es.symbol.js");
11+
require("core-js/modules/es.symbol.to-primitive");
12+
13+
const symbol = Symbol("test");
14+
15+
// Use eval to not let Babel transform this `typeof`
16+
expect(eval('typeof symbol')).toBe("object");
17+
expect(typeof symbol).toBe("symbol");
18+
19+
function objectWithPrimitive(prim) {
20+
return { [Symbol.toPrimitive]: () => prim }
21+
}
22+
23+
expect(() => {
24+
class AxiosHeaders {
25+
[objectWithPrimitive(symbol)]() {
26+
return;
27+
}
28+
}
29+
}).not.toThrow()
30+
31+
expect(() => {
32+
class AxiosHeaders {
33+
[objectWithPrimitive({})]() {
34+
return;
35+
}
36+
}
37+
}).toThrow()
38+
39+
return "ok";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"parserOpts": {
3+
"allowReturnOutsideFunction": true
4+
},
5+
"presets": [
6+
[
7+
"env",
8+
{
9+
"targets": ["chrome 22", "last 12 years"],
10+
"modules": "systemjs"
11+
}
12+
]
13+
],
14+
"externalHelpers": false
15+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
System.register([], function (_export, _context) {
2+
"use strict";
3+
4+
var symbol;
5+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
7+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
8+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
10+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
11+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12+
function objectWithPrimitive(prim) {
13+
return _defineProperty({}, Symbol.toPrimitive, function () {
14+
return prim;
15+
});
16+
}
17+
return {
18+
setters: [],
19+
execute: function execute() {
20+
// Ref: https://github.com/babel/babel/issues/16219
21+
22+
// delete global.Symbol doesn't work with jest in node 6
23+
Object.defineProperty(global, "Symbol", {
24+
configurable: true,
25+
writable: true,
26+
value: undefined
27+
});
28+
require("core-js/modules/es.symbol.js");
29+
require("core-js/modules/es.symbol.to-primitive");
30+
symbol = Symbol("test"); // Use eval to not let Babel transform this `typeof`
31+
expect(eval('typeof symbol')).toBe("object");
32+
expect(_typeof(symbol)).toBe("symbol");
33+
expect(function () {
34+
var AxiosHeaders = /*#__PURE__*/function (_objectWithPrimitive) {
35+
"use strict";
36+
37+
function AxiosHeaders() {
38+
_classCallCheck(this, AxiosHeaders);
39+
}
40+
_createClass(AxiosHeaders, [{
41+
key: _objectWithPrimitive,
42+
value: function value() {
43+
return;
44+
}
45+
}]);
46+
return AxiosHeaders;
47+
}(objectWithPrimitive(symbol));
48+
}).not.toThrow();
49+
expect(function () {
50+
var AxiosHeaders = /*#__PURE__*/function (_objectWithPrimitive2) {
51+
"use strict";
52+
53+
function AxiosHeaders() {
54+
_classCallCheck(this, AxiosHeaders);
55+
}
56+
_createClass(AxiosHeaders, [{
57+
key: _objectWithPrimitive2,
58+
value: function value() {
59+
return;
60+
}
61+
}]);
62+
return AxiosHeaders;
63+
}(objectWithPrimitive({}));
64+
}).toThrow();
65+
return "ok";
66+
}
67+
};
68+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
11
import runner from "@babel/helper-plugin-test-runner";
2+
import { readFileSync } from "fs";
3+
import { commonJS } from "$repo-utils";
4+
import path from "path";
5+
import {
6+
runCodeInTestContext,
7+
createTestContext,
8+
} from "@babel/helper-transform-fixture-test-runner";
9+
10+
const { __dirname } = commonJS(import.meta.url);
211

312
runner(import.meta.url);
13+
14+
describe("systemjs exec", function () {
15+
// https://github.com/babel/babel/issues/16219
16+
it("should requeue helpers", function () {
17+
const filename = path.join(
18+
__dirname,
19+
"fixtures/preset-env/requeue-helpers/output.js",
20+
);
21+
const content = readFileSync(filename, "utf8");
22+
23+
let res;
24+
25+
const context = createTestContext();
26+
context.System = {
27+
register: function (_, module) {
28+
res = module().execute();
29+
},
30+
};
31+
32+
runCodeInTestContext(content, { filename }, context);
33+
34+
expect(res).toBe("ok");
35+
});
36+
});

‎packages/babel-preset-env/test/fixtures/dynamic-import-babel-7/modules-systemjs/output.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ System.register([], function (_export, _context) {
33

44
return {
55
setters: [],
6-
execute: function () {
6+
execute: function execute() {
77
_context["import"]("foo");
88
}
99
};

0 commit comments

Comments
 (0)