feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -731,25 +731,25 @@ const helpers = exports.default = {
getPrototypeOf: ["body.0.body.body.0.test.right.right.right.callee"]
}
}),
superPropGet: helper("7.25.0", "function _superPropertyGet(t,e,r,o){var p=get(getPrototypeOf(1&o?t.prototype:t),e,r);return 2&o?function(t){return p.apply(r,t)}:p}", {
superPropGet: helper("7.25.0", 'function _superPropGet(t,o,e,r){var p=get(getPrototypeOf(1&r?t.prototype:t),o,e);return 2&r&&"function"==typeof p?function(t){return p.apply(e,t)}:p}', {
globals: [],
locals: {
_superPropertyGet: ["body.0.id"]
_superPropGet: ["body.0.id"]
},
exportBindingAssignments: [],
exportName: "_superPropertyGet",
exportName: "_superPropGet",
dependencies: {
get: ["body.0.body.body.0.declarations.0.init.callee"],
getPrototypeOf: ["body.0.body.body.0.declarations.0.init.arguments.0.callee"]
}
}),
superPropSet: helper("7.25.0", "function _superPropertySet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}", {
superPropSet: helper("7.25.0", "function _superPropSet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}", {
globals: [],
locals: {
_superPropertySet: ["body.0.id"]
_superPropSet: ["body.0.id"]
},
exportBindingAssignments: [],
exportName: "_superPropertySet",
exportName: "_superPropSet",
dependencies: {
set: ["body.0.body.body.0.argument.callee"],
getPrototypeOf: ["body.0.body.body.0.argument.arguments.0.callee"]

File diff suppressed because one or more lines are too long

View File

@@ -3,12 +3,12 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _superPropertyGet;
exports.default = _superPropGet;
var _get = require("./get.js");
var _getPrototypeOf = require("./getPrototypeOf.js");
function _superPropertyGet(classArg, property, receiver, flags) {
function _superPropGet(classArg, property, receiver, flags) {
var result = (0, _get.default)((0, _getPrototypeOf.default)(flags & 1 ? classArg.prototype : classArg), property, receiver);
return flags & 2 ? function (args) {
return flags & 2 && typeof result === "function" ? function (args) {
return result.apply(receiver, args);
} : result;
}

View File

@@ -1 +1 @@
{"version":3,"names":["_get","require","_getPrototypeOf","_superPropertyGet","classArg","property","receiver","flags","result","get","getPrototypeOf","prototype","args","apply"],"sources":["../../src/helpers/superPropGet.ts"],"sourcesContent":["/* @minVersion 7.25.0 */\n\nimport get from \"./get.ts\";\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\n\nconst enum Flags {\n Prototype = 0b1,\n Call = 0b10,\n}\n\nexport default function _superPropertyGet(\n classArg: any,\n property: string,\n receiver: any,\n flags?: number,\n) {\n var result = get(\n getPrototypeOf(\n // @ts-expect-error flags may be undefined\n flags & Flags.Prototype ? classArg.prototype : classArg,\n ),\n property,\n receiver,\n );\n // @ts-expect-error flags may be undefined\n return flags & Flags.Call\n ? function (args: any[]) {\n return result.apply(receiver, args);\n }\n : result;\n}\n"],"mappings":";;;;;;AAEA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAOe,SAASE,iBAAiBA,CACvCC,QAAa,EACbC,QAAgB,EAChBC,QAAa,EACbC,KAAc,EACd;EACA,IAAIC,MAAM,GAAG,IAAAC,YAAG,EACd,IAAAC,uBAAc,EAEZH,KAAK,IAAkB,GAAGH,QAAQ,CAACO,SAAS,GAAGP,QACjD,CAAC,EACDC,QAAQ,EACRC,QACF,CAAC;EAED,OAAOC,KAAK,IAAa,GACrB,UAAUK,IAAW,EAAE;IACrB,OAAOJ,MAAM,CAACK,KAAK,CAACP,QAAQ,EAAEM,IAAI,CAAC;EACrC,CAAC,GACDJ,MAAM;AACZ","ignoreList":[]}
{"version":3,"names":["_get","require","_getPrototypeOf","_superPropGet","classArg","property","receiver","flags","result","get","getPrototypeOf","prototype","args","apply"],"sources":["../../src/helpers/superPropGet.ts"],"sourcesContent":["/* @minVersion 7.25.0 */\n\nimport get from \"./get.ts\";\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\n\nconst enum Flags {\n Prototype = 0b1,\n Call = 0b10,\n}\n\nexport default function _superPropGet(\n classArg: any,\n property: string,\n receiver: any,\n flags?: number,\n) {\n var result = get(\n getPrototypeOf(\n // @ts-expect-error flags may be undefined\n flags & Flags.Prototype ? classArg.prototype : classArg,\n ),\n property,\n receiver,\n );\n // @ts-expect-error flags may be undefined\n return flags & Flags.Call && typeof result === \"function\"\n ? function (args: any[]) {\n return result.apply(receiver, args);\n }\n : result;\n}\n"],"mappings":";;;;;;AAEA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAOe,SAASE,aAAaA,CACnCC,QAAa,EACbC,QAAgB,EAChBC,QAAa,EACbC,KAAc,EACd;EACA,IAAIC,MAAM,GAAG,IAAAC,YAAG,EACd,IAAAC,uBAAc,EAEZH,KAAK,IAAkB,GAAGH,QAAQ,CAACO,SAAS,GAAGP,QACjD,CAAC,EACDC,QAAQ,EACRC,QACF,CAAC;EAED,OAAOC,KAAK,IAAa,IAAI,OAAOC,MAAM,KAAK,UAAU,GACrD,UAAUI,IAAW,EAAE;IACrB,OAAOJ,MAAM,CAACK,KAAK,CAACP,QAAQ,EAAEM,IAAI,CAAC;EACrC,CAAC,GACDJ,MAAM;AACZ","ignoreList":[]}

View File

@@ -3,10 +3,10 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _superPropertySet;
exports.default = _superPropSet;
var _set = require("./set.js");
var _getPrototypeOf = require("./getPrototypeOf.js");
function _superPropertySet(classArg, property, value, receiver, isStrict, prototype) {
function _superPropSet(classArg, property, value, receiver, isStrict, prototype) {
return (0, _set.default)((0, _getPrototypeOf.default)(prototype ? classArg.prototype : classArg), property, value, receiver, isStrict);
}

View File

@@ -1 +1 @@
{"version":3,"names":["_set","require","_getPrototypeOf","_superPropertySet","classArg","property","value","receiver","isStrict","prototype","set","getPrototypeOf"],"sources":["../../src/helpers/superPropSet.ts"],"sourcesContent":["/* @minVersion 7.25.0 */\n\nimport set from \"./set.ts\";\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\n\nexport default function _superPropertySet(\n classArg: any,\n property: string,\n value: any,\n receiver: any,\n isStrict: boolean,\n prototype?: 1,\n) {\n return set(\n getPrototypeOf(prototype ? classArg.prototype : classArg),\n property,\n value,\n receiver,\n isStrict,\n );\n}\n"],"mappings":";;;;;;AAEA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAEe,SAASE,iBAAiBA,CACvCC,QAAa,EACbC,QAAgB,EAChBC,KAAU,EACVC,QAAa,EACbC,QAAiB,EACjBC,SAAa,EACb;EACA,OAAO,IAAAC,YAAG,EACR,IAAAC,uBAAc,EAACF,SAAS,GAAGL,QAAQ,CAACK,SAAS,GAAGL,QAAQ,CAAC,EACzDC,QAAQ,EACRC,KAAK,EACLC,QAAQ,EACRC,QACF,CAAC;AACH","ignoreList":[]}
{"version":3,"names":["_set","require","_getPrototypeOf","_superPropSet","classArg","property","value","receiver","isStrict","prototype","set","getPrototypeOf"],"sources":["../../src/helpers/superPropSet.ts"],"sourcesContent":["/* @minVersion 7.25.0 */\n\nimport set from \"./set.ts\";\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\n\nexport default function _superPropSet(\n classArg: any,\n property: string,\n value: any,\n receiver: any,\n isStrict: boolean,\n prototype?: 1,\n) {\n return set(\n getPrototypeOf(prototype ? classArg.prototype : classArg),\n property,\n value,\n receiver,\n isStrict,\n );\n}\n"],"mappings":";;;;;;AAEA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AAEe,SAASE,aAAaA,CACnCC,QAAa,EACbC,QAAgB,EAChBC,KAAU,EACVC,QAAa,EACbC,QAAiB,EACjBC,SAAa,EACb;EACA,OAAO,IAAAC,YAAG,EACR,IAAAC,uBAAc,EAACF,SAAS,GAAGL,QAAQ,CAACK,SAAS,GAAGL,QAAQ,CAAC,EACzDC,QAAQ,EACRC,KAAK,EACLC,QAAQ,EACRC,QACF,CAAC;AACH","ignoreList":[]}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -15,7 +15,7 @@ function _wrapNativeSuper(Class) {
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache !== undefined) {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}

View File

@@ -1 +1 @@
{"version":3,"names":["_getPrototypeOf","require","_setPrototypeOf","_isNativeFunction","_construct","_wrapNativeSuper","Class","_cache","Map","undefined","exports","default","isNativeFunction","TypeError","has","get","set","Wrapper","construct","arguments","getPrototypeOf","constructor","prototype","Object","create","value","enumerable","writable","configurable","setPrototypeOf"],"sources":["../../src/helpers/wrapNativeSuper.ts"],"sourcesContent":["/* @minVersion 7.0.0-beta.0 */\n\n// Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes\n\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\nimport setPrototypeOf from \"./setPrototypeOf.ts\";\nimport isNativeFunction from \"./isNativeFunction.ts\";\nimport construct from \"./construct.ts\";\n\nexport default function _wrapNativeSuper(Class: Function | null) {\n var _cache = typeof Map === \"function\" ? new Map() : undefined;\n\n // @ts-expect-error -- reuse function id for helper size\n _wrapNativeSuper = function _wrapNativeSuper(Class: Function | null) {\n if (Class === null || !isNativeFunction(Class)) return Class;\n if (typeof Class !== \"function\") {\n throw new TypeError(\"Super expression must either be null or a function\");\n }\n if (typeof _cache !== \"undefined\") {\n if (_cache.has(Class)) return _cache.get(Class);\n _cache.set(Class, Wrapper);\n }\n\n function Wrapper() {\n // @ts-expect-error -- we are sure Class is a function here\n return construct(Class, arguments, getPrototypeOf(this).constructor);\n }\n Wrapper.prototype = Object.create(Class.prototype, {\n constructor: {\n value: Wrapper,\n enumerable: false,\n writable: true,\n configurable: true,\n },\n });\n\n return setPrototypeOf(Wrapper, Class);\n };\n\n return _wrapNativeSuper(Class);\n}\n"],"mappings":";;;;;;AAIA,IAAAA,eAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAEe,SAASI,gBAAgBA,CAACC,KAAsB,EAAE;EAC/D,IAAIC,MAAM,GAAG,OAAOC,GAAG,KAAK,UAAU,GAAG,IAAIA,GAAG,CAAC,CAAC,GAAGC,SAAS;EAG9DC,OAAA,CAAAC,OAAA,GAAAN,gBAAgB,GAAG,SAASA,gBAAgBA,CAACC,KAAsB,EAAE;IACnE,IAAIA,KAAK,KAAK,IAAI,IAAI,CAAC,IAAAM,yBAAgB,EAACN,KAAK,CAAC,EAAE,OAAOA,KAAK;IAC5D,IAAI,OAAOA,KAAK,KAAK,UAAU,EAAE;MAC/B,MAAM,IAAIO,SAAS,CAAC,oDAAoD,CAAC;IAC3E;IACA,IAAI,OAAON,MAAM,KAAK,WAAW,EAAE;MACjC,IAAIA,MAAM,CAACO,GAAG,CAACR,KAAK,CAAC,EAAE,OAAOC,MAAM,CAACQ,GAAG,CAACT,KAAK,CAAC;MAC/CC,MAAM,CAACS,GAAG,CAACV,KAAK,EAAEW,OAAO,CAAC;IAC5B;IAEA,SAASA,OAAOA,CAAA,EAAG;MAEjB,OAAO,IAAAC,kBAAS,EAACZ,KAAK,EAAEa,SAAS,EAAE,IAAAC,uBAAc,EAAC,IAAI,CAAC,CAACC,WAAW,CAAC;IACtE;IACAJ,OAAO,CAACK,SAAS,GAAGC,MAAM,CAACC,MAAM,CAAClB,KAAK,CAACgB,SAAS,EAAE;MACjDD,WAAW,EAAE;QACXI,KAAK,EAAER,OAAO;QACdS,UAAU,EAAE,KAAK;QACjBC,QAAQ,EAAE,IAAI;QACdC,YAAY,EAAE;MAChB;IACF,CAAC,CAAC;IAEF,OAAO,IAAAC,uBAAc,EAACZ,OAAO,EAAEX,KAAK,CAAC;EACvC,CAAC;EAED,OAAOD,gBAAgB,CAACC,KAAK,CAAC;AAChC","ignoreList":[]}
{"version":3,"names":["_getPrototypeOf","require","_setPrototypeOf","_isNativeFunction","_construct","_wrapNativeSuper","Class","_cache","Map","undefined","exports","default","isNativeFunction","TypeError","has","get","set","Wrapper","construct","arguments","getPrototypeOf","constructor","prototype","Object","create","value","enumerable","writable","configurable","setPrototypeOf"],"sources":["../../src/helpers/wrapNativeSuper.ts"],"sourcesContent":["/* @minVersion 7.0.0-beta.0 */\n\n// Based on https://github.com/WebReflection/babel-plugin-transform-builtin-classes\n\nimport getPrototypeOf from \"./getPrototypeOf.ts\";\nimport setPrototypeOf from \"./setPrototypeOf.ts\";\nimport isNativeFunction from \"./isNativeFunction.ts\";\nimport construct from \"./construct.ts\";\n\nexport default function _wrapNativeSuper(Class: Function | null) {\n var _cache = typeof Map === \"function\" ? new Map() : undefined;\n\n // @ts-expect-error -- reuse function id for helper size\n _wrapNativeSuper = function _wrapNativeSuper(Class: Function | null) {\n if (Class === null || !isNativeFunction(Class)) return Class;\n if (typeof Class !== \"function\") {\n throw new TypeError(\"Super expression must either be null or a function\");\n }\n if (_cache !== undefined) {\n if (_cache.has(Class)) return _cache.get(Class);\n _cache.set(Class, Wrapper);\n }\n\n function Wrapper() {\n // @ts-expect-error -- we are sure Class is a function here\n return construct(Class, arguments, getPrototypeOf(this).constructor);\n }\n Wrapper.prototype = Object.create(Class.prototype, {\n constructor: {\n value: Wrapper,\n enumerable: false,\n writable: true,\n configurable: true,\n },\n });\n\n return setPrototypeOf(Wrapper, Class);\n };\n\n return _wrapNativeSuper(Class);\n}\n"],"mappings":";;;;;;AAIA,IAAAA,eAAA,GAAAC,OAAA;AACA,IAAAC,eAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAEe,SAASI,gBAAgBA,CAACC,KAAsB,EAAE;EAC/D,IAAIC,MAAM,GAAG,OAAOC,GAAG,KAAK,UAAU,GAAG,IAAIA,GAAG,CAAC,CAAC,GAAGC,SAAS;EAG9DC,OAAA,CAAAC,OAAA,GAAAN,gBAAgB,GAAG,SAASA,gBAAgBA,CAACC,KAAsB,EAAE;IACnE,IAAIA,KAAK,KAAK,IAAI,IAAI,CAAC,IAAAM,yBAAgB,EAACN,KAAK,CAAC,EAAE,OAAOA,KAAK;IAC5D,IAAI,OAAOA,KAAK,KAAK,UAAU,EAAE;MAC/B,MAAM,IAAIO,SAAS,CAAC,oDAAoD,CAAC;IAC3E;IACA,IAAIN,MAAM,KAAKE,SAAS,EAAE;MACxB,IAAIF,MAAM,CAACO,GAAG,CAACR,KAAK,CAAC,EAAE,OAAOC,MAAM,CAACQ,GAAG,CAACT,KAAK,CAAC;MAC/CC,MAAM,CAACS,GAAG,CAACV,KAAK,EAAEW,OAAO,CAAC;IAC5B;IAEA,SAASA,OAAOA,CAAA,EAAG;MAEjB,OAAO,IAAAC,kBAAS,EAACZ,KAAK,EAAEa,SAAS,EAAE,IAAAC,uBAAc,EAAC,IAAI,CAAC,CAACC,WAAW,CAAC;IACtE;IACAJ,OAAO,CAACK,SAAS,GAAGC,MAAM,CAACC,MAAM,CAAClB,KAAK,CAACgB,SAAS,EAAE;MACjDD,WAAW,EAAE;QACXI,KAAK,EAAER,OAAO;QACdS,UAAU,EAAE,KAAK;QACjBC,QAAQ,EAAE,IAAI;QACdC,YAAY,EAAE;MAChB;IACF,CAAC,CAAC;IAEF,OAAO,IAAAC,uBAAc,EAACZ,OAAO,EAAEX,KAAK,CAAC;EACvC,CAAC;EAED,OAAOD,gBAAgB,CAACC,KAAK,CAAC;AAChC","ignoreList":[]}

View File

@@ -1,6 +1,6 @@
{
"name": "@babel/helpers",
"version": "7.25.0",
"version": "7.26.0",
"description": "Collection of helper functions used by Babel transforms.",
"author": "The Babel Team (https://babel.dev/team)",
"homepage": "https://babel.dev/docs/en/next/babel-helpers",
@@ -15,13 +15,13 @@
},
"main": "./lib/index.js",
"dependencies": {
"@babel/template": "^7.25.0",
"@babel/types": "^7.25.0"
"@babel/template": "^7.25.9",
"@babel/types": "^7.26.0"
},
"devDependencies": {
"@babel/generator": "^7.25.0",
"@babel/helper-plugin-test-runner": "^7.24.7",
"@babel/parser": "^7.25.0",
"@babel/generator": "^7.26.0",
"@babel/helper-plugin-test-runner": "^7.25.9",
"@babel/parser": "^7.26.0",
"regenerator-runtime": "^0.14.0"
},
"engines": {

View File

@@ -1,209 +0,0 @@
// NOTE: This file must be compatible with old Node.js versions, since it runs
// during testing.
/**
* @typedef {Object} HelperMetadata
* @property {string[]} globals
* @property {{ [name: string]: string[] }} locals
* @property {{ [name: string]: string[] }} dependencies
* @property {string[]} exportBindingAssignments
* @property {string} exportName
*/
/**
* Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render
* the helper is whatever context it is needed in.
*
* @param {typeof import("@babel/core")} babel
*
* @returns {HelperMetadata}
*/
export function getHelperMetadata(babel, code, helperName) {
const globals = new Set();
// Maps imported identifier name -> helper name
const dependenciesBindings = new Map();
let exportName;
const exportBindingAssignments = [];
// helper name -> reference paths
const dependencies = new Map();
// local variable name -> reference paths
const locals = new Map();
const spansToRemove = [];
const validateDefaultExport = decl => {
if (exportName) {
throw new Error(
`Helpers can have only one default export (in ${helperName})`
);
}
if (!decl.isFunctionDeclaration() || !decl.node.id) {
throw new Error(
`Helpers can only export named function declarations (in ${helperName})`
);
}
};
/** @type {import("@babel/traverse").Visitor} */
const dependencyVisitor = {
Program(path) {
for (const child of path.get("body")) {
if (child.isImportDeclaration()) {
if (
child.get("specifiers").length !== 1 ||
!child.get("specifiers.0").isImportDefaultSpecifier()
) {
throw new Error(
`Helpers can only import a default value (in ${helperName})`
);
}
dependenciesBindings.set(
child.node.specifiers[0].local.name,
child.node.source.value
);
dependencies.set(child.node.source.value, []);
spansToRemove.push([child.node.start, child.node.end]);
child.remove();
}
}
for (const child of path.get("body")) {
if (child.isExportDefaultDeclaration()) {
const decl = child.get("declaration");
validateDefaultExport(decl);
exportName = decl.node.id.name;
spansToRemove.push([child.node.start, decl.node.start]);
child.replaceWith(decl.node);
} else if (
child.isExportNamedDeclaration() &&
child.node.specifiers.length === 1 &&
child.get("specifiers.0.exported").isIdentifier({ name: "default" })
) {
const { name } = child.node.specifiers[0].local;
validateDefaultExport(child.scope.getBinding(name).path);
exportName = name;
spansToRemove.push([child.node.start, child.node.end]);
child.remove();
} else if (
process.env.IS_BABEL_OLD_E2E &&
child.isExportNamedDeclaration() &&
child.node.specifiers.length === 0
) {
spansToRemove.push([child.node.start, child.node.end]);
child.remove();
} else if (
child.isExportAllDeclaration() ||
child.isExportNamedDeclaration()
) {
throw new Error(`Helpers can only export default (in ${helperName})`);
}
}
path.scope.crawl();
const bindings = path.scope.getAllBindings();
Object.keys(bindings).forEach(name => {
if (dependencies.has(name)) return;
const binding = bindings[name];
const references = [
...binding.path.getBindingIdentifierPaths(true)[name].map(makePath),
...binding.referencePaths.map(makePath),
];
for (const violation of binding.constantViolations) {
violation.getBindingIdentifierPaths(true)[name].forEach(path => {
references.push(makePath(path));
});
}
locals.set(name, references);
});
},
ReferencedIdentifier(child) {
const name = child.node.name;
const binding = child.scope.getBinding(name);
if (!binding) {
if (dependenciesBindings.has(name)) {
dependencies
.get(dependenciesBindings.get(name))
.push(makePath(child));
} else if (name !== "arguments" || child.scope.path.isProgram()) {
globals.add(name);
}
}
},
AssignmentExpression(child) {
const left = child.get("left");
if (!(exportName in left.getBindingIdentifiers())) return;
if (!left.isIdentifier()) {
throw new Error(
`Only simple assignments to exports are allowed in helpers (in ${helperName})`
);
}
const binding = child.scope.getBinding(exportName);
if (binding && binding.scope.path.isProgram()) {
exportBindingAssignments.push(makePath(child));
}
},
};
babel.transformSync(code, {
configFile: false,
babelrc: false,
plugins: [() => ({ visitor: dependencyVisitor })],
});
if (!exportName) throw new Error("Helpers must have a named default export.");
// Process these in reverse so that mutating the references does not invalidate any later paths in
// the list.
exportBindingAssignments.reverse();
spansToRemove.sort(([start1], [start2]) => start2 - start1);
for (const [start, end] of spansToRemove) {
code = code.slice(0, start) + code.slice(end);
}
return [
code,
{
globals: Array.from(globals),
locals: Object.fromEntries(locals),
dependencies: Object.fromEntries(dependencies),
exportBindingAssignments,
exportName,
},
];
}
function makePath(path) {
const parts = [];
for (; path.parentPath; path = path.parentPath) {
parts.push(path.key);
if (path.inList) parts.push(path.listKey);
}
return parts.reverse().join(".");
}
export function stringifyMetadata(metadata) {
return `\
{
globals: ${JSON.stringify(metadata.globals)},
locals: ${JSON.stringify(metadata.locals)},
exportBindingAssignments: ${JSON.stringify(metadata.exportBindingAssignments)},
exportName: ${JSON.stringify(metadata.exportName)},
dependencies: ${JSON.stringify(metadata.dependencies)},
}
`;
}

View File

@@ -1,207 +0,0 @@
/* eslint-disable import/no-extraneous-dependencies */
import fs from "fs";
import { join } from "path";
import { URL, fileURLToPath } from "url";
import { minify } from "terser";
import { babel, presetTypescript } from "$repo-utils/babel-top-level";
import { IS_BABEL_8 } from "$repo-utils";
import { gzipSync } from "zlib";
import {
getHelperMetadata,
stringifyMetadata,
} from "./build-helper-metadata.js";
const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
const IGNORED_FILES = new Set(["package.json", "tsconfig.json"]);
export default async function generateHelpers() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'yarn gulp generate-runtime-helpers'
*/
import template from "@babel/template";
import type * as t from "@babel/types";
interface Helper {
minVersion: string;
ast: () => t.Program;
metadata: HelperMetadata;
}
export interface HelperMetadata {
globals: string[];
locals: { [name: string]: string[] };
dependencies: { [name: string]: string[] };
exportBindingAssignments: string[];
exportName: string;
}
function helper(minVersion: string, source: string, metadata: HelperMetadata): Helper {
return Object.freeze({
minVersion,
ast: () => template.program.ast(source, { preserveComments: true }),
metadata,
})
}
export { helpers as default };
const helpers: Record<string, Helper> = {
__proto__: null,
`;
let babel7extraOutput = "";
for (const file of (await fs.promises.readdir(HELPERS_FOLDER)).sort()) {
if (IGNORED_FILES.has(file)) continue;
if (file.startsWith(".")) continue; // ignore e.g. vim swap files
const [helperName] = file.split(".");
const isTs = file.endsWith(".ts");
const filePath = join(fileURLToPath(HELPERS_FOLDER), file);
if (!file.endsWith(".js") && !isTs) {
console.error("ignoring", filePath);
continue;
}
let code = await fs.promises.readFile(filePath, "utf8");
const minVersionMatch = code.match(
/^\s*\/\*\s*@minVersion\s+(?<minVersion>\S+)\s*\*\/\s*$/m
);
if (!minVersionMatch) {
throw new Error(`@minVersion number missing in ${filePath}`);
}
const { minVersion } = minVersionMatch.groups;
const onlyBabel7 = code.includes("@onlyBabel7");
const mangleFns = code.includes("@mangleFns");
const noMangleFns = [];
code = babel.transformSync(code, {
configFile: false,
babelrc: false,
filename: filePath,
presets: [
[
presetTypescript,
{
onlyRemoveTypeImports: true,
optimizeConstEnums: true,
},
],
],
plugins: [
/**
* @type {import("@babel/core").PluginObj}
*/
({ types: t }) => ({
// These pre/post hooks are needed because the TS transform is,
// when building in the old Babel e2e test, removing the
// `export { OverloadYield as default }` in the OverloadYield helper.
// TODO: Remove in Babel 8.
pre(file) {
if (!process.env.IS_BABEL_OLD_E2E) return;
file.metadata.exportName = null;
file.path.traverse({
ExportSpecifier(path) {
if (path.node.exported.name === "default") {
file.metadata.exportName = path.node.local.name;
}
},
});
},
post(file) {
if (!process.env.IS_BABEL_OLD_E2E) return;
if (!file.metadata.exportName) return;
file.path.traverse({
ExportNamedDeclaration(path) {
if (
!path.node.declaration &&
path.node.specifiers.length === 0
) {
path.node.specifiers.push(
t.exportSpecifier(
t.identifier(file.metadata.exportName),
t.identifier("default")
)
);
}
},
});
},
visitor: {
ImportDeclaration(path) {
const source = path.node.source;
source.value = source.value
.replace(/\.ts$/, "")
.replace(/^\.\//, "");
},
FunctionDeclaration(path) {
if (
mangleFns &&
path.node.leadingComments?.find(c =>
c.value.includes("@no-mangle")
)
) {
const name = path.node.id.name;
if (name) noMangleFns.push(name);
}
},
},
}),
],
}).code;
code = (
await minify(code, {
ecma: 5,
mangle: {
keep_fnames: mangleFns ? new RegExp(noMangleFns.join("|")) : true,
},
// The _typeof helper has a custom directive that we must keep
compress: {
directives: false,
passes: 10,
unsafe: true,
unsafe_proto: true,
},
})
).code;
let metadata;
// eslint-disable-next-line prefer-const
[code, metadata] = getHelperMetadata(babel, code, helperName);
const helperStr = `\
// size: ${code.length}, gzip size: ${gzipSync(code).length}
${JSON.stringify(helperName)}: helper(
${JSON.stringify(minVersion)},
${JSON.stringify(code)},
${stringifyMetadata(metadata)}
),
`;
if (onlyBabel7) {
if (!IS_BABEL_8()) babel7extraOutput += helperStr;
} else {
output += helperStr;
}
}
output += "};";
if (babel7extraOutput) {
output += `
if (!process.env.BABEL_8_BREAKING) {
Object.assign(helpers, {
${babel7extraOutput}
});
}
`;
}
return output;
}

View File

@@ -1,61 +0,0 @@
import fs from "fs";
import { createRequire } from "module";
const [parse, generate] = await Promise.all([
import("@babel/parser").then(ns => ns.parse),
import("@babel/generator").then(ns => ns.default.default || ns.default),
]).catch(error => {
console.error(error);
throw new Error(
"Before running generate-helpers.js you must compile @babel/parser and @babel/generator.",
{ cause: error }
);
});
const REGENERATOR_RUNTIME_IN_FILE = fs.readFileSync(
createRequire(import.meta.url).resolve("regenerator-runtime"),
"utf8"
);
const MIN_VERSION = "7.18.0";
const HEADER = `/* @minVersion ${MIN_VERSION} */
/*
* This file is auto-generated! Do not modify it directly.
* To re-generate, update the regenerator-runtime dependency of
* @babel/helpers and run 'yarn gulp generate-runtime-helpers'.
*/
/* eslint-disable */
`;
const COPYRIGHT = `/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */`;
export default function generateRegeneratorRuntimeHelper() {
const ast = parse(REGENERATOR_RUNTIME_IN_FILE, { sourceType: "script" });
const factoryFunction = ast.program.body[0].declarations[0].init.callee;
factoryFunction.type = "FunctionDeclaration";
factoryFunction.id = { type: "Identifier", name: "_regeneratorRuntime" };
factoryFunction.params = [];
factoryFunction.body.body.unshift(
...stmts(`
${COPYRIGHT}
_regeneratorRuntime = function () { return exports; };
var exports = {};
`)
);
const { code } = generate({
type: "ExportDefaultDeclaration",
declaration: factoryFunction,
});
return HEADER + code;
}
function stmts(code) {
return parse(`function _() { ${code} }`, {
sourceType: "script",
}).program.body[0].body.body;
}