UNPKG

react-magnetic-di

Version:
164 lines (162 loc) 9.34 kB
"use strict"; 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var _require = require('./constants'), PACKAGE_NAME = _require.PACKAGE_NAME; var getComponentDeclaration = function 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.parentBlock.type.includes('Class')) return scope.parent.block.id; return null; }; var assert = { isValidArgument: function isValidArgument(t, node, ref, self) { if (!t.isIdentifier(node)) { throw ref.buildCodeFrameError('Invalid di(...) arguments: must be called with plain identifiers. '); } if (node.name === (self === null || self === void 0 ? void 0 : self.name)) { throw ref.buildCodeFrameError('Invalid di(...) call: cannot inject self.'); } }, isValidLocation: function isValidLocation(path, ref) { if (!path) { throw ref.buildCodeFrameError('Invalid di(...) call: must be inside a render function of a component. '); } } }; var createNamedImport = function createNamedImport(t, pkgName, pkgFns, localNames) { var statement = t.importDeclaration([], t.stringLiteral(pkgName)); statement.specifiers = pkgFns.map(function (v, i) { return t.importSpecifier(t.identifier(localNames[i].name), t.identifier(v)); }); return statement; }; function collectDepsReferencePaths(t, bodyPaths) { var references = []; var imports = { sources: new Map(), specifiers: new Map() }; function addRef(path) { var importSource = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var _ref = path.scope.getBinding(path) || {}, _ref$referencePaths = _ref.referencePaths, referencePaths = _ref$referencePaths === void 0 ? [] : _ref$referencePaths; references.push.apply(references, _toConsumableArray(referencePaths)); if (importSource) { imports.specifiers.set(path.node.name, importSource); imports.sources.set(importSource, (imports.sources.get(importSource) || []).concat(path.node.name)); } } // we could use scope.bindings to get all top level bindings // but it is hard to track local only vs later exported values bodyPaths.forEach(function (path) { if (path.isImportDeclaration()) { if (path.node.importKind === 'type') return; if (path.node.source.value === PACKAGE_NAME) return; path.get('specifiers').forEach(function (sp) { if (sp.node.importKind === 'type') return; if (sp.isImportDefaultSpecifier() || sp.isImportSpecifier()) { addRef(sp.get('local'), path.node.source.value); } }); } if (path.isExportNamedDeclaration()) { if (path.node.exportKind === 'type') return; if (path.node.declaration) { if (path.get('declaration.id').isIdentifier()) { addRef(path.get('declaration.id')); } else { path.get('declaration.declarations').forEach(function (dp) { if (dp.get('id').isIdentifier()) { addRef(dp.get('id')); } }); } } else { path.get('specifiers').forEach(function (sp) { if (sp.node.exportKind === 'type') return; if (sp.get('local').isIdentifier()) { addRef(sp.get('local')); } }); } } if (path.isExportDefaultDeclaration()) { if (path.node.exportKind === 'type') return; var ref = path.get('declaration').isIdentifier() ? path.get('declaration') : path.get('declaration.id'); if (ref.isIdentifier()) { addRef(ref); } } }); return { references: references, imports: imports }; } function collectReferencePaths(t, identifier, scope) { // we locate all usages of the method var _ref2 = scope.getBinding(identifier.name) || {}, _ref2$referencePaths = _ref2.referencePaths, referencePaths = _ref2$referencePaths === void 0 ? [] : _ref2$referencePaths; return referencePaths.filter(function (ref) { return t.isCallExpression(ref.container); }); } var parsePathsToRegexp = function parsePathsToRegexp() { var paths = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return paths.map(function (v) { return v instanceof RegExp ? v : new RegExp(v.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')); }); }; var parseOptions = function parseOptions(opts) { var _opts$defaultMockedMo, _opts$defaultMockedMo2; return _objectSpread(_objectSpread({}, opts), {}, { exclude: parsePathsToRegexp(opts.exclude), defaultMockedModules: { include: parsePathsToRegexp(Array.isArray(opts.defaultMockedModules) ? opts.defaultMockedModules : (_opts$defaultMockedMo = opts.defaultMockedModules) === null || _opts$defaultMockedMo === void 0 ? void 0 : _opts$defaultMockedMo.include), exclude: parsePathsToRegexp((_opts$defaultMockedMo2 = opts.defaultMockedModules) === null || _opts$defaultMockedMo2 === void 0 ? void 0 : _opts$defaultMockedMo2.exclude) } }); }; var isMatchingAny = function isMatchingAny(regexes, value) { return regexes.some(function (rx) { return rx.test(value); }); }; var isEnabledEnv = function isEnabledEnv() { var enabledEnvs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['development', 'test']; return enabledEnvs.includes(process.env.BABEL_ENV) || enabledEnvs.includes(process.env.NODE_ENV); }; var hasDisableComment = function hasDisableComment(path) { 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'); }); }; module.exports = { assert: assert, createNamedImport: createNamedImport, collectReferencePaths: collectReferencePaths, collectDepsReferencePaths: collectDepsReferencePaths, getComponentDeclaration: getComponentDeclaration, isEnabledEnv: isEnabledEnv, isMatchingAny: isMatchingAny, hasDisableComment: hasDisableComment, parseOptions: parseOptions };