Skip to content

Commit 2c98751

Browse files
refactor(cli): reuse existing CLI helpers in the wizard AST utils
Address review feedback on the shared wizard helpers: - Enable unicorn/no-array-for-each repo-wide and convert every .forEach call site to for...of, including getLastRequireIndex. - Add buildProjectIssuesUrl to lib/sentry-urls.ts, built on getOrgBaseUrl like buildEventSearchUrl, and delete the wizard-specific wizard/utils/url.ts. - Move findFile into lib/dsn/fs-utils.ts and build it on isRegularFile, so a directory or FIFO with a matching name is skipped. It is async now. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
1 parent c4786b3 commit 2c98751

32 files changed

Lines changed: 193 additions & 144 deletions

File tree

‎.oxlintrc.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"typescript/no-useless-default-assignment": "error",
7070
"jsx-a11y/interactive-supports-focus": "off",
7171
"unicorn/prefer-spread": "error",
72+
"unicorn/no-array-for-each": "error",
7273
"typescript/no-misused-promises": "error",
7374
"typescript/switch-exhaustiveness-check": [
7475
"error",

‎apps/games-api/src/leaderboard.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ describe("games leaderboard route", () => {
179179
"kk",
180180
"ll",
181181
];
182-
names.forEach((n, i) => rows.push([`${n}-${n}-100${i % 10}`, 300 - i]));
182+
for (const [i, n] of names.entries()) {
183+
rows.push([`${n}-${n}-100${i % 10}`, 300 - i]);
184+
}
183185
fetchMock.mockResolvedValue(jsonResponse(upstreamBody(rows)));
184186

185187
const res = await app.request(PATH, REQ, makeEnv());

‎apps/local/src/lib/profile-session.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export function mergeProfileChunks(
128128
const frameMap = new Map<number, number>(),
129129
stackMap = new Map<number, number>();
130130
if (Array.isArray(profile["frames"]))
131-
profile["frames"].forEach((frame, index) => {
132-
if (!Object.keys(record(frame)).length) return;
131+
for (const [index, frame] of profile["frames"].entries()) {
132+
if (!Object.keys(record(frame)).length) continue;
133133
const key = profileValueKey(frame);
134134
let id = frameIds.get(key);
135135
if (id === undefined) {
@@ -138,15 +138,15 @@ export function mergeProfileChunks(
138138
frameIds.set(key, id);
139139
}
140140
frameMap.set(index, id);
141-
});
141+
}
142142
if (Array.isArray(profile["stacks"]))
143-
profile["stacks"].forEach((stack, index) => {
144-
if (!Array.isArray(stack)) return;
143+
stacks: for (const [index, stack] of profile["stacks"].entries()) {
144+
if (!Array.isArray(stack)) continue;
145145
const mapped: number[] = [];
146146
for (const frame of stack) {
147147
const id =
148148
typeof frame === "number" ? frameMap.get(frame) : undefined;
149-
if (id === undefined) return; // Broken indexes cannot silently describe another frame.
149+
if (id === undefined) continue stacks; // Broken indexes cannot silently describe another frame.
150150
mapped.push(id);
151151
}
152152
const key = JSON.stringify(mapped);
@@ -157,7 +157,7 @@ export function mergeProfileChunks(
157157
stackIds.set(key, id);
158158
}
159159
stackMap.set(index, id);
160-
});
160+
}
161161
const chunkThreads = new Map<string, Record<string, unknown>[]>();
162162
if (Array.isArray(profile["samples"]))
163163
for (const value of profile["samples"]) {

‎packages/cli/src/commands/api.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,9 @@ function logResponse(response: {
14871487
log.debug(
14881488
`< HTTP ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`,
14891489
);
1490-
response.headers.forEach((value, key) => {
1490+
for (const [key, value] of response.headers) {
14911491
log.debug(`< ${key}: ${value}`);
1492-
});
1492+
}
14931493
log.debug("<");
14941494
}
14951495

‎packages/cli/src/lib/db/completion-cache.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function cacheCompletionEntities(
114114
)
115115
.get(kind, orgSlug) as { latest: number | null } | undefined;
116116
const now = Math.max(Date.now(), (latest?.latest ?? 0) + valid.length);
117-
valid.forEach((entity, index) => {
117+
for (const [index, entity] of valid.entries()) {
118118
runUpsert(
119119
db,
120120
"completion_cache",
@@ -127,7 +127,7 @@ export function cacheCompletionEntities(
127127
},
128128
["kind", "org_slug", "value"],
129129
);
130-
});
130+
}
131131
db.query(
132132
`DELETE FROM completion_cache
133133
WHERE kind = ? AND org_slug = ? AND rowid NOT IN (

‎packages/cli/src/lib/dsn/errors.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import type { DsnDetectionResult } from "./types.js";
2222
export function formatConflictError(result: DsnDetectionResult): string {
2323
const lines = ["Error: Multiple Sentry DSNs detected in this project.\n"];
2424

25-
result.all.forEach((dsn, i) => {
25+
for (const [i, dsn] of result.all.entries()) {
2626
lines.push(` DSN ${i + 1}: ${dsn.raw}`);
2727
lines.push(` Source: ${getDsnSourceDescription(dsn)}`);
2828
if (dsn.projectId) {
2929
lines.push(` Project ID: ${dsn.projectId}`);
3030
}
3131
lines.push("");
32-
});
32+
}
3333

3434
lines.push("To resolve, specify which project to use:");
3535
lines.push(` ${sentry`<command> <org>/<project>`}`);

‎packages/cli/src/lib/dsn/fs-utils.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,25 @@ export async function isRegularFile(
113113
return false;
114114
}
115115
}
116+
117+
/**
118+
* Find the first of `basePath` + each extension that is a regular file, for
119+
* config files whose extension isn't known yet (`next.config` →
120+
* `next.config.mjs`).
121+
*
122+
* @param basePath - Path without the extension
123+
* @param extensions - Extensions to try, in order, each with its leading dot
124+
* @returns The first matching path, or undefined when none is a regular file
125+
*/
126+
export async function findFile(
127+
basePath: string,
128+
extensions: readonly string[] = [".js", ".ts", ".mjs", ".cjs"],
129+
): Promise<string | undefined> {
130+
for (const extension of extensions) {
131+
const candidate = `${basePath}${extension}`;
132+
if (await isRegularFile(candidate, "findFile")) {
133+
return candidate;
134+
}
135+
}
136+
return undefined;
137+
}

‎packages/cli/src/lib/formatters/alerts.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ function conditionRows(value: unknown, detector: boolean): Row[] {
8181
const rows: Row[] = [["Logic", valueText(group["logicType"])]];
8282
const conditions = group["conditions"];
8383
if (Array.isArray(conditions) && conditions.length > 0) {
84-
conditions.forEach((condition, index) => {
84+
for (const [index, condition] of conditions.entries()) {
8585
rows.push([`Condition ${index + 1}`, conditionText(condition, detector)]);
86-
});
86+
}
8787
} else {
8888
rows.push([
8989
"Conditions",
@@ -150,11 +150,11 @@ function workflowSections(workflow: GetOrganizationWorkflowResponse): string[] {
150150
...workflowGroupSections(workflow.triggers, "Triggers"),
151151
];
152152
if (workflow.actionFilters?.length) {
153-
workflow.actionFilters.forEach((group, index) => {
153+
for (const [index, group] of workflow.actionFilters.entries()) {
154154
sections.push(
155155
...workflowGroupSections(group, `Action filter ${index + 1}`),
156156
);
157-
});
157+
}
158158
} else {
159159
sections.push(table([["Action filters", "none"]]));
160160
}

‎packages/cli/src/lib/formatters/human.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,15 +1286,15 @@ function formatSpanSimple(span: TraceSpan, opts: FormatSpanOptions): void {
12861286
if (currentDepth < maxDepth) {
12871287
const children = span.children ?? [];
12881288
const childCount = children.length;
1289-
children.forEach((child, i) => {
1289+
for (const [i, child] of children.entries()) {
12901290
formatSpanSimple(child, {
12911291
lines,
12921292
prefix: childPrefix,
12931293
isLast: i === childCount - 1,
12941294
currentDepth: currentDepth + 1,
12951295
maxDepth,
12961296
});
1297-
});
1297+
}
12981298
}
12991299
}
13001300

@@ -1367,15 +1367,15 @@ export function formatSimpleSpanTree(
13671367
const displaySpans = truncated ? spans.slice(0, MAX_ROOT_SPANS) : spans;
13681368
const displayCount = displaySpans.length;
13691369

1370-
displaySpans.forEach((span, i) => {
1370+
for (const [i, span] of displaySpans.entries()) {
13711371
formatSpanSimple(span, {
13721372
lines,
13731373
prefix: "",
13741374
isLast: !truncated && i === displayCount - 1,
13751375
currentDepth: 1,
13761376
maxDepth: effectiveMaxDepth,
13771377
});
1378-
});
1378+
}
13791379

13801380
if (truncated) {
13811381
const remaining = totalRootSpans - MAX_ROOT_SPANS;

‎packages/cli/src/lib/games/snake-game.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ export function renderSnake(
313313
if (state.food) {
314314
cells[state.food.y * state.width + state.food.x] = FOOD;
315315
}
316-
state.body.forEach((part, index) => {
316+
for (const [index, part] of state.body.entries()) {
317317
cells[part.y * state.width + part.x] = index === 0 ? HEAD : BODY;
318-
});
318+
}
319319

320320
const lines: string[] = [];
321321
for (let row = 0; row < snakeTerminalRows(state.height); row++) {

0 commit comments

Comments
 (0)