All files index.js

100% Statements 35/35
100% Branches 36/36
100% Functions 5/5
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    14x             29x   29x 11x     18x 18x   18x   18x 7x     11x       11x       2x     9x             39x 39x 39x   39x 4x 4x     35x 4x 4x       31x 31x     31x 4x 1x   4x     27x 12x                                     45x     45x 54x 30x   54x        
 
export default function () {
  return {
    name: 'transform-remove-imports',
    visitor: {
 
      // https://babeljs.io/docs/en/babel-types#callexpression
      CallExpression(path, state) {
 
        const { node } = path;
 
        if (node.callee.name !== 'require') {
          return;
        }
 
        const argument = node.arguments[0];
        const moduleId = argument.value;
 
        const options = state.opts;
 
        if (options.test && !testMatches(moduleId, options.test)) {
          return;
        }
 
        const parentType = path.parentPath.node.type;
 
        // In remove effects mode we should delete only requires that are
        // simple expression statements
        if (
          options.remove === 'effects' &&
          parentType !== 'ExpressionStatement'
        ) {
          return;
        }
 
        path.remove();
 
      },
 
      // https://babeljs.io/docs/en/babel-types#importdeclaration
      ImportDeclaration(path, state) {
 
        const { node } = path;
        const { source } = node;
        const { opts } = state;
 
        if (opts.removeAll) {
          path.remove();
          return;
        }
 
        if (!opts.test) {
          console.warn('transform-remove-imports: "test" option should be specified');
          return;
        }
 
        /** @var {string} importName */
        const importName = (source && source.value ? source.value : undefined);
        const isMatch = testMatches(importName, opts.test);
 
        // https://github.com/uiwjs/babel-plugin-transform-remove-imports/issues/3
        if (opts.remove === 'effects') {
          if (node.specifiers && node.specifiers.length === 0 && importName && isMatch) {
            path.remove();
          }
          return;
        }
 
        if (importName && isMatch) {
          path.remove();
        }
 
      },
    }
  };
}
 
 
/**
 * Determines if the import matches the specified tests.
 *
 * @param {string} importName
 * @param {RegExp|RegExp[]|string|string[]} test
 * @returns {Boolean}
 */
function testMatches(importName, test) {
 
  // Normalizing tests
  const tests = Array.isArray(test) ? test : [test];
 
  // Finding out if at least one test matches
  return tests.some(regex => {
    if (typeof regex === 'string') {
      regex = new RegExp(regex);
    }
    return regex.test(importName || '');
  });
 
}