react-magnetic-di
Version:
Context driven dependency injection
53 lines (49 loc) • 4.27 kB
JavaScript
;
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var _require = require('./utils'),
isMatchingAny = _require.isMatchingAny;
function processInjectable(t, path, state, opts) {
var _state$injectIdentifi, _configPath$node$prop;
var _path$get = path.get('arguments'),
_path$get2 = _slicedToArray(_path$get, 3),
depPath = _path$get2[0],
configPath = _path$get2[2];
if (!depPath || !(state !== null && state !== void 0 && state.injectIdentifier) || path.get('callee').node.name !== ((_state$injectIdentifi = state.injectIdentifier) === null || _state$injectIdentifi === void 0 ? void 0 : _state$injectIdentifi.name)) {
return;
}
// check if third argument is not an object with module: false
var moduleFlag = configPath === null || configPath === void 0 || (_configPath$node$prop = configPath.node.properties) === null || _configPath$node$prop === void 0 || (_configPath$node$prop = _configPath$node$prop.find(function (n) {
var _n$key;
return ((_n$key = n.key) === null || _n$key === void 0 ? void 0 : _n$key.name) === 'module';
})) === null || _configPath$node$prop === void 0 || (_configPath$node$prop = _configPath$node$prop.value) === null || _configPath$node$prop === void 0 ? void 0 : _configPath$node$prop.value;
var depName = depPath.node.name;
var importSource = state.imports.specifiers.get(depName);
// There are several conditions under which we should mock:
// - if the import source matches any defaultMockedModules regexp
// and not consuming multiple exports or module flag is false
// - if injectable is called with explicit module: true
var isSourceAllowed = isMatchingAny(opts.defaultMockedModules.include, importSource) && !isMatchingAny(opts.defaultMockedModules.exclude, importSource);
if (!isSourceAllowed && !moduleFlag || moduleFlag === false) return;
var allSpecifiers = state.imports.sources.get(importSource);
var remainingSpecifiers = moduleFlag ? [] : allSpecifiers === null || allSpecifiers === void 0 ? void 0 : allSpecifiers.filter(function (s) {
return s !== depName;
});
state.imports.sources.set(importSource, remainingSpecifiers);
if ((remainingSpecifiers === null || remainingSpecifiers === void 0 ? void 0 : remainingSpecifiers.length) !== 0 || (allSpecifiers === null || allSpecifiers === void 0 ? void 0 : allSpecifiers.length) === 0) {
return;
}
var statement = t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('jest'), t.identifier('mock')), [t.stringLiteral(importSource)]));
// add statement var to programPath body after all imports but before variables
state.programPath.get('body').some(function (n, i) {
if (!t.isImportDeclaration(n)) {
state.programPath.get('body')[i].insertBefore(statement);
return true;
}
});
}
module.exports = processInjectable;