How to use the babel-plugin-macros.MacroError function in babel-plugin-macros

To help you get started, weโ€™ve selected a few babel-plugin-macros examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dsherret / ts-nameof / packages / ts-nameof.macro / src / index.js View on Github external
function getPath() {
            const parentPath = path.parentPath; // identifier;
            if (parentPath.type === "CallExpression")
                return parentPath;
            const grandParentPath = parentPath.parentPath;
            if (parentPath.type === "MemberExpression" && grandParentPath.type === "CallExpression")
                return grandParentPath;
            throw new MacroError("[ts-nameof]: Could not find a call expression at path: " + grandParentPath.getSource());
        }
    });
github planttheidea / inline-loops.macro / src / inline-loops.macro.js View on Github external
}
      const args = path.parent.arguments;

      if (args.some(arg => t.isSpreadElement(arg))) {
        throw new MacroError(
          'You cannot use spread arguments with the macro, please declare the arguments explicitly.',
        );
      }

      const [object, handler, initialValue] = args;
      const isHandlerMacro = allMethods.find(
        ({ path: methodPath }) => methodPath.node !== path.node && handler === methodPath.node,
      );

      if (isHandlerMacro) {
        throw new MacroError(
          'You cannot use the macro directly as a handler, please wrap it in a function call.',
        );
      }

      transform({
        t,
        path,
        object,
        handler,
        initialValue,
        isDecrementing,
        isObject,
      });
    };
  }
github planttheidea / inline-loops.macro / src / inline-loops.macro.js View on Github external
return function _transform(path) {
      if (path.findParent(_path => _path.isConditionalExpression())) {
        throw new MacroError(`You cannot use ${name} in a conditional expression.`);
      }
      const args = path.parent.arguments;

      if (args.some(arg => t.isSpreadElement(arg))) {
        throw new MacroError(
          'You cannot use spread arguments with the macro, please declare the arguments explicitly.',
        );
      }

      const [object, handler, initialValue] = args;
      const isHandlerMacro = allMethods.find(
        ({ path: methodPath }) => methodPath.node !== path.node && handler === methodPath.node,
      );

      if (isHandlerMacro) {
        throw new MacroError(
github suchipi / run-on-server / packages / transform / src / macro.js View on Github external
"\n" +
          oneLine`
            Saving the result of ${createClientFunctionName} to a variable
            is the only supported way to use the run-on-server macro.
          ` +
          "\n" +
          stripIndent`
            For example, try:
          ` +
          `\n  const runOnServer = ${createClientFunctionName}("http://somewhere:3000");`
      );
    }

    const id = declarator.get("id");
    if (!id.isIdentifier()) {
      throw new MacroError(
        oneLine`
          The result of calling ${createClientFunctionName} was saved to a
          variable, but that variable was created in an unexpected way.
        ` +
          "\n" +
          getCodeFrame(id.node) +
          "\n" +
          oneLine`
            The only variable declaration forms supported by
            the run-on-server macro are:
          ` +
          "\n  " +
          stripIndent`
              const runOnServer = ${createClientFunctionName}("http://somewhere:3000");
            OR
              let runOnServer = ${createClientFunctionName}("http://somewhere:3000");
github indico / js-flask-urls / packages / flask-urls.macro / src / flask-urls.macro.js View on Github external
const macro = ({babel: {types: t, template}, config: localConfig, references, state}) => {
  const config = {...defaultConfig, ...localConfig};

  if (!references.default) {
    throw new MacroError('flask-urls.macro requires a default import');
  } else if (Object.keys(references).length !== 1) {
    throw new MacroError('flask-urls.macro only supports a default import');
  } else if (references.default.length === 0) {
    throw new MacroError('flask-urls.macro is imported but not used');
  }

  const buildFunc = template.expression('FUNC.bind(null, RULE, BASE)');
  const buildFuncMock = template.expression('FUNC.bind(null, ENDPOINT)');

  let builderFuncId;
  references.default.forEach(({parentPath}) => {
    if (parentPath.type !== 'TaggedTemplateExpression') {
      throw new MacroError('flask-urls.macro only supports tagged template expressions');
    }

    const quasi = parentPath.node.quasi;
github indico / js-flask-urls / packages / flask-urls.macro / src / flask-urls.macro.js View on Github external
references.default.forEach(({parentPath}) => {
    if (parentPath.type !== 'TaggedTemplateExpression') {
      throw new MacroError('flask-urls.macro only supports tagged template expressions');
    }

    const quasi = parentPath.node.quasi;
    if (quasi.expressions.length) {
      throw new MacroError('flask-url.macro cannot contain expressions');
    }

    const endpoint = quasi.quasis[0].value.cooked;

    if (builderFuncId) {
      builderFuncId = t.cloneDeep(builderFuncId);
    }

    let replacement;
    if (config.mock) {
      if (!builderFuncId) {
github vhfmag / tsguard.macro / src / macro.js View on Github external
function typeGuardMacro({ references, state, babel: { types }, source }) {
  for (const path of references.default) {
    const callExpression = path.parent;

    if (
      !t.isCallExpression(callExpression) ||
      callExpression.typeParameters.params.length !== 1
    ) {
      throw new MacroError(
        "Macro should be called as a function and passed exactly one type parameter",
      );
    }

    const macroArgument = callExpression.arguments[0];

    if (!t.isIdentifier(macroArgument)) {
      throw new MacroError(
        "For now, this macro works with identifiers only, sorry",
      );
    }

    const generatedImport = addNamespace(path, "generic-type-guard");

    if (!t.isIdentifier(generatedImport)) {
      throw new MacroError("Something wrong happened at our side, sorry");
github indico / js-flask-urls / packages / flask-urls.macro / src / flask-urls.macro.js View on Github external
const macro = ({babel: {types: t, template}, config: localConfig, references, state}) => {
  const config = {...defaultConfig, ...localConfig};

  if (!references.default) {
    throw new MacroError('flask-urls.macro requires a default import');
  } else if (Object.keys(references).length !== 1) {
    throw new MacroError('flask-urls.macro only supports a default import');
  } else if (references.default.length === 0) {
    throw new MacroError('flask-urls.macro is imported but not used');
  }

  const buildFunc = template.expression('FUNC.bind(null, RULE, BASE)');
  const buildFuncMock = template.expression('FUNC.bind(null, ENDPOINT)');

  let builderFuncId;
  references.default.forEach(({parentPath}) => {
    if (parentPath.type !== 'TaggedTemplateExpression') {
      throw new MacroError('flask-urls.macro only supports tagged template expressions');
    }

    const quasi = parentPath.node.quasi;
    if (quasi.expressions.length) {
      throw new MacroError('flask-url.macro cannot contain expressions');
github cometkim / react-hot-reload.macro / index.js View on Github external
function macro({ babel, references, state }) {
    if (!references.default) {
        throw new MacroError('react-hot-reload.macro requires a default import')
    }

    const { types: t } = babel
    const { node: referenceNode, scope } = references.default[0]

    if (process.env.NODE_ENV === 'production') {
        state.file.path.traverse({
            CallExpression(path) {
                if (path.node.callee.name !== referenceNode.name) {
                    return
                }

                path.replaceWith(t.identifier(
                    path.node.arguments[0].name,
                ))
            },
github vhfmag / tsguard.macro / src / macro.js View on Github external
"Macro should be called as a function and passed exactly one type parameter",
      );
    }

    const macroArgument = callExpression.arguments[0];

    if (!t.isIdentifier(macroArgument)) {
      throw new MacroError(
        "For now, this macro works with identifiers only, sorry",
      );
    }

    const generatedImport = addNamespace(path, "generic-type-guard");

    if (!t.isIdentifier(generatedImport)) {
      throw new MacroError("Something wrong happened at our side, sorry");
    }

    const expectedType = callExpression.typeParameters.params[0];
    const identifier = macroArgument.name;
    const generatedCode = `(${typeToPartialGuard(
      expectedType,
      generatedImport.name,
    )})(${identifier})`;
    const generatedAst = parser.parse(generatedCode);
    path.parentPath.replaceWith(generatedAst.program.body[0]);
  }
}

babel-plugin-macros

Allows you to build compile-time libraries

MIT
Latest version published 3 years ago

Package Health Score

79 / 100
Full package analysis