react-magnetic-di
Advanced tools
Comparing version 2.2.10 to 2.2.11
"use strict"; | ||
var _require = require('./constants'), | ||
PACKAGE_NAME = _require.PACKAGE_NAME, | ||
PACKAGE_FUNCTION = _require.PACKAGE_FUNCTION, | ||
HOC_FUNCTION = _require.HOC_FUNCTION; | ||
PACKAGE_NAME = _require.PACKAGE_NAME, | ||
PACKAGE_FUNCTION = _require.PACKAGE_FUNCTION, | ||
HOC_FUNCTION = _require.HOC_FUNCTION; | ||
var processDIReference = require('./processor-di'); | ||
var processHOCReference = require('./processor-hoc'); | ||
var _require2 = require('./utils'), | ||
isEnabledEnv = _require2.isEnabledEnv; | ||
isEnabledEnv = _require2.isEnabledEnv; | ||
module.exports = function (babel) { | ||
@@ -21,3 +17,3 @@ var t = babel.types; | ||
var _ref$opts = _ref.opts, | ||
opts = _ref$opts === void 0 ? {} : _ref$opts; | ||
opts = _ref$opts === void 0 ? {} : _ref$opts; | ||
// first we look at the imports: | ||
@@ -33,3 +29,2 @@ // if not our package and not the right function, ignore | ||
if (importSource !== PACKAGE_NAME) return; | ||
if (importDISpecifier) { | ||
@@ -44,4 +39,5 @@ // then we locate all usages of the method | ||
}); | ||
var isEnabled = isEnabledEnv() || Boolean(opts.forceEnable); // for each of that location we apply a tranformation | ||
var isEnabled = isEnabledEnv() || Boolean(opts.forceEnable); | ||
// for each of that location we apply a tranformation | ||
references.forEach(function (ref) { | ||
@@ -51,3 +47,2 @@ return processDIReference(t, ref, isEnabled); | ||
} | ||
if (importHOCSpecifier) { | ||
@@ -57,12 +52,9 @@ // then we locate all usages of the method | ||
var _methodIdentifier = importHOCSpecifier.local.name; | ||
var _binding = path.scope.getBinding(_methodIdentifier); | ||
if (!_binding) return; | ||
var _references = _binding.referencePaths.filter(function (ref) { | ||
return t.isCallExpression(ref.container); | ||
}); // for each of that location we apply a tranformation | ||
}); | ||
// for each of that location we apply a tranformation | ||
_references.forEach(function (ref) { | ||
@@ -69,0 +61,0 @@ return processHOCReference(t, ref); |
"use strict"; | ||
var _require = require('babel-plugin-macros'), | ||
createMacro = _require.createMacro; | ||
createMacro = _require.createMacro; | ||
var _require2 = require('./constants'), | ||
PACKAGE_NAME = _require2.PACKAGE_NAME, | ||
PACKAGE_FUNCTION = _require2.PACKAGE_FUNCTION, | ||
HOC_FUNCTION = _require2.HOC_FUNCTION; | ||
PACKAGE_NAME = _require2.PACKAGE_NAME, | ||
PACKAGE_FUNCTION = _require2.PACKAGE_FUNCTION, | ||
HOC_FUNCTION = _require2.HOC_FUNCTION; | ||
var processDIReference = require('./processor-di'); | ||
var processHOCReference = require('./processor-hoc'); | ||
var _require3 = require('./utils'), | ||
createNamedImport = _require3.createNamedImport, | ||
isEnabledEnv = _require3.isEnabledEnv; | ||
createNamedImport = _require3.createNamedImport, | ||
isEnabledEnv = _require3.isEnabledEnv; | ||
var diMacro = function diMacro(_ref) { | ||
var references = _ref.references, | ||
babel = _ref.babel, | ||
_ref$config = _ref.config, | ||
config = _ref$config === void 0 ? {} : _ref$config; | ||
babel = _ref.babel, | ||
_ref$config = _ref.config, | ||
config = _ref$config === void 0 ? {} : _ref$config; | ||
var t = babel.types; | ||
@@ -30,14 +25,17 @@ var isEnabled = isEnabledEnv() || Boolean(config.forceEnable); | ||
var diImport = references[PACKAGE_FUNCTION] || []; | ||
var hocImports = references[HOC_FUNCTION] || []; // process all di calls | ||
var hocImports = references[HOC_FUNCTION] || []; | ||
// process all di calls | ||
diImport.forEach(function (ref) { | ||
return processDIReference(t, ref, isEnabled); | ||
}); // process all HOC calls | ||
}); | ||
// process all HOC calls | ||
hocImports.forEach(function (ref) { | ||
return processHOCReference(t, ref); | ||
}); // if not enabled and only di was imported, let import to be stripped | ||
}); | ||
if (importedMethods.length === 1 && importedMethods[0] === PACKAGE_FUNCTION && !isEnabled) return; // add named imports | ||
// if not enabled and only di was imported, let import to be stripped | ||
if (importedMethods.length === 1 && importedMethods[0] === PACKAGE_FUNCTION && !isEnabled) return; | ||
// add named imports | ||
var methodScope = references[importedMethods[0]][0].scope; | ||
@@ -51,3 +49,2 @@ var statement = createNamedImport(t, PACKAGE_NAME, importedMethods, importedMethods.map(function (k) { | ||
}); | ||
if (targetPath) { | ||
@@ -62,5 +59,4 @@ // if we find the macro import, we add the clean import right before | ||
}; | ||
module.exports = createMacro(diMacro, { | ||
configName: 'reactMagneticDi' | ||
}); |
"use strict"; | ||
var _require = require('./utils'), | ||
assert = _require.assert, | ||
getComponentDeclaration = _require.getComponentDeclaration; | ||
assert = _require.assert, | ||
getComponentDeclaration = _require.getComponentDeclaration; | ||
function processReference(t, ref, isEnabled) { | ||
assert.isValidBlock(t, ref); | ||
assert.isValidCall(t, ref); // from the arguments of the method we generate the list of dependency identifiers | ||
assert.isValidCall(t, ref); | ||
// from the arguments of the method we generate the list of dependency identifiers | ||
var args = ref.container.arguments; | ||
@@ -15,12 +15,13 @@ var dependencyIdentifiers = args.map(function (v) { | ||
}); | ||
var statement = ref.getStatementParent(); // if should not be enabled, just remove the statement and exit | ||
var statement = ref.getStatementParent(); | ||
// if should not be enabled, just remove the statement and exit | ||
if (!isEnabled) { | ||
statement.remove(); | ||
return; | ||
} // generating variable declarations with array destructuring | ||
} | ||
// generating variable declarations with array destructuring | ||
// assigning them the result of the method call, with arguments | ||
// now wrapped in an array | ||
statement.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.arrayPattern(dependencyIdentifiers), t.callExpression(ref.node, [t.arrayExpression(args), getComponentDeclaration(t, ref.scope) || t.nullLiteral()]))])); | ||
@@ -38,3 +39,2 @@ ref.scope.registerDeclaration(statement); | ||
} | ||
module.exports = processReference; |
@@ -9,3 +9,2 @@ "use strict"; | ||
}; | ||
module.exports = processReference; |
@@ -6,13 +6,11 @@ "use strict"; | ||
if (scope.parentBlock.declaration) return scope.parentBlock.declaration.id; | ||
if (scope.getBlockParent().block.id) return scope.getBlockParent().block.id; // variable declaration | ||
if (scope.parentBlock.id) return scope.parentBlock.id; // class declarations | ||
if (scope.getBlockParent().block.id) return scope.getBlockParent().block.id; | ||
// variable declaration | ||
if (scope.parentBlock.id) return scope.parentBlock.id; | ||
// class declarations | ||
if (scope.parentBlock.type.includes('Class')) return scope.parent.block.id; | ||
}; | ||
var assert = { | ||
isValidBlock: function isValidBlock(t, ref) { | ||
var block = ref.scope.block; | ||
if (!t.isFunctionDeclaration(block) && !t.isFunctionExpression(block) && !t.isArrowFunctionExpression(block) && !t.isClassMethod(block)) { | ||
@@ -26,3 +24,2 @@ throw ref.buildCodeFrameError('Invalid di(...) call: must be inside a render function of a component. '); | ||
} | ||
if (!ref.container.arguments.every(function (node) { | ||
@@ -33,5 +30,3 @@ return t.isIdentifier(node); | ||
} | ||
var decl = getComponentDeclaration(t, ref.scope); | ||
if (decl && ref.container.arguments.some(function (v) { | ||
@@ -44,3 +39,2 @@ return v.name === decl.name; | ||
}; | ||
var createNamedImport = function createNamedImport(t, pkgName, pkgFns, localNames) { | ||
@@ -53,7 +47,5 @@ var statement = t.importDeclaration([], t.stringLiteral(pkgName)); | ||
}; | ||
var isEnabledEnv = function isEnabledEnv() { | ||
return ['development', 'test'].includes(process.env.BABEL_ENV) || ['development', 'test'].includes(process.env.NODE_ENV) || !process.env.BABEL_ENV && !process.env.NODE_ENV; | ||
}; | ||
module.exports = { | ||
@@ -60,0 +52,0 @@ getComponentDeclaration: getComponentDeclaration, |
"use strict"; | ||
var order = require('./rules/order'); | ||
var exhaustiveInject = require('./rules/exhaustive-inject'); | ||
var noDuplicate = require('./rules/no-duplicate'); | ||
var noExtraneous = require('./rules/no-extraneous'); | ||
var sortDependencies = require('./rules/sort-dependencies'); | ||
module.exports = { | ||
@@ -14,0 +9,0 @@ rules: { |
"use strict"; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(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); } | ||
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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var _require = require('../utils'), | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getParentDiStatements = _require.getParentDiStatements, | ||
getDiVars = _require.getDiVars, | ||
isHookName = _require.isHookName, | ||
isComponentName = _require.isComponentName, | ||
isLocalVariable = _require.isLocalVariable; | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getParentDiStatements = _require.getParentDiStatements, | ||
getDiVars = _require.getDiVars, | ||
isHookName = _require.isHookName, | ||
isComponentName = _require.isComponentName, | ||
isLocalVariable = _require.isLocalVariable; | ||
var getReactIdentifiers = function getReactIdentifiers(node) { | ||
@@ -33,3 +26,2 @@ if (node.source.value === 'react') { | ||
}; | ||
var isDefaultProp = function isDefaultProp(node, diStatement) { | ||
@@ -40,3 +32,2 @@ // we assume order rule is enabled, so if the variable is used in an assignment | ||
}; | ||
module.exports = { | ||
@@ -73,3 +64,2 @@ meta: { | ||
}, context.options[0]); | ||
var isInjected = function isInjected(vars, n) { | ||
@@ -80,6 +70,4 @@ return vars === null || vars === void 0 ? void 0 : vars.some(function (v) { | ||
}; | ||
var isReactIgnored = function isReactIgnored(n) { | ||
var _reactVars; | ||
return (_reactVars = reactVars) === null || _reactVars === void 0 ? void 0 : _reactVars.some(function (v) { | ||
@@ -89,7 +77,5 @@ return v.name === n.name; | ||
}; | ||
var isOptionsIgnored = function isOptionsIgnored(n) { | ||
return userOptions.ignore.includes(n.name); | ||
}; | ||
var report = function report(node, diStatement) { | ||
@@ -104,12 +90,9 @@ return context.report({ | ||
var lastArg = diStatement.expression.arguments.slice(-1)[0]; | ||
if (!lastArg) { | ||
// if injection without args, let's add the var inside call | ||
var _diStatement$expressi = _slicedToArray(diStatement.expression.callee.range, 2), | ||
start = _diStatement$expressi[0], | ||
end = _diStatement$expressi[1]; | ||
start = _diStatement$expressi[0], | ||
end = _diStatement$expressi[1]; | ||
return fixer.insertTextAfterRange([start, end + 1], node.name); | ||
} | ||
return fixer.insertTextAfter(lastArg, ", ".concat(node.name)); | ||
@@ -119,3 +102,2 @@ } | ||
}; | ||
return { | ||
@@ -135,4 +117,4 @@ ImportDeclaration: function ImportDeclaration(node) { | ||
}); | ||
var diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
var diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -149,7 +131,7 @@ var diVars = getDiVars(diStatements); | ||
'JSXOpeningElement:exit': function JSXOpeningElementExit(node) { | ||
if (!diIdentifier) return; // ignore if the component is declared locally | ||
if (!diIdentifier) return; | ||
// ignore if the component is declared locally | ||
if (isLocalVariable(node.name, context.getScope())) return; | ||
var varNode; | ||
switch (node.name.type) { | ||
@@ -163,17 +145,13 @@ case 'JSXIdentifier': | ||
} | ||
case 'JSXNamespacedName': | ||
// TODO handle foo:Bar | ||
return; | ||
case 'JSXMemberExpression': | ||
// TODO handle foo.Bar (but ignoring this.Bar) | ||
return; | ||
default: | ||
return; | ||
} | ||
var diStatements = getParentDiStatements(varNode, diIdentifier); // ignore locations where di was not explicitly set | ||
var diStatements = getParentDiStatements(varNode, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -180,0 +158,0 @@ var diVars = getDiVars(diStatements); |
"use strict"; | ||
var _require = require('../utils'), | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getDiVars = _require.getDiVars; | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getDiVars = _require.getDiVars; | ||
module.exports = { | ||
@@ -24,3 +23,2 @@ meta: { | ||
var diIdentifier; | ||
var report = function report(node) { | ||
@@ -35,3 +33,2 @@ return context.report({ | ||
}; | ||
return { | ||
@@ -43,4 +40,4 @@ ImportDeclaration: function ImportDeclaration(node) { | ||
if (!diIdentifier) return; | ||
var diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
var diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -47,0 +44,0 @@ var dupeDiVars = getDiVars(diStatements).filter(function (id, i, arr) { |
"use strict"; | ||
var _require = require('../utils'), | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getParentDiBlock = _require.getParentDiBlock, | ||
getDiVars = _require.getDiVars; | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements, | ||
getParentDiBlock = _require.getParentDiBlock, | ||
getDiVars = _require.getDiVars; | ||
module.exports = { | ||
@@ -37,3 +36,2 @@ meta: { | ||
var blockReferences = new WeakMap(); | ||
var report = function report(node) { | ||
@@ -45,7 +43,7 @@ return context.report({ | ||
name: node.name | ||
} // fix(fixer) { | ||
} | ||
// fix(fixer) { | ||
// const lastArg = diStatement.expression.arguments.slice(-1)[0]; | ||
// return fixer.insertTextAfter(lastArg, `, ${node.name}`); | ||
// }, | ||
}); | ||
@@ -62,4 +60,4 @@ }; | ||
if (!diIdentifier) return; | ||
var diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
var diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -79,3 +77,2 @@ var diVars = getDiVars(diStatements); | ||
var varNode; | ||
switch (node.name.type) { | ||
@@ -87,15 +84,11 @@ case 'JSXIdentifier': | ||
} | ||
case 'JSXNamespacedName': | ||
// TODO handle foo:Bar | ||
return; | ||
case 'JSXMemberExpression': | ||
// TODO handle foo.Bar (but ignoring this.Bar) | ||
return; | ||
default: | ||
return; | ||
} | ||
var parentBlock = getParentDiBlock(varNode, diIdentifier); | ||
@@ -102,0 +95,0 @@ var blockVars = blockReferences.get(parentBlock); |
"use strict"; | ||
var _require = require('../utils'), | ||
getDiIdentifier = _require.getDiIdentifier, | ||
isDiStatement = _require.isDiStatement; | ||
getDiIdentifier = _require.getDiIdentifier, | ||
isDiStatement = _require.isDiStatement; | ||
module.exports = { | ||
@@ -32,3 +31,2 @@ meta: { | ||
var prev = node.body[i - 1]; | ||
if (!isDiStatement(prev, diIdentifier)) { | ||
@@ -35,0 +33,0 @@ context.report({ |
"use strict"; | ||
var _require = require('../utils'), | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements; // use simple numeric comparison to have uppercase (components) first | ||
getDiIdentifier = _require.getDiIdentifier, | ||
getDiStatements = _require.getDiStatements; | ||
// use simple numeric comparison to have uppercase (components) first | ||
var compareName = function compareName(a, b) { | ||
return a.name > b.name ? 1 : -1; | ||
}; | ||
module.exports = { | ||
@@ -28,3 +27,2 @@ meta: { | ||
var diIdentifier; | ||
var report = function report(node, prevNode, args, sortedArgs) { | ||
@@ -45,5 +43,5 @@ return context.report({ | ||
return n.name; | ||
}).join(separator); // fixes all order issues at once | ||
}).join(separator); | ||
// fixes all order issues at once | ||
// so avoids the need of multiple saves | ||
return fixer.replaceTextRange([start, end], sorted); | ||
@@ -53,3 +51,2 @@ } | ||
}; | ||
return { | ||
@@ -61,13 +58,12 @@ ImportDeclaration: function ImportDeclaration(node) { | ||
if (!diIdentifier) return; | ||
var diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
var diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
diStatements.forEach(function (statement) { | ||
var args = statement.expression.arguments; // sort uppercase first, lowercase after | ||
var args = statement.expression.arguments; | ||
// sort uppercase first, lowercase after | ||
// so we get components and hooks grouped | ||
var sortedArgs = args.slice().sort(compareName); | ||
args.forEach(function (arg, i) { | ||
var prevArg = args[i - 1]; | ||
if (prevArg && compareName(arg, prevArg) < 0) { | ||
@@ -74,0 +70,0 @@ report(arg, prevArg, args, sortedArgs); |
@@ -5,19 +5,14 @@ "use strict"; | ||
var PACKAGE_FUNCTION = 'di'; | ||
var isDiStatement = function isDiStatement(stm, spec) { | ||
return stm.type === 'ExpressionStatement' && stm.expression && stm.expression.callee && stm.expression.callee.name === spec.name; | ||
}; | ||
var isHookName = function isHookName(node) { | ||
return /^use[A-Z0-9].*$/.test(node.name); | ||
}; | ||
var isComponentName = function isComponentName(node) { | ||
return !/^[a-z]/.test(node.name); | ||
}; | ||
var isLocalVariable = function isLocalVariable(node, scope) { | ||
do { | ||
var _scope; | ||
// if we reach module/global scope then is not local | ||
@@ -28,8 +23,8 @@ if (scope.type === 'module' || scope.type === 'global') return false; | ||
}); | ||
if (isLocal) return true; // eslint-disable-next-line no-cond-assign | ||
if (isLocal) return true; | ||
// eslint-disable-next-line no-cond-assign | ||
} while (scope = scope.upper); | ||
return false; | ||
}; | ||
var getDiIdentifier = function getDiIdentifier(node) { | ||
@@ -40,3 +35,2 @@ var importSource = node.source.value; | ||
}); | ||
if (importSource.startsWith(PACKAGE_NAME) && importSpecifier) { | ||
@@ -46,3 +40,2 @@ return importSpecifier.local; | ||
}; | ||
var getDiStatements = function getDiStatements(node, diIdentifier) { | ||
@@ -53,3 +46,2 @@ return (node.body || []).reduce(function (acc, statement) { | ||
}; | ||
var getParentDiBlock = function getParentDiBlock(node, diIdentifier) { | ||
@@ -62,6 +54,4 @@ // eslint-disable-next-line no-cond-assign | ||
} | ||
return null; | ||
}; | ||
var getParentDiStatements = function getParentDiStatements(node, diIdentifier) { | ||
@@ -72,3 +62,2 @@ var parentBlock = getParentDiBlock(node, diIdentifier); | ||
}; | ||
var getDiVars = function getDiVars(statements) { | ||
@@ -79,3 +68,2 @@ return statements.reduce(function (acc, s) { | ||
}; | ||
module.exports = { | ||
@@ -82,0 +70,0 @@ isDiStatement: isDiStatement, |
@@ -36,7 +36,4 @@ "use strict"; | ||
}); | ||
var _consumer = require("./react/consumer"); | ||
var _provider = require("./react/provider"); | ||
var _utils = require("./react/utils"); |
@@ -7,9 +7,5 @@ "use strict"; | ||
exports.di = di; | ||
var _constants = require("./constants"); | ||
var _context = require("./context"); | ||
var _utils = require("./utils"); | ||
function di(deps, target) { | ||
@@ -21,7 +17,6 @@ // check if babel plugin has been added | ||
var _ref = _context.Context._currentValue || {}, | ||
_ref$getDependencies = _ref.getDependencies, | ||
getDependencies = _ref$getDependencies === void 0 ? function (v) { | ||
return v; | ||
} : _ref$getDependencies; | ||
_ref$getDependencies = _ref.getDependencies, | ||
getDependencies = _ref$getDependencies === void 0 ? function (v) { | ||
return v; | ||
} : _ref$getDependencies; | ||
return getDependencies(deps, target); | ||
@@ -32,5 +27,4 @@ } else { | ||
} | ||
/** @deprecated use injectable instead */ | ||
di.mock = _utils.mock; |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.Context = void 0; | ||
var _react = _interopRequireDefault(require("react")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var Context = /*#__PURE__*/_react["default"].createContext({ | ||
@@ -18,3 +15,2 @@ getDependencies: function getDependencies(deps) { | ||
}); | ||
exports.Context = Context; |
"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); } | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,30 +9,19 @@ value: true | ||
exports.withDi = withDi; | ||
var _react = _interopRequireWildcard(require("react")); | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
var _constants = require("./constants"); | ||
var _context = require("./context"); | ||
var _utils = require("./utils"); | ||
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 _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 children = _ref.children, | ||
use = _ref.use, | ||
target = _ref.target; | ||
use = _ref.use, | ||
target = _ref.target; | ||
var _useContext = (0, _react.useContext)(_context.Context), | ||
_getDependencies = _useContext.getDependencies; // memo provider value so gets computed only once | ||
_getDependencies = _useContext.getDependencies; | ||
// memo provider value so gets computed only once | ||
var value = (0, _react.useMemo)(function () { | ||
@@ -43,4 +31,4 @@ // create a map of dependency real -> replacement for fast lookup | ||
return m.set(d[_constants.KEY], d); | ||
}, new Map()); // support single or multiple targets | ||
}, new Map()); | ||
// support single or multiple targets | ||
var targets = target && (Array.isArray(target) ? target : [target]); | ||
@@ -50,5 +38,4 @@ return { | ||
// First we collect dependencies from parent provider(s) (if any) | ||
var dependencies = _getDependencies(realDeps, targetChild); // If no target or target is in the array of targets, map use | ||
var dependencies = _getDependencies(realDeps, targetChild); | ||
// If no target or target is in the array of targets, map use | ||
if (!targetChild || !targets || targets.includes(targetChild)) { | ||
@@ -64,3 +51,2 @@ return dependencies.map(function (dep) { | ||
} | ||
return dependencies; | ||
@@ -75,3 +61,2 @@ } | ||
}; | ||
exports.DiProvider = DiProvider; | ||
@@ -83,3 +68,2 @@ DiProvider.propTypes = { | ||
}; | ||
function withDi(Comp, deps) { | ||
@@ -86,0 +70,0 @@ var target = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; |
@@ -10,7 +10,4 @@ "use strict"; | ||
exports.warnOnce = warnOnce; | ||
var _constants = require("./constants"); | ||
var hasWarned = false; | ||
function warnOnce(message) { | ||
@@ -23,3 +20,2 @@ if (!hasWarned) { | ||
} | ||
function getDisplayName(Comp) { | ||
@@ -30,17 +26,13 @@ var wrapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
} | ||
function injectable(from, implementation) { | ||
implementation.displayName = getDisplayName(implementation) || getDisplayName(from, 'di'); | ||
if (implementation[_constants.KEY] && implementation[_constants.KEY] !== from) { | ||
warnOnce("You are trying to use replacement \"".concat(implementation.displayName, "\" on multiple injectables. ") + "That will override only the last dependency, as each replacement is uniquely linked."); | ||
} | ||
implementation[_constants.KEY] = from; | ||
return implementation; | ||
} | ||
/** @deprecated use injectable instead */ | ||
var mock = injectable; | ||
exports.mock = mock; |
@@ -6,11 +6,7 @@ const { | ||
} = require('./constants'); | ||
const processDIReference = require('./processor-di'); | ||
const processHOCReference = require('./processor-hoc'); | ||
const { | ||
isEnabledEnv | ||
} = require('./utils'); | ||
module.exports = function (babel) { | ||
@@ -32,3 +28,2 @@ const { | ||
if (importSource !== PACKAGE_NAME) return; | ||
if (importDISpecifier) { | ||
@@ -41,7 +36,7 @@ // then we locate all usages of the method | ||
const references = binding.referencePaths.filter(ref => t.isCallExpression(ref.container)); | ||
const isEnabled = isEnabledEnv() || Boolean(opts.forceEnable); // for each of that location we apply a tranformation | ||
const isEnabled = isEnabledEnv() || Boolean(opts.forceEnable); | ||
// for each of that location we apply a tranformation | ||
references.forEach(ref => processDIReference(t, ref, isEnabled)); | ||
} | ||
if (importHOCSpecifier) { | ||
@@ -53,10 +48,10 @@ // then we locate all usages of the method | ||
if (!binding) return; | ||
const references = binding.referencePaths.filter(ref => t.isCallExpression(ref.container)); // for each of that location we apply a tranformation | ||
const references = binding.referencePaths.filter(ref => t.isCallExpression(ref.container)); | ||
// for each of that location we apply a tranformation | ||
references.forEach(ref => processHOCReference(t, ref)); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
const { | ||
createMacro | ||
} = require('babel-plugin-macros'); | ||
const { | ||
@@ -10,7 +9,4 @@ PACKAGE_NAME, | ||
} = require('./constants'); | ||
const processDIReference = require('./processor-di'); | ||
const processHOCReference = require('./processor-hoc'); | ||
const { | ||
@@ -20,3 +16,2 @@ createNamedImport, | ||
} = require('./utils'); | ||
const diMacro = _ref => { | ||
@@ -34,10 +29,13 @@ let { | ||
const diImport = references[PACKAGE_FUNCTION] || []; | ||
const hocImports = references[HOC_FUNCTION] || []; // process all di calls | ||
const hocImports = references[HOC_FUNCTION] || []; | ||
diImport.forEach(ref => processDIReference(t, ref, isEnabled)); // process all HOC calls | ||
// process all di calls | ||
diImport.forEach(ref => processDIReference(t, ref, isEnabled)); | ||
// process all HOC calls | ||
hocImports.forEach(ref => processHOCReference(t, ref)); | ||
hocImports.forEach(ref => processHOCReference(t, ref)); // if not enabled and only di was imported, let import to be stripped | ||
// if not enabled and only di was imported, let import to be stripped | ||
if (importedMethods.length === 1 && importedMethods[0] === PACKAGE_FUNCTION && !isEnabled) return; | ||
if (importedMethods.length === 1 && importedMethods[0] === PACKAGE_FUNCTION && !isEnabled) return; // add named imports | ||
// add named imports | ||
const { | ||
@@ -49,3 +47,2 @@ scope: methodScope | ||
const targetPath = programPath.get('body').find(p => p.node && p.node.source && p.node.source.value === PACKAGE_NAME + '/macro'); | ||
if (targetPath) { | ||
@@ -60,5 +57,4 @@ // if we find the macro import, we add the clean import right before | ||
}; | ||
module.exports = createMacro(diMacro, { | ||
configName: 'reactMagneticDi' | ||
}); |
@@ -5,19 +5,20 @@ const { | ||
} = require('./utils'); | ||
function processReference(t, ref, isEnabled) { | ||
assert.isValidBlock(t, ref); | ||
assert.isValidCall(t, ref); // from the arguments of the method we generate the list of dependency identifiers | ||
assert.isValidCall(t, ref); | ||
// from the arguments of the method we generate the list of dependency identifiers | ||
const args = ref.container.arguments; | ||
const dependencyIdentifiers = args.map(v => t.identifier(v.name)); | ||
const statement = ref.getStatementParent(); // if should not be enabled, just remove the statement and exit | ||
const statement = ref.getStatementParent(); | ||
// if should not be enabled, just remove the statement and exit | ||
if (!isEnabled) { | ||
statement.remove(); | ||
return; | ||
} // generating variable declarations with array destructuring | ||
} | ||
// generating variable declarations with array destructuring | ||
// assigning them the result of the method call, with arguments | ||
// now wrapped in an array | ||
statement.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.arrayPattern(dependencyIdentifiers), t.callExpression(ref.node, [t.arrayExpression(args), getComponentDeclaration(t, ref.scope) || t.nullLiteral()]))])); | ||
@@ -35,3 +36,2 @@ ref.scope.registerDeclaration(statement); | ||
} | ||
module.exports = processReference; |
@@ -7,3 +7,2 @@ const processReference = (t, ref) => { | ||
}; | ||
module.exports = processReference; |
const getComponentDeclaration = (t, scope) => { | ||
// function declarations | ||
if (scope.parentBlock.declaration) return scope.parentBlock.declaration.id; | ||
if (scope.getBlockParent().block.id) return scope.getBlockParent().block.id; // variable declaration | ||
if (scope.parentBlock.id) return scope.parentBlock.id; // class declarations | ||
if (scope.getBlockParent().block.id) return scope.getBlockParent().block.id; | ||
// variable declaration | ||
if (scope.parentBlock.id) return scope.parentBlock.id; | ||
// class declarations | ||
if (scope.parentBlock.type.includes('Class')) return scope.parent.block.id; | ||
}; | ||
const assert = { | ||
@@ -16,3 +15,2 @@ isValidBlock(t, ref) { | ||
} = ref.scope; | ||
if (!t.isFunctionDeclaration(block) && !t.isFunctionExpression(block) && !t.isArrowFunctionExpression(block) && !t.isClassMethod(block)) { | ||
@@ -22,3 +20,2 @@ throw ref.buildCodeFrameError('Invalid di(...) call: must be inside a render function of a component. '); | ||
}, | ||
isValidCall(t, ref) { | ||
@@ -28,9 +25,6 @@ if (!ref.container.arguments.length) { | ||
} | ||
if (!ref.container.arguments.every(node => t.isIdentifier(node))) { | ||
throw ref.buildCodeFrameError('Invalid di(...) arguments: must be called with plain identifiers. '); | ||
} | ||
const decl = getComponentDeclaration(t, ref.scope); | ||
if (decl && ref.container.arguments.some(v => v.name === decl.name)) { | ||
@@ -40,5 +34,3 @@ throw ref.buildCodeFrameError('Invalid di(...) call: cannot inject self.'); | ||
} | ||
}; | ||
const createNamedImport = (t, pkgName, pkgFns, localNames) => { | ||
@@ -49,7 +41,5 @@ const statement = t.importDeclaration([], t.stringLiteral(pkgName)); | ||
}; | ||
const isEnabledEnv = () => { | ||
return ['development', 'test'].includes(process.env.BABEL_ENV) || ['development', 'test'].includes(process.env.NODE_ENV) || !process.env.BABEL_ENV && !process.env.NODE_ENV; | ||
}; | ||
module.exports = { | ||
@@ -56,0 +46,0 @@ getComponentDeclaration, |
const order = require('./rules/order'); | ||
const exhaustiveInject = require('./rules/exhaustive-inject'); | ||
const noDuplicate = require('./rules/no-duplicate'); | ||
const noExtraneous = require('./rules/no-extraneous'); | ||
const sortDependencies = require('./rules/sort-dependencies'); | ||
module.exports = { | ||
@@ -12,0 +7,0 @@ rules: { |
@@ -10,3 +10,2 @@ const { | ||
} = require('../utils'); | ||
const getReactIdentifiers = node => { | ||
@@ -17,3 +16,2 @@ if (node.source.value === 'react') { | ||
}; | ||
const isDefaultProp = (node, diStatement) => { | ||
@@ -24,3 +22,2 @@ // we assume order rule is enabled, so if the variable is used in an assignment | ||
}; | ||
module.exports = { | ||
@@ -57,13 +54,8 @@ meta: { | ||
}, context.options[0]); | ||
const isInjected = (vars, n) => vars == null ? void 0 : vars.some(v => v.name === n.name); | ||
const isReactIgnored = n => { | ||
var _reactVars; | ||
return (_reactVars = reactVars) == null ? void 0 : _reactVars.some(v => v.name === n.name); | ||
}; | ||
const isOptionsIgnored = n => userOptions.ignore.includes(n.name); | ||
const report = (node, diStatement) => context.report({ | ||
@@ -75,6 +67,4 @@ node: diStatement, | ||
}, | ||
fix(fixer) { | ||
const lastArg = diStatement.expression.arguments.slice(-1)[0]; | ||
if (!lastArg) { | ||
@@ -85,8 +75,5 @@ // if injection without args, let's add the var inside call | ||
} | ||
return fixer.insertTextAfter(lastArg, `, ${node.name}`); | ||
} | ||
}); | ||
return { | ||
@@ -97,3 +84,2 @@ ImportDeclaration(node) { | ||
}, | ||
// this is to handle hooks and components recognised as used variables | ||
@@ -104,4 +90,4 @@ // it does not cover JSX variables | ||
const throughVars = context.getScope().through.map(v => v.identifier).filter(v => v.name !== diIdentifier.name); | ||
const diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
const diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -115,11 +101,10 @@ const diVars = getDiVars(diStatements); | ||
}, | ||
// as JSX elements are not treated as variables, for each JSX tag | ||
// we check if there is a block with di() above and if that includes it | ||
'JSXOpeningElement:exit'(node) { | ||
if (!diIdentifier) return; // ignore if the component is declared locally | ||
if (!diIdentifier) return; | ||
// ignore if the component is declared locally | ||
if (isLocalVariable(node.name, context.getScope())) return; | ||
let varNode; | ||
switch (node.name.type) { | ||
@@ -133,17 +118,13 @@ case 'JSXIdentifier': | ||
} | ||
case 'JSXNamespacedName': | ||
// TODO handle foo:Bar | ||
return; | ||
case 'JSXMemberExpression': | ||
// TODO handle foo.Bar (but ignoring this.Bar) | ||
return; | ||
default: | ||
return; | ||
} | ||
const diStatements = getParentDiStatements(varNode, diIdentifier); // ignore locations where di was not explicitly set | ||
const diStatements = getParentDiStatements(varNode, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -154,5 +135,4 @@ const diVars = getDiVars(diStatements); | ||
} | ||
}; | ||
} | ||
}; |
@@ -6,3 +6,2 @@ const { | ||
} = require('../utils'); | ||
module.exports = { | ||
@@ -24,3 +23,2 @@ meta: { | ||
let diIdentifier; | ||
const report = node => context.report({ | ||
@@ -33,3 +31,2 @@ node, | ||
}); | ||
return { | ||
@@ -39,7 +36,6 @@ ImportDeclaration(node) { | ||
}, | ||
BlockStatement(node) { | ||
if (!diIdentifier) return; | ||
const diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
const diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -49,5 +45,4 @@ const dupeDiVars = getDiVars(diStatements).filter((id, i, arr) => arr.findIndex(n => n.name === id.name) !== i); | ||
} | ||
}; | ||
} | ||
}; |
@@ -7,3 +7,2 @@ const { | ||
} = require('../utils'); | ||
module.exports = { | ||
@@ -37,3 +36,2 @@ meta: { | ||
const blockReferences = new WeakMap(); | ||
const report = node => context.report({ | ||
@@ -44,7 +42,7 @@ node, | ||
name: node.name | ||
} // fix(fixer) { | ||
} | ||
// fix(fixer) { | ||
// const lastArg = diStatement.expression.arguments.slice(-1)[0]; | ||
// return fixer.insertTextAfter(lastArg, `, ${node.name}`); | ||
// }, | ||
}); | ||
@@ -56,3 +54,2 @@ | ||
}, | ||
// this is to handle hooks and components recognised as used variables | ||
@@ -62,4 +59,4 @@ // it does not cover JSX variables | ||
if (!diIdentifier) return; | ||
const diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
const diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
@@ -72,3 +69,2 @@ const diVars = getDiVars(diStatements); | ||
}, | ||
// as JSX elements are not treated as variables, for each JSX tag | ||
@@ -79,3 +75,2 @@ // we check if there is a block with di() above and collect the tag as var | ||
let varNode; | ||
switch (node.name.type) { | ||
@@ -87,15 +82,11 @@ case 'JSXIdentifier': | ||
} | ||
case 'JSXNamespacedName': | ||
// TODO handle foo:Bar | ||
return; | ||
case 'JSXMemberExpression': | ||
// TODO handle foo.Bar (but ignoring this.Bar) | ||
return; | ||
default: | ||
return; | ||
} | ||
const parentBlock = getParentDiBlock(varNode, diIdentifier); | ||
@@ -106,3 +97,2 @@ const blockVars = blockReferences.get(parentBlock); | ||
}, | ||
'BlockStatement:exit'(node) { | ||
@@ -118,5 +108,4 @@ if (!diIdentifier) return; | ||
} | ||
}; | ||
} | ||
}; |
@@ -5,3 +5,2 @@ const { | ||
} = require('../utils'); | ||
module.exports = { | ||
@@ -27,3 +26,2 @@ meta: { | ||
}, | ||
BlockStatement(node) { | ||
@@ -34,3 +32,2 @@ if (!diIdentifier) return; | ||
const prev = node.body[i - 1]; | ||
if (!isDiStatement(prev, diIdentifier)) { | ||
@@ -44,5 +41,4 @@ context.report({ | ||
} | ||
}; | ||
} | ||
}; |
const { | ||
getDiIdentifier, | ||
getDiStatements | ||
} = require('../utils'); // use simple numeric comparison to have uppercase (components) first | ||
} = require('../utils'); | ||
// use simple numeric comparison to have uppercase (components) first | ||
const compareName = (a, b) => a.name > b.name ? 1 : -1; | ||
module.exports = { | ||
@@ -25,3 +24,2 @@ meta: { | ||
let diIdentifier; | ||
const report = (node, prevNode, args, sortedArgs) => context.report({ | ||
@@ -34,3 +32,2 @@ node, | ||
}, | ||
fix(fixer) { | ||
@@ -41,10 +38,8 @@ // grab whatever between 1st arg end / 2nd arg start as separator | ||
const end = args[args.length - 1].range[1]; | ||
const sorted = sortedArgs.map(n => n.name).join(separator); // fixes all order issues at once | ||
const sorted = sortedArgs.map(n => n.name).join(separator); | ||
// fixes all order issues at once | ||
// so avoids the need of multiple saves | ||
return fixer.replaceTextRange([start, end], sorted); | ||
} | ||
}); | ||
return { | ||
@@ -54,16 +49,14 @@ ImportDeclaration(node) { | ||
}, | ||
BlockStatement(node) { | ||
if (!diIdentifier) return; | ||
const diStatements = getDiStatements(node, diIdentifier); // ignore locations where di was not explicitly set | ||
const diStatements = getDiStatements(node, diIdentifier); | ||
// ignore locations where di was not explicitly set | ||
if (!diStatements.length) return; | ||
diStatements.forEach(statement => { | ||
const args = statement.expression.arguments; // sort uppercase first, lowercase after | ||
const args = statement.expression.arguments; | ||
// sort uppercase first, lowercase after | ||
// so we get components and hooks grouped | ||
const sortedArgs = args.slice().sort(compareName); | ||
args.forEach((arg, i) => { | ||
const prevArg = args[i - 1]; | ||
if (prevArg && compareName(arg, prevArg) < 0) { | ||
@@ -75,5 +68,4 @@ report(arg, prevArg, args, sortedArgs); | ||
} | ||
}; | ||
} | ||
}; |
const PACKAGE_NAME = 'react-magnetic-di'; | ||
const PACKAGE_FUNCTION = 'di'; | ||
const isDiStatement = (stm, spec) => stm.type === 'ExpressionStatement' && stm.expression && stm.expression.callee && stm.expression.callee.name === spec.name; | ||
const isHookName = node => /^use[A-Z0-9].*$/.test(node.name); | ||
const isComponentName = node => !/^[a-z]/.test(node.name); | ||
const isLocalVariable = (node, scope) => { | ||
do { | ||
var _scope; | ||
// if we reach module/global scope then is not local | ||
if (scope.type === 'module' || scope.type === 'global') return false; | ||
const isLocal = (_scope = scope) == null ? void 0 : _scope.variables.some(v => v.name === node.name); | ||
if (isLocal) return true; // eslint-disable-next-line no-cond-assign | ||
if (isLocal) return true; | ||
// eslint-disable-next-line no-cond-assign | ||
} while (scope = scope.upper); | ||
return false; | ||
}; | ||
const getDiIdentifier = node => { | ||
const importSource = node.source.value; | ||
const importSpecifier = node.specifiers.find(s => s.imported && s.imported.name === PACKAGE_FUNCTION); | ||
if (importSource.startsWith(PACKAGE_NAME) && importSpecifier) { | ||
@@ -31,5 +25,3 @@ return importSpecifier.local; | ||
}; | ||
const getDiStatements = (node, diIdentifier) => (node.body || []).reduce((acc, statement) => isDiStatement(statement, diIdentifier) ? acc.concat(statement) : acc, []); | ||
const getParentDiBlock = (node, diIdentifier) => { | ||
@@ -42,6 +34,4 @@ // eslint-disable-next-line no-cond-assign | ||
} | ||
return null; | ||
}; | ||
const getParentDiStatements = (node, diIdentifier) => { | ||
@@ -52,5 +42,3 @@ const parentBlock = getParentDiBlock(node, diIdentifier); | ||
}; | ||
const getDiVars = statements => statements.reduce((acc, s) => acc.concat(s.expression.arguments), []); | ||
module.exports = { | ||
@@ -57,0 +45,0 @@ isDiStatement, |
import { PACKAGE_NAME } from './constants'; | ||
import { Context } from './context'; | ||
import { warnOnce, mock } from './utils'; | ||
function di(deps, target) { | ||
@@ -18,6 +17,5 @@ // check if babel plugin has been added | ||
} | ||
/** @deprecated use injectable instead */ | ||
di.mock = mock; | ||
export { di }; |
@@ -6,3 +6,2 @@ import React from 'react'; | ||
} | ||
}); |
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'; | ||
@@ -16,8 +15,9 @@ import PropTypes from 'prop-types'; | ||
getDependencies | ||
} = useContext(Context); // memo provider value so gets computed only once | ||
} = useContext(Context); | ||
// memo provider value so gets computed only once | ||
const value = useMemo(() => { | ||
// create a map of dependency real -> replacement for fast lookup | ||
const replacementMap = use.reduce((m, d) => m.set(d[KEY], d), new Map()); // support single or multiple targets | ||
const replacementMap = use.reduce((m, d) => m.set(d[KEY], d), new Map()); | ||
// support single or multiple targets | ||
const targets = target && (Array.isArray(target) ? target : [target]); | ||
@@ -27,4 +27,4 @@ return { | ||
// First we collect dependencies from parent provider(s) (if any) | ||
const dependencies = getDependencies(realDeps, targetChild); // If no target or target is in the array of targets, map use | ||
const dependencies = getDependencies(realDeps, targetChild); | ||
// If no target or target is in the array of targets, map use | ||
if (!targetChild || !targets || targets.includes(targetChild)) { | ||
@@ -40,6 +40,4 @@ return dependencies.map(dep => { | ||
} | ||
return dependencies; | ||
} | ||
}; | ||
@@ -61,3 +59,2 @@ }, [getDependencies]); // ignore use & target props | ||
} | ||
const WrappedComponent = /*#__PURE__*/forwardRef((props, ref) => /*#__PURE__*/React.createElement(DiProvider, { | ||
@@ -64,0 +61,0 @@ use: deps, |
@@ -14,3 +14,2 @@ import { KEY } from './constants'; | ||
} | ||
const name = Comp.displayName || Comp.name; | ||
@@ -21,12 +20,10 @@ return !name || !wrapper ? name : `${wrapper}(${name})`; | ||
implementation.displayName = getDisplayName(implementation) || getDisplayName(from, 'di'); | ||
if (implementation[KEY] && implementation[KEY] !== from) { | ||
warnOnce(`You are trying to use replacement "${implementation.displayName}" on multiple injectables. ` + `That will override only the last dependency, as each replacement is uniquely linked.`); | ||
} | ||
implementation[KEY] = from; | ||
return implementation; | ||
} | ||
/** @deprecated use injectable instead */ | ||
export const mock = injectable; |
{ | ||
"name": "react-magnetic-di", | ||
"version": "2.2.10", | ||
"version": "2.2.11", | ||
"description": "Context driven dependency injection", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
"prop-types": "^15.0.0", | ||
"react": "^16.9.0 || ^17.0.0" | ||
"react": "^16.9.0 || ^17.0.0 || ^18.0.0" | ||
}, | ||
@@ -51,16 +51,15 @@ "peerDependenciesMeta": { | ||
"@babel/cli": "^7.19.3", | ||
"@babel/core": "^7.19.3", | ||
"@babel/core": "^7.20.2", | ||
"@babel/eslint-parser": "^7.19.1", | ||
"@babel/plugin-proposal-class-properties": "^7.18.6", | ||
"@babel/plugin-transform-runtime": "^7.19.1", | ||
"@babel/preset-env": "^7.19.3", | ||
"@babel/plugin-transform-runtime": "^7.19.6", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-flow": "^7.18.6", | ||
"@babel/preset-react": "^7.18.6", | ||
"@babel/runtime": "^7.19.0", | ||
"@testing-library/react": "^12.1.2", | ||
"@types/jest": "^29.1.1", | ||
"@types/react": "^17.0.38", | ||
"@types/react-dom": "^17.0.11", | ||
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.7", | ||
"babel-jest": "^29.1.2", | ||
"@babel/runtime": "^7.20.1", | ||
"@testing-library/react": "^13.4.0", | ||
"@types/jest": "^29.2.3", | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.9", | ||
"babel-jest": "^29.3.1", | ||
"babel-loader": "^8.2.5", | ||
@@ -70,18 +69,17 @@ "babel-plugin-macros": "^3.1.0", | ||
"dtslint": "^4.2.1", | ||
"enzyme": "^3.11.0", | ||
"eslint": "^8.24.0", | ||
"eslint": "^8.27.0", | ||
"eslint-plugin-flowtype": "^8.0.3", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-local": "^1.0.0", | ||
"eslint-plugin-react": "^7.31.8", | ||
"eslint-plugin-react": "^7.31.10", | ||
"eslint-plugin-react-hooks": "^4.6.0", | ||
"flow-bin": "^0.169.0", | ||
"flow-copy-source": "^2.0.9", | ||
"jest": "^29.1.2", | ||
"jest-environment-jsdom": "^29.1.2", | ||
"jest": "^29.3.1", | ||
"jest-environment-jsdom": "^29.3.1", | ||
"prettier": "^2.7.1", | ||
"prop-types": "^15.8.1", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"typescript": "^4.8.4", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"typescript": "^4.9.3", | ||
"webpack": "^4.46.0", | ||
@@ -88,0 +86,0 @@ "webpack-cli": "^3.3.12", |
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
36
1816
87722