dependency-cruiser 18.8.0-beta-2 → 18.8.0

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": "18.8.0-beta-2",
3
+ "version": "18.8.0",
4
4
  "description": "Validate and visualize dependencies. With your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.",
5
5
  "keywords": [
6
6
  "static analysis",
@@ -20,6 +20,7 @@
20
20
  "contributors": [
21
21
  "0xflotus (https://github.com/0xflotus)",
22
22
  "Álvaro Cuesta (https://github.com/alvaro-cuesta)",
23
+ "Arpit Jain (https://github.com/arpitjain099)",
23
24
  "anna (https://github.com/annamooseity)",
24
25
  "Bastian Hess (https://github.com/bashess)",
25
26
  "Bin (https://github.com/soulhat)",
@@ -30,6 +31,7 @@
30
31
  "Daniel Edholm Ignat (https://github.com/dignite)",
31
32
  "Daniel Rodríguez Rivero (https://danielorodriguez.com)",
32
33
  "davidparkagoda (https://github.com/davidparkagoda)",
34
+ "Dmytro Kurovskyi (https://github.com/kurovskyiii)",
33
35
  "electrovir (https://github.com/electrovir)",
34
36
  "Emily Marigold Klassen (https://forivall.com)",
35
37
  "Frederik Schubert (https://github.com/ferdynator)",
@@ -40,6 +42,7 @@
40
42
  "Ivan (https://github.com/Winner95)",
41
43
  "Jeremy Magland (https://github.com/magland)",
42
44
  "Jessica Kerr (https://jessitron.com)",
45
+ "João Ribeiro (https://github.com/JonnyBGod)",
43
46
  "Jon Lauridsen (https://jonlauridsen.com)",
44
47
  "Joshua T (https://github.com/radiantly)",
45
48
  "KenjiroKubota (https://github.com/kubotak-is)",
@@ -55,12 +58,15 @@
55
58
  "Radosław Kłos (https://klos.dev)",
56
59
  "Richard Musiol (https://github.com/neelance)",
57
60
  "Roy Swinkels (https://github.com/donroyco)",
61
+ "Samuel Gunter (https://github.com/Samathingamajig)",
62
+ "Sebastian Danielsson (https://github.com/sebdanielsson)",
58
63
  "Sebastian Landwehr (https://sebastianlandwehr.com)",
59
64
  "Sharang Pai (https://sharangpai.me)",
60
65
  "Stefan Gojan (https://stefan-gojan.de)",
61
66
  "Tharun Rajendran (https://github.com/tharun208)",
62
67
  "TruongSinh Tran-Nguyen (https://truongsinh.pro)",
63
68
  "Valentin Semirulnik (https://github.com/7rulnik)",
69
+ "Yohannes (https://github.com/Johannes7549)",
64
70
  "Yuanhai He (https://bestmike007.com)",
65
71
  "정건우 (https://www.zigae.com/)"
66
72
  ],
@@ -148,9 +154,9 @@
148
154
  "acorn-jsx-walk": "2.0.0",
149
155
  "acorn-loose": "8.5.2",
150
156
  "acorn-walk": "8.3.5",
151
- "commander": "15.0.0",
152
- "enhanced-resolve": "5.26.0",
153
- "ignore": "7.0.12",
157
+ "commander": "15.1.0",
158
+ "enhanced-resolve": "5.27.0",
159
+ "ignore": "7.0.14",
154
160
  "interpret": "3.1.1",
155
161
  "is-installed-globally": "1.0.1",
156
162
  "json5": "2.2.3",
@@ -158,7 +164,7 @@
158
164
  "prompts": "2.4.2",
159
165
  "rechoir": "0.8.0",
160
166
  "safe-regex": "2.1.1",
161
- "semver": "7.8.5",
167
+ "semver": "7.8.6",
162
168
  "tsconfig-paths-webpack-plugin": "4.2.0",
163
169
  "watskeburt": "6.0.0"
164
170
  },
@@ -89,17 +89,22 @@ function determineManifestDependencyTypes(
89
89
  * an array, and not an object, hence needs different treatment
90
90
  */
91
91
  function dependencyIsBundled(pModule, pPackageDeps) {
92
- let lReturnValue = false;
93
-
94
92
  if (pPackageDeps) {
95
93
  const lBundledDependencies =
96
94
  pPackageDeps.bundledDependencies || pPackageDeps.bundleDependencies;
95
+ // "bundledDependencies": true means: all production dependencies get
96
+ // bundled
97
+ if (lBundledDependencies === true) {
98
+ return Object.hasOwn(pPackageDeps.dependencies || {}, pModule);
99
+ }
97
100
 
98
101
  if (lBundledDependencies) {
99
- lReturnValue = lBundledDependencies.includes(pModule);
102
+ return lBundledDependencies.includes(pModule);
100
103
  }
101
104
  }
102
- return lReturnValue;
105
+ // false when there's no bundledDependencies or
106
+ // when bundledDependencies equals false
107
+ return false;
103
108
  }
104
109
 
105
110
  /**
@@ -116,9 +121,10 @@ function determineNodeModuleDependencyTypes(
116
121
  pFileDirectory,
117
122
  pResolveOptions,
118
123
  ) {
124
+ const lPackageRoot = getPackageRoot(pModuleName);
119
125
  /** @type { DependencyType[] } */
120
126
  const lReturnValue = determineManifestDependencyTypes(
121
- getPackageRoot(pModuleName),
127
+ lPackageRoot,
122
128
  pPackageDeps,
123
129
  pResolveOptions.modules,
124
130
  );
@@ -128,7 +134,7 @@ function determineNodeModuleDependencyTypes(
128
134
  ) {
129
135
  lReturnValue.push("deprecated");
130
136
  }
131
- if (dependencyIsBundled(pModuleName, pPackageDeps)) {
137
+ if (dependencyIsBundled(lPackageRoot, pPackageDeps)) {
132
138
  lReturnValue.push("npm-bundled");
133
139
  }
134
140
  return lReturnValue;
@@ -1,7 +1,7 @@
1
1
  import { uniq } from "#utl/array-util.mjs";
2
2
 
3
3
  /* eslint-disable security/detect-object-injection */
4
- function normalizeManifestKeys(pManifest) {
4
+ function normalizeBundleManifestKeys(pManifest) {
5
5
  const lReturnValue = pManifest;
6
6
 
7
7
  if (pManifest.bundleDependencies) {
@@ -10,6 +10,9 @@ function normalizeManifestKeys(pManifest) {
10
10
  );
11
11
  Reflect.deleteProperty(pManifest, "bundleDependencies");
12
12
  }
13
+ if (pManifest.bundledDependencies === true) {
14
+ pManifest.bundledDependencies = Object.keys(pManifest.dependencies || {});
15
+ }
13
16
  return lReturnValue;
14
17
  }
15
18
 
@@ -64,8 +67,8 @@ function isAnArrayKey(pKey) {
64
67
  */
65
68
  export default function mergeManifests(pClosestManifest, pFurtherManifest) {
66
69
  return getJointUniqueDependencyKeys(
67
- normalizeManifestKeys(pClosestManifest),
68
- normalizeManifestKeys(pFurtherManifest),
70
+ normalizeBundleManifestKeys(pClosestManifest),
71
+ normalizeBundleManifestKeys(pFurtherManifest),
69
72
  )
70
73
  .map((pKey) => ({
71
74
  key: pKey,
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Groups the modules that are on a cycle, so callers can quickly tell whether
3
+ * a dependency could be part of one.
4
+ *
5
+ * Two modules end up in the same group when each one can reach the other by
6
+ * following dependencies. So a dependency from a to b can only be part of a
7
+ * cycle when a and b are in the same group. Modules that aren't on any cycle
8
+ * don't get a group.
9
+ *
10
+ * Uses Tarjan's algorithm for strongly connected components.
11
+ *
12
+ * @param {Map<string, string[]>} pGraph module name => names of the modules it depends on
13
+ * @returns {Map<string, number>} module name => group number
14
+ */
15
+ export default function findCycleComponents(pGraph) {
16
+ /** @type {Map<string, number>} */
17
+ const lIndexOf = new Map();
18
+ /** @type {Map<string, number>} */
19
+ const lLowLinkOf = new Map();
20
+ /** @type {string[]} */
21
+ const lComponentStack = [];
22
+ const lOnComponentStack = new Set();
23
+ /** @type {Map<string, number>} */
24
+ const lReturnValue = new Map();
25
+ let lNextIndex = 0;
26
+ let lNextComponent = 0;
27
+
28
+ /**
29
+ * A module, the modules it depends on, and the index of the next one to follow
30
+ * @typedef {{name: string, successors: string[], nextEdge: number}} IFrame
31
+ */
32
+
33
+ /** @returns {IFrame} */
34
+ function visit(pName) {
35
+ lIndexOf.set(pName, lNextIndex);
36
+ lLowLinkOf.set(pName, lNextIndex);
37
+ lNextIndex += 1;
38
+ lComponentStack.push(pName);
39
+ lOnComponentStack.add(pName);
40
+
41
+ return { name: pName, successors: pGraph.get(pName), nextEdge: 0 };
42
+ }
43
+
44
+ function lowerLowLink(pName, pCandidate) {
45
+ lLowLinkOf.set(pName, Math.min(lLowLinkOf.get(pName), pCandidate));
46
+ }
47
+
48
+ /** @param {IFrame} pRoot */
49
+ function closeComponent(pRoot) {
50
+ const lMembers = [];
51
+ let lMember;
52
+
53
+ do {
54
+ lMember = lComponentStack.pop();
55
+ lOnComponentStack.delete(lMember);
56
+ lMembers.push(lMember);
57
+ } while (lMember !== pRoot.name);
58
+
59
+ if (lMembers.length > 1 || pRoot.successors.includes(pRoot.name)) {
60
+ for (const lName of lMembers) {
61
+ lReturnValue.set(lName, lNextComponent);
62
+ }
63
+ lNextComponent += 1;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Follows the next edge of the frame on top of the stack: visits the
69
+ * successor when it's new, or takes its index into account when it's on
70
+ * the component stack.
71
+ *
72
+ * @param {IFrame[]} pFrames
73
+ */
74
+ function followNextEdge(pFrames) {
75
+ const lFrame = pFrames.at(-1);
76
+ const lSuccessor = lFrame.successors[lFrame.nextEdge];
77
+
78
+ lFrame.nextEdge += 1;
79
+ if (!pGraph.has(lSuccessor)) {
80
+ return;
81
+ }
82
+ if (!lIndexOf.has(lSuccessor)) {
83
+ pFrames.push(visit(lSuccessor));
84
+ } else if (lOnComponentStack.has(lSuccessor)) {
85
+ lowerLowLink(lFrame.name, lIndexOf.get(lSuccessor));
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Pops the frame on top of the stack once all its edges are followed,
91
+ * passes its low link on to its parent and closes the component when
92
+ * it's the component's root.
93
+ *
94
+ * @param {IFrame[]} pFrames
95
+ */
96
+ function finishFrame(pFrames) {
97
+ const lFrame = pFrames.pop();
98
+
99
+ if (pFrames.length > 0) {
100
+ lowerLowLink(pFrames.at(-1).name, lLowLinkOf.get(lFrame.name));
101
+ }
102
+ if (lLowLinkOf.get(lFrame.name) === lIndexOf.get(lFrame.name)) {
103
+ closeComponent(lFrame);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Runs the depth-first search from pStart, with an explicit stack instead
109
+ * of recursion so deep graphs don't exhaust the call stack.
110
+ *
111
+ * @param {string} pStart
112
+ */
113
+ function strongConnect(pStart) {
114
+ const lFrames = [visit(pStart)];
115
+
116
+ while (lFrames.length > 0) {
117
+ const lFrame = lFrames.at(-1);
118
+
119
+ if (lFrame.nextEdge < lFrame.successors.length) {
120
+ followNextEdge(lFrames);
121
+ } else {
122
+ finishFrame(lFrames);
123
+ }
124
+ }
125
+ }
126
+
127
+ for (const lStart of pGraph.keys()) {
128
+ if (!lIndexOf.has(lStart)) {
129
+ strongConnect(lStart);
130
+ }
131
+ }
132
+ return lReturnValue;
133
+ }
@@ -1,4 +1,6 @@
1
1
  /* eslint-disable security/detect-object-injection */
2
+ import findCycleComponents from "./find-cycle-components.mjs";
3
+
2
4
  /**
3
5
  * @import { IFolderDependency, IDependency, IFolder, IModule } from "../../types/cruise-result.mjs"
4
6
  * @import { DependencyType, IMiniDependency } from "../../types/shared-types.mjs"
@@ -10,6 +12,8 @@
10
12
 
11
13
  export default class IndexedModuleGraph {
12
14
  #indexedGraph;
15
+ /** @type {Map<string, number>|undefined} */
16
+ #cycleComponents;
13
17
  /**
14
18
  * @param {IModuleOrFolder} pModule
15
19
  * @returns {IVertex}
@@ -180,6 +184,24 @@ export default class IndexedModuleGraph {
180
184
  return [];
181
185
  }
182
186
 
187
+ /**
188
+ * Computed once per graph, on first use.
189
+ *
190
+ * @returns {Map<string, number>} module name => the strongly connected
191
+ * component it's in. Only has entries for modules on a cycle.
192
+ */
193
+ #getCycleComponents() {
194
+ this.#cycleComponents ??= findCycleComponents(
195
+ new Map(
196
+ Array.from(this.#indexedGraph, ([pName, pVertex]) => [
197
+ pName,
198
+ pVertex.dependencies.map(({ name }) => name),
199
+ ]),
200
+ ),
201
+ );
202
+ return this.#cycleComponents;
203
+ }
204
+
183
205
  /**
184
206
  * Returns the first non-zero length path from pInitialSource to pInitialSource
185
207
  * Returns the empty array if there is no such path
@@ -191,10 +213,18 @@ export default class IndexedModuleGraph {
191
213
  */
192
214
  #getCycle(pInitialSource, pCurrentDependency, pVisited) {
193
215
  const lVisited = pVisited || new Set();
216
+ const lCycleComponents = this.#getCycleComponents();
217
+ const lCycleComponent = lCycleComponents.get(pInitialSource);
194
218
  /** @type {IVertex} */
195
219
  const lCurrentVertex = this.#indexedGraph.get(pCurrentDependency.name);
220
+ // A vertex outside pInitialSource's strongly connected component can't
221
+ // lead back to it, and neither can anything it leads to. Skipping those
222
+ // keeps the search from exploring the rest of the graph, while it visits
223
+ // the remaining vertices in the same order - so it finds the same cycle.
196
224
  const lEdges = lCurrentVertex.dependencies.filter(
197
- (pDependency) => !lVisited.has(pDependency.name),
225
+ (pDependency) =>
226
+ !lVisited.has(pDependency.name) &&
227
+ lCycleComponents.get(pDependency.name) === lCycleComponent,
198
228
  );
199
229
  const lInitialAsDependency = lEdges.find(
200
230
  (pDependency) => pDependency.name === pInitialSource,
@@ -242,6 +272,17 @@ export default class IndexedModuleGraph {
242
272
  * @returns {Array<IMiniDependency>} see description above
243
273
  */
244
274
  getCycle(pInitialSource, pCurrentSource) {
275
+ const lCycleComponents = this.#getCycleComponents();
276
+ const lCycleComponent = lCycleComponents.get(pInitialSource);
277
+
278
+ // no cycle can contain the dependency unless both ends are in the same
279
+ // strongly connected component
280
+ if (
281
+ lCycleComponent === undefined ||
282
+ lCycleComponent !== lCycleComponents.get(pCurrentSource)
283
+ ) {
284
+ return [];
285
+ }
245
286
  /** @type {IVertex} */
246
287
  const lInitialNode = this.#indexedGraph.get(pInitialSource);
247
288
  const lCurrentDependency = lInitialNode.dependencies.find(
package/src/meta.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /* generated - don't edit */
2
2
  module.exports = {
3
- version: "18.8.0-beta-2",
3
+ version: "18.8.0",
4
4
  engines: {
5
5
  node: "^22||^24||>=26",
6
6
  },
@@ -233,25 +233,32 @@ function report(pResults, pOptions) {
233
233
  showAliasedModulesUnresolved: false,
234
234
  ...pOptions,
235
235
  };
236
+ // replacer functions so '$&', "$'" and '$$' in rule comments or module names
237
+ // are inserted as is instead of being interpreted as replacement patterns
236
238
  return template
237
- .replace("{{totalCruised}}", pResults.summary.totalCruised)
239
+ .replace("{{totalCruised}}", () => pResults.summary.totalCruised)
238
240
  .replace(
239
241
  "{{totalDependenciesCruised}}",
240
- pResults.summary.totalDependenciesCruised,
242
+ () => pResults.summary.totalDependenciesCruised,
241
243
  )
242
- .replace("{{error}}", pResults.summary.error)
243
- .replace("{{warn}}", pResults.summary.warn)
244
- .replace("{{info}}", pResults.summary.info)
245
- .replace("{{ignore}}", pResults.summary.ignore ?? 0)
246
- .replace("{{staleDiv}}", constructStaleDiv(pResults.summary.baselineStale))
247
- .replace("{{violatedRulesTable}}", constructViolatedRulesTable(pResults))
248
- .replace("{{violationsList}}", constructViolationsList(pResults, lOptions))
249
- .replace(
250
- "{{baselineStaleTable}}",
244
+ .replace("{{error}}", () => pResults.summary.error)
245
+ .replace("{{warn}}", () => pResults.summary.warn)
246
+ .replace("{{info}}", () => pResults.summary.info)
247
+ .replace("{{ignore}}", () => pResults.summary.ignore ?? 0)
248
+ .replace("{{staleDiv}}", () =>
249
+ constructStaleDiv(pResults.summary.baselineStale),
250
+ )
251
+ .replace("{{violatedRulesTable}}", () =>
252
+ constructViolatedRulesTable(pResults),
253
+ )
254
+ .replace("{{violationsList}}", () =>
255
+ constructViolationsList(pResults, lOptions),
256
+ )
257
+ .replace("{{baselineStaleTable}}", () =>
251
258
  constructBaselineStaleTable(pResults, pOptions),
252
259
  )
253
- .replace("{{depcruiseVersion}}", `dependency-cruiser@${meta.version}`)
254
- .replace("{{runDate}}", new Date().toISOString());
260
+ .replace("{{depcruiseVersion}}", () => `dependency-cruiser@${meta.version}`)
261
+ .replace("{{runDate}}", () => new Date().toISOString());
255
262
  }
256
263
 
257
264
  /**
@@ -103,7 +103,7 @@ function report(pResults) {
103
103
  const lModules = dependencyToIncidenceTransformer(pResults.modules).map(
104
104
  addShowTitle,
105
105
  );
106
- return templateText.replace("{{table-here}}", constructTable(lModules));
106
+ return templateText.replace("{{table-here}}", () => constructTable(lModules));
107
107
  }
108
108
 
109
109
  /**
@@ -86,14 +86,14 @@ export function getURLForModule(pModule, pPrefix, pSuffix) {
86
86
  // Default to Node.js API documentation
87
87
  return "https://nodejs.org/api/{{packageName}}.html".replace(
88
88
  "{{packageName}}",
89
- lPackageName,
89
+ () => lPackageName,
90
90
  );
91
91
  } else if (
92
92
  pModule.dependencyTypes?.some((pType) => pType.startsWith("npm"))
93
93
  ) {
94
94
  return "https://www.npmjs.com/package/{{packageName}}".replace(
95
95
  "{{packageName}}",
96
- deriveExternalPackageName(pModule.source),
96
+ () => deriveExternalPackageName(pModule.source),
97
97
  );
98
98
  } else if (pPrefix || pSuffix) {
99
99
  let lURL = pModule.source;
@@ -15,7 +15,7 @@ function splitLine(pLine, pMaxWidth) {
15
15
  let lCurrentWidth = 0;
16
16
 
17
17
  for (const lWord of lWords) {
18
- if (lCurrentWidth + lWord.length > pMaxWidth) {
18
+ if (lCurrentLine && lCurrentWidth + lWord.length > pMaxWidth) {
19
19
  lWrappedLines.push(lCurrentLine.trimEnd());
20
20
  lCurrentLine = "";
21
21
  lCurrentWidth = 0;