Read only
    function transform(file, { j }, options) {
      const root = j(file.source);
      let dirtyFlag = false;
    
      const { identifier } = removeImport("array.prototype.entries", root, j);
    
      root
        .find(j.CallExpression, {
          callee: { name: identifier },
        })
        .forEach((path) => {
          const { arguments: args } = path.node;
    
          // Ensure the call expression has exactly one argument
          if (args.length === 1) {
            const arg = args[0];
    
            // Check if the argument is an array expression or an identifier
            if (j.ArrayExpression.check(arg) || j.Identifier.check(arg)) {
              // Replace the call expression with a method call on the argument
              j(path).replaceWith(
                j.callExpression(
                  j.memberExpression(arg, j.identifier("entries")),
                  []
                )
              );
              dirtyFlag = true;
            }
          }
        });
    
      return dirtyFlag ? root.toSource(options) : file.source;
    }
    
    export default transform;
    
    function removeImport(name, root, j) {
      // Find the import or require statement for 'is-boolean-object'
      const importDeclaration = root.find(j.ImportDeclaration, {
        source: {
          value: name,
        },
      });
    
      const requireDeclaration = root
        .find(j.CallExpression, {
          callee: {
            name: "require",
          },
          arguments: [
            {
              value: name,
            },
          ],
        })
        .closest(j.VariableDeclarator);
    
      // Require statements without declarations like `Object.is = require("object-is");`
      const requireAssignment = root.find(j.AssignmentExpression, {
        operator: "=",
        right: {
          callee: {
            name: "require",
          },
          arguments: [
            {
              value: name,
            },
          ],
        },
      });
    
      // Side effect requires statements like `require("error-cause/auto");`
      const sideEffectRequireExpression = root.find(j.ExpressionStatement, {
        expression: {
          callee: {
            name: "require",
          },
          arguments: [
            {
              value: name,
            },
          ],
        },
      });
    
      // Return the identifier name, e.g. 'fn' in `import { fn } from 'is-boolean-object'`
      // or `var fn = require('is-boolean-object')`
      const identifier =
        importDeclaration.paths().length > 0
          ? importDeclaration.get().node.specifiers[0].local.name
          : requireDeclaration.paths().length > 0
          ? requireDeclaration.find(j.Identifier).get().node.name
          : requireAssignment.paths().length > 0
          ? requireAssignment.find(j.Identifier).get().node.name
          : null;
    
      importDeclaration.remove();
      requireDeclaration.remove();
      requireAssignment.remove();
      sideEffectRequireExpression.remove();
    
      const dirtyFlag =
        importDeclaration.length > 0 ||
        requireDeclaration.length > 0 ||
        requireAssignment.length > 0 ||
        sideEffectRequireExpression.length > 0;
    
      return { identifier, dirtyFlag };
    }
    
    
    Input
    var entries = require('array.prototype.entries');
    var assert = require('assert');
    var iterate = require('iterate-iterator');
    
    assert.deepStrictEqual(iterate(entries([1, 2, 3])), [[0, 1], [1, 2], [2, 3]]);
    assert.deepStrictEqual(iterate(entries([1, 0, 1])), [[0, 1], [1, 0], [2, 1]]);
    assert.deepStrictEqual(iterate(entries([NaN])), [[0, NaN]]);
    assert.deepStrictEqual(iterate(entries([1,,3])), [[0, 1], [1, undefined], [2, 3]]);
    Output
    loading
    Read-only
    Open on CodeSandboxOpen Sandbox