All files / babel-plugin index.js

85.29% Statements 29/34
71.42% Branches 20/28
100% Functions 4/4
85.29% Lines 29/34

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 871x   1x   1x 1x 1x   1x   1x 1x 1x   1x             7x   6x   6x 8x       8x   12x       8x       8x       8x       8x 18x 18x                 6x 8x         8x     8x         8x     6x     6x              
const babel = require("@babel/core");
 
const { MACRO_IMPORT } = require("./constants");
 
const componentMacro = require("./component-macro");
const defaultPropsMacro = require("./default-props-macro");
const useRenderMacro = require("./use-render");
 
const t = babel.types;
 
module.exports = function babelPluginFeMacros() {
  const macros = [componentMacro, useRenderMacro, defaultPropsMacro];
  const macroImport = MACRO_IMPORT;
 
  return {
    name: "babel-plugin-fe-macros",
    /**
     * @type {babel.Visitor}
     */
    visitor: {
      ImportDeclaration(path) {
        if (path.node.source.value === macroImport) {
          /** @type {Map<string, number>} */
          const macroCountMap = new Map();
 
          for (const specifier of path.node.specifiers) {
            Iif (!t.isImportSpecifier(specifier)) {
              continue;
            }
 
            const macro = macros.find(
              (macro) =>
                t.isIdentifier(specifier.imported) &&
                specifier.imported.name === macro.name
            );
 
            Iif (!macro) {
              continue;
            }
 
            const referencePaths = path.scope.getBinding(
              specifier.local.name
            )?.referencePaths;
 
            Iif (!referencePaths) {
              continue;
            }
 
            for (const path of referencePaths) {
              Eif (path.isIdentifier() && path.parentPath.isCallExpression()) {
                macro?.transform(path.parentPath) &&
                  macroCountMap.set(
                    macro.name,
                    1 + (macroCountMap.get(macro.name) || 0)
                  );
              }
            }
          }
 
          const specifiers = path.node.specifiers.filter((specifier) => {
            Iif (!t.isImportSpecifier(specifier)) {
              return true;
            }
 
            const initialCount =
              path.scope.getBinding(specifier.local.name)?.references || 0;
 
            const macroCount =
              (t.isIdentifier(specifier.imported) &&
                specifier.imported.name &&
                macroCountMap.get(specifier.imported.name)) ||
              0;
 
            return initialCount > macroCount;
          });
 
          Iif (specifiers.length > 0) {
            path.node.specifiers = specifiers;
          } else {
            path.remove();
          }
        }
      },
    },
  };
};