react-magnetic-di
Advanced tools
Comparing version 3.0.1 to 3.1.0
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -104,4 +104,4 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } | ||
depRef.findParent(function (p) { | ||
var _p$parentPath, _p$parentPath$node, _p$parentPath$node$ca; | ||
if (p.isFunction() && ((_p$parentPath = p.parentPath) === null || _p$parentPath === void 0 ? void 0 : (_p$parentPath$node = _p$parentPath.node) === null || _p$parentPath$node === void 0 ? void 0 : (_p$parentPath$node$ca = _p$parentPath$node.callee) === null || _p$parentPath$node$ca === void 0 ? void 0 : _p$parentPath$node$ca.name) !== INJECT_FUNCTION) { | ||
var _p$parentPath; | ||
if (p.isFunction() && ((_p$parentPath = p.parentPath) === null || _p$parentPath === void 0 || (_p$parentPath = _p$parentPath.node) === null || _p$parentPath === void 0 || (_p$parentPath = _p$parentPath.callee) === null || _p$parentPath === void 0 ? void 0 : _p$parentPath.name) !== INJECT_FUNCTION) { | ||
// add ref for every function scope up to the root one | ||
@@ -108,0 +108,0 @@ _this.getValueOrInit(p).dependencyRefs.add(depRef); |
@@ -7,3 +7,3 @@ "use strict"; | ||
function processReference(t, path, locationValue, state) { | ||
var _locationValue$diRef, _locationValue$diRef$, _locationValue$diRef$2; | ||
var _locationValue$diRef; | ||
var self = getComponentDeclaration(t, path.scope); | ||
@@ -28,3 +28,3 @@ var bodyPath = path.get('body'); | ||
}); | ||
(_locationValue$diRef = locationValue.diRef) === null || _locationValue$diRef === void 0 ? void 0 : (_locationValue$diRef$ = _locationValue$diRef.container) === null || _locationValue$diRef$ === void 0 ? void 0 : (_locationValue$diRef$2 = _locationValue$diRef$.arguments) === null || _locationValue$diRef$2 === void 0 ? void 0 : _locationValue$diRef$2.forEach(function (n) { | ||
(_locationValue$diRef = locationValue.diRef) === null || _locationValue$diRef === void 0 || (_locationValue$diRef = _locationValue$diRef.container) === null || _locationValue$diRef === void 0 || (_locationValue$diRef = _locationValue$diRef.arguments) === null || _locationValue$diRef === void 0 || _locationValue$diRef.forEach(function (n) { | ||
assert.isValidArgument(t, n, locationValue.diRef, self); | ||
@@ -31,0 +31,0 @@ if (!depNames.includes(n.name)) depNames.push(n.name); |
"use strict"; | ||
var processReference = function processReference(t, ref) { | ||
var _nextSibling$node$exp, _nextSibling$node$exp2, _nextSibling$node$exp3; | ||
var _nextSibling$node$exp; | ||
var container = ref.parentPath.container; | ||
@@ -11,3 +11,3 @@ if (container.type !== 'VariableDeclarator') return; | ||
var nextSibling = ref.getStatementParent().getNextSibling(); | ||
if (nextSibling.isExpressionStatement() && ((_nextSibling$node$exp = nextSibling.node.expression) === null || _nextSibling$node$exp === void 0 ? void 0 : (_nextSibling$node$exp2 = _nextSibling$node$exp.left) === null || _nextSibling$node$exp2 === void 0 ? void 0 : (_nextSibling$node$exp3 = _nextSibling$node$exp2.property) === null || _nextSibling$node$exp3 === void 0 ? void 0 : _nextSibling$node$exp3.name) == 'displayName') { | ||
if (nextSibling.isExpressionStatement() && ((_nextSibling$node$exp = nextSibling.node.expression) === null || _nextSibling$node$exp === void 0 || (_nextSibling$node$exp = _nextSibling$node$exp.left) === null || _nextSibling$node$exp === void 0 || (_nextSibling$node$exp = _nextSibling$node$exp.property) === null || _nextSibling$node$exp === void 0 ? void 0 : _nextSibling$node$exp.name) == 'displayName') { | ||
return; | ||
@@ -14,0 +14,0 @@ } |
@@ -118,4 +118,4 @@ "use strict"; | ||
var hasDisableComment = function hasDisableComment(path) { | ||
var _path$node, _path$node$body, _path$node2, _path$node2$body, _path$node2$body$body, _path$node2$body$body2; | ||
return [].concat(_toConsumableArray(((_path$node = path.node) === null || _path$node === void 0 ? void 0 : (_path$node$body = _path$node.body) === null || _path$node$body === void 0 ? void 0 : _path$node$body.leadingComments) || []), _toConsumableArray(((_path$node2 = path.node) === null || _path$node2 === void 0 ? void 0 : (_path$node2$body = _path$node2.body) === null || _path$node2$body === void 0 ? void 0 : (_path$node2$body$body = _path$node2$body.body) === null || _path$node2$body$body === void 0 ? void 0 : (_path$node2$body$body2 = _path$node2$body$body[0]) === null || _path$node2$body$body2 === void 0 ? void 0 : _path$node2$body$body2.leadingComments) || [])).some(function (c) { | ||
var _path$node, _path$node2; | ||
return [].concat(_toConsumableArray(((_path$node = path.node) === null || _path$node === void 0 || (_path$node = _path$node.body) === null || _path$node === void 0 ? void 0 : _path$node.leadingComments) || []), _toConsumableArray(((_path$node2 = path.node) === null || _path$node2 === void 0 || (_path$node2 = _path$node2.body) === null || _path$node2 === void 0 || (_path$node2 = _path$node2.body) === null || _path$node2 === void 0 || (_path$node2 = _path$node2[0]) === null || _path$node2 === void 0 ? void 0 : _path$node2.leadingComments) || [])).some(function (c) { | ||
return c.value.includes('di-ignore'); | ||
@@ -122,0 +122,0 @@ }); |
@@ -49,3 +49,2 @@ "use strict"; | ||
}; | ||
return { | ||
@@ -52,0 +51,0 @@ ImportDeclaration: function ImportDeclaration(node) { |
@@ -5,5 +5,5 @@ "use strict"; | ||
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 _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } | ||
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(arr) { if (Array.isArray(arr)) return arr; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
@@ -98,5 +98,5 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } | ||
var restrictedValue = restrictedVars.get(firstArg === null || firstArg === void 0 ? void 0 : firstArg.name); | ||
var enabledTarget = (restrictedValue === null || restrictedValue === void 0 ? void 0 : restrictedValue.allowTargeted) && (thirdArg === null || thirdArg === void 0 ? void 0 : (_thirdArg$properties = thirdArg.properties) === null || _thirdArg$properties === void 0 ? void 0 : _thirdArg$properties.some(function (n) { | ||
var enabledTarget = (restrictedValue === null || restrictedValue === void 0 ? void 0 : restrictedValue.allowTargeted) && (thirdArg === null || thirdArg === void 0 || (_thirdArg$properties = thirdArg.properties) === null || _thirdArg$properties === void 0 ? void 0 : _thirdArg$properties.some(function (n) { | ||
var _n$key; | ||
return (n === null || n === void 0 ? void 0 : (_n$key = n.key) === null || _n$key === void 0 ? void 0 : _n$key.name) === 'target'; | ||
return (n === null || n === void 0 || (_n$key = n.key) === null || _n$key === void 0 ? void 0 : _n$key.name) === 'target'; | ||
})); | ||
@@ -103,0 +103,0 @@ if (restrictedValue && !enabledTarget) { |
@@ -7,3 +7,3 @@ "use strict"; | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } | ||
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } | ||
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(arr) { if (Array.isArray(arr)) return arr; } | ||
@@ -10,0 +10,0 @@ var PACKAGE_NAME = 'react-magnetic-di'; |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.diRegistry = exports.PACKAGE_NAME = void 0; | ||
var diRegistry = new WeakMap(); | ||
exports.diRegistry = diRegistry; | ||
var PACKAGE_NAME = 'react-magnetic-di'; | ||
exports.PACKAGE_NAME = PACKAGE_NAME; | ||
var diRegistry = exports.diRegistry = new WeakMap(); | ||
var PACKAGE_NAME = exports.PACKAGE_NAME = 'react-magnetic-di'; |
@@ -10,5 +10,4 @@ "use strict"; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var Context = /*#__PURE__*/_react["default"].createContext({ | ||
var Context = exports.Context = /*#__PURE__*/_react["default"].createContext({ | ||
getDependencies: _global.globalDi.getDependencies | ||
}); | ||
exports.Context = Context; | ||
}); |
@@ -10,5 +10,5 @@ "use strict"; | ||
var _utils = require("./utils"); | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } | ||
var replacementMap = new Map(); | ||
var globalDi = { | ||
var globalDi = exports.globalDi = { | ||
getDependencies: function getDependencies(realDeps, targetChild) { | ||
@@ -30,5 +30,15 @@ return realDeps.map(function (dep) { | ||
replacementMap.clear(); | ||
}, | ||
_fromProvider: function _fromProvider(injs) { | ||
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
injs.forEach(function (inj) { | ||
if (props.global || _constants.diRegistry.get(inj).global) (0, _utils.addInjectableToMap)(replacementMap, inj); | ||
}); | ||
}, | ||
_remove: function _remove(injs) { | ||
injs.forEach(function (inj) { | ||
return (0, _utils.removeInjectableFromMap)(replacementMap, inj); | ||
}); | ||
} | ||
}; | ||
exports.globalDi = globalDi; | ||
function runWithDi(thunk, deps) { | ||
@@ -35,0 +45,0 @@ globalDi.use(deps); |
@@ -12,3 +12,3 @@ "use strict"; | ||
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } | ||
function injectable(from, implementation) { | ||
@@ -19,3 +19,5 @@ var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, | ||
_ref$track = _ref.track, | ||
track = _ref$track === void 0 ? true : _ref$track; | ||
track = _ref$track === void 0 ? true : _ref$track, | ||
_ref$global = _ref.global, | ||
global = _ref$global === void 0 ? false : _ref$global; | ||
var impl = implementation; | ||
@@ -37,5 +39,6 @@ if (typeof impl === 'function') { | ||
track: track, | ||
cause: new Error('Injectable created but not used. If this is on purpose, add "{track: false}"') | ||
global: global, | ||
cause: track ? new Error('Injectable created but not used. If this is on purpose, add "{track: false}"') : null | ||
}); | ||
return impl; | ||
} |
"use strict"; | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -14,10 +14,12 @@ value: true | ||
var _utils = require("./utils"); | ||
var _global = require("./global"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } | ||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } | ||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
var DiProvider = function DiProvider(_ref) { | ||
var DiProvider = exports.DiProvider = function DiProvider(_ref) { | ||
var children = _ref.children, | ||
use = _ref.use, | ||
target = _ref.target; | ||
target = _ref.target, | ||
global = _ref.global; | ||
var _useContext = (0, _react.useContext)(_context.Context), | ||
@@ -29,3 +31,10 @@ _getDependencies = _useContext.getDependencies; | ||
// create a map of dependency real -> replacements for fast lookup | ||
var replacementMap = use.reduce(_utils.addInjectableToMap, new Map()); | ||
var replacementMap = use.reduce(function (acc, inj) { | ||
(0, _utils.addInjectableToMap)(acc, inj); | ||
return acc; | ||
}, new Map()); | ||
// supports global di if needed | ||
_global.globalDi._fromProvider(use, { | ||
global: global | ||
}); | ||
// support single or multiple targets | ||
@@ -54,2 +63,9 @@ var targets = target && (Array.isArray(target) ? target : [target]); | ||
// on unmount | ||
(0, _react.useEffect)(function () { | ||
return function () { | ||
return _global.globalDi._remove(use); | ||
}; | ||
}, []); // ignore use prop | ||
return /*#__PURE__*/_react["default"].createElement(_context.Context.Provider, { | ||
@@ -59,5 +75,5 @@ value: value | ||
}; | ||
exports.DiProvider = DiProvider; | ||
DiProvider.propTypes = { | ||
children: _propTypes["default"].oneOfType([_propTypes["default"].func, _propTypes["default"].node]), | ||
global: _propTypes["default"].bool, | ||
target: _propTypes["default"].oneOfType([_propTypes["default"].func, _propTypes["default"].arrayOf(_propTypes["default"].func)]), | ||
@@ -64,0 +80,0 @@ use: _propTypes["default"].arrayOf(_propTypes["default"].oneOfType([_propTypes["default"].func, _propTypes["default"].object])).isRequired |
@@ -9,5 +9,5 @@ "use strict"; | ||
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 _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } | ||
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(arr) { if (Array.isArray(arr)) return arr; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
@@ -22,3 +22,3 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } | ||
}; | ||
var stats = { | ||
var stats = exports.stats = { | ||
state: createState(), | ||
@@ -43,2 +43,4 @@ set: function set(injObj) { | ||
})); | ||
// reset to avoid potential memory leaks via stack traces | ||
injObj.cause = null; | ||
}, | ||
@@ -69,3 +71,2 @@ track: function track(inj) { | ||
} | ||
}; | ||
exports.stats = stats; | ||
}; |
@@ -10,6 +10,7 @@ "use strict"; | ||
exports.getDisplayName = getDisplayName; | ||
exports.removeInjectableFromMap = removeInjectableFromMap; | ||
exports.warnOnce = warnOnce; | ||
var _constants = require("./constants"); | ||
var _stats = require("./stats"); | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
@@ -19,3 +20,3 @@ 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 _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } | ||
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } | ||
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(arr) { if (Array.isArray(arr)) return arr; } | ||
@@ -37,8 +38,17 @@ var hasWarned = false; | ||
if (replacementMap.has(injObj.from)) { | ||
replacementMap.get(injObj.from).unshift(injObj); | ||
replacementMap.get(injObj.from).add(injObj); | ||
} else { | ||
replacementMap.set(injObj.from, [injObj]); | ||
replacementMap.set(injObj.from, new Set([injObj])); | ||
} | ||
return replacementMap; | ||
} | ||
function removeInjectableFromMap(replacementMap, inj) { | ||
var injObj = _constants.diRegistry.get(inj); | ||
var injectables = replacementMap.get(injObj.from) || new Set(); | ||
if (injectables.size === 1) { | ||
replacementMap["delete"](injObj.from); | ||
} else { | ||
injectables["delete"](injObj); | ||
} | ||
} | ||
function getDisplayName(Comp) { | ||
@@ -57,5 +67,6 @@ var wrapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
function findInjectable(replacementMap, dep, targetChild) { | ||
var injectables = replacementMap.get(dep) || []; | ||
var candidates = []; | ||
// loop all injectables for the dep, ranking targeted ones higher | ||
var injectables = replacementMap.get(dep) || new Set(); | ||
// loop all injectables for the dep, with targeted ones preferred | ||
var anyCandidate = null; | ||
var targetCandidate = null; | ||
var _iterator = _createForOfIteratorHelper(injectables), | ||
@@ -67,4 +78,4 @@ _step; | ||
var inj = _step.value; | ||
if (!inj.targets) candidates.push(inj); | ||
if ((_inj$targets = inj.targets) !== null && _inj$targets !== void 0 && _inj$targets.includes(targetChild)) candidates.unshift(inj); | ||
if (!inj.targets) anyCandidate = inj; | ||
if ((_inj$targets = inj.targets) !== null && _inj$targets !== void 0 && _inj$targets.includes(targetChild)) targetCandidate = inj; | ||
} | ||
@@ -76,4 +87,5 @@ } catch (err) { | ||
} | ||
_stats.stats.track(candidates[0]); | ||
return candidates[0] || null; | ||
var candidate = targetCandidate || anyCandidate; | ||
_stats.stats.track(candidate); | ||
return candidate; | ||
} |
@@ -75,4 +75,4 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
depRef.findParent(p => { | ||
var _p$parentPath, _p$parentPath$node, _p$parentPath$node$ca; | ||
if (p.isFunction() && ((_p$parentPath = p.parentPath) == null ? void 0 : (_p$parentPath$node = _p$parentPath.node) == null ? void 0 : (_p$parentPath$node$ca = _p$parentPath$node.callee) == null ? void 0 : _p$parentPath$node$ca.name) !== INJECT_FUNCTION) { | ||
var _p$parentPath; | ||
if (p.isFunction() && ((_p$parentPath = p.parentPath) == null || (_p$parentPath = _p$parentPath.node) == null || (_p$parentPath = _p$parentPath.callee) == null ? void 0 : _p$parentPath.name) !== INJECT_FUNCTION) { | ||
// add ref for every function scope up to the root one | ||
@@ -79,0 +79,0 @@ this.getValueOrInit(p).dependencyRefs.add(depRef); |
@@ -6,3 +6,3 @@ const { | ||
function processReference(t, path, locationValue, state) { | ||
var _locationValue$diRef, _locationValue$diRef$, _locationValue$diRef$2; | ||
var _locationValue$diRef; | ||
const self = getComponentDeclaration(t, path.scope); | ||
@@ -27,3 +27,3 @@ const bodyPath = path.get('body'); | ||
}); | ||
(_locationValue$diRef = locationValue.diRef) == null ? void 0 : (_locationValue$diRef$ = _locationValue$diRef.container) == null ? void 0 : (_locationValue$diRef$2 = _locationValue$diRef$.arguments) == null ? void 0 : _locationValue$diRef$2.forEach(n => { | ||
(_locationValue$diRef = locationValue.diRef) == null || (_locationValue$diRef = _locationValue$diRef.container) == null || (_locationValue$diRef = _locationValue$diRef.arguments) == null || _locationValue$diRef.forEach(n => { | ||
assert.isValidArgument(t, n, locationValue.diRef, self); | ||
@@ -30,0 +30,0 @@ if (!depNames.includes(n.name)) depNames.push(n.name); |
const processReference = (t, ref) => { | ||
var _nextSibling$node$exp, _nextSibling$node$exp2, _nextSibling$node$exp3; | ||
var _nextSibling$node$exp; | ||
const container = ref.parentPath.container; | ||
@@ -9,3 +9,3 @@ if (container.type !== 'VariableDeclarator') return; | ||
const nextSibling = ref.getStatementParent().getNextSibling(); | ||
if (nextSibling.isExpressionStatement() && ((_nextSibling$node$exp = nextSibling.node.expression) == null ? void 0 : (_nextSibling$node$exp2 = _nextSibling$node$exp.left) == null ? void 0 : (_nextSibling$node$exp3 = _nextSibling$node$exp2.property) == null ? void 0 : _nextSibling$node$exp3.name) == 'displayName') { | ||
if (nextSibling.isExpressionStatement() && ((_nextSibling$node$exp = nextSibling.node.expression) == null || (_nextSibling$node$exp = _nextSibling$node$exp.left) == null || (_nextSibling$node$exp = _nextSibling$node$exp.property) == null ? void 0 : _nextSibling$node$exp.name) == 'displayName') { | ||
return; | ||
@@ -12,0 +12,0 @@ } |
@@ -100,4 +100,4 @@ const { | ||
const hasDisableComment = path => { | ||
var _path$node, _path$node$body, _path$node2, _path$node2$body, _path$node2$body$body, _path$node2$body$body2; | ||
return [...(((_path$node = path.node) == null ? void 0 : (_path$node$body = _path$node.body) == null ? void 0 : _path$node$body.leadingComments) || []), ...(((_path$node2 = path.node) == null ? void 0 : (_path$node2$body = _path$node2.body) == null ? void 0 : (_path$node2$body$body = _path$node2$body.body) == null ? void 0 : (_path$node2$body$body2 = _path$node2$body$body[0]) == null ? void 0 : _path$node2$body$body2.leadingComments) || [])].some(c => c.value.includes('di-ignore')); | ||
var _path$node, _path$node2; | ||
return [...(((_path$node = path.node) == null || (_path$node = _path$node.body) == null ? void 0 : _path$node.leadingComments) || []), ...(((_path$node2 = path.node) == null || (_path$node2 = _path$node2.body) == null || (_path$node2 = _path$node2.body) == null || (_path$node2 = _path$node2[0]) == null ? void 0 : _path$node2.leadingComments) || [])].some(c => c.value.includes('di-ignore')); | ||
}; | ||
@@ -104,0 +104,0 @@ module.exports = { |
@@ -46,3 +46,2 @@ const { | ||
}); | ||
return { | ||
@@ -49,0 +48,0 @@ ImportDeclaration(node) { |
@@ -68,5 +68,5 @@ const { | ||
const restrictedValue = restrictedVars.get(firstArg == null ? void 0 : firstArg.name); | ||
const enabledTarget = (restrictedValue == null ? void 0 : restrictedValue.allowTargeted) && (thirdArg == null ? void 0 : (_thirdArg$properties = thirdArg.properties) == null ? void 0 : _thirdArg$properties.some(n => { | ||
const enabledTarget = (restrictedValue == null ? void 0 : restrictedValue.allowTargeted) && (thirdArg == null || (_thirdArg$properties = thirdArg.properties) == null ? void 0 : _thirdArg$properties.some(n => { | ||
var _n$key; | ||
return (n == null ? void 0 : (_n$key = n.key) == null ? void 0 : _n$key.name) === 'target'; | ||
return (n == null || (_n$key = n.key) == null ? void 0 : _n$key.name) === 'target'; | ||
})); | ||
@@ -73,0 +73,0 @@ if (restrictedValue && !enabledTarget) { |
@@ -1,3 +0,3 @@ | ||
import { PACKAGE_NAME } from './constants'; | ||
import { addInjectableToMap, findInjectable } from './utils'; | ||
import { PACKAGE_NAME, diRegistry } from './constants'; | ||
import { addInjectableToMap, removeInjectableFromMap, findInjectable } from './utils'; | ||
const replacementMap = new Map(); | ||
@@ -19,2 +19,10 @@ export const globalDi = { | ||
replacementMap.clear(); | ||
}, | ||
_fromProvider(injs, props = {}) { | ||
injs.forEach(inj => { | ||
if (props.global || diRegistry.get(inj).global) addInjectableToMap(replacementMap, inj); | ||
}); | ||
}, | ||
_remove(injs) { | ||
injs.forEach(inj => removeInjectableFromMap(replacementMap, inj)); | ||
} | ||
@@ -21,0 +29,0 @@ }; |
@@ -6,3 +6,4 @@ import { diRegistry } from './constants'; | ||
target, | ||
track = true | ||
track = true, | ||
global = false | ||
} = {}) { | ||
@@ -27,5 +28,6 @@ let impl = implementation; | ||
track, | ||
cause: new Error('Injectable created but not used. If this is on purpose, add "{track: false}"') | ||
global, | ||
cause: track ? new Error('Injectable created but not used. If this is on purpose, add "{track: false}"') : null | ||
}); | ||
return impl; | ||
} |
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
import React, { useContext, useMemo, forwardRef } from 'react'; | ||
import React, { useContext, useMemo, forwardRef, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
@@ -7,6 +7,8 @@ import { diRegistry } from './constants'; | ||
import { addInjectableToMap, getDisplayName, findInjectable } from './utils'; | ||
import { globalDi } from './global'; | ||
export const DiProvider = ({ | ||
children, | ||
use, | ||
target | ||
target, | ||
global | ||
}) => { | ||
@@ -20,3 +22,10 @@ const { | ||
// create a map of dependency real -> replacements for fast lookup | ||
const replacementMap = use.reduce(addInjectableToMap, new Map()); | ||
const replacementMap = use.reduce((acc, inj) => { | ||
addInjectableToMap(acc, inj); | ||
return acc; | ||
}, new Map()); | ||
// supports global di if needed | ||
globalDi._fromProvider(use, { | ||
global | ||
}); | ||
// support single or multiple targets | ||
@@ -45,2 +54,5 @@ const targets = target && (Array.isArray(target) ? target : [target]); | ||
// on unmount | ||
useEffect(() => () => globalDi._remove(use), []); // ignore use prop | ||
return /*#__PURE__*/React.createElement(Context.Provider, { | ||
@@ -52,2 +64,3 @@ value: value | ||
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | ||
global: PropTypes.bool, | ||
target: PropTypes.oneOfType([PropTypes.func, PropTypes.arrayOf(PropTypes.func)]), | ||
@@ -54,0 +67,0 @@ use: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object])).isRequired |
@@ -15,2 +15,4 @@ const createState = () => ({ | ||
})); | ||
// reset to avoid potential memory leaks via stack traces | ||
injObj.cause = null; | ||
}, | ||
@@ -17,0 +19,0 @@ track(inj) { |
@@ -18,8 +18,17 @@ import { diRegistry } from './constants'; | ||
if (replacementMap.has(injObj.from)) { | ||
replacementMap.get(injObj.from).unshift(injObj); | ||
replacementMap.get(injObj.from).add(injObj); | ||
} else { | ||
replacementMap.set(injObj.from, [injObj]); | ||
replacementMap.set(injObj.from, new Set([injObj])); | ||
} | ||
return replacementMap; | ||
} | ||
export function removeInjectableFromMap(replacementMap, inj) { | ||
const injObj = diRegistry.get(inj); | ||
const injectables = replacementMap.get(injObj.from) || new Set(); | ||
if (injectables.size === 1) { | ||
replacementMap.delete(injObj.from); | ||
} else { | ||
injectables.delete(injObj); | ||
} | ||
} | ||
export function getDisplayName(Comp, wrapper = '') { | ||
@@ -35,12 +44,14 @@ const name = Comp.displayName || Comp.name; | ||
export function findInjectable(replacementMap, dep, targetChild) { | ||
const injectables = replacementMap.get(dep) || []; | ||
const candidates = []; | ||
// loop all injectables for the dep, ranking targeted ones higher | ||
const injectables = replacementMap.get(dep) || new Set(); | ||
// loop all injectables for the dep, with targeted ones preferred | ||
let anyCandidate = null; | ||
let targetCandidate = null; | ||
for (const inj of injectables) { | ||
var _inj$targets; | ||
if (!inj.targets) candidates.push(inj); | ||
if ((_inj$targets = inj.targets) != null && _inj$targets.includes(targetChild)) candidates.unshift(inj); | ||
if (!inj.targets) anyCandidate = inj; | ||
if ((_inj$targets = inj.targets) != null && _inj$targets.includes(targetChild)) targetCandidate = inj; | ||
} | ||
stats.track(candidates[0]); | ||
return candidates[0] || null; | ||
const candidate = targetCandidate || anyCandidate; | ||
stats.track(candidate); | ||
return candidate; | ||
} |
{ | ||
"name": "react-magnetic-di", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Context driven dependency injection", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"test": "jest", | ||
"types": "dtslint --expectOnly --localTs node_modules/typescript/lib ./types && tsc && flow --max-warnings=0", | ||
"types": "tsc && flow --max-warnings=0", | ||
"lint": "eslint ./src", | ||
@@ -48,33 +48,32 @@ "preversion": "npm run lint -s && npm run types -s && npm run test -s", | ||
"devDependencies": { | ||
"@babel/cli": "^7.21.0", | ||
"@babel/core": "^7.21.4", | ||
"@babel/eslint-parser": "^7.21.3", | ||
"@babel/cli": "^7.23.4", | ||
"@babel/core": "^7.23.5", | ||
"@babel/eslint-parser": "^7.23.3", | ||
"@babel/plugin-proposal-class-properties": "^7.18.6", | ||
"@babel/plugin-proposal-decorators": "^7.22.7", | ||
"@babel/plugin-transform-modules-commonjs": "^7.22.5", | ||
"@babel/plugin-transform-runtime": "^7.21.4", | ||
"@babel/preset-env": "^7.21.4", | ||
"@babel/preset-flow": "^7.21.4", | ||
"@babel/preset-react": "^7.18.6", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@babel/runtime": "^7.21.0", | ||
"@testing-library/react": "^14.0.0", | ||
"@types/jest": "^29.5.1", | ||
"@types/react": "^18.2.0", | ||
"@types/react-dom": "^18.2.1", | ||
"babel-jest": "^29.5.0", | ||
"babel-loader": "^9.1.2", | ||
"@babel/plugin-proposal-decorators": "^7.23.5", | ||
"@babel/plugin-transform-modules-commonjs": "^7.23.3", | ||
"@babel/plugin-transform-runtime": "^7.23.4", | ||
"@babel/preset-env": "^7.23.5", | ||
"@babel/preset-flow": "^7.23.3", | ||
"@babel/preset-react": "^7.23.3", | ||
"@babel/preset-typescript": "^7.23.3", | ||
"@babel/runtime": "^7.23.5", | ||
"@testing-library/react": "^14.1.2", | ||
"@types/jest": "^29.5.11", | ||
"@types/react": "^18.2.42", | ||
"@types/react-dom": "^18.2.17", | ||
"babel-jest": "^29.7.0", | ||
"babel-loader": "^9.1.3", | ||
"babel-plugin-module-resolver": "^5.0.0", | ||
"dtslint": "^4.2.1", | ||
"eslint": "^8.39.0", | ||
"eslint": "^8.55.0", | ||
"eslint-plugin-flowtype": "^8.0.3", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-import": "^2.29.0", | ||
"eslint-plugin-local": "^1.0.0", | ||
"eslint-plugin-react": "^7.32.2", | ||
"eslint-plugin-react": "^7.33.2", | ||
"eslint-plugin-react-hooks": "^4.6.0", | ||
"flow-bin": "^0.169.0", | ||
"flow-copy-source": "^2.0.9", | ||
"jest": "^29.5.0", | ||
"jest-environment-jsdom": "^29.5.0", | ||
"prettier": "^2.8.8", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"prettier": "^3.1.0", | ||
"prop-types": "^15.8.1", | ||
@@ -84,6 +83,6 @@ "react": "^18.2.0", | ||
"react-test-renderer": "^18.2.0", | ||
"typescript": "^5.0.4", | ||
"webpack": "^5.81.0", | ||
"webpack-cli": "^5.0.2", | ||
"webpack-dev-server": "^4.13.3" | ||
"typescript": "^5.3.2", | ||
"webpack": "^5.89.0", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-dev-server": "^4.15.1" | ||
}, | ||
@@ -90,0 +89,0 @@ "engines": { |
@@ -18,3 +18,3 @@ <p align="center"> | ||
- Promotes type safety for mocks | ||
- Works with any kind of value (funcitons, objects, strings) and in all closures / React components | ||
- Works with any kind of value (functions, objects, strings) and in all closures / React components | ||
- Replaces dependencies at any depth of the call chain / React tree | ||
@@ -49,3 +49,3 @@ - Allows selective injection | ||
This is where the magic happens: we safely rewrite the code to prepend `di(...)` in every function scope, so that the dependency value can be swapped. We recommend to only add the plugin in development/test enviroments to avoid useless const assignment in production. You can either do that via multiple babel environment configs or by using `enabledEnvs` option. | ||
This is where the magic happens: we safely rewrite the code to prepend `di(...)` in every function scope, so that the dependency value can be swapped. We recommend to only add the plugin in development/test environments to avoid useless const assignment in production. You can either do that via multiple babel environment configs or by using `enabledEnvs` option. | ||
@@ -86,3 +86,3 @@ ### Using dependency replacement | ||
### Usin dependency replacement in React tests and storybooks | ||
### Using dependency replacement in React tests and storybooks | ||
@@ -157,3 +157,3 @@ For React, we provide a specific `DiProvider` to enable replacements across the entire tree. Given a component with complex UI interaction or data dependencies, like a Modal or an Apollo Query, we want to easily be able to integration test it: | ||
#### Allowing globals replacement | ||
#### Allowing globals (variables) replacement | ||
@@ -187,3 +187,3 @@ Currently the library does not enable automatic replacement of globals. To do that, you need to manually "tag" a global for replacement with `di(myGlobal)` in the function scope. For instance: | ||
Other times, there might be places in code where auto injection is problematic and might cause infine loops. It might be the case if you are creating an injectable that then imports the replacement source itself. | ||
Other times, there might be places in code where auto injection is problematic and might cause infinite loops. It might be the case if you are creating an injectable that then imports the replacement source itself. | ||
@@ -267,2 +267,16 @@ For those scenarios, you can add a comment at the top of the function scope to tell the Babel plugin to skip that scope: | ||
• `global`: allows a replacement to be available everywhere, at any point, until `DiProvider` unmounts (alternatively use `global` prop on `DiProvider` to make all `use` replacements act globally): | ||
```js | ||
const fetchApiDi = injectable(fetchApi, jest.fn(), { global: true }); | ||
``` | ||
#### DiProvider props | ||
• `use`: required prop, it is an array of replacements | ||
• `target`: allows a replacement to only apply to specific components(s) | ||
• `global`: boolean, allows replacements to be available outside the render phase | ||
```` | ||
## ESLint plugin and rules | ||
@@ -272,9 +286,9 @@ | ||
| rule | description | | ||
| -------------------------- | -------------------------------------------------------------------------------------------------------------------- | | ||
| `order` | enforces `di(...)` to be the top of the block, to reduce chances of partial replacements | | ||
| `no-duplicate` | prohibits marking the same dependency as injectable more than once in the same scope | | ||
| `no-extraneous` | enforces dependencies to be consumed in the scope, to prevent unused variables | | ||
| `no-restricted-injectable` | prohibits certains values from being injected: `paths: [{ name: string, importNames?: string[], message?: string }]` | | ||
| `sort-dependencies` | require injectable dependencies to be sorted | | ||
| rule | description | | ||
| -------------------------- | ------------------------------------------------------------------------------------------------------------------- | | ||
| `order` | enforces `di(...)` to be the top of the block, to reduce chances of partial replacements | | ||
| `no-duplicate` | prohibits marking the same dependency as injectable more than once in the same scope | | ||
| `no-extraneous` | enforces dependencies to be consumed in the scope, to prevent unused variables | | ||
| `no-restricted-injectable` | prohibits certain values from being injected: `paths: [{ name: string, importNames?: string[], message?: string }]` | | ||
| `sort-dependencies` | require injectable dependencies to be sorted | | ||
@@ -301,4 +315,7 @@ The rules are exported from `react-magnetic-di/eslint-plugin`. Unfortunately ESLint does not allow plugins that are not npm packages, so rules needs to be imported via other means for now. | ||
// It will print ['fetchApi'] | ||
``` | ||
```` | ||
One possible reason for it to happen is that the context has been lost. Typical occurrences are async or deeply nested functions (especially in React). | ||
The solution is setting the prop `global` on `DiProvider` (or the same injectable config) to better handle those scenarios (but refrain from abusing it). | ||
## Contributing | ||
@@ -305,0 +322,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128701
38
2526
317