@blundergoat/gruff-ts 0.5.0 → 0.6.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/CHANGELOG.md +82 -0
- package/CONTRIBUTING.md +32 -25
- package/README.md +49 -50
- package/SECURITY.md +18 -31
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -10
- package/docs/agent-hook.md +32 -35
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +160 -89
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -172
- package/docs/rules.md +126 -98
- package/package.json +10 -8
- package/src/analyser.ts +817 -323
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +62 -68
- package/src/blocks.ts +512 -336
- package/src/changed-regions.ts +380 -79
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +445 -436
- package/src/cli-program.ts +915 -172
- package/src/cli.ts +9 -3
- package/src/comment-rules.ts +236 -418
- package/src/comment-scanner.ts +339 -47
- package/src/complexity-metrics.ts +111 -35
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +751 -223
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +182 -54
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +493 -140
- package/src/doc-rules.ts +602 -126
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +134 -109
- package/src/findings.ts +47 -27
- package/src/fixture-purpose-rules.ts +194 -50
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +328 -121
- package/src/hook-contract.ts +681 -255
- package/src/init-config.ts +163 -118
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -468
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +32 -127
- package/src/parsed-script.ts +868 -314
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +86 -74
- package/src/project-config-rules.ts +324 -140
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +102 -38
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +821 -203
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +249 -55
- package/src/security-flow-rules.ts +791 -115
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +460 -387
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +378 -104
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +343 -253
- package/src/text-scans.ts +62 -11
- package/src/types.ts +220 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -163
- package/src/test-fixtures.ts +0 -671
package/src/source-text.ts
CHANGED
|
@@ -14,11 +14,11 @@ interface DiagnosticSourceFile {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Syntax-only TypeScript parser diagnostics for script files; non-script inputs stay unparsed.
|
|
17
|
-
*
|
|
18
|
-
* analyser consumes the boundary result once per script.
|
|
17
|
+
* Uses the shared parser so callers that only need diagnostics get the same summary as the analyser.
|
|
19
18
|
*
|
|
20
19
|
* @param file - Source metadata used to skip non-script inputs and report paths.
|
|
21
20
|
* @param source - Raw file text to parse for syntax diagnostics.
|
|
21
|
+
* @returns One summarized parse-error diagnostic when the script fails to parse; empty for non-script inputs and clean parses.
|
|
22
22
|
*/
|
|
23
23
|
function parseDiagnostics(file: DiagnosticSourceFile, source: string): RunDiagnostic[] {
|
|
24
24
|
return parseScript(file, source)?.diagnostics ?? [];
|
|
@@ -32,11 +32,10 @@ interface DelimiterCounts {
|
|
|
32
32
|
brackets: number;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
// Lexer state for the delimiter scanner
|
|
36
|
-
//
|
|
37
|
-
// `
|
|
38
|
-
//
|
|
39
|
-
// flip out of quote mode early and start counting code-level braces inside the string body.
|
|
35
|
+
// Lexer state for the delimiter and masking scanner across quotes, comments, regex literals, and nested templates.
|
|
36
|
+
//
|
|
37
|
+
// - `previousCode` tracks the last non-whitespace code character so `/` disambiguates regex literals from division;
|
|
38
|
+
// - `templateInterpolationStack` records the brace depth at each `${` so matching `}` re-enters template text cleanly.
|
|
40
39
|
interface DelimiterScanState {
|
|
41
40
|
quote: string | undefined;
|
|
42
41
|
isEscaped: boolean;
|
|
@@ -61,8 +60,12 @@ interface ScanStep {
|
|
|
61
60
|
shouldStopLine: boolean;
|
|
62
61
|
}
|
|
63
62
|
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Builds fresh delimiter-scan state for a new file with no active literal or comment and no prior code character.
|
|
65
|
+
* An empty `previousCode` lets a slash at the start of code open a regex; later code updates that context.
|
|
66
|
+
*
|
|
67
|
+
* @returns Initialized `DelimiterScanState` with cleared literal, comment, and escape flags.
|
|
68
|
+
*/
|
|
66
69
|
function defaultDelimiterScanState(): DelimiterScanState {
|
|
67
70
|
return {
|
|
68
71
|
quote: undefined,
|
|
@@ -76,8 +79,15 @@ function defaultDelimiterScanState(): DelimiterScanState {
|
|
|
76
79
|
};
|
|
77
80
|
}
|
|
78
81
|
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Updates the supplied delimiter totals and scanner state across one source line.
|
|
84
|
+
* Stops at a line comment because the rest of the line is comment text; block comments, string text, and regex
|
|
85
|
+
* bodies leave counts unchanged while template interpolations resume counting.
|
|
86
|
+
*
|
|
87
|
+
* @param line - Source line text to scan from left to right.
|
|
88
|
+
* @param ctx - Mutable delimiter counts and lexer state updated in place for `line`.
|
|
89
|
+
* @returns Nothing. Mutates `ctx.counts` and `ctx.scan` for each scanned character until EOL or a line comment.
|
|
90
|
+
*/
|
|
81
91
|
function scanDelimiterLine(line: string, ctx: DelimiterScanContext): void {
|
|
82
92
|
for (let offset = 0; offset < line.length; offset += 1) {
|
|
83
93
|
const step = scanDelimiterCharacter(line, offset, ctx);
|
|
@@ -88,8 +98,14 @@ function scanDelimiterLine(line: string, ctx: DelimiterScanContext): void {
|
|
|
88
98
|
}
|
|
89
99
|
}
|
|
90
100
|
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Scans `line` at `offset` according to the lexical mode stored in `ctx`, mutating its counts and scan state.
|
|
103
|
+
*
|
|
104
|
+
* @param line - Source line text containing the character to inspect.
|
|
105
|
+
* @param offset - Zero-based index of the current character within `line`.
|
|
106
|
+
* @param ctx - Mutable delimiter counts and lexer state carried across characters.
|
|
107
|
+
* @returns Step telling the line walker whether to continue, skip a character, or stop at a line comment.
|
|
108
|
+
*/
|
|
93
109
|
function scanDelimiterCharacter(line: string, offset: number, ctx: DelimiterScanContext): ScanStep {
|
|
94
110
|
const character = line[offset] ?? "";
|
|
95
111
|
const next = line[offset + 1] ?? "";
|
|
@@ -106,8 +122,15 @@ function scanDelimiterCharacter(line: string, offset: number, ctx: DelimiterScan
|
|
|
106
122
|
return scanCodeDelimiter(line, offset, character, next, ctx);
|
|
107
123
|
}
|
|
108
124
|
|
|
109
|
-
|
|
110
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Scans inside a block comment, leaving delimiter counts unchanged until the closing delimiter.
|
|
127
|
+
* Mutates `scan.isInBlockComment` to false on the closer so the next step resumes in code mode.
|
|
128
|
+
*
|
|
129
|
+
* @param character - Current character inside the block comment.
|
|
130
|
+
* @param next - Lookahead character immediately following `character`.
|
|
131
|
+
* @param scan - Mutable lexer state updated when the closing delimiter is matched.
|
|
132
|
+
* @returns Step with `skip: 1` to swallow the trailing `/` on a closer, or a continuing step otherwise.
|
|
133
|
+
*/
|
|
111
134
|
function scanBlockCommentDelimiter(character: string, next: string, scan: DelimiterScanState): ScanStep {
|
|
112
135
|
if (character === "*" && next === "/") {
|
|
113
136
|
scan.isInBlockComment = false;
|
|
@@ -116,11 +139,15 @@ function scanBlockCommentDelimiter(character: string, next: string, scan: Delimi
|
|
|
116
139
|
return continueScan();
|
|
117
140
|
}
|
|
118
141
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Scans inside a quoted literal, mutating quote, escape, and template-interpolation state in `ctx`.
|
|
144
|
+
* Escapes protect the next character; template interpolation saves brace depth and resumes code until its closing brace.
|
|
145
|
+
*
|
|
146
|
+
* @param character - Current character inside the quoted literal.
|
|
147
|
+
* @param next - Lookahead character immediately following `character`.
|
|
148
|
+
* @param ctx - Mutable delimiter counts and lexer state updated on escapes, quotes, or `${`.
|
|
149
|
+
* @returns Continuing step, or a skip step after `${` so the opening brace is not re-counted.
|
|
150
|
+
*/
|
|
124
151
|
function scanQuotedDelimiter(character: string, next: string, ctx: DelimiterScanContext): ScanStep {
|
|
125
152
|
const scan = ctx.scan;
|
|
126
153
|
if (scan.isEscaped) {
|
|
@@ -144,8 +171,14 @@ function scanQuotedDelimiter(character: string, next: string, ctx: DelimiterScan
|
|
|
144
171
|
return continueScan();
|
|
145
172
|
}
|
|
146
173
|
|
|
147
|
-
|
|
148
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Scans inside a regex literal, mutating `scan` for escapes, character classes, and the closing slash.
|
|
176
|
+
* Closing slash is only honoured when `isInRegexCharClass` is false because `[...]` can contain `/`.
|
|
177
|
+
*
|
|
178
|
+
* @param character - Current character inside the regex literal body.
|
|
179
|
+
* @param scan - Mutable lexer state updated for escapes, `[` / `]` class boundaries, and regex exit.
|
|
180
|
+
* @returns Nothing. Mutates `scan` in place and sets `scan.previousCode` to `"x"` when the regex closes.
|
|
181
|
+
*/
|
|
149
182
|
function scanRegexDelimiter(character: string, scan: DelimiterScanState): void {
|
|
150
183
|
if (scan.isRegexEscaped) {
|
|
151
184
|
scan.isRegexEscaped = false;
|
|
@@ -161,8 +194,17 @@ function scanRegexDelimiter(character: string, scan: DelimiterScanState): void {
|
|
|
161
194
|
}
|
|
162
195
|
}
|
|
163
196
|
|
|
164
|
-
|
|
165
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Classifies `character` and lookahead `next` in executable code, using `line` and `offset` for regex detection.
|
|
199
|
+
* Mutates delimiter counts and lexical state in `ctx` according to the matched token handler.
|
|
200
|
+
*
|
|
201
|
+
* @param line - Source line text providing preceding context for the regex-opening heuristic.
|
|
202
|
+
* @param offset - Zero-based index of `character` in `line`.
|
|
203
|
+
* @param character - Current character in executable code.
|
|
204
|
+
* @param next - Lookahead character immediately following `character`.
|
|
205
|
+
* @param ctx - Mutable delimiter counts and lexer state updated by the matching token handler.
|
|
206
|
+
* @returns Matched token's scan step, or the ordinary-code step when no opener matches.
|
|
207
|
+
*/
|
|
166
208
|
function scanCodeDelimiter(line: string, offset: number, character: string, next: string, ctx: DelimiterScanContext): ScanStep {
|
|
167
209
|
return (
|
|
168
210
|
scanLineCommentStart(character, next) ??
|
|
@@ -173,7 +215,13 @@ function scanCodeDelimiter(line: string, offset: number, character: string, next
|
|
|
173
215
|
);
|
|
174
216
|
}
|
|
175
217
|
|
|
176
|
-
|
|
218
|
+
/**
|
|
219
|
+
* Detects a `//` line-comment opener that ends delimiter scanning for the remainder of the line.
|
|
220
|
+
*
|
|
221
|
+
* @param character - Current character in executable code.
|
|
222
|
+
* @param next - Lookahead character immediately following `character`.
|
|
223
|
+
* @returns Stopping step when `//` is matched, or `undefined` to try another token kind.
|
|
224
|
+
*/
|
|
177
225
|
function scanLineCommentStart(character: string, next: string): ScanStep | undefined {
|
|
178
226
|
if (character === "/" && next === "/") {
|
|
179
227
|
return stopLineScan();
|
|
@@ -181,7 +229,15 @@ function scanLineCommentStart(character: string, next: string): ScanStep | undef
|
|
|
181
229
|
return undefined;
|
|
182
230
|
}
|
|
183
231
|
|
|
184
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Detects a block-comment opener (`/*`) and mutates `scan.isInBlockComment` so following text is skipped.
|
|
234
|
+
* Uses `skip: 1` to swallow the `*` so it is not re-evaluated as part of a closer.
|
|
235
|
+
*
|
|
236
|
+
* @param character - Current character in executable code.
|
|
237
|
+
* @param next - Lookahead character immediately following `character`.
|
|
238
|
+
* @param scan - Mutable lexer state updated when a block comment opens.
|
|
239
|
+
* @returns Skip step when a block comment opens, or `undefined` to try another token handler.
|
|
240
|
+
*/
|
|
185
241
|
function scanBlockCommentStart(character: string, next: string, scan: DelimiterScanState): ScanStep | undefined {
|
|
186
242
|
if (character === "/" && next === "*") {
|
|
187
243
|
scan.isInBlockComment = true;
|
|
@@ -190,7 +246,13 @@ function scanBlockCommentStart(character: string, next: string, scan: DelimiterS
|
|
|
190
246
|
return undefined;
|
|
191
247
|
}
|
|
192
248
|
|
|
193
|
-
|
|
249
|
+
/**
|
|
250
|
+
* Recognises a string or template quote opener and stores it in `scan.quote`.
|
|
251
|
+
*
|
|
252
|
+
* @param character - Current character in executable code.
|
|
253
|
+
* @param scan - Mutable lexer state updated with the active quote delimiter on match.
|
|
254
|
+
* @returns Continuing step when `character` is a quote, or `undefined` to try another token kind.
|
|
255
|
+
*/
|
|
194
256
|
function scanQuoteStart(character: string, scan: DelimiterScanState): ScanStep | undefined {
|
|
195
257
|
if (isQuote(character)) {
|
|
196
258
|
scan.quote = character;
|
|
@@ -199,9 +261,16 @@ function scanQuoteStart(character: string, scan: DelimiterScanState): ScanStep |
|
|
|
199
261
|
return undefined;
|
|
200
262
|
}
|
|
201
263
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Distinguishes a regex literal opener from division using `scan.previousCode` and the line prefix before `offset`.
|
|
266
|
+
* Mutates `scan` regex flags when `/` opens a regex literal.
|
|
267
|
+
*
|
|
268
|
+
* @param line - Source line text containing the candidate slash.
|
|
269
|
+
* @param offset - Zero-based index of `character` in `line`.
|
|
270
|
+
* @param character - Current character in executable code.
|
|
271
|
+
* @param scan - Mutable lexer state supplying `previousCode` and receiving updated regex flags.
|
|
272
|
+
* @returns Continuing step on a regex opener, or `undefined` for the next token handler.
|
|
273
|
+
*/
|
|
205
274
|
function scanRegexStart(line: string, offset: number, character: string, scan: DelimiterScanState): ScanStep | undefined {
|
|
206
275
|
if (character === "/" && isRegexLiteralStart(scan.previousCode, line.slice(0, offset))) {
|
|
207
276
|
scan.isInRegex = true;
|
|
@@ -212,10 +281,14 @@ function scanRegexStart(line: string, offset: number, character: string, scan: D
|
|
|
212
281
|
return undefined;
|
|
213
282
|
}
|
|
214
283
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
284
|
+
/**
|
|
285
|
+
* Counts `character` in `ctx.counts` and updates `ctx.scan` so later slashes classify as regex or division.
|
|
286
|
+
* Ignores whitespace when recording `previousCode`; a closing interpolation brace restores template-literal mode.
|
|
287
|
+
*
|
|
288
|
+
* @param character - Current ordinary character in executable code.
|
|
289
|
+
* @param ctx - Mutable delimiter counts and lexer state updated in place.
|
|
290
|
+
* @returns Continuing scan step after updating counts and interpolation/code state.
|
|
291
|
+
*/
|
|
219
292
|
function scanPlainCodeDelimiter(character: string, ctx: DelimiterScanContext): ScanStep {
|
|
220
293
|
countDelimiter(character, ctx.counts);
|
|
221
294
|
if (character === "}" && ctx.scan.templateInterpolationStack.length > 0 && ctx.counts.braces === (ctx.scan.templateInterpolationStack[ctx.scan.templateInterpolationStack.length - 1] ?? -1)) {
|
|
@@ -230,8 +303,14 @@ function scanPlainCodeDelimiter(character: string, ctx: DelimiterScanContext): S
|
|
|
230
303
|
return continueScan();
|
|
231
304
|
}
|
|
232
305
|
|
|
233
|
-
|
|
234
|
-
|
|
306
|
+
/**
|
|
307
|
+
* Updates the supplied delimiter totals for `character` by incrementing on openers and decrementing on closers.
|
|
308
|
+
* Other characters are ignored so callers can pass every code character unconditionally.
|
|
309
|
+
*
|
|
310
|
+
* @param character - Character tested against `{`, `}`, `(`, `)`, `[`, and `]`.
|
|
311
|
+
* @param counts - Mutable delimiter totals updated in place.
|
|
312
|
+
* @returns Nothing. Mutates `counts` when `character` is a tracked delimiter.
|
|
313
|
+
*/
|
|
235
314
|
function countDelimiter(character: string, counts: DelimiterCounts): void {
|
|
236
315
|
if (character === "{") {
|
|
237
316
|
counts.braces += 1;
|
|
@@ -248,21 +327,33 @@ function countDelimiter(character: string, counts: DelimiterCounts): void {
|
|
|
248
327
|
}
|
|
249
328
|
}
|
|
250
329
|
|
|
251
|
-
|
|
252
|
-
|
|
330
|
+
/**
|
|
331
|
+
* Checks whether `counts` has any negative brace, parenthesis, or bracket balance from a closer without an opener.
|
|
332
|
+
*
|
|
333
|
+
* @param counts - Running delimiter totals to inspect.
|
|
334
|
+
* @returns True if any delimiter balance is below zero; otherwise false.
|
|
335
|
+
*/
|
|
253
336
|
function hasNegativeDelimiterCount(counts: DelimiterCounts): boolean {
|
|
254
337
|
return counts.braces < 0 || counts.parentheses < 0 || counts.brackets < 0;
|
|
255
338
|
}
|
|
256
339
|
|
|
257
|
-
|
|
340
|
+
/**
|
|
341
|
+
* Checks whether `counts` has any nonzero brace, parenthesis, or bracket balance indicating unmatched delimiters.
|
|
342
|
+
*
|
|
343
|
+
* @param counts - Final delimiter totals at end of file.
|
|
344
|
+
* @returns True if any delimiter count is nonzero; otherwise false.
|
|
345
|
+
*/
|
|
258
346
|
function hasUnbalancedDelimiterCount(counts: DelimiterCounts): boolean {
|
|
259
347
|
return counts.braces !== 0 || counts.parentheses !== 0 || counts.brackets !== 0;
|
|
260
348
|
}
|
|
261
349
|
|
|
262
|
-
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
350
|
+
/**
|
|
351
|
+
* Builds a parse-error diagnostic for a caller to include in scan results.
|
|
352
|
+
*
|
|
353
|
+
* @param file - source metadata whose displayPath identifies the failed file
|
|
354
|
+
* @param line - source line to attach to the error
|
|
355
|
+
* @param message - explanation copied into the diagnostic
|
|
356
|
+
* @returns a parse-error record containing the supplied message, display path, and line
|
|
266
357
|
*/
|
|
267
358
|
function parseErrorDiagnostic(file: DiagnosticSourceFile, line: number, message: string): RunDiagnostic {
|
|
268
359
|
return {
|
|
@@ -273,15 +364,25 @@ function parseErrorDiagnostic(file: DiagnosticSourceFile, line: number, message:
|
|
|
273
364
|
};
|
|
274
365
|
}
|
|
275
366
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
367
|
+
/**
|
|
368
|
+
* Determines whether a slash looks like a regex opener using a conservative heuristic to avoid masking real code.
|
|
369
|
+
* Empty `previousCode`, selected punctuation, or trailing `return` before the slash permits a regex.
|
|
370
|
+
*
|
|
371
|
+
* @param previousCode - Last non-whitespace code character, synthetic operand marker, or empty start marker.
|
|
372
|
+
* @param beforeSlash - Preceding line slice before the candidate `/` character.
|
|
373
|
+
* @returns True when the slash context permits a regex literal opener; otherwise false.
|
|
374
|
+
*/
|
|
279
375
|
function isRegexLiteralStart(previousCode: string, beforeSlash: string): boolean {
|
|
280
376
|
return previousCode === "" || "([{=,:!&|?;".includes(previousCode) || /\breturn$/.test(beforeSlash.trimEnd());
|
|
281
377
|
}
|
|
282
378
|
|
|
283
|
-
|
|
284
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Masks recognized comment and literal bodies in `source` while preserving UTF-16 code-unit offsets and newlines.
|
|
381
|
+
* Regex-driven rules use the unchanged positions to report source locations without matching text inside those bodies.
|
|
382
|
+
*
|
|
383
|
+
* @param source - Raw source file text to mask.
|
|
384
|
+
* @returns Copy of `source` with comment, string, template, and regex bodies replaced by spaces.
|
|
385
|
+
*/
|
|
285
386
|
function maskNonCode(source: string): string {
|
|
286
387
|
let result = "";
|
|
287
388
|
const state = defaultMaskState();
|
|
@@ -293,10 +394,13 @@ function maskNonCode(source: string): string {
|
|
|
293
394
|
return result;
|
|
294
395
|
}
|
|
295
396
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
397
|
+
/**
|
|
398
|
+
* Masks template-literal bodies in `source` with spaces while preserving offsets and line-feed characters.
|
|
399
|
+
* Single- and double-quoted bodies remain visible so syntax-pattern rules can read real import specifiers.
|
|
400
|
+
*
|
|
401
|
+
* @param source - Raw source file text to mask.
|
|
402
|
+
* @returns Copy of `source` with backtick template contents replaced by spaces.
|
|
403
|
+
*/
|
|
300
404
|
function maskTemplateLiteralBodies(source: string): string {
|
|
301
405
|
let result = "";
|
|
302
406
|
const state = templateMaskState();
|
|
@@ -319,13 +423,24 @@ interface TemplateMaskState {
|
|
|
319
423
|
isBlockComment: boolean;
|
|
320
424
|
}
|
|
321
425
|
|
|
322
|
-
|
|
426
|
+
/**
|
|
427
|
+
* Creates fresh default state for a single-file template-mask pass.
|
|
428
|
+
*
|
|
429
|
+
* @returns Initialized `TemplateMaskState` with no active quote or comment context.
|
|
430
|
+
*/
|
|
323
431
|
function templateMaskState(): TemplateMaskState {
|
|
324
432
|
return { quote: undefined, isEscaped: false, isLineComment: false, isBlockComment: false };
|
|
325
433
|
}
|
|
326
434
|
|
|
327
|
-
|
|
328
|
-
|
|
435
|
+
/**
|
|
436
|
+
* Transforms `character` with lookahead `next` for template-body masking, mutating `state` in place.
|
|
437
|
+
* Preserves delimiters, newlines, single/double-quoted strings, and comments while masking template body text.
|
|
438
|
+
*
|
|
439
|
+
* @param character - Current character from the source text.
|
|
440
|
+
* @param next - Lookahead character immediately following `character`.
|
|
441
|
+
* @param state - Mutable quote and comment context carried across characters.
|
|
442
|
+
* @returns Output text and extra-character skip count; regex literals receive no dedicated handling.
|
|
443
|
+
*/
|
|
329
444
|
function templateMaskCharacter(character: string, next: string, state: TemplateMaskState): MaskStep {
|
|
330
445
|
if (character === "\n") return templateMaskNewline(state);
|
|
331
446
|
if (state.isLineComment) return { text: character, skip: 0 };
|
|
@@ -334,7 +449,12 @@ function templateMaskCharacter(character: string, next: string, state: TemplateM
|
|
|
334
449
|
return templateMaskCodeCharacter(character, next, state);
|
|
335
450
|
}
|
|
336
451
|
|
|
337
|
-
|
|
452
|
+
/**
|
|
453
|
+
* Clears line-comment state and any non-template quote at a newline while preserving template-literal mode.
|
|
454
|
+
*
|
|
455
|
+
* @param state - Mutable template-mask state updated in place at the line boundary.
|
|
456
|
+
* @returns Step emitting `"\n"` with no extra-character skip.
|
|
457
|
+
*/
|
|
338
458
|
function templateMaskNewline(state: TemplateMaskState): MaskStep {
|
|
339
459
|
state.isLineComment = false;
|
|
340
460
|
if (state.quote !== "`") {
|
|
@@ -343,7 +463,14 @@ function templateMaskNewline(state: TemplateMaskState): MaskStep {
|
|
|
343
463
|
return { text: "\n", skip: 0 };
|
|
344
464
|
}
|
|
345
465
|
|
|
346
|
-
|
|
466
|
+
/**
|
|
467
|
+
* Preserves block-comment text and clears `state.isBlockComment` when the closing delimiter is reached.
|
|
468
|
+
*
|
|
469
|
+
* @param character - Current character inside the block comment.
|
|
470
|
+
* @param next - Lookahead character immediately following `character`.
|
|
471
|
+
* @param state - Mutable template-mask state updated on a block-comment closer.
|
|
472
|
+
* @returns Preserved text with `skip: 1` on the closing pair, or `skip: 0` otherwise.
|
|
473
|
+
*/
|
|
347
474
|
function templateMaskBlockComment(character: string, next: string, state: TemplateMaskState): MaskStep {
|
|
348
475
|
if (character === "*" && next === "/") {
|
|
349
476
|
state.isBlockComment = false;
|
|
@@ -352,7 +479,14 @@ function templateMaskBlockComment(character: string, next: string, state: Templa
|
|
|
352
479
|
return { text: character, skip: 0 };
|
|
353
480
|
}
|
|
354
481
|
|
|
355
|
-
|
|
482
|
+
/**
|
|
483
|
+
* Processes a character inside a quoted literal, masking backtick bodies while passing single/double quotes through.
|
|
484
|
+
* Preserves an unescaped closing quote and mutates escape and quote flags in `state`.
|
|
485
|
+
*
|
|
486
|
+
* @param character - Current character inside the active literal.
|
|
487
|
+
* @param state - Mutable template-mask state tracking escapes and the active quote delimiter.
|
|
488
|
+
* @returns Transformed character step with `skip: 0`.
|
|
489
|
+
*/
|
|
356
490
|
function templateMaskQuotedCharacter(character: string, state: TemplateMaskState): MaskStep {
|
|
357
491
|
const masked = state.quote === "`";
|
|
358
492
|
if (state.isEscaped) {
|
|
@@ -370,7 +504,15 @@ function templateMaskQuotedCharacter(character: string, state: TemplateMaskState
|
|
|
370
504
|
return { text: masked ? " " : character, skip: 0 };
|
|
371
505
|
}
|
|
372
506
|
|
|
373
|
-
|
|
507
|
+
/**
|
|
508
|
+
* Detects comment openers and the three quote types in code, passing ordinary characters through unchanged.
|
|
509
|
+
* Mutates `state` when a comment or quote opener is matched.
|
|
510
|
+
*
|
|
511
|
+
* @param character - Current character in code context.
|
|
512
|
+
* @param next - Lookahead character immediately following `character`.
|
|
513
|
+
* @param state - Mutable template-mask state updated on comment or quote entry.
|
|
514
|
+
* @returns Preserved text and extra-character skip count (`1` for comment openers, `0` otherwise).
|
|
515
|
+
*/
|
|
374
516
|
function templateMaskCodeCharacter(character: string, next: string, state: TemplateMaskState): MaskStep {
|
|
375
517
|
if (character === "/" && next === "/") {
|
|
376
518
|
state.isLineComment = true;
|
|
@@ -398,7 +540,10 @@ interface MaskState {
|
|
|
398
540
|
isRegexCharClass: boolean;
|
|
399
541
|
isRegexEscaped: boolean;
|
|
400
542
|
previousCode: string;
|
|
401
|
-
|
|
543
|
+
// One entry per open `${`, innermost last, counting the braces still open inside that interpolation. A scalar
|
|
544
|
+
// depth cannot tell a nested template's own `}` from the enclosing interpolation's, so it resumed code where the
|
|
545
|
+
// inner template body continued and masked the rest of the file.
|
|
546
|
+
templateInterpolationDepths: number[];
|
|
402
547
|
}
|
|
403
548
|
|
|
404
549
|
// `text` is what gets written for this character (space for masked, original for code, " " for
|
|
@@ -408,7 +553,11 @@ interface MaskStep {
|
|
|
408
553
|
skip: number;
|
|
409
554
|
}
|
|
410
555
|
|
|
411
|
-
|
|
556
|
+
/**
|
|
557
|
+
* Builds fresh `MaskState` with all flags cleared so the first scanner step treats source as code.
|
|
558
|
+
*
|
|
559
|
+
* @returns Initialized `MaskState` with empty prior code and no active literal or comment flags.
|
|
560
|
+
*/
|
|
412
561
|
function defaultMaskState(): MaskState {
|
|
413
562
|
return {
|
|
414
563
|
quote: undefined,
|
|
@@ -419,12 +568,19 @@ function defaultMaskState(): MaskState {
|
|
|
419
568
|
isRegexCharClass: false,
|
|
420
569
|
isRegexEscaped: false,
|
|
421
570
|
previousCode: "",
|
|
422
|
-
|
|
571
|
+
templateInterpolationDepths: [],
|
|
423
572
|
};
|
|
424
573
|
}
|
|
425
574
|
|
|
426
|
-
|
|
427
|
-
|
|
575
|
+
/**
|
|
576
|
+
* Masks or preserves `source[index]` according to `state`, mutating `state` as comments and literals open or close.
|
|
577
|
+
* Newlines end line comments and single-line quote or regex state while template literals and block comments remain active.
|
|
578
|
+
*
|
|
579
|
+
* @param source - Complete source text being masked.
|
|
580
|
+
* @param index - Zero-based UTF-16 offset of the current character in `source`.
|
|
581
|
+
* @param state - Mutable masking context carried across characters.
|
|
582
|
+
* @returns Output text and extra-character skip count for the current character or opener/closer pair.
|
|
583
|
+
*/
|
|
428
584
|
function maskNonCodeCharacter(source: string, index: number, state: MaskState): MaskStep {
|
|
429
585
|
const character = source[index] ?? "";
|
|
430
586
|
const next = source[index + 1] ?? "";
|
|
@@ -446,8 +602,13 @@ function maskNonCodeCharacter(source: string, index: number, state: MaskState):
|
|
|
446
602
|
return maskCodeCharacter(source, index, character, next, state);
|
|
447
603
|
}
|
|
448
604
|
|
|
449
|
-
|
|
450
|
-
|
|
605
|
+
/**
|
|
606
|
+
* Updates `state` at a newline by clearing line-comment, single-quote, double-quote, and regex modes.
|
|
607
|
+
* Keeps template-literal and block-comment modes because both can span lines, preserving source offsets.
|
|
608
|
+
*
|
|
609
|
+
* @param state - Mutable mask state updated in place at the newline.
|
|
610
|
+
* @returns Step emitting `"\n"` with `skip: 0`.
|
|
611
|
+
*/
|
|
451
612
|
function maskNewline(state: MaskState): MaskStep {
|
|
452
613
|
state.isLineComment = false;
|
|
453
614
|
if (state.quote !== "`") {
|
|
@@ -459,8 +620,15 @@ function maskNewline(state: MaskState): MaskStep {
|
|
|
459
620
|
return { text: "\n", skip: 0 };
|
|
460
621
|
}
|
|
461
622
|
|
|
462
|
-
|
|
463
|
-
|
|
623
|
+
/**
|
|
624
|
+
* Replaces block-comment text with spaces, turning the closing delimiter into two spaces to preserve character alignment.
|
|
625
|
+
* Clears `state.isBlockComment` when the closing pair is consumed.
|
|
626
|
+
*
|
|
627
|
+
* @param character - Current character inside the block comment.
|
|
628
|
+
* @param next - Lookahead character immediately following `character`.
|
|
629
|
+
* @param state - Mutable mask state updated when the block comment closes.
|
|
630
|
+
* @returns Two spaces with `skip: 1` on a closer, or a single masked space step otherwise.
|
|
631
|
+
*/
|
|
464
632
|
function maskBlockComment(character: string, next: string, state: MaskState): MaskStep {
|
|
465
633
|
if (character === "*" && next === "/") {
|
|
466
634
|
state.isBlockComment = false;
|
|
@@ -469,8 +637,15 @@ function maskBlockComment(character: string, next: string, state: MaskState): Ma
|
|
|
469
637
|
return maskSingleCharacter();
|
|
470
638
|
}
|
|
471
639
|
|
|
472
|
-
|
|
473
|
-
|
|
640
|
+
/**
|
|
641
|
+
* Masks characters inside a quoted literal while keeping the closing quote and template `${` openers visible.
|
|
642
|
+
* Mutates `state` for escapes, closing quotes, and pushing brace depth when a template interpolation opens.
|
|
643
|
+
*
|
|
644
|
+
* @param character - Current character inside the quoted literal.
|
|
645
|
+
* @param next - Lookahead character immediately following `character`.
|
|
646
|
+
* @param state - Mutable mask state tracking escape, quote, and template interpolation stack state.
|
|
647
|
+
* @returns Masking step with spaces for literal body text, `"${"` on interpolation, or the closing quote.
|
|
648
|
+
*/
|
|
474
649
|
function maskQuotedCharacter(character: string, next: string, state: MaskState): MaskStep {
|
|
475
650
|
if (state.isEscaped) {
|
|
476
651
|
state.isEscaped = false;
|
|
@@ -482,7 +657,7 @@ function maskQuotedCharacter(character: string, next: string, state: MaskState):
|
|
|
482
657
|
}
|
|
483
658
|
if (state.quote === "`" && character === "$" && next === "{") {
|
|
484
659
|
state.quote = undefined;
|
|
485
|
-
state.
|
|
660
|
+
state.templateInterpolationDepths.push(1);
|
|
486
661
|
state.previousCode = "{";
|
|
487
662
|
return { text: "${", skip: 1 };
|
|
488
663
|
}
|
|
@@ -494,8 +669,14 @@ function maskQuotedCharacter(character: string, next: string, state: MaskState):
|
|
|
494
669
|
return maskSingleCharacter();
|
|
495
670
|
}
|
|
496
671
|
|
|
497
|
-
|
|
498
|
-
|
|
672
|
+
/**
|
|
673
|
+
* Masks regex literal body characters and passes through the closing `/`, tracking escapes and `[...]` classes.
|
|
674
|
+
* Character classes suspend the closing-slash check so `/[/]/` does not terminate at the inner slash.
|
|
675
|
+
*
|
|
676
|
+
* @param character - Current character inside the regex literal.
|
|
677
|
+
* @param state - Mutable mask state updated for escapes, class boundaries, and regex exit.
|
|
678
|
+
* @returns Space-mask step for body characters, or the closing `/` with `skip: 0`.
|
|
679
|
+
*/
|
|
499
680
|
function maskRegexCharacter(character: string, state: MaskState): MaskStep {
|
|
500
681
|
if (state.isRegexEscaped) {
|
|
501
682
|
state.isRegexEscaped = false;
|
|
@@ -521,8 +702,17 @@ function maskRegexCharacter(character: string, state: MaskState): MaskStep {
|
|
|
521
702
|
return maskSingleCharacter();
|
|
522
703
|
}
|
|
523
704
|
|
|
524
|
-
|
|
525
|
-
|
|
705
|
+
/**
|
|
706
|
+
* Dispatches code-context masking for `source[index]`, mutating `state` on openers, braces, or code tokens.
|
|
707
|
+
* Checks comment openers before regex starts so a comment opener cannot enter regex mode, preserving offsets.
|
|
708
|
+
*
|
|
709
|
+
* @param source - Complete source text used to inspect context before a candidate regex slash.
|
|
710
|
+
* @param index - Zero-based offset of `character` in `source`.
|
|
711
|
+
* @param character - Current character in code context.
|
|
712
|
+
* @param next - Lookahead character immediately following `character`.
|
|
713
|
+
* @param state - Mutable mask state updated by the matching detector or plain-code fallback.
|
|
714
|
+
* @returns Output text and extra-character skip count from the first matching detector.
|
|
715
|
+
*/
|
|
526
716
|
function maskCodeCharacter(source: string, index: number, character: string, next: string, state: MaskState): MaskStep {
|
|
527
717
|
return (
|
|
528
718
|
maskTemplateInterpolationBrace(character, state) ??
|
|
@@ -534,21 +724,30 @@ function maskCodeCharacter(source: string, index: number, character: string, nex
|
|
|
534
724
|
);
|
|
535
725
|
}
|
|
536
726
|
|
|
537
|
-
|
|
538
|
-
|
|
727
|
+
/**
|
|
728
|
+
* Tracks `{` and `}` inside the active template interpolation, mutating only its innermost brace depth.
|
|
729
|
+
* Closing the final brace pops the interpolation entry and resumes template-body masking.
|
|
730
|
+
*
|
|
731
|
+
* @param character - Current character in code context.
|
|
732
|
+
* @param state - Mutable mask state carrying `templateInterpolationDepths` and `previousCode`.
|
|
733
|
+
* @returns Step preserving the brace with `skip: 0`, or `undefined` outside interpolations or for other characters.
|
|
734
|
+
*/
|
|
539
735
|
function maskTemplateInterpolationBrace(character: string, state: MaskState): MaskStep | undefined {
|
|
540
|
-
|
|
736
|
+
const openBraceDepths = state.templateInterpolationDepths;
|
|
737
|
+
const innermost = openBraceDepths.length - 1;
|
|
738
|
+
if (innermost < 0) {
|
|
541
739
|
return undefined;
|
|
542
740
|
}
|
|
543
741
|
if (character === "{") {
|
|
544
|
-
|
|
742
|
+
openBraceDepths[innermost] = (openBraceDepths[innermost] ?? 0) + 1;
|
|
545
743
|
state.previousCode = character;
|
|
546
744
|
return { text: character, skip: 0 };
|
|
547
745
|
}
|
|
548
746
|
if (character === "}") {
|
|
549
|
-
|
|
747
|
+
openBraceDepths[innermost] = (openBraceDepths[innermost] ?? 0) - 1;
|
|
550
748
|
state.previousCode = character;
|
|
551
|
-
if (
|
|
749
|
+
if (openBraceDepths[innermost] === 0) {
|
|
750
|
+
openBraceDepths.pop();
|
|
552
751
|
state.quote = "`";
|
|
553
752
|
}
|
|
554
753
|
return { text: character, skip: 0 };
|
|
@@ -556,8 +755,14 @@ function maskTemplateInterpolationBrace(character: string, state: MaskState): Ma
|
|
|
556
755
|
return undefined;
|
|
557
756
|
}
|
|
558
757
|
|
|
559
|
-
|
|
560
|
-
|
|
758
|
+
/**
|
|
759
|
+
* Detects a `//` line-comment opener and mutates `state.isLineComment` so the rest of the line is masked.
|
|
760
|
+
*
|
|
761
|
+
* @param character - Current character in code context.
|
|
762
|
+
* @param next - Lookahead character immediately following `character`.
|
|
763
|
+
* @param state - Mutable mask state updated when a line comment starts.
|
|
764
|
+
* @returns Two spaces with `skip: 1` on match, or `undefined` for the next detector.
|
|
765
|
+
*/
|
|
561
766
|
function maskLineCommentStart(character: string, next: string, state: MaskState): MaskStep | undefined {
|
|
562
767
|
if (character === "/" && next === "/") {
|
|
563
768
|
state.isLineComment = true;
|
|
@@ -566,7 +771,14 @@ function maskLineCommentStart(character: string, next: string, state: MaskState)
|
|
|
566
771
|
return undefined;
|
|
567
772
|
}
|
|
568
773
|
|
|
569
|
-
|
|
774
|
+
/**
|
|
775
|
+
* Detects a block-comment opener (`/*`) and mutates `state.isBlockComment` so comment text is masked.
|
|
776
|
+
*
|
|
777
|
+
* @param character - Current character in code context.
|
|
778
|
+
* @param next - Lookahead character immediately following `character`.
|
|
779
|
+
* @param state - Mutable mask state updated when a block comment starts.
|
|
780
|
+
* @returns Two spaces with `skip: 1` on match, or `undefined` for the next detector.
|
|
781
|
+
*/
|
|
570
782
|
function maskBlockCommentStart(character: string, next: string, state: MaskState): MaskStep | undefined {
|
|
571
783
|
if (character === "/" && next === "*") {
|
|
572
784
|
state.isBlockComment = true;
|
|
@@ -575,8 +787,15 @@ function maskBlockCommentStart(character: string, next: string, state: MaskState
|
|
|
575
787
|
return undefined;
|
|
576
788
|
}
|
|
577
789
|
|
|
578
|
-
|
|
579
|
-
|
|
790
|
+
/**
|
|
791
|
+
* Detects a regex literal opener using up to 80 characters before `index` and mutates `state.isRegex`.
|
|
792
|
+
*
|
|
793
|
+
* @param source - Complete source text supplying preceding context for the regex-start heuristic.
|
|
794
|
+
* @param index - Zero-based offset of `character` in `source`.
|
|
795
|
+
* @param character - Current character in code context.
|
|
796
|
+
* @param state - Mutable mask state supplying `previousCode` and updated on regex entry.
|
|
797
|
+
* @returns Step preserving `/` with `skip: 0` on match, or `undefined` for the next detector.
|
|
798
|
+
*/
|
|
580
799
|
function maskRegexStart(source: string, index: number, character: string, state: MaskState): MaskStep | undefined {
|
|
581
800
|
if (character === "/" && isRegexLiteralStart(state.previousCode, source.slice(Math.max(0, index - 80), index))) {
|
|
582
801
|
state.isRegex = true;
|
|
@@ -586,8 +805,14 @@ function maskRegexStart(source: string, index: number, character: string, state:
|
|
|
586
805
|
return undefined;
|
|
587
806
|
}
|
|
588
807
|
|
|
589
|
-
|
|
590
|
-
|
|
808
|
+
/**
|
|
809
|
+
* Detects a string or template quote opener and mutates `state.quote` and `state.previousCode`.
|
|
810
|
+
* Later calls mask literal text while keeping quote boundaries and template interpolation code visible.
|
|
811
|
+
*
|
|
812
|
+
* @param character - Current character in code context.
|
|
813
|
+
* @param state - Mutable mask state updated with the active quote delimiter.
|
|
814
|
+
* @returns Step preserving the quote with `skip: 0` on match, or `undefined` for the plain-code fallback.
|
|
815
|
+
*/
|
|
591
816
|
function maskQuoteStart(character: string, state: MaskState): MaskStep | undefined {
|
|
592
817
|
if (isQuote(character)) {
|
|
593
818
|
state.quote = character;
|
|
@@ -597,8 +822,14 @@ function maskQuoteStart(character: string, state: MaskState): MaskStep | undefin
|
|
|
597
822
|
return undefined;
|
|
598
823
|
}
|
|
599
824
|
|
|
600
|
-
|
|
601
|
-
|
|
825
|
+
/**
|
|
826
|
+
* Passes an ordinary code character through into masked output, updating `state.previousCode` only for non-whitespace.
|
|
827
|
+
* Only non-whitespace updates `previousCode` because the regex-start heuristic must see the last real token.
|
|
828
|
+
*
|
|
829
|
+
* @param character - Current ordinary character in code context.
|
|
830
|
+
* @param state - Mutable mask state whose `previousCode` is updated for non-whitespace characters.
|
|
831
|
+
* @returns Step preserving `character` with `skip: 0`.
|
|
832
|
+
*/
|
|
602
833
|
function maskPlainCodeCharacter(character: string, state: MaskState): MaskStep {
|
|
603
834
|
if (isNonWhitespaceCharacter(character)) {
|
|
604
835
|
state.previousCode = character;
|
|
@@ -606,14 +837,22 @@ function maskPlainCodeCharacter(character: string, state: MaskState): MaskStep {
|
|
|
606
837
|
return { text: character, skip: 0 };
|
|
607
838
|
}
|
|
608
839
|
|
|
609
|
-
|
|
840
|
+
/**
|
|
841
|
+
* Builds a single-space mask step to obscure one UTF-16 code unit while preserving source offsets.
|
|
842
|
+
*
|
|
843
|
+
* @returns `MaskStep` emitting `" "` with `skip: 0`.
|
|
844
|
+
*/
|
|
610
845
|
function maskSingleCharacter(): MaskStep {
|
|
611
846
|
return { text: " ", skip: 0 };
|
|
612
847
|
}
|
|
613
848
|
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
849
|
+
/**
|
|
850
|
+
* Strips string bodies and line comments from a single line while leaving quote delimiters for matching.
|
|
851
|
+
* This line-local scanner has no block-comment state; use `maskNonCode` to preserve state across lines.
|
|
852
|
+
*
|
|
853
|
+
* @param line - Single source line to strip for local pattern matching.
|
|
854
|
+
* @returns Line text with quoted contents and trailing line comments removed.
|
|
855
|
+
*/
|
|
617
856
|
function codeLineForMatching(line: string): string {
|
|
618
857
|
let result = "";
|
|
619
858
|
const state: CodeLineState = { quote: undefined, isEscaped: false };
|
|
@@ -640,8 +879,15 @@ interface CodeLineStep {
|
|
|
640
879
|
shouldStopLine: boolean;
|
|
641
880
|
}
|
|
642
881
|
|
|
643
|
-
|
|
644
|
-
|
|
882
|
+
/**
|
|
883
|
+
* Evaluates `line[index]` for line-local matching, mutating `state` to track string delimiters and escapes.
|
|
884
|
+
* A line-comment opener outside a string emits empty text and stops the line; other characters continue scanning.
|
|
885
|
+
*
|
|
886
|
+
* @param line - Raw source line being scanned.
|
|
887
|
+
* @param index - Zero-based offset of the current character in `line`.
|
|
888
|
+
* @param state - Mutable quote and escape state carried along the line.
|
|
889
|
+
* @returns Output text (keeping quotes and code, dropping string bodies) and `shouldStopLine` flag.
|
|
890
|
+
*/
|
|
645
891
|
function codeLineCharacter(line: string, index: number, state: CodeLineState): CodeLineStep {
|
|
646
892
|
const character = line[index] ?? "";
|
|
647
893
|
const next = line[index + 1] ?? "";
|
|
@@ -658,8 +904,14 @@ function codeLineCharacter(line: string, index: number, state: CodeLineState): C
|
|
|
658
904
|
return { text: character, shouldStopLine: false };
|
|
659
905
|
}
|
|
660
906
|
|
|
661
|
-
|
|
662
|
-
|
|
907
|
+
/**
|
|
908
|
+
* Suppresses quoted literal contents while keeping the unescaped closing quote visible as the literal boundary.
|
|
909
|
+
* Mutates `state` to track escapes and clears `state.quote` when the literal closes.
|
|
910
|
+
*
|
|
911
|
+
* @param character - Current character inside the quoted literal.
|
|
912
|
+
* @param state - Mutable quote and escape state updated in place.
|
|
913
|
+
* @returns Closing quote or empty text, always with `shouldStopLine: false`.
|
|
914
|
+
*/
|
|
663
915
|
function quotedCodeLineCharacter(character: string, state: CodeLineState): CodeLineStep {
|
|
664
916
|
if (state.isEscaped) {
|
|
665
917
|
state.isEscaped = false;
|
|
@@ -676,28 +928,50 @@ function quotedCodeLineCharacter(character: string, state: CodeLineState): CodeL
|
|
|
676
928
|
return { text: "", shouldStopLine: false };
|
|
677
929
|
}
|
|
678
930
|
|
|
679
|
-
|
|
931
|
+
/**
|
|
932
|
+
* Default scan step advancing by one character while keeping the line scan active.
|
|
933
|
+
*
|
|
934
|
+
* @returns `ScanStep` with `skip: 0` and `shouldStopLine: false`.
|
|
935
|
+
*/
|
|
680
936
|
function continueScan(): ScanStep {
|
|
681
937
|
return { skip: 0, shouldStopLine: false };
|
|
682
938
|
}
|
|
683
939
|
|
|
684
|
-
|
|
940
|
+
/**
|
|
941
|
+
* Builds a scan step that skips the second character of a two-character token and keeps scanning.
|
|
942
|
+
*
|
|
943
|
+
* @returns `ScanStep` with `skip: 1` and `shouldStopLine: false`.
|
|
944
|
+
*/
|
|
685
945
|
function skipNextCharacter(): ScanStep {
|
|
686
946
|
return { skip: 1, shouldStopLine: false };
|
|
687
947
|
}
|
|
688
948
|
|
|
689
|
-
|
|
949
|
+
/**
|
|
950
|
+
* Builds a scan step that ends the current line scan at a line comment so remaining text contributes no delimiters.
|
|
951
|
+
*
|
|
952
|
+
* @returns `ScanStep` with `skip: 0` and `shouldStopLine: true`.
|
|
953
|
+
*/
|
|
690
954
|
function stopLineScan(): ScanStep {
|
|
691
955
|
return { skip: 0, shouldStopLine: true };
|
|
692
956
|
}
|
|
693
957
|
|
|
694
|
-
|
|
958
|
+
/**
|
|
959
|
+
* Checks whether `character` can delimit a single-quoted string, double-quoted string, or template literal.
|
|
960
|
+
*
|
|
961
|
+
* @param character - Single character to check against `"`, `'`, and `` ` ``.
|
|
962
|
+
* @returns True when `character` is one of the three JS/TS quote delimiters; otherwise false.
|
|
963
|
+
*/
|
|
695
964
|
function isQuote(character: string): boolean {
|
|
696
965
|
return character === "\"" || character === "'" || character === "`";
|
|
697
966
|
}
|
|
698
967
|
|
|
699
|
-
|
|
700
|
-
|
|
968
|
+
/**
|
|
969
|
+
* Checks whether `character` is non-empty and not horizontal or vertical ASCII whitespace.
|
|
970
|
+
* Used by `maskPlainCodeCharacter` so whitespace must not overwrite `previousCode` for the regex heuristic.
|
|
971
|
+
*
|
|
972
|
+
* @param character - Character inspected before updating `previousCode`.
|
|
973
|
+
* @returns True if `character` is non-empty and not space, tab, CR, or LF; otherwise false.
|
|
974
|
+
*/
|
|
701
975
|
function isNonWhitespaceCharacter(character: string): boolean {
|
|
702
976
|
return character !== "" && character !== " " && character !== "\t" && character !== "\r" && character !== "\n";
|
|
703
977
|
}
|