How to use the babel-plugin-macros.createMacro 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 kentcdodds / babel-plugin-codegen / src / macro.js View on Github external
// const printAST = require('ast-pretty-print')
const {createMacro} = require('babel-plugin-macros')
const getReplacers = require('./replace')

module.exports = createMacro(codegenMacros)

function codegenMacros({references, state, babel}) {
  const {asIdentifier} = getReplacers(babel)
  const fileOpts = state.file.opts
  references.default.forEach(referencePath => {
    if (asIdentifier(referencePath, fileOpts) === false) {
      throw referencePath.buildCodeFrameError(
        'codegen macro must be used as a tagged template literal, function, jsx, or .require call',
        Error,
      )
    }
  })
}
github kentcdodds / import-all.macro / src / macro.js View on Github external
const path = require('path')
// const printAST = require('ast-pretty-print')
const {createMacro} = require('babel-plugin-macros')
const glob = require('glob')

module.exports = createMacro(prevalMacros)

function prevalMacros({references, state, babel}) {
  references.default.forEach(referencePath => {
    if (referencePath.parentPath.type === 'CallExpression') {
      asyncVersion({referencePath, state, babel})
    } else if (
      referencePath.parentPath.type === 'MemberExpression' &&
      referencePath.parentPath.node.property.name === 'sync'
    ) {
      syncVersion({referencePath, state, babel})
    } else if (
      referencePath.parentPath.type === 'MemberExpression' &&
      referencePath.parentPath.node.property.name === 'deferred'
    ) {
      deferredVersion({referencePath, state, babel})
    } else {
github jaredLunde / react-broker / src / macro / index.js View on Github external
import path from 'path'
import {createMacro} from 'babel-plugin-macros'

const pkgName = 'react-broker'
export default createMacro(evaluateMacros)

// cycles through each call to the macro and determines an output for each
function evaluateMacros({references, state, babel}) {
  references.default.forEach(referencePath =>
    makeLegacyEnsure({
      state,
      babel,
      referencePath,
    })
  )
}

// if Broker is defined in the scope then it will use that Broker, otherwise
// it requires the module.
function makeLegacyEnsure({state, babel, referencePath}) {
  const brokerTemplate = babel.template.smart(
github bohdanbirdie / react-css-modules.macro / src / macro.js View on Github external
stylesArgument,
          marcoConfig.warning
            ? t.booleanLiteral(true)
            : t.booleanLiteral(false),
        ]),
      ),
    ]);

    firstImportDeclarationNode.insertBefore(helperImportDeclaration);
    firstNonImportDeclarationNode.insertBefore(bindedStylesDeclaration);

    programPath.traverse(visitor(t, getStyleNameIdentifier, marcoConfig));
  });
};

export default createMacro(myMacro, { configName: "reactCssModulesMacro" });
github theKashey / react-imported-component / src / macro.ts View on Github external
throw new Error(
    'you have used `react-imported-component/macro` without `babel-plugin-macro` or `react-hot-loader/babel` installed'
  );
};

const lazy: typeof import('./HOC').lazy = neverCallMe;
const imported: typeof import('./HOC').default = neverCallMe;
const importedModule: typeof import('./Module').importedModule = neverCallMe;
const useImported: typeof import('./useImported').useImported = neverCallMe;

const ImportedModule: typeof import('./Module').ImportedModule = neverCallMe;
const ImportedComponent: typeof import('./Component').ImportedComponent = neverCallMe;

export { lazy, imported, importedModule, ImportedModule, ImportedComponent, useImported, assignImportedComponents };

export default createMacro(macro);
github evenchange4 / svgr.macro / src / macro.js View on Github external
const arrowFunctionExpression =
          ast.program.body[1].declarations[0].init;
        return t.objectProperty(
          t.stringLiteral(componentName),
          arrowFunctionExpression,
        );
      });

      referencePath.parentPath.replaceWith(
        t.objectExpression(objectProperties),
      );
    }
  });
}

export default createMacro(svgrMacro);
github esamattis / babel-plugin-ts-optchain / packages / ts-optchain.macro / src / macro.ts View on Github external
path: NodePath,
) {
    if (!t.isIdentifier(path.node.callee)) {
        return;
    }

    const name = path.node.callee.name;

    const importDecl = findImportDeclaration(t, name, path);

    if (importDecl) {
        importDecl.source.value = RUNTIME_IMPORT;
    }
}

export default createMacro(function tsOptChainMacro(macro: Macro) {
    const t = macro.babel.types;

    for (const key of Object.keys(macro.references)) {
        const paths = macro.references[key];
        for (const path of paths) {
            if (t.isCallExpression(path.parent)) {
                const callPath = path.parentPath as NodePath<
                    BabelTypes.CallExpression
                >;
                transfromMacroImport(t, callPath);
                transformOptchainCall(t, callPath);
            }
        }
    }

    return {
github Andarist / regexgen.macro / src / index.js View on Github external
const flags = regExpConstructor.get('arguments.1')
        regExpConstructor.replaceWith(
          t.regExpLiteral(concatenated, flags && flags.node.value),
        )
        return
      }

      regexgenCall.replaceWith(t.stringLiteral(compiledRegExp))
      return
    }

    regexgenCall.replaceWith(t.regExpLiteral(compiledRegExp))
  })
}

export default createMacro(regexgenMacro)
github mattphillips / babel-plugin-console / src / macros / scope.macro.js View on Github external
import { createMacro } from 'babel-plugin-macros';
import generateScopeLog from '../scope';

module.exports = createMacro(({ babel: { template, types }, references }) =>
  references.default.forEach(({ parentPath }) =>
    parentPath.replaceWithMultiple(generateScopeLog(parentPath, template, types))
  )
);
github planttheidea / inline-loops.macro / src / inline-loops.macro.js View on Github external
};
  }

  allMethods.forEach(({
    method, path, sourceMethod, type,
  }) => {
    const isDecrementing = type === 'decrementing';
    const isObject = type === 'object';

    const handler = createTransformer(method, handlers[sourceMethod], isDecrementing, isObject);

    handler(path);
  });
}

module.exports = createMacro(inlineLoops);

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