Skip to content

Instantly share code, notes, and snippets.

@mattcompiles
Created March 5, 2020 22:24
Show Gist options
  • Save mattcompiles/abc5e06e675b3568d8d8019cb0714469 to your computer and use it in GitHub Desktop.
Save mattcompiles/abc5e06e675b3568d8d8019cb0714469 to your computer and use it in GitHub Desktop.
const { createMacro, MacroError } = require('babel-plugin-macros');
const { default: generate } = require('@babel/generator');
function stringify({ references, babel }) {
const { types: t } = babel;
references.default.forEach(({ parentPath }) => {
if (!t.isCallExpression(parentPath.node)) {
throw new MacroError('stringify must be a CallExpression');
}
const [firstArg] = parentPath.node.arguments;
if (!t.isCallExpression(parentPath.firstArg)) {
throw new MacroError('stringify must be passed a CallExpression');
}
firstArg.
const { code } = generate(firstArg);
parentPath.replaceWith(t.stringLiteral(code));
});
}
module.exports = {
stringify: createMacro(stringify),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment