-
Notifications
You must be signed in to change notification settings - Fork 248
/
normalize.mjs
239 lines (213 loc) · 7.19 KB
/
normalize.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* eslint-disable security/detect-object-injection */
import { normalizeREProperties } from "../helpers.mjs";
import defaults from "./defaults.mjs";
import { uniq } from "#utl/array-util.mjs";
const DEFAULT_CACHE_FOLDER = "node_modules/.cache/dependency-cruiser";
const DEFAULT_CACHE_STRATEGY = "metadata";
function normalizeFilterOption(pFilterOption) {
let lReturnValue = pFilterOption || {};
if (typeof lReturnValue === "string" || Array.isArray(lReturnValue)) {
lReturnValue = {
path: lReturnValue,
};
}
return normalizeREProperties(lReturnValue, ["path"]);
}
function normalizeReporterOptions(pReporterOptions) {
const lNormalizeableOptions = [
"archi.collapsePattern",
"archi.filters.includeOnly.path",
"archi.filters.focus.path",
"archi.filters.exclude.path",
"dot.collapsePattern",
"dot.filters.includeOnly.path",
"dot.filters.focus.path",
"dot.filters.exclude.path",
"ddot.collapsePattern",
"ddot.filters.includeOnly.path",
"ddot.filters.focus.path",
"ddot.filters.exclude.path",
];
return normalizeREProperties(pReporterOptions, lNormalizeableOptions);
}
function normalizeFilterOptions(pOptions, pFilterOptionKeys) {
let lReturnValue = { ...pOptions };
for (let lFilterOptionKey of pFilterOptionKeys) {
if (pOptions[lFilterOptionKey]) {
lReturnValue[lFilterOptionKey] = normalizeFilterOption(
lReturnValue[lFilterOptionKey],
);
}
}
return lReturnValue;
}
function normalizeCollapse(pCollapse) {
let lReturnValue = pCollapse;
const lOneOrMoreNonSlashes = "[^/]+";
const lFolderPattern = `${lOneOrMoreNonSlashes}/`;
const lFolderBelowNodeModules = `node_modules/${lOneOrMoreNonSlashes}`;
const lSingleDigitRe = /^\d$/;
if (typeof pCollapse === "number" || pCollapse.match(lSingleDigitRe)) {
lReturnValue = `${lFolderBelowNodeModules}|^${lFolderPattern.repeat(
Number.parseInt(pCollapse, 10),
)}`;
}
return lReturnValue;
}
function normalizeFocusDepth(pFormatOptions) {
/** @type {import("../../../types/dependency-cruiser.js").IFormatOptions}*/
let lFormatOptions = structuredClone(pFormatOptions);
if (Object.hasOwn(lFormatOptions, "focusDepth")) {
if (lFormatOptions?.focus) {
lFormatOptions.focus.depth = Number.parseInt(
lFormatOptions.focusDepth,
10,
);
}
delete lFormatOptions.focusDepth;
}
return lFormatOptions;
}
/**
*
* @param {import("../../../types/dependency-cruiser.js").IForbiddenRuleType} pRule
* @returns {boolean}
*/
function hasMetricsRule(pRule) {
// TODO: philosophy: is a rule with 'folder' in it a metrics rule?
// Or is it a misuse to ensure folder derivations (like cycles) get
// kicked off?
return (
Object.hasOwn(pRule?.to ?? {}, "moreUnstable") ||
(pRule?.scope ?? "module") === "folder"
);
}
/**
*
* @param {import("../../../types/dependency-cruiser.js").IFlattenedRuleSet} pRuleSet
* @returns {boolean}
*/
function ruleSetHasMetricsRule(pRuleSet) {
const lRuleSet = pRuleSet || {};
return (
(lRuleSet.forbidden || []).some(hasMetricsRule) ||
(lRuleSet.allowed || []).some(hasMetricsRule)
);
}
/**
*
* @param {import('../../../types/dependency-cruiser.js').ICruiseOptions} pOptions
* @returns Boolean
*/
function reporterShowsMetrics(pOptions) {
return (
(pOptions.reporterOptions?.[pOptions?.outputType]?.showMetrics ?? false) ===
true
);
}
/**
* Determines whether (instability) metrics should be calculated
*
* @param {import('../../../types/dependency-cruiser.js').ICruiseOptions} pOptions
* @returns Boolean
*/
function shouldCalculateMetrics(pOptions) {
return (
pOptions.metrics ||
pOptions.outputType === "metrics" ||
reporterShowsMetrics(pOptions) ||
ruleSetHasMetricsRule(pOptions.ruleSet)
);
}
/**
* @param {string|boolean|Partial<import("../../../types/cache-options.js").ICacheOptions>} pCacheOptions
* @returns {import("../../../types/cache-options.js").ICacheOptions}
*/
function normalizeCacheOptions(pCacheOptions) {
let lNormalizedCacheOptions = pCacheOptions;
if (typeof pCacheOptions === "string") {
lNormalizedCacheOptions = {
folder: pCacheOptions,
};
}
if (pCacheOptions === true) {
lNormalizedCacheOptions = {};
}
// TODO: put these values in a central spot as constants
// (folder is already in src/cli/defaults.js but depending on the UI (cli)
// is not OK. We might b.t.w. wanna deduplicate the defaulting)
return {
folder: DEFAULT_CACHE_FOLDER,
strategy: DEFAULT_CACHE_STRATEGY,
...lNormalizedCacheOptions,
};
}
/**
*
* @param {import('../../../types/options.mjs').ICruiseOptions} pOptions
* @param {string[]} pFileAndDirectoryArray
* @returns {import('../../../types/strict-options.js').IStrictCruiseOptions}
*/
export function normalizeCruiseOptions(pOptions, pFileAndDirectoryArray = []) {
/** @type {import('../../../types/strict-options.js').IStrictCruiseOptions} */
let lReturnValue = {
baseDir: process.cwd(),
...defaults,
...pOptions,
args: pFileAndDirectoryArray.join(" "),
};
// @ts-expect-error the idea of normalizing maxDepth to number is that after
// that we're sure it's a number. Should maybe best be solved by
// having two types/ interfaces
lReturnValue.maxDepth = Number.parseInt(lReturnValue.maxDepth, 10);
lReturnValue.moduleSystems = uniq(lReturnValue.moduleSystems);
if (Object.hasOwn(lReturnValue, "collapse")) {
lReturnValue.collapse = normalizeCollapse(lReturnValue.collapse);
}
// TODO: further down the execution path code still relies on .doNotFollow
// and .exclude existing. We should treat them the same as the
// other filters (so either make all exist always or only
// when they're actually defined)
lReturnValue.doNotFollow = normalizeFilterOption(lReturnValue.doNotFollow);
lReturnValue.exclude = normalizeFilterOption(lReturnValue.exclude);
lReturnValue.extraExtensionsToScan = lReturnValue.extraExtensionsToScan || [];
lReturnValue = normalizeFilterOptions(lReturnValue, [
"focus",
"includeOnly",
"reaches",
"highlight",
]);
lReturnValue.exoticRequireStrings = uniq(lReturnValue.exoticRequireStrings);
if (lReturnValue.reporterOptions) {
lReturnValue.reporterOptions = normalizeReporterOptions(
lReturnValue.reporterOptions,
);
}
lReturnValue.metrics = shouldCalculateMetrics(pOptions);
// if (has(pOptions, "ruleSet")) {
// lReturnValue.ruleSet = normalizeRuleSet(pOptions.ruleSet);
// }
if (lReturnValue.cache) {
lReturnValue.cache = normalizeCacheOptions(lReturnValue.cache);
}
return normalizeFocusDepth(lReturnValue);
}
/**
*
* @param {import("../../../types/dependency-cruiser.js").IFormatOptions} pFormatOptions
* @returns {import("../../../types/strict-options.js").IStrictFormatOptions}
*/
export function normalizeFormatOptions(pFormatOptions) {
let lFormatOptions = structuredClone(pFormatOptions);
if (Object.hasOwn(lFormatOptions, "collapse")) {
lFormatOptions.collapse = normalizeCollapse(lFormatOptions.collapse);
}
lFormatOptions = normalizeFilterOptions(lFormatOptions, [
"exclude",
"focus",
"highlight",
"includeOnly",
"reaches",
]);
return normalizeFocusDepth(lFormatOptions);
}