UNPKG

babel-plugin-redux-action-compose

Version:
207 lines (170 loc) 6.36 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _path = require('path'); var _babelTypes = require('babel-types'); var t = _interopRequireWildcard(_babelTypes); var _babelPluginSyntaxFlow = require('babel-plugin-syntax-flow'); var _babelPluginSyntaxFlow2 = _interopRequireDefault(_babelPluginSyntaxFlow); var _babelFileLoader = require('babel-file-loader'); var _babelExplodeModule = require('babel-explode-module'); var _babelAddFlowComments = require('babel-add-flow-comments'); var _uppercamelcase = require('uppercamelcase'); var _uppercamelcase2 = _interopRequireDefault(_uppercamelcase); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function hasAction(path) { var name = path.get('id').get('name').node; if (name !== 'Action') { return false; } if (!t.isExportNamedDeclaration(path.parentPath)) { return false; } return true; } function isActionFile(path) { var isActionFile = false; path.traverse({ TypeAlias: function TypeAlias(path) { if (hasAction(path)) { isActionFile = true; } } }); return isActionFile; } function getImportPath(from, to) { var relativePath = (0, _path.relative)((0, _path.dirname)(from), to); var fomattedPath = (0, _path.extname)(relativePath) === '.js' ? relativePath.replace('.js', '') : relativePath; if (!/^\.\.?/.test(fomattedPath)) { return './' + fomattedPath; } return fomattedPath; } function actionName(path) { var parentPath = (0, _path.normalize)((0, _path.dirname)(path)).split('/'); return (0, _uppercamelcase2.default)(parentPath[parentPath.length - 1]) + 'Action'; } exports.default = function () { return { inherits: _babelPluginSyntaxFlow2.default, visitor: { // eslint-disable-next-line Program: function Program(path, _ref) { var file = _ref.file, opts = _ref.opts; if (!opts.input) { return false; } var from = file.opts.filename; var to = opts.input; var importPath = getImportPath(from, to); try { var inputFile = (0, _babelFileLoader.loadFileSync)(to); if (!isActionFile(inputFile.path)) { return false; } // already imported? var exploded = (0, _babelExplodeModule.explodeModule)(path.node); var _hasAction = exploded.imports.some(function (v) { return v.kind === 'type' && v.source === importPath; }); if (_hasAction) { return false; } var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = /* : Path */path.get('body')[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; if (t.isImportDeclaration(item)) { continue; } var specifiers = [t.importSpecifier( // ./other/action.js → OtherAction t.identifier(actionName(importPath)), t.identifier('Action'))]; var importAction = t.importDeclaration(specifiers, t.stringLiteral(importPath)); // $FlowFixMe importAction.importKind = 'type'; item.insertBefore(importAction); item.insertBefore(t.noop()); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } catch (err) { if (err.code !== 'ENOENT') { throw err; } // remove `import type notFound from 'path not found'` var _iteratorNormalCompletion2 = true; var _didIteratorError2 = false; var _iteratorError2 = undefined; try { for (var _iterator2 = path.get('body')[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { var _item = _step2.value; if (t.isImportDeclaration(_item) && _item.node.source.value === importPath) { _item.remove(); } } } catch (err) { _didIteratorError2 = true; _iteratorError2 = err; } finally { try { if (!_iteratorNormalCompletion2 && _iterator2.return) { _iterator2.return(); } } finally { if (_didIteratorError2) { throw _iteratorError2; } } } } if (path.node.body.length > 0) { // create `export type Action = A | B | C` var _exploded = (0, _babelExplodeModule.explodeModule)(path.node); // babelLog(path.node) var actions = _exploded.imports.filter(function (v) { return v.kind === 'type'; }).map(function (v) { return t.identifier(v.local); }); // remove `type Action` path.traverse({ TypeAlias: function TypeAlias(path) { if (path.node.id.name === 'Action') { path.remove(); } } }); if (actions.length === 0) { return false; } path.pushContainer('body', // export t.exportNamedDeclaration( // type Action = UnionType t.typeAlias(t.identifier('Action'), null, t.unionTypeAnnotation(actions)), [], null)); // add `// ` (0, _babelAddFlowComments.addFlowComment)(path); } } } }; };