Skip to content

Commit

Permalink
fix: systemjs re-traverses helpers (#16225)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Jan 22, 2024
1 parent eccbd20 commit dfa4cc6
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile.js

Large diffs are not rendered by default.

Expand Up @@ -68,7 +68,7 @@ const cachedScripts = new LruCache<
{ code: string; cachedData?: Buffer }
>({ max: 10 });
const contextModuleCache = new WeakMap();
const sharedTestContext = createContext();
const sharedTestContext = createTestContext();

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

function createContext() {
export function createTestContext() {
const context = vm.createContext({
...helpers,
process: process,
Expand Down Expand Up @@ -302,7 +302,7 @@ async function run(task: Test) {
let resultExec;

if (execCode) {
const context = createContext();
const context = createTestContext();
const execOpts = getOpts(exec);

// Ignore Babel logs of exec.js files.
Expand Down
Expand Up @@ -3,7 +3,7 @@ System.register([], function (_export, _context) {

return {
setters: [],
execute: function () {
execute: function execute() {
_export("default", function (_a) {
return {
a: function a() {
Expand Down
Expand Up @@ -26,7 +26,9 @@
},
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^",
"@babel/helper-transform-fixture-test-runner": "workspace:^",
"core-js": "^3.35.0"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-systemjs",
"engines": {
Expand Down
Expand Up @@ -709,6 +709,7 @@ export default declare<PluginState>((api, options: Options) => {
CONTEXT_IDENTIFIER: t.identifier(contextIdent),
}),
];
path.requeue(path.get("body.0"));
},
},
},
Expand Down
@@ -0,0 +1,39 @@
// Ref: https://github.com/babel/babel/issues/16219

// delete global.Symbol doesn't work with jest in node 6
Object.defineProperty(global, "Symbol", {
configurable: true,
writable: true,
value: undefined
});

require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.to-primitive");

const symbol = Symbol("test");

// Use eval to not let Babel transform this `typeof`
expect(eval('typeof symbol')).toBe("object");
expect(typeof symbol).toBe("symbol");

function objectWithPrimitive(prim) {
return { [Symbol.toPrimitive]: () => prim }
}

expect(() => {
class AxiosHeaders {
[objectWithPrimitive(symbol)]() {
return;
}
}
}).not.toThrow()

expect(() => {
class AxiosHeaders {
[objectWithPrimitive({})]() {
return;
}
}
}).toThrow()

return "ok";
@@ -0,0 +1,15 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
},
"presets": [
[
"env",
{
"targets": ["chrome 22", "last 12 years"],
"modules": "systemjs"
}
]
],
"externalHelpers": false
}
@@ -0,0 +1,68 @@
System.register([], function (_export, _context) {
"use strict";

var symbol;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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); }
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); }
function objectWithPrimitive(prim) {
return _defineProperty({}, Symbol.toPrimitive, function () {
return prim;
});
}
return {
setters: [],
execute: function execute() {
// Ref: https://github.com/babel/babel/issues/16219

// delete global.Symbol doesn't work with jest in node 6
Object.defineProperty(global, "Symbol", {
configurable: true,
writable: true,
value: undefined
});
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.to-primitive");
symbol = Symbol("test"); // Use eval to not let Babel transform this `typeof`
expect(eval('typeof symbol')).toBe("object");
expect(_typeof(symbol)).toBe("symbol");
expect(function () {
var AxiosHeaders = /*#__PURE__*/function (_objectWithPrimitive) {
"use strict";

function AxiosHeaders() {
_classCallCheck(this, AxiosHeaders);
}
_createClass(AxiosHeaders, [{
key: _objectWithPrimitive,
value: function value() {
return;
}
}]);
return AxiosHeaders;
}(objectWithPrimitive(symbol));
}).not.toThrow();
expect(function () {
var AxiosHeaders = /*#__PURE__*/function (_objectWithPrimitive2) {
"use strict";

function AxiosHeaders() {
_classCallCheck(this, AxiosHeaders);
}
_createClass(AxiosHeaders, [{
key: _objectWithPrimitive2,
value: function value() {
return;
}
}]);
return AxiosHeaders;
}(objectWithPrimitive({}));
}).toThrow();
return "ok";
}
};
});
33 changes: 33 additions & 0 deletions packages/babel-plugin-transform-modules-systemjs/test/index.js
@@ -1,3 +1,36 @@
import runner from "@babel/helper-plugin-test-runner";
import { readFileSync } from "fs";
import { commonJS } from "$repo-utils";
import path from "path";
import {
runCodeInTestContext,
createTestContext,
} from "@babel/helper-transform-fixture-test-runner";

const { __dirname } = commonJS(import.meta.url);

runner(import.meta.url);

describe("systemjs exec", function () {
// https://github.com/babel/babel/issues/16219
it("should requeue helpers", function () {
const filename = path.join(
__dirname,
"fixtures/preset-env/requeue-helpers/output.js",
);
const content = readFileSync(filename, "utf8");

let res;

const context = createTestContext();
context.System = {
register: function (_, module) {
res = module().execute();
},
};

runCodeInTestContext(content, { filename }, context);

expect(res).toBe("ok");
});
});
Expand Up @@ -3,7 +3,7 @@ System.register([], function (_export, _context) {

return {
setters: [],
execute: function () {
execute: function execute() {
_context["import"]("foo");
}
};
Expand Down
Expand Up @@ -3,7 +3,7 @@ System.register([], function (_export, _context) {

return {
setters: [],
execute: function () {
execute: function execute() {
_context["import"]("foo");
}
};
Expand Down
Expand Up @@ -4,8 +4,8 @@ System.register(["a"], function (_export, _context) {
var a;
return {
setters: [function (_a) {
a = _a.default;
a = _a["default"];
}],
execute: function () {}
execute: function execute() {}
};
});
10 changes: 6 additions & 4 deletions yarn.lock
Expand Up @@ -2977,7 +2977,9 @@ __metadata:
"@babel/helper-module-transforms": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-transform-fixture-test-runner": "workspace:^"
"@babel/helper-validator-identifier": "workspace:^"
core-js: "npm:^3.35.0"
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down Expand Up @@ -8095,10 +8097,10 @@ __metadata:
languageName: node
linkType: hard

"core-js@npm:^3.30.2, core-js@npm:^3.31.1":
version: 3.31.1
resolution: "core-js@npm:3.31.1"
checksum: f3b0b424f9ba02c6235c8739651943f06823e7ec3e02e0f819490a05f15a08cadc3a011853947466bececb6e7f4987f826142293fc4bc2ea11216e839579a257
"core-js@npm:^3.30.2, core-js@npm:^3.31.1, core-js@npm:^3.35.0":
version: 3.35.0
resolution: "core-js@npm:3.35.0"
checksum: 0815fce6bcc91d79d4b28885975453b0faa4d17fc2230635102b4f3832cd621035e4032aa3307e1dbe0ee14d5e34bcb64b507fd89bd8f567aedaf29538522e6a
languageName: node
linkType: hard

Expand Down

0 comments on commit dfa4cc6

Please sign in to comment.