Repository: swc-project/swc
File: packages/helpers/scripts/ast_grep.js (JavaScript)

Code before:
            const func_name = func.text();
            if (func_name !== filename) {
                report_export_mismatch(tree.filename(), match);
            }

            edits.push(
                match.replace(`exports._ = ${func_name};`),
            );

            // since we match the { export x as _ } pattern,
            // we need to find the assignment expression from the root
            root_node
                .findAll({
                    rule: {
                        pattern: func_name,
                        kind: "identifier",
                        inside: { kind: "assignment_expression", field: "left" },
                    },
                })
                .forEach((node) => {
                    edits.push(
                        prepend(node, `exports._ = `),
                    );
                });
        } else {
            report_noexport(tree.filename(tree.filename()));
        }

        // rewrite import
        root_node
            .findAll({ rule: { pattern: `import { _ as $BINDING } from "$SOURCE"` } })
            .forEach((match) => {
                const import_binding = match.getMatch("BINDING").text();
                const import_source = match.getMatch("SOURCE").text();

                const import_basename = path.basename(import_source, ".js");

                if (import_binding !== import_basename) {
                    report_import_mismatch(tree.filename(), match);
                }

                edits.push(
                    match.replace(`var ${import_binding} = require("./${import_binding}.cjs");`),
                );

                root_node
                    .findAll({
                        rule: {
                            pattern: import_binding,
                            kind: "identifier",
                            inside: {

Review comment (bug):
The function call `tree.filename(tree.filename())` is incorrect. It should be `tree.filename()` without passing itself as an argument. This appears to be a typo introduced during the refactoring.
```suggestion
            report_noexport(tree.filename());
```

Code after:
            const func_name = func.text();
            if (func_name !== filename) {
                report_export_mismatch(tree.filename(), match);
            }

            edits.push(
                match.replace(`exports._ = ${func_name};`),
            );

            // since we match the { export x as _ } pattern,
            // we need to find the assignment expression from the root
            root_node
                .findAll({
                    rule: {
                        pattern: func_name,
                        kind: "identifier",
                        inside: { kind: "assignment_expression", field: "left" },
                    },
                })
                .forEach((node) => {
                    edits.push(
                        prepend(node, `exports._ = `),
                    );
                });
        } else {
            report_noexport(tree.filename());
        }

        // rewrite import
        root_node
            .findAll({ rule: { pattern: `import { _ as $BINDING } from "$SOURCE"` } })
            .forEach((match) => {
                const import_binding = match.getMatch("BINDING").text();
                const import_source = match.getMatch("SOURCE").text();

                const import_basename = path.basename(import_source, ".js");

                if (import_binding !== import_basename) {
                    report_import_mismatch(tree.filename(), match);
                }

                edits.push(
                    match.replace(`var ${import_binding} = require("./${import_binding}.cjs");`),
                );

                root_node
                    .findAll({
                        rule: {
                            pattern: import_binding,
                            kind: "identifier",
                            inside: {