Read only
    function transform(file, { j }, options) {
      const root = j(file.source);
    
      const { identifier } = removeImport('arraybuffer.prototype.slice', root, j);
    
      let dirtyFlag = false;
    
      root
        .find(j.CallExpression, {
          callee: {
            type: 'Identifier',
            name: identifier
          }
        })
        .forEach((path) => {
          const [bufferArg, ...otherArgs] = path.node.arguments;
          if (
            j.Identifier.check(bufferArg) ||
            (j.NewExpression.check(bufferArg) &&
              bufferArg.callee.type === 'Identifier' &&
              bufferArg.callee.name === 'ArrayBuffer')
          ) {
            path.replace(
              j.callExpression(
                j.memberExpression(bufferArg, j.identifier('slice')),
                otherArgs
              )
            );
            dirtyFlag = true;
          }
        });
    
      return dirtyFlag ? root.toSource(options) : file.source;
    }
    
    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 };
    }
    
    
    export default transform;
    
    
    Input
    var assert = require('assert');
    var slice = require('arraybuffer.prototype.slice');
    
    var ab = new ArrayBuffer(1);
    var arr = new Uint8Array(ab);
    arr[0] = 123;
    
    var ab2 = slice(ab);
    
    var arr2 = new Uint8Array(ab2);
    arr2[0] = 234;
    
    assert.deepEqual(arr, new Uint8Array([123]));
    assert.deepEqual(arr2, new Uint8Array([234]));
    
    if (!ArrayBuffer.prototype.transfer) {
    	slice.shim();
    }
    
    var ab2 = ab.slice();
    
    var arr2 = new Uint8Array(ab2);
    arr2[0] = 234;
    
    assert.deepEqual(arr, new Uint8Array([123]));
    assert.deepEqual(arr2, new Uint8Array([234]));
    Output
    loading
    Read-only
    Open on CodeSandboxOpen Sandbox