Created
March 5, 2020 22:24
-
-
Save mattcompiles/abc5e06e675b3568d8d8019cb0714469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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