@ryuugg/stanza 0.3.1 → 0.3.3

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/src/depth.ts ADDED
@@ -0,0 +1,372 @@
1
+ // Worker threads get a far smaller native stack than main and oxc's parser recurses, so a deep file aborts the whole process in a worker; these sit at an eighth of the crash depths measured in a worker.
2
+ export const FRAME_LIMIT = 300;
3
+ export const TOKEN_LIMIT = 1400;
4
+
5
+ const NEWLINE = 10;
6
+ const RETURN = 13;
7
+
8
+ const DOUBLE = 34;
9
+ const SINGLE = 39;
10
+ const BACKTICK = 96;
11
+ const DOLLAR = 36;
12
+ const BACKSLASH = 92;
13
+
14
+ const OPEN_PAREN = 40;
15
+ const OPEN_BRACKET = 91;
16
+ const OPEN_BRACE = 123;
17
+
18
+ const CLOSE_PAREN = 41;
19
+ const CLOSE_BRACKET = 93;
20
+ const CLOSE_BRACE = 125;
21
+
22
+ const SLASH = 47;
23
+ const STAR = 42;
24
+ const LESS = 60;
25
+ const GREATER = 62;
26
+
27
+ const COMMA = 44;
28
+ const SEMICOLON = 59;
29
+
30
+ const enum Frame {
31
+ Bracket,
32
+ Interpolation,
33
+ Element,
34
+ }
35
+
36
+ const enum Last {
37
+ Start,
38
+ Name,
39
+ Keyword,
40
+ Literal,
41
+ Close,
42
+ Paren,
43
+ Punctuator,
44
+ }
45
+
46
+ const REGEX_AFTER = [
47
+ "return",
48
+ "typeof",
49
+ "instanceof",
50
+ "in",
51
+ "of",
52
+ "new",
53
+ "delete",
54
+ "void",
55
+ "throw",
56
+ "case",
57
+ "do",
58
+ "else",
59
+ "yield",
60
+ "await",
61
+ ];
62
+
63
+ const KEYWORDS = new Set([
64
+ ...REGEX_AFTER,
65
+ "keyof",
66
+ "readonly",
67
+ "infer",
68
+ "unique",
69
+ "async",
70
+ "export",
71
+ "declare",
72
+ "abstract",
73
+ "static",
74
+ "public",
75
+ "private",
76
+ "protected",
77
+ "override",
78
+ "get",
79
+ "set",
80
+ "accessor",
81
+ "let",
82
+ "const",
83
+ "var",
84
+ "function",
85
+ "class",
86
+ "interface",
87
+ "type",
88
+ "enum",
89
+ "namespace",
90
+ "module",
91
+ "import",
92
+ "from",
93
+ "default",
94
+ "extends",
95
+ "implements",
96
+ "as",
97
+ "satisfies",
98
+ "is",
99
+ "asserts",
100
+ "if",
101
+ "for",
102
+ "while",
103
+ "with",
104
+ "switch",
105
+ "try",
106
+ "catch",
107
+ "finally",
108
+ ]);
109
+
110
+ const CONTINUES = new Set([
111
+ "else",
112
+ "catch",
113
+ "finally",
114
+ "while",
115
+ "instanceof",
116
+ "in",
117
+ "of",
118
+ "as",
119
+ "satisfies",
120
+ "extends",
121
+ "implements",
122
+ ]);
123
+
124
+ interface Scan {
125
+ text: string;
126
+ frames: Frame[];
127
+ segments: number[];
128
+ tokens: number;
129
+ }
130
+
131
+ function isWord(code: number): boolean {
132
+ return (
133
+ (code >= 97 && code <= 122) ||
134
+ (code >= 65 && code <= 90) ||
135
+ (code >= 48 && code <= 57) ||
136
+ code === 95 ||
137
+ code === DOLLAR ||
138
+ code > 127
139
+ );
140
+ }
141
+
142
+ function isBlank(code: number): boolean {
143
+ return (
144
+ code === 32 || code === 9 || code === 11 || code === 12 || code === 0xa0 || code === 0xfeff
145
+ );
146
+ }
147
+
148
+ function isLineEnd(code: number): boolean {
149
+ return code === NEWLINE || code === RETURN;
150
+ }
151
+
152
+ function quotedEnd(text: string, start: number, quote: number): number {
153
+ for (let index = start + 1; index < text.length; index++) {
154
+ const code = text.charCodeAt(index);
155
+ if (code === quote) return index + 1;
156
+ if (code === BACKSLASH) index++;
157
+ else if (isLineEnd(code)) return -1;
158
+ }
159
+
160
+ return -1;
161
+ }
162
+
163
+ function regexEnd(text: string, start: number): number {
164
+ let inClass = false;
165
+ for (let index = start + 1; index < text.length; index++) {
166
+ const code = text.charCodeAt(index);
167
+ if (code === BACKSLASH) index++;
168
+ else if (isLineEnd(code)) return -1;
169
+ else if (code === OPEN_BRACKET) inClass = true;
170
+ else if (code === CLOSE_BRACKET) inClass = false;
171
+ else if (code === SLASH && !inClass) {
172
+ let end = index + 1;
173
+ while (end < text.length && isWord(text.charCodeAt(end))) end++;
174
+ return end;
175
+ }
176
+ }
177
+
178
+ return -1;
179
+ }
180
+
181
+ function count(scan: Scan): boolean {
182
+ scan.segments[scan.segments.length - 1]!++;
183
+ scan.tokens++;
184
+
185
+ const nested = (scan.frames.length - 1) * TOKEN_LIMIT;
186
+ return nested + scan.tokens * FRAME_LIMIT > FRAME_LIMIT * TOKEN_LIMIT;
187
+ }
188
+
189
+ function reset(scan: Scan): void {
190
+ scan.tokens -= scan.segments[scan.segments.length - 1]!;
191
+ scan.segments[scan.segments.length - 1] = 0;
192
+ }
193
+
194
+ function push(scan: Scan, frame: Frame): void {
195
+ scan.frames.push(frame);
196
+ scan.segments.push(0);
197
+ }
198
+
199
+ function pop(scan: Scan): void {
200
+ scan.tokens -= scan.segments.pop()!;
201
+ scan.frames.pop();
202
+ }
203
+
204
+ function top(scan: Scan): Frame {
205
+ return scan.frames[scan.frames.length - 1]!;
206
+ }
207
+
208
+ function closeElement(scan: Scan): void {
209
+ pop(scan);
210
+ if (top(scan) === Frame.Element) reset(scan);
211
+ }
212
+
213
+ function templateEnd(scan: Scan, start: number): number {
214
+ const { text } = scan;
215
+ for (let index = start; index < text.length; index++) {
216
+ const code = text.charCodeAt(index);
217
+ if (code === BACKSLASH) index++;
218
+ else if (code === BACKTICK) return index + 1;
219
+ else if (code === DOLLAR && text.charCodeAt(index + 1) === OPEN_BRACE) {
220
+ push(scan, Frame.Interpolation);
221
+ return -(index + 2);
222
+ }
223
+ }
224
+
225
+ return text.length;
226
+ }
227
+
228
+ export function deepRisk(text: string, jsx: boolean): boolean {
229
+ const scan: Scan = { text, frames: [Frame.Bracket], segments: [0], tokens: 0 };
230
+
231
+ let last = Last.Start;
232
+ let lineBreak = false;
233
+ let index = 0;
234
+ while (index < text.length) {
235
+ const code = text.charCodeAt(index);
236
+ if (isBlank(code)) {
237
+ index++;
238
+ continue;
239
+ }
240
+
241
+ if (isLineEnd(code)) {
242
+ lineBreak ||= last === Last.Name || last === Last.Literal || last === Last.Close;
243
+ index++;
244
+ continue;
245
+ }
246
+
247
+ const next = text.charCodeAt(index + 1);
248
+ if (code === SLASH && next === SLASH) {
249
+ while (index < text.length && !isLineEnd(text.charCodeAt(index))) index++;
250
+ continue;
251
+ }
252
+
253
+ const commentEnd = code === SLASH && next === STAR ? text.indexOf("*/", index + 2) : -1;
254
+ if (commentEnd >= 0) {
255
+ index = commentEnd + 2;
256
+ continue;
257
+ }
258
+
259
+ if (isWord(code)) {
260
+ const start = index;
261
+ while (index < text.length && isWord(text.charCodeAt(index))) index++;
262
+
263
+ const word = index - start <= 10 ? text.slice(start, index) : "";
264
+ if (lineBreak && !CONTINUES.has(word)) reset(scan);
265
+
266
+ lineBreak = false;
267
+ last = KEYWORDS.has(word) ? Last.Keyword : Last.Name;
268
+ if (count(scan)) return true;
269
+ continue;
270
+ }
271
+
272
+ lineBreak = false;
273
+
274
+ if (code === SINGLE || code === DOUBLE) {
275
+ const end = quotedEnd(text, index, code);
276
+ index = end < 0 ? index + 1 : end;
277
+ last = end < 0 ? Last.Punctuator : Last.Literal;
278
+ if (count(scan)) return true;
279
+ continue;
280
+ }
281
+
282
+ if (code === BACKTICK) {
283
+ const end = templateEnd(scan, index + 1);
284
+ index = Math.abs(end);
285
+ last = end < 0 ? Last.Punctuator : Last.Literal;
286
+ if (count(scan)) return true;
287
+ continue;
288
+ }
289
+
290
+ const divides =
291
+ last === Last.Name || last === Last.Literal || last === Last.Close || last === Last.Paren;
292
+
293
+ const regex = code === SLASH && !divides ? regexEnd(text, index) : -1;
294
+ if (regex >= 0) {
295
+ index = regex;
296
+ last = Last.Literal;
297
+ if (count(scan)) return true;
298
+ continue;
299
+ }
300
+
301
+ index++;
302
+
303
+ if (code === SEMICOLON || code === COMMA) {
304
+ reset(scan);
305
+ last = Last.Punctuator;
306
+ continue;
307
+ }
308
+
309
+ if (code === OPEN_PAREN || code === OPEN_BRACKET || code === OPEN_BRACE) {
310
+ if (count(scan)) return true;
311
+
312
+ push(scan, Frame.Bracket);
313
+ last = Last.Punctuator;
314
+ continue;
315
+ }
316
+
317
+ if (code === CLOSE_PAREN || code === CLOSE_BRACKET || code === CLOSE_BRACE) {
318
+ while (scan.frames.length > 1 && top(scan) === Frame.Element) pop(scan);
319
+
320
+ if (code === CLOSE_BRACE && scan.frames.length > 1 && top(scan) === Frame.Interpolation) {
321
+ pop(scan);
322
+
323
+ const end = templateEnd(scan, index);
324
+ index = Math.abs(end);
325
+ last = end < 0 ? Last.Punctuator : Last.Literal;
326
+ continue;
327
+ }
328
+
329
+ if (scan.frames.length > 1) pop(scan);
330
+
331
+ last = code === CLOSE_PAREN ? Last.Paren : Last.Close;
332
+ if (count(scan)) return true;
333
+ continue;
334
+ }
335
+
336
+ if (jsx && code === LESS && next === SLASH) {
337
+ if (top(scan) === Frame.Element) closeElement(scan);
338
+
339
+ last = Last.Punctuator;
340
+ if (count(scan)) return true;
341
+ continue;
342
+ }
343
+
344
+ const opensElement =
345
+ top(scan) === Frame.Element ||
346
+ last === Last.Start ||
347
+ last === Last.Punctuator ||
348
+ last === Last.Keyword;
349
+
350
+ if (jsx && code === LESS && opensElement && (isWord(next) || next === GREATER)) {
351
+ if (count(scan)) return true;
352
+
353
+ push(scan, Frame.Element);
354
+ last = Last.Punctuator;
355
+ continue;
356
+ }
357
+
358
+ if (jsx && code === SLASH && next === GREATER && top(scan) === Frame.Element) {
359
+ index++;
360
+ closeElement(scan);
361
+ }
362
+
363
+ last = Last.Punctuator;
364
+ if (count(scan)) return true;
365
+ }
366
+
367
+ return false;
368
+ }
369
+
370
+ export function tooDeep(path: string, text: string): boolean {
371
+ return deepRisk(text, !/\.[cm]?ts$/.test(path));
372
+ }
@@ -0,0 +1,77 @@
1
+ import { existsSync, readdirSync, realpathSync } from "node:fs";
2
+ import { basename, dirname, join } from "node:path";
3
+
4
+ type Listing = { names: Set<string>; folded: Set<string> } | null;
5
+
6
+ const maps: Map<unknown, unknown>[] = [];
7
+ export function invocationMap<K, V>(): Map<K, V> {
8
+ const map = new Map<K, V>();
9
+ maps.push(map);
10
+ return map;
11
+ }
12
+
13
+ export function beginInvocation(): void {
14
+ for (const map of maps) map.clear();
15
+ }
16
+
17
+ const realDirectories = invocationMap<string, string>();
18
+ const listings = invocationMap<string, Listing>();
19
+ const existence = invocationMap<string, boolean>();
20
+ const realPaths = invocationMap<string, string>();
21
+
22
+ export function realDirectory(dir: string): string {
23
+ const cached = realDirectories.get(dir);
24
+ if (cached !== undefined) return cached;
25
+
26
+ const parent = dirname(dir);
27
+ const real = existsSync(dir)
28
+ ? realpathSync(dir)
29
+ : parent === dir
30
+ ? dir
31
+ : join(realDirectory(parent), basename(dir));
32
+
33
+ realDirectories.set(dir, real);
34
+ realDirectories.set(real, real);
35
+ return real;
36
+ }
37
+
38
+ function listing(dir: string): Listing {
39
+ const cached = listings.get(dir);
40
+ if (cached !== undefined) return cached;
41
+
42
+ let result: Listing;
43
+ try {
44
+ const names = readdirSync(dir);
45
+ result = { names: new Set(names), folded: new Set(names.map((name) => name.toLowerCase())) };
46
+ } catch {
47
+ result = null;
48
+ }
49
+
50
+ listings.set(dir, result);
51
+ return result;
52
+ }
53
+
54
+ export function holds(dir: string, name: string): boolean {
55
+ const path = join(dir, name);
56
+ const cached = existence.get(path);
57
+ if (cached !== undefined) return cached;
58
+
59
+ const entries = listing(dir);
60
+ const exists = (entries === null || entries.folded.has(name.toLowerCase())) && existsSync(path);
61
+
62
+ existence.set(path, exists);
63
+ return exists;
64
+ }
65
+
66
+ export function listed(dir: string, name: string): boolean {
67
+ return listing(dir)?.names.has(name) ?? false;
68
+ }
69
+
70
+ export function realPath(path: string): string {
71
+ const cached = realPaths.get(path);
72
+ if (cached !== undefined) return cached;
73
+
74
+ const real = realpathSync(path);
75
+ realPaths.set(path, real);
76
+ return real;
77
+ }
@@ -290,9 +290,14 @@ export function explainer(
290
290
  gapsAt.set(start, [...(gapsAt.get(start) ?? []), [list, gap]]);
291
291
  }
292
292
 
293
+ const lineCount =
294
+ original.lines.at(-1) === "" ? original.lines.length - 1 : original.lines.length;
295
+
293
296
  return (line: number): Explanation => {
294
- if (line > original.lines.length)
295
- return { error: `${request.display(path)} has ${original.lines.length} lines, not ${line}` };
297
+ if (line > lineCount)
298
+ return {
299
+ error: `${request.display(path)} has ${lineCount} ${lineCount === 1 ? "line" : "lines"}, not ${line}`,
300
+ };
296
301
 
297
302
  const sections = (gapsAt.get(line) ?? []).map(([list, gap]) => explainGap(traced, list, gap));
298
303
  for (const block of traced.first.braces?.opening(line) ?? [])
@@ -189,20 +189,25 @@ export function blockSpacing(doc: Doc, gap: Gap): Finding[] {
189
189
  return [finding(doc, next.start, "block-spacing")];
190
190
  }
191
191
 
192
+ function fixKeepsBlank(gap: Gap, underComment = gap.next.detached): boolean {
193
+ return gap.decision.want !== "none" || underComment;
194
+ }
195
+
196
+ function commentAbove(doc: Doc, stmt: Stmt): boolean {
197
+ return stmt.detached || lineAt(doc, stmt.start) > stmt.startLine;
198
+ }
199
+
192
200
  function gapEdits(doc: Doc, gap: Gap, edits: LineEdits): Finding[] {
193
201
  const { prev, next, blank, decision } = gap;
194
- if (
195
- decision.want === "keep" ||
196
- decision.want === "frozen" ||
197
- next.detached ||
198
- next.startLine <= prev.endLine ||
199
- (decision.want === "none" ? blank === 0 : blank > 0)
200
- )
201
- return [];
202
+ if (decision.want === "keep" || decision.want === "frozen") return [];
202
203
 
203
- if (decision.want === "none")
204
+ if (decision.want === "none") {
205
+ if (blank === 0 || fixKeepsBlank(gap)) return [];
204
206
  for (const line of blankLines(doc, prev.endLine, next.startLine)) edits.deleteLines.add(line);
205
- else edits.insertAfter.add(next.startLine - 1);
207
+ } else {
208
+ if (blank > 0 || next.startLine <= prev.endLine) return [];
209
+ edits.insertAfter.add(next.startLine - 1);
210
+ }
206
211
 
207
212
  if ("name" in decision)
208
213
  return [finding(doc, next.start, decision.rule, decision.name, decision.reader)];
@@ -252,6 +257,12 @@ function edgeEdits(
252
257
  return findings;
253
258
  }
254
259
 
260
+ function separatedAfterFix(gap: Gap, touched: boolean): boolean {
261
+ if (gap.decision.want === "frozen") return true;
262
+ if (!touched) return gap.blank > 0;
263
+ return gap.decision.want === "at-least-one" || (gap.blank > 0 && fixKeepsBlank(gap));
264
+ }
265
+
255
266
  function walls(
256
267
  doc: Doc,
257
268
  list: StatementList,
@@ -261,20 +272,37 @@ function walls(
261
272
  const findings: Finding[] = [];
262
273
  if (list.kind === "switch") return findings;
263
274
 
275
+ const excluded = new Set<number>();
276
+
277
+ let groupStart = 0;
278
+ for (let index = 0; index <= list.stmts.length; index++) {
279
+ const stmt = list.stmts[index];
280
+ const gap = gaps[index - 1];
281
+ const counted = stmt && !stmt.multiline && stmt.kind !== "import";
282
+ const forcedJoined =
283
+ gap &&
284
+ !fixKeepsBlank(gap, commentAbove(doc, gap.next)) &&
285
+ (gap.blank === 0 || touches(gap.prev.startLine, gap.next.endLine));
286
+
287
+ if (!counted || !forcedJoined) {
288
+ if (index - groupStart >= 6)
289
+ for (let forcedIndex = groupStart; forcedIndex < index; forcedIndex++)
290
+ excluded.add(forcedIndex);
291
+
292
+ groupStart = index;
293
+ }
294
+
295
+ if (!counted) groupStart = index + 1;
296
+ }
297
+
264
298
  let runStart: Stmt | undefined;
265
299
  let length = 0;
266
300
  for (let index = 0; index <= list.stmts.length; index++) {
267
301
  const stmt = list.stmts[index];
268
302
  const gap = gaps[index - 1];
269
- const separated =
270
- gap &&
271
- (touches(gap.prev.startLine, gap.next.endLine)
272
- ? gap.decision.want === "frozen" ||
273
- gap.decision.want === "at-least-one" ||
274
- (gap.decision.want === "keep" && gap.blank > 0)
275
- : gap.decision.want === "frozen" || gap.blank > 0);
303
+ const separated = gap && separatedAfterFix(gap, touches(gap.prev.startLine, gap.next.endLine));
276
304
 
277
- const counted = stmt && !stmt.multiline && stmt.kind !== "import";
305
+ const counted = stmt && !stmt.multiline && stmt.kind !== "import" && !excluded.has(index);
278
306
  if (!counted || separated) {
279
307
  if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
280
308
  findings.push(finding(doc, runStart.start, "wall"));
@@ -91,7 +91,8 @@ export const RULES = {
91
91
  wall: {
92
92
  fixable: false,
93
93
  gap: null,
94
- summary: "six or more consecutive single-line statements with no blank line",
94
+ summary:
95
+ "six or more consecutive single-line statements with no blank line, reported only where a blank line `--fix` keeps can split the run",
95
96
  message: () => "6 or more statements with no blank line between them; separate the steps",
96
97
  },
97
98
  } as const satisfies Record<string, Rule>;