MCPcopy Create free account
hub / github.com/babel/babel / pluginGeneratorOptimization

Function pluginGeneratorOptimization

babel.config.ts:577–643  ·  view source on GitHub ↗
({ types: t }: PluginAPI)

Source from the content-addressed store, hash-verified

575}
576
577function pluginGeneratorOptimization({ types: t }: PluginAPI) {
578 const generatorNames: string[] = [];
579 fs.globSync(
580 pathUtils.join(__dirname, "./packages/babel-generator/src/generators/*.ts")
581 ).forEach(file => {
582 if (file.endsWith("deprecated.ts")) return;
583 const content = fs.readFileSync(file, "utf8");
584 const ast = parseSync(content, {
585 configFile: false,
586 parserOpts: { plugins: ["typescript"] },
587 });
588 t.traverseFast(ast, node => {
589 let name;
590 if (t.isExportSpecifier(node)) {
591 name = t.isStringLiteral(node.exported)
592 ? node.exported.value
593 : node.exported.name;
594 } else if (
595 t.isExportNamedDeclaration(node) &&
596 t.isFunctionDeclaration(node.declaration)
597 ) {
598 name = node.declaration.id!.name;
599 }
600 if (name && !name.startsWith("_")) generatorNames.push(name);
601 });
602 });
603 generatorNames.sort();
604 return {
605 visitor: {
606 CallExpression: {
607 exit(path) {
608 const { callee, arguments: args } = path.node;
609 if (t.isIdentifier(callee) && callee.name === "__node") {
610 if (!t.isStringLiteral(args[0])) {
611 throw new Error("Expected StringLiteral");
612 }
613 const type = args[0].value;
614 const generatorIndex = generatorNames.indexOf(type);
615 if (generatorIndex === -1) {
616 throw path.buildCodeFrameError(`Unknown generator type: ${type}`);
617 }
618 path.replaceWith(t.numericLiteral(generatorIndex));
619 return;
620 }
621
622 if (
623 t.isMemberExpression(callee) &&
624 t.isIdentifier(callee.property) &&
625 t.isThisExpression(callee.object)
626 ) {
627 if (
628 callee.property.name === "token" &&
629 args.length === 1 &&
630 t.isStringLiteral(args[0])
631 ) {
632 const str = args[0].value;
633 if (str.length === 1) {
634 callee.property.name = "tokenChar";

Callers

nothing calls this directly

Calls 7

parseSyncFunction · 0.90
isExportSpecifierMethod · 0.80
isStringLiteralMethod · 0.80
isFunctionDeclarationMethod · 0.80
endsWithMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected