dependency-cruiser 17.3.4-beta-1 → 17.3.5-beta-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dependency-cruiser",
3
- "version": "17.3.4-beta-1",
3
+ "version": "17.3.5-beta-2",
4
4
  "description": "Validate and visualize dependencies. With your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.",
5
5
  "keywords": [
6
6
  "static analysis",
@@ -1,5 +1,7 @@
1
1
  /** @import { IFlattenedRuleSet } from "../../../types/rule-set.mjs" */
2
2
 
3
+ import ModuleGraphWithDependencySet from "#graph-utl/module-graph-with-dependency-set.mjs";
4
+
3
5
  function isDependentsRule(pRule) {
4
6
  // used in folder rules and when moreUnstable is in the 'to' => governed by
5
7
  // the 'metrics' flag in options, so not going to repeat that here
@@ -15,13 +17,6 @@ function isDependentsRule(pRule) {
15
17
  );
16
18
  }
17
19
 
18
- export function getDependents(pModule, pModulesWithDependencySet) {
19
- // perf O(n) as dependency-lookups are O(1)
20
- return pModulesWithDependencySet
21
- .filter(({ dependencies }) => dependencies.has(pModule.source))
22
- .map((pDependentModule) => pDependentModule.source);
23
- }
24
-
25
20
  /**
26
21
  * @param {IFlattenedRuleSet} pRuleSet
27
22
  * @returns {boolean}
@@ -42,15 +37,13 @@ export default function addDependents(
42
37
  // large graphs it pays off significantly - creation is a few centiseconds
43
38
  // but it cuts analysis in half on large graphs (and even on smaller
44
39
  // graphs it's a win, even though just a few milliseconds)
45
- const lModulesWithDependencySet = pModules.map((pModule) => ({
46
- source: pModule.source,
47
- dependencies: new Set(
48
- pModule.dependencies.map((pDependency) => pDependency.resolved),
49
- ),
50
- }));
40
+ const lModulesWithDependencySet = new ModuleGraphWithDependencySet(
41
+ pModules,
42
+ );
43
+
51
44
  return pModules.map((pModule) => ({
52
45
  ...pModule,
53
- dependents: getDependents(pModule, lModulesWithDependencySet),
46
+ dependents: lModulesWithDependencySet.getDependents(pModule),
54
47
  }));
55
48
  }
56
49
  return pModules;
@@ -1,10 +1,3 @@
1
- export function isDependent(pResolvedName) {
2
- return (pModule) =>
3
- pModule.dependencies.some(
4
- (pDependency) => pDependency.resolved === pResolvedName,
5
- );
6
- }
7
-
8
1
  export function metricsAreCalculable(pModule) {
9
2
  return (
10
3
  !pModule.coreModule &&
@@ -1,4 +1,5 @@
1
1
  import isOrphan from "./is-orphan.mjs";
2
+ import ModuleGraphWithDependencySet from "#graph-utl/module-graph-with-dependency-set.mjs";
2
3
 
3
4
  /** @import { IFlattenedRuleSet } from "../../../../types/rule-set.mjs" */
4
5
 
@@ -25,9 +26,19 @@ export default function deriveOrphans(
25
26
  { skipAnalysisNotInRules, ruleSet },
26
27
  ) {
27
28
  if (!skipAnalysisNotInRules || hasOrphanRule(ruleSet)) {
29
+ // just like the dependents derivation, creating this data structure
30
+ // might seem overkill, but it can save effort here as well, even though
31
+ // isOrphan has early exits the dependents derivation doesn't:
32
+ // - when the module already has dependencies (quick length check)
33
+ // - when dependents are already calculated (quick length check as well). If
34
+ // they are, the lModulesWithDependencySet is created for naught.
35
+ const lModulesWithDependencySet = new ModuleGraphWithDependencySet(
36
+ pModules,
37
+ );
38
+
28
39
  return pModules.map((pModule) => ({
29
40
  ...pModule,
30
- orphan: isOrphan(pModule, pModules),
41
+ orphan: isOrphan(pModule, lModulesWithDependencySet),
31
42
  }));
32
43
  }
33
44
  return pModules;
@@ -1,6 +1,11 @@
1
- import { isDependent } from "../module-utl.mjs";
2
-
3
- export default function isOrphan(pModule, pGraph) {
1
+ /**
2
+ * returns true if the given module has no dependencies and no dependents
3
+ *
4
+ * @param {import("../../../../types/dependency-cruiser.mjs").IModule} pModule
5
+ * @param {import("#graph-utl/module-graph-with-dependency-set.mjs").default} pModulesWithDependencySet
6
+ * @returns {boolean}
7
+ */
8
+ export default function isOrphan(pModule, pModulesWithDependencySet) {
4
9
  if (pModule.dependencies.length > 0) {
5
10
  return false;
6
11
  }
@@ -10,5 +15,5 @@ export default function isOrphan(pModule, pGraph) {
10
15
  return pModule.dependents.length === 0;
11
16
  }
12
17
  // ... otherwise calculate them
13
- return !pGraph.some(isDependent(pModule.source));
18
+ return !pModulesWithDependencySet.moduleHasDependents(pModule);
14
19
  }
@@ -0,0 +1,41 @@
1
+ export default class ModuleGraphWithDependencySet {
2
+ /** @type {Array<{source: string, dependencies: Set<string>}>} */
3
+ #modulesWithDependencySet;
4
+ /**
5
+ * Creates a module graph optimized for querying dependents
6
+ * @constructor
7
+ * @param {import("../../types/dependency-cruiser.mjs").IModule[]} pModules
8
+ */
9
+ constructor(pModules) {
10
+ this.#modulesWithDependencySet = pModules.map((pModule) => ({
11
+ source: pModule.source,
12
+ dependencies: new Set(
13
+ pModule.dependencies.map((pDependency) => pDependency.resolved),
14
+ ),
15
+ }));
16
+ }
17
+
18
+ /**
19
+ * Returns true if the given module has dependents in the graph, false otherwise
20
+ *
21
+ * @param {import("../../types/dependency-cruiser.mjs").IModule} pModule
22
+ * @returns {boolean}
23
+ */
24
+ moduleHasDependents(pModule) {
25
+ return this.#modulesWithDependencySet.some(({ dependencies }) =>
26
+ dependencies.has(pModule.source),
27
+ );
28
+ }
29
+
30
+ /**
31
+ * Returns an array of module source paths that depend on the given module
32
+ *
33
+ * @param {import("../../types/dependency-cruiser.mjs").IModule} pModule
34
+ * @returns {Array<string>}
35
+ */
36
+ getDependents(pModule) {
37
+ return this.#modulesWithDependencySet
38
+ .filter(({ dependencies }) => dependencies.has(pModule.source))
39
+ .map(({ source }) => source);
40
+ }
41
+ }
package/src/meta.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /* generated - don't edit */
2
2
  module.exports = {
3
- version: "17.3.4-beta-1",
3
+ version: "17.3.5-beta-2",
4
4
  engines: {
5
5
  node: "^20.12||^22||>=24",
6
6
  },