the-forest/client/node_modules/eslint-plugin-import/lib/scc.js
2024-09-17 20:35:18 -04:00

86 lines
12 KiB
JavaScript

'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _createClass = function () {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, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _scc = require('@rtsao/scc');var _scc2 = _interopRequireDefault(_scc);
var _hash = require('eslint-module-utils/hash');
var _resolve = require('eslint-module-utils/resolve');var _resolve2 = _interopRequireDefault(_resolve);
var _builder = require('./exportMap/builder');var _builder2 = _interopRequireDefault(_builder);
var _childContext = require('./exportMap/childContext');var _childContext2 = _interopRequireDefault(_childContext);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {arr2[i] = arr[i];}return arr2;} else {return Array.from(arr);}}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}
var cache = new Map();var
StronglyConnectedComponentsBuilder = function () {function StronglyConnectedComponentsBuilder() {_classCallCheck(this, StronglyConnectedComponentsBuilder);}_createClass(StronglyConnectedComponentsBuilder, null, [{ key: 'clearCache', value: function () {function clearCache()
{
cache = new Map();
}return clearCache;}() }, { key: 'get', value: function () {function get(
source, context) {
var path = (0, _resolve2['default'])(source, context);
if (path == null) {return null;}
return StronglyConnectedComponentsBuilder['for']((0, _childContext2['default'])(path, context));
}return get;}() }, { key: 'for', value: function () {function _for(
context) {
var cacheKey = context.cacheKey || (0, _hash.hashObject)(context).digest('hex');
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
var scc = StronglyConnectedComponentsBuilder.calculate(context);
cache.set(cacheKey, scc);
return scc;
}return _for;}() }, { key: 'calculate', value: function () {function calculate(
context) {
var exportMap = _builder2['default']['for'](context);
var adjacencyList = this.exportMapToAdjacencyList(exportMap);
var calculatedScc = (0, _scc2['default'])(adjacencyList);
return StronglyConnectedComponentsBuilder.calculatedSccToPlainObject(calculatedScc);
}return calculate;}()
/** @returns {Map<string, Set<string>>} for each dep, what are its direct deps */ }, { key: 'exportMapToAdjacencyList', value: function () {function exportMapToAdjacencyList(
initialExportMap) {
var adjacencyList = new Map();
// BFS
function visitNode(exportMap) {
if (!exportMap) {
return;
}
exportMap.imports.forEach(function (v, importedPath) {
var from = exportMap.path;
var to = importedPath;
// Ignore type-only imports, because we care only about SCCs of value imports
var toTraverse = [].concat(_toConsumableArray(v.declarations)).filter(function (_ref) {var isOnlyImportingTypes = _ref.isOnlyImportingTypes;return !isOnlyImportingTypes;});
if (toTraverse.length === 0) {return;}
if (!adjacencyList.has(from)) {
adjacencyList.set(from, new Set());
}
if (adjacencyList.get(from).has(to)) {
return; // prevent endless loop
}
adjacencyList.get(from).add(to);
visitNode(v.getter());
});
}
visitNode(initialExportMap);
// Fill gaps
adjacencyList.forEach(function (values) {
values.forEach(function (value) {
if (!adjacencyList.has(value)) {
adjacencyList.set(value, new Set());
}
});
});
return adjacencyList;
}return exportMapToAdjacencyList;}()
/** @returns {Record<string, number>} for each key, its SCC's index */ }, { key: 'calculatedSccToPlainObject', value: function () {function calculatedSccToPlainObject(
sccs) {
var obj = {};
sccs.forEach(function (scc, index) {
scc.forEach(function (node) {
obj[node] = index;
});
});
return obj;
}return calculatedSccToPlainObject;}() }]);return StronglyConnectedComponentsBuilder;}();exports['default'] = StronglyConnectedComponentsBuilder;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9zY2MuanMiXSwibmFtZXMiOlsiY2FjaGUiLCJNYXAiLCJTdHJvbmdseUNvbm5lY3R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