Repository: babel/babel
File: packages/babel-helpers/scripts/generate-helpers.js (JavaScript)

Code before:

/**
 * Generate Babel plugin process helpers
 * The helper will
 * - rewrite relative imports to other helpers and replace .ts to .js
 * - collect
 * @param {boolean} mangleFns Whether the function names in the helper should be mangled
 * @param {string[]} noMangleFns A list of function names that should not be mangled
 * @returns
 */
function BabelPluginProcessHelpersFactory(mangleFns, noMangleFns) {
  /**
   *
   * @param {import("@babel/core").PluginAPI} api
   * @returns {import("@babel/core").PluginObject}
   */
  return function BabelPluginProcessHelpers({ types: t }) {
    /**
     * @type {import("@babel/core").PluginObject}
     */
    const pluginObj = {
      pre: undefined,
      post: undefined,
      visitor: {
        ImportDeclaration(path) {
          const source = path.node.source;
          source.value = source.value.replace(/(^\.\/|\.ts$)/g, "");
        },
        FunctionDeclaration: mangleFns
          ? path => {
              if (
                path.node.leadingComments?.find(c =>
                  c.value.includes("@no-mangle")
                )
              ) {
                const name = path.node.id.name;
                if (name) noMangleFns.push(name);
              }
            }
          : () => {},
      },
    };
    if (!IS_BABEL_8() && process.env.IS_BABEL_OLD_E2E) {
      // 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.
      pluginObj.pre = file => {
        file.metadata.exportName = null;
        file.path.traverse({
          ExportSpecifier(path) {
            if (path.node.exported.name === "default") {

Review comment (suggestion):
This regex is revised from
```js
source.value = source.value
  .replace(/\.ts$/, "")
  .replace(/^\.\//, "");
```

Code after:

/**
 * Generate Babel plugin for helpers processing
 * The plugin will
 * - rewrite relative imports to other helpers and remove .ts extension
 * - collect function names to `noMangleFns` if `mangleFns` is `true`.
 * @param {boolean} mangleFns Whether the function names in the helper should be mangled
 * @param {string[]} noMangleFns A list of function names that should not be mangled
 * @returns
 */
function BabelPluginProcessHelpersFactory(mangleFns, noMangleFns) {
  /**
   * @param {import("@babel/core").PluginAPI} api
   * @returns {import("@babel/core").PluginObject}
   */
  return function BabelPluginProcessHelpers({ types: t }) {
    /**
     * @type {import("@babel/core").PluginObject}
     */
    const pluginObj = {
      pre: undefined,
      post: undefined,
      visitor: {
        ImportDeclaration(path) {
          const source = path.node.source;
          source.value = source.value.replace(/(^\.\/|\.ts$)/g, "");
        },
        FunctionDeclaration: mangleFns
          ? path => {
              if (
                path.node.leadingComments?.find(c =>
                  c.value.includes("@no-mangle")
                )
              ) {
                const name = path.node.id.name;
                if (name) noMangleFns.push(name);
              }
            }
          : () => {},
      },
    };
    if (!IS_BABEL_8() && process.env.IS_BABEL_OLD_E2E) {
      // 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.
      pluginObj.pre = file => {
        file.metadata.exportName = null;
        file.path.traverse({
          ExportSpecifier(path) {
            if (path.node.exported.name === "default") {
              file.metadata.exportName = path.node.local.name;