feat: initial commit

This commit is contained in:
Carlos
2024-08-12 22:57:35 -04:00
commit 6f68ae8259
13140 changed files with 1104801 additions and 0 deletions

View File

@ -0,0 +1,209 @@
// 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)},
}
`;
}

207
node_modules/@babel/helpers/scripts/generate-helpers.js generated vendored Normal file
View File

@ -0,0 +1,207 @@
/* 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

@ -0,0 +1,61 @@
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;
}