@ryuugg/stanza 0.3.1 → 0.3.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 +2 -2
- package/src/cli.ts +35 -142
- package/src/compile/darwin-arm64.ts +1 -1
- package/src/compile/darwin-x64.ts +1 -1
- package/src/compile/linux-arm64-musl.ts +1 -1
- package/src/compile/linux-arm64.ts +1 -1
- package/src/compile/linux-x64-musl.ts +1 -1
- package/src/compile/linux-x64.ts +1 -1
- package/src/depth.ts +368 -0
- package/src/directories.ts +77 -0
- package/src/engine/gaps.ts +26 -2
- package/src/files.ts +176 -22
- package/src/format.ts +159 -0
- package/src/hook.ts +32 -6
- package/src/languages/javascript/config/find.ts +7 -10
- package/src/languages/javascript/config/index.ts +1 -1
- package/src/languages/javascript/config/module.ts +3 -2
- package/src/languages/javascript/config/width.ts +32 -26
- package/src/pool.ts +312 -0
- package/src/usage.ts +5 -2
- package/src/worker.ts +4 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryuugg/stanza",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "An opinionated code formatter for TypeScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"formatter",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"oxfmt": "^0.70.0",
|
|
55
55
|
"oxlint": "^1.85.0",
|
|
56
56
|
"tegami": "^1.5.0",
|
|
57
|
-
"typescript": "
|
|
57
|
+
"typescript": "^7.0.2"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|
|
60
60
|
"bun": ">=1.4.0"
|
package/src/cli.ts
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import { version } from "../package.json" with { type: "json" };
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
chmodSync,
|
|
7
|
-
constants,
|
|
8
|
-
readFileSync,
|
|
9
|
-
realpathSync,
|
|
10
|
-
renameSync,
|
|
11
|
-
type Stats,
|
|
12
|
-
statSync,
|
|
13
|
-
unlinkSync,
|
|
14
|
-
writeFileSync,
|
|
15
|
-
} from "node:fs";
|
|
16
|
-
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
4
|
+
import { dirname, relative, resolve } from "node:path";
|
|
5
|
+
import { beginInvocation } from "./directories.ts";
|
|
17
6
|
import {
|
|
18
7
|
collectChanged,
|
|
19
8
|
collectFiles,
|
|
@@ -36,8 +25,9 @@ import { blockReason, claudeCodeHooks, hookCall, rereadLine, type HookCall } fro
|
|
|
36
25
|
import { systemIo, type Io } from "./start.ts";
|
|
37
26
|
import type { Changed } from "./engine/model.ts";
|
|
38
27
|
import { RULES } from "./engine/rules.ts";
|
|
39
|
-
import { decode,
|
|
40
|
-
import {
|
|
28
|
+
import { decode, withoutMark } from "./step.ts";
|
|
29
|
+
import { type Braces, type Finding, type Mode } from "./engine/types.ts";
|
|
30
|
+
import { formatFile, mergeOutcomes, readText, type Formatted, type Input } from "./format.ts";
|
|
41
31
|
import { bothBraces, columns, flags, usage } from "./usage.ts";
|
|
42
32
|
|
|
43
33
|
export type { Io } from "./start.ts";
|
|
@@ -61,12 +51,6 @@ interface ExplainArguments {
|
|
|
61
51
|
noBraces: boolean;
|
|
62
52
|
}
|
|
63
53
|
|
|
64
|
-
interface Input {
|
|
65
|
-
path: string;
|
|
66
|
-
read: () => Decoded;
|
|
67
|
-
changedLines?: Changed | undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
54
|
interface Run {
|
|
71
55
|
cwd: string;
|
|
72
56
|
mode: Mode;
|
|
@@ -75,15 +59,6 @@ interface Run {
|
|
|
75
59
|
display?: (path: string) => string;
|
|
76
60
|
}
|
|
77
61
|
|
|
78
|
-
interface Formatted {
|
|
79
|
-
findings: Finding[];
|
|
80
|
-
failed: boolean;
|
|
81
|
-
rewritten: string[];
|
|
82
|
-
kept: Map<string, string>;
|
|
83
|
-
unread: Set<string>;
|
|
84
|
-
unreadWidth: Set<string>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
62
|
const switches = new Set(["--braces", "--changed", "--hunks", "--json", "--no-braces", "--staged"]);
|
|
88
63
|
const standalone = new Set(["--help", "-h", "--version"]);
|
|
89
64
|
|
|
@@ -208,14 +183,6 @@ function printFindings(findings: Finding[], json: boolean, print: (line: string)
|
|
|
208
183
|
print(`${finding.path}:${finding.line}:${finding.col} ${finding.rule} ${finding.message}`);
|
|
209
184
|
}
|
|
210
185
|
|
|
211
|
-
function readText(path: string): Decoded {
|
|
212
|
-
try {
|
|
213
|
-
return decode(readFileSync(path));
|
|
214
|
-
} catch (error: unknown) {
|
|
215
|
-
return { message: String(error) };
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
186
|
function runStdin(input: string, args: Arguments, io: Io): number {
|
|
220
187
|
let source: Uint8Array;
|
|
221
188
|
try {
|
|
@@ -309,102 +276,13 @@ function runExplain(argv: string[], io: Io): number {
|
|
|
309
276
|
return result.found ? 0 : 1;
|
|
310
277
|
}
|
|
311
278
|
|
|
312
|
-
function removeQuietly(path: string): void {
|
|
313
|
-
try {
|
|
314
|
-
unlinkSync(path);
|
|
315
|
-
} catch {}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function replaceFile(real: string, text: string, target: Stats): boolean {
|
|
319
|
-
const temp = join(dirname(real), `.stanza-${randomUUID()}.tmp`);
|
|
320
|
-
try {
|
|
321
|
-
writeFileSync(temp, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
322
|
-
} catch (error: unknown) {
|
|
323
|
-
removeQuietly(temp);
|
|
324
|
-
if (["EACCES", "EPERM"].includes(errorCode(error))) return false;
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
try {
|
|
329
|
-
const created = statSync(temp);
|
|
330
|
-
if (created.uid !== target.uid || created.gid !== target.gid) {
|
|
331
|
-
removeQuietly(temp);
|
|
332
|
-
return false;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
chmodSync(temp, target.mode & 0o7777);
|
|
336
|
-
renameSync(temp, real);
|
|
337
|
-
return true;
|
|
338
|
-
} catch (error: unknown) {
|
|
339
|
-
removeQuietly(temp);
|
|
340
|
-
throw error;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function writeInPlace(real: string, text: string): void {
|
|
345
|
-
const original = readFileSync(real);
|
|
346
|
-
try {
|
|
347
|
-
writeFileSync(real, text, "utf8");
|
|
348
|
-
} catch (error: unknown) {
|
|
349
|
-
try {
|
|
350
|
-
writeFileSync(real, original);
|
|
351
|
-
} catch {}
|
|
352
|
-
|
|
353
|
-
throw error;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function writeFixed(path: string, text: string, output: string): Finding | undefined {
|
|
358
|
-
try {
|
|
359
|
-
const real = realpathSync(path);
|
|
360
|
-
const target = statSync(real);
|
|
361
|
-
accessSync(real, constants.W_OK);
|
|
362
|
-
|
|
363
|
-
if (target.nlink > 1 || !replaceFile(real, text, target)) writeInPlace(real, text);
|
|
364
|
-
} catch (error: unknown) {
|
|
365
|
-
return { path: output, line: 1, col: 1, rule: "write", message: String(error), fixable: false };
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
279
|
function formatInputs(inputs: Input[], run: Run): Formatted {
|
|
370
|
-
const
|
|
371
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const unreadWidth = new Set<string>();
|
|
375
|
-
|
|
376
|
-
let failed = false;
|
|
377
|
-
for (const input of inputs) {
|
|
378
|
-
const output = run.display?.(input.path) ?? printedPath(input.path, run.cwd);
|
|
379
|
-
const result = formatText(input.path, input.read(), {
|
|
380
|
-
mode: run.mode,
|
|
381
|
-
braces: run.braces,
|
|
382
|
-
changedLines: input.changedLines,
|
|
383
|
-
unread,
|
|
384
|
-
unreadWidth,
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
findings.push(...result.findings.map((finding) => ({ ...finding, path: output })));
|
|
388
|
-
failed ||= result.parseError;
|
|
389
|
-
|
|
390
|
-
if (result.fixed === undefined) continue;
|
|
391
|
-
|
|
392
|
-
if (!run.write) {
|
|
393
|
-
kept.set(input.path, result.fixed);
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
const unwritten = writeFixed(input.path, result.fixed, output);
|
|
398
|
-
if (unwritten) {
|
|
399
|
-
findings.push(unwritten);
|
|
400
|
-
failed = true;
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
rewritten.push(output);
|
|
405
|
-
}
|
|
280
|
+
const warnings = { unread: new Set<string>(), unreadWidth: new Set<string>() };
|
|
281
|
+
const outcomes = inputs.map((input) =>
|
|
282
|
+
formatFile(input, run, run.display?.(input.path) ?? printedPath(input.path, run.cwd), warnings),
|
|
283
|
+
);
|
|
406
284
|
|
|
407
|
-
return
|
|
285
|
+
return mergeOutcomes(outcomes, warnings);
|
|
408
286
|
}
|
|
409
287
|
|
|
410
288
|
function status(
|
|
@@ -449,21 +327,31 @@ function runFiles(args: Arguments, io: Io): number {
|
|
|
449
327
|
warn(io, messages[reason]);
|
|
450
328
|
}
|
|
451
329
|
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
);
|
|
330
|
+
const inputs = collected.files.map((path) => ({
|
|
331
|
+
path,
|
|
332
|
+
output: printedPath(path, cwd),
|
|
333
|
+
changedLines: args.hunks ? changed?.changedLines?.get(path) : undefined,
|
|
334
|
+
}));
|
|
335
|
+
|
|
336
|
+
const run = { cwd, mode: args.mode, braces: args.braces, write: true };
|
|
337
|
+
const { workerCount, formatPooled } = require("./pool.ts") as typeof import("./pool.ts");
|
|
338
|
+
const count = workerCount(inputs.length, io.env);
|
|
339
|
+
const pooled = count > 0 ? formatPooled(inputs, run, count) : undefined;
|
|
340
|
+
const result =
|
|
341
|
+
pooled?.formatted ??
|
|
342
|
+
formatInputs(
|
|
343
|
+
inputs.map((input) => ({ ...input, read: () => readText(input.path) })),
|
|
344
|
+
run,
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
for (const error of pooled?.errors ?? []) warn(io, `stanza: ${error}`);
|
|
460
348
|
|
|
461
349
|
printFindings(result.findings, args.json, (line) => io.stdout(`${line}\n`));
|
|
462
350
|
|
|
463
351
|
for (const error of collected.errors) warn(io, `stanza: ${error}`);
|
|
464
352
|
|
|
465
353
|
warnUnread(io, cwd, result.unread, result.unreadWidth);
|
|
466
|
-
return status(collected.errors, result);
|
|
354
|
+
return status([...collected.errors, ...(pooled?.errors ?? [])], result);
|
|
467
355
|
}
|
|
468
356
|
|
|
469
357
|
function shellWord(word: string): string {
|
|
@@ -730,6 +618,8 @@ function runHook(args: string[], io: Io): number {
|
|
|
730
618
|
}
|
|
731
619
|
|
|
732
620
|
export function runHookCall(call: HookCall, args: string[], io: Io): number {
|
|
621
|
+
beginInvocation();
|
|
622
|
+
|
|
733
623
|
if (call.event === "write") return runWriteHook(call.cwd, call.toolInput, args, io);
|
|
734
624
|
if (call.event === "edit") return runEditHook(call, args, io);
|
|
735
625
|
return runStopHook(call, args, io);
|
|
@@ -737,6 +627,9 @@ export function runHookCall(call: HookCall, args: string[], io: Io): number {
|
|
|
737
627
|
|
|
738
628
|
export function main(argv: string[], io: Io): number {
|
|
739
629
|
if (argv[0] === "hook") return runHook(argv.slice(1), io);
|
|
630
|
+
|
|
631
|
+
beginInvocation();
|
|
632
|
+
|
|
740
633
|
if (argv[0] === "explain") return runExplain(argv.slice(1), io);
|
|
741
634
|
|
|
742
635
|
const [only] = argv;
|
package/src/compile/linux-x64.ts
CHANGED
package/src/depth.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
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
|
+
}
|