Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Gulpfile.mts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ function buildRollup(packages: PackageInfo[], buildStandalone?: boolean) {
preferBuiltins: !buildStandalone,
}),
rollupJson(),
src === "packages/babel-parser" &&
["packages/babel-parser", "packages/babel-generator"].includes(
src
) &&
getBabelOutputPlugin({
configFile: false,
babelrc: false,
Expand Down Expand Up @@ -517,14 +519,19 @@ function buildRollup(packages: PackageInfo[], buildStandalone?: boolean) {
!vals.has(node.property.name)
) {
all = false;
return;
}
parentPath.replaceWith(
t.numericLiteral(vals.get(node.property.name))
);
});

if (all) path.remove();
if (all) {
binding.referencePaths.forEach(({ parentPath }) => {
const { node } = parentPath;
parentPath.replaceWith(
// @ts-expect-error checked above
t.numericLiteral(vals.get(node.property.name))
);
});
path.remove();
}
},
},
};
Expand Down
51 changes: 43 additions & 8 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ module.exports = function (api) {
plugins: ["babel-plugin-transform-charcodes"],
},
{
test: ["packages/babel-generator"].map(normalize),
test: ["packages/babel-generator/src"].map(normalize),
plugins: [pluginGeneratorOptimization],
},
{
Expand Down Expand Up @@ -549,25 +549,60 @@ function pluginBabelParserTokenType({
* @returns {import("@babel/core").PluginObject}
*/
function pluginGeneratorOptimization({ types: t }) {
const generatorNames = [];
fs.globSync(
pathUtils.join(__dirname, "./packages/babel-generator/src/generators/*.ts")
).forEach(file => {
if (file.endsWith("deprecated.ts")) return;
const content = fs.readFileSync(file, "utf8");
const ast = parseSync(content, {
configFile: false,
parserOpts: { plugins: ["typescript"] },
});
t.traverseFast(ast, node => {
let name;
if (t.isExportSpecifier(node)) {
name = t.isStringLiteral(node.exported)
? node.exported.value
: node.exported.name;
} else if (
t.isExportNamedDeclaration(node) &&
t.isFunctionDeclaration(node.declaration)
) {
name = node.declaration.id.name;
}
if (name && !name.startsWith("_")) generatorNames.push(name);
});
});
generatorNames.sort();
return {
visitor: {
CallExpression: {
exit(path) {
const node = path.node;
const { callee, arguments: args } = path.node;
if (t.isIdentifier(callee) && callee.name === "__node") {
t.assertStringLiteral(args[0]);
const type = args[0].value;
const generatorIndex = generatorNames.indexOf(type);
if (generatorIndex === -1) {
throw path.buildCodeFrameError(`Unknown generator type: ${type}`);
}
path.replaceWith(t.numericLiteral(generatorIndex));
return;
}

if (
t.isMemberExpression(node.callee) &&
t.isThisExpression(node.callee.object)
t.isMemberExpression(callee) &&
t.isThisExpression(callee.object)
) {
const args = node.arguments;

if (
node.callee.property.name === "token" &&
callee.property.name === "token" &&
args.length === 1 &&
t.isStringLiteral(args[0])
) {
const str = args[0].value;
if (str.length === 1) {
node.callee.property.name = "tokenChar";
callee.property.name = "tokenChar";
args[0] = t.numericLiteral(str.charCodeAt(0));
}
}
Expand Down
Loading