All files / babel-plugin/component-macro index.js

87.5% Statements 21/24
50% Branches 3/6
100% Functions 2/2
87.5% Lines 21/24

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 541x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 16x 16x       16x 16x       16x 16x     1x                     16x 16x     16x                      
const babel = require("@babel/core");
 
const { COMPONENT_MACRO } = require("../constants");
const defineMacro = require("../define-macro");
const findPathToReplace = require("./find-path-to-replace");
const parseComponent = require("./parse-component");
const parseDefaults = require("./parse-defaults");
const parseEmits = require("./parse-emits");
const parseProps = require("./parse-props");
const renderComponent = require("./render-component");
const parseVariableName = require("./parse-variable-name");
 
const componentMacro = defineMacro(COMPONENT_MACRO, (path) => {
  const parsed = parse(path);
  Iif (!parsed) {
    return false;
  }
 
  const component = renderComponent(parsed.pathToReplace, parsed.options);
  Iif (!component) {
    return false;
  }
 
  parsed.pathToReplace.replaceWith(component);
  return true;
});
 
module.exports = componentMacro;
 
/**
 *
 * @param {babel.NodePath<babel.types.CallExpression>} path
 * @returns {null | {
 *  pathToReplace: babel.NodePath,
 *  options: Parameters<typeof renderComponent>[1]
 * }}
 */
function parse(path) {
  const pathToReplace = findPathToReplace(path);
  Iif (!pathToReplace) {
    return null;
  }
  return {
    pathToReplace,
    options: {
      props: parseProps(path),
      emits: parseEmits(path),
      defaultProps: parseDefaults(path),
      component: parseComponent(path),
      variableName: parseVariableName(path),
    },
  };
}