typed-redux-saga
Version:
Redux-Saga effects with strong types.
33 lines (25 loc) • 907 B
JavaScript
;
var helperModuleImports = require('@babel/helper-module-imports');
var babelPluginMacros = require('babel-plugin-macros');
/* eslint-disable functional/no-loop-statement */
var macro = babelPluginMacros.createMacro(({ references, babel, state }) => {
const program = state.file.path;
for (const refName of Object.keys(references)) {
const identifierNode = helperModuleImports.addNamed(program, refName, "redux-saga/effects", {
nameHint: refName,
});
for (const refPath of references[refName]) {
refPath.node.name = identifierNode.name;
const parentPath = refPath.parentPath.parentPath;
if (
parentPath.isYieldExpression() &&
parentPath.node.delegate === true
) {
parentPath.replaceWith(
babel.types.yieldExpression(parentPath.get("argument").node),
);
}
}
}
});
module.exports = macro;