@code-yeongyu/senpi-codemode 2026.11.25 → 2026.11.27
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 +31 -0
- package/README.md +87 -0
- package/package.json +5 -5
- package/src/bridge/reserved.ts +2 -0
- package/src/config/feature-settings.ts +19 -0
- package/src/config/settings.ts +8 -0
- package/src/extension/eval-status-ticker.ts +5 -2
- package/src/extension/runtime-factory.ts +31 -5
- package/src/extension/session-manager.ts +4 -1
- package/src/index.ts +14 -6
- package/src/kernels/js/babel-ast.ts +64 -0
- package/src/kernels/js/binding-patterns.js +166 -0
- package/src/kernels/js/cell-run-context.js +8 -0
- package/src/kernels/js/declaration-extent.js +157 -0
- package/src/kernels/js/erase-types.ts +174 -0
- package/src/kernels/js/js-source-scan.js +181 -0
- package/src/kernels/js/kernel-contract.ts +2 -0
- package/src/kernels/js/last-expression.js +207 -0
- package/src/kernels/js/local-module-loader.ts +7 -2
- package/src/kernels/js/persist-declarations.js +233 -0
- package/src/kernels/js/rewrite-imports.ts +3 -60
- package/src/kernels/js/typescript-cell.ts +39 -0
- package/src/kernels/js/worker-core.js +1 -0
- package/src/kernels/js/worker-indirect-eval.js +4 -860
- package/src/kernels/js/worker-runtime.js +2 -0
- package/src/kernels/js/worker-scope.js +71 -0
- package/src/kernels/js/worker-startup.ts +1 -0
- package/src/prompt/eval-prompt-template.ts +4 -2
- package/src/prompt/eval-prompt.ts +4 -0
- package/src/skill/bun/SKILL.md +1 -0
- package/src/tool/cell-handler.ts +17 -1
- package/src/tool/detached-cell-manager.ts +42 -4
- package/src/tool/detached-notification-queue.ts +48 -7
- package/src/tool/detached-settling.ts +26 -0
- package/src/tool/eval-tool-options.ts +5 -1
- package/src/tool/eval-tool.ts +3 -1
- package/src/tool/js-cell-parse-message.ts +11 -3
- package/src/tool/managed-cell.ts +3 -0
- package/src/tool/run-eval-cell.ts +36 -11
- package/src/tool/scoped-watches.ts +53 -0
- package/src/tool/types.ts +14 -0
- package/src/written-files/model-writes.ts +55 -0
- package/src/written-files/ownership.ts +73 -0
- package/src/written-files/report.ts +63 -0
- package/src/written-files/scan-boundaries.ts +164 -0
- package/src/written-files/source-file.ts +31 -0
- package/src/written-files/tracker.ts +361 -0
- package/src/written-files/window.ts +92 -0
- package/src/written-files/workspace-fs.ts +104 -0
- package/src/written-files/workspace-scan.ts +253 -0
- package/src/written-files/write-hooks.ts +155 -0
- package/src/written-files/written-files.ts +155 -0
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { captureLastExpression, hasTopLevelReturn } from "./last-expression.js";
|
|
2
|
+
import { persistTopLevelDeclarations, USER_SCOPE } from "./persist-declarations.js";
|
|
2
3
|
|
|
3
4
|
// A cell may declare a name the kernel or the platform already defines (`const log = []`, `const fetch = ...`). The
|
|
4
5
|
// binding lives in a per-kernel user scope that later cells read through `with`, so it persists for the user's cells
|
|
5
6
|
// while the kernel, its prelude and imported libraries keep the original global. `delete <name>` in a cell restores it.
|
|
6
7
|
const USER_SCOPE_KEY = Symbol.for("senpi.kernel.userScope");
|
|
7
|
-
const USER_SCOPE = "__senpiUserScope";
|
|
8
|
-
// The persistence rewrite itself writes through `globalThis`, so that one name cannot be shadowed.
|
|
9
|
-
const UNSHADOWABLE = new Set(["globalThis"]);
|
|
10
|
-
let shadowedNames = null;
|
|
11
8
|
|
|
12
9
|
export function indirectEval(source, filename) {
|
|
13
10
|
const withPragma = filename ? `${source}\n//# sourceURL=${filename}` : source;
|
|
@@ -23,863 +20,10 @@ export async function awaitMaybePromise(value) {
|
|
|
23
20
|
|
|
24
21
|
/** Wraps a cell for persistent evaluation; `shadowed` receives the kernel-global names the cell declares. */
|
|
25
22
|
export function wrapUserCode(code, shadowed = []) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
persistentCode = persistTopLevelDeclarations(code);
|
|
30
|
-
} finally {
|
|
31
|
-
shadowedNames = null;
|
|
32
|
-
}
|
|
33
|
-
const body = scanTopLevelStatements(persistentCode).hasTopLevelReturn ? persistentCode : captureLastExpression(persistentCode);
|
|
23
|
+
const persistentCode = persistTopLevelDeclarations(code, shadowed);
|
|
24
|
+
const body = hasTopLevelReturn(persistentCode) ? persistentCode : captureLastExpression(persistentCode);
|
|
34
25
|
const scope = globalThis[USER_SCOPE_KEY];
|
|
35
26
|
if (shadowed.length === 0 && (scope === undefined || Object.keys(scope).length === 0)) return `(async () => {\n${body}\n})()`;
|
|
36
27
|
globalThis[USER_SCOPE_KEY] ??= Object.create(null);
|
|
37
28
|
return `(async (${USER_SCOPE}) => { with (${USER_SCOPE}) { return await (async () => {\n${body}\n})(); } })(globalThis[Symbol.for("senpi.kernel.userScope")])`;
|
|
38
29
|
}
|
|
39
|
-
|
|
40
|
-
function persistTarget(name) {
|
|
41
|
-
if (UNSHADOWABLE.has(name)) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`eval cell declares top-level \`${name}\`, which the JS kernel needs to keep cell variables between cells. Rename the binding (for example \`${name}Local\`).`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
if (!isKernelGlobal(name)) return `globalThis[${JSON.stringify(name)}]`;
|
|
47
|
-
if (shadowedNames !== null && !shadowedNames.includes(name)) shadowedNames.push(name);
|
|
48
|
-
return `${USER_SCOPE}[${JSON.stringify(name)}]`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const IDENTIFIER_START_RE = /[$_\p{ID_Start}]/u;
|
|
52
|
-
const IDENTIFIER_CONTINUE_RE = /[$_\p{ID_Continue}\u200c\u200d]/u;
|
|
53
|
-
const IDENTIFIER_RE = /^[\p{ID_Start}$_][\p{ID_Continue}\u200c\u200d]*$/u;
|
|
54
|
-
const DECLARATION_KEYWORDS = new Set(["const", "let", "var"]);
|
|
55
|
-
const REGEX_PREFIX_KEYWORDS = new Set([
|
|
56
|
-
"await",
|
|
57
|
-
"case",
|
|
58
|
-
"delete",
|
|
59
|
-
"do",
|
|
60
|
-
"else",
|
|
61
|
-
"in",
|
|
62
|
-
"instanceof",
|
|
63
|
-
"new",
|
|
64
|
-
"of",
|
|
65
|
-
"return",
|
|
66
|
-
"throw",
|
|
67
|
-
"typeof",
|
|
68
|
-
"void",
|
|
69
|
-
"yield",
|
|
70
|
-
]);
|
|
71
|
-
const EXPRESSION_PREFIX_KEYWORDS = new Set(["await", "delete", "new", "throw", "typeof", "void", "yield"]);
|
|
72
|
-
|
|
73
|
-
function persistTopLevelDeclarations(code) {
|
|
74
|
-
const edits = [];
|
|
75
|
-
let round = 0;
|
|
76
|
-
let square = 0;
|
|
77
|
-
let curly = 0;
|
|
78
|
-
let statementStart = true;
|
|
79
|
-
let canStartRegex = true;
|
|
80
|
-
|
|
81
|
-
for (let index = 0; index < code.length; index += 1) {
|
|
82
|
-
const char = code[index];
|
|
83
|
-
const next = code[index + 1];
|
|
84
|
-
if (char === "/" && next === "/") {
|
|
85
|
-
index = skipLineComment(code, index) - 1;
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (char === "/" && next === "*") {
|
|
89
|
-
index = skipBlockComment(code, index) - 1;
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (char === "'" || char === '"') {
|
|
93
|
-
index = skipQuotedLiteral(code, index) - 1;
|
|
94
|
-
statementStart = false;
|
|
95
|
-
canStartRegex = false;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (char === "`") {
|
|
99
|
-
index = skipTemplateLiteral(code, index) - 1;
|
|
100
|
-
statementStart = false;
|
|
101
|
-
canStartRegex = false;
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
if (char === "/" && canStartRegex) {
|
|
105
|
-
index = skipRegexLiteral(code, index) - 1;
|
|
106
|
-
statementStart = false;
|
|
107
|
-
canStartRegex = false;
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
if (isIdentifierStart(char)) {
|
|
111
|
-
const end = readIdentifier(code, index);
|
|
112
|
-
const token = code.slice(index, end);
|
|
113
|
-
if (round === 0 && square === 0 && curly === 0 && statementStart && DECLARATION_KEYWORDS.has(token)) {
|
|
114
|
-
const declarationEnd = findDeclarationEnd(code, end);
|
|
115
|
-
const replacement = rewriteDeclaration(code, index, end, declarationEnd, token);
|
|
116
|
-
if (replacement !== undefined) edits.push({ start: index, end: declarationEnd, text: replacement });
|
|
117
|
-
}
|
|
118
|
-
statementStart = false;
|
|
119
|
-
canStartRegex = REGEX_PREFIX_KEYWORDS.has(token);
|
|
120
|
-
index = end - 1;
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
if (isDecimalDigit(char)) {
|
|
124
|
-
index = skipNumberLiteral(code, index) - 1;
|
|
125
|
-
statementStart = false;
|
|
126
|
-
canStartRegex = false;
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
if (char === "(") {
|
|
130
|
-
round += 1;
|
|
131
|
-
statementStart = false;
|
|
132
|
-
canStartRegex = true;
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (char === ")") {
|
|
136
|
-
round = Math.max(0, round - 1);
|
|
137
|
-
statementStart = false;
|
|
138
|
-
canStartRegex = false;
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (char === "[") {
|
|
142
|
-
square += 1;
|
|
143
|
-
statementStart = false;
|
|
144
|
-
canStartRegex = true;
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
if (char === "]") {
|
|
148
|
-
square = Math.max(0, square - 1);
|
|
149
|
-
statementStart = false;
|
|
150
|
-
canStartRegex = false;
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
if (char === "{") {
|
|
154
|
-
curly += 1;
|
|
155
|
-
statementStart = false;
|
|
156
|
-
canStartRegex = true;
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
if (char === "}") {
|
|
160
|
-
curly = Math.max(0, curly - 1);
|
|
161
|
-
statementStart = curly === 0 && round === 0 && square === 0;
|
|
162
|
-
canStartRegex = false;
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (char === ";") {
|
|
166
|
-
statementStart = round === 0 && square === 0 && curly === 0;
|
|
167
|
-
canStartRegex = true;
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (isLineTerminator(char) && round === 0 && square === 0 && curly === 0) {
|
|
171
|
-
statementStart = true;
|
|
172
|
-
canStartRegex = true;
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
if (!/\s/u.test(char)) {
|
|
176
|
-
statementStart = false;
|
|
177
|
-
canStartRegex = isExpressionOperator(char);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return applyTextEdits(code, edits);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function findDeclarationEnd(code, start) {
|
|
185
|
-
let round = 0;
|
|
186
|
-
let square = 0;
|
|
187
|
-
let curly = 0;
|
|
188
|
-
let canEnd = false;
|
|
189
|
-
let canStartRegex = true;
|
|
190
|
-
|
|
191
|
-
for (let index = start; index < code.length; index += 1) {
|
|
192
|
-
const char = code[index];
|
|
193
|
-
const next = code[index + 1];
|
|
194
|
-
if (char === "/" && next === "/") {
|
|
195
|
-
index = skipLineComment(code, index) - 1;
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
if (char === "/" && next === "*") {
|
|
199
|
-
index = skipBlockComment(code, index) - 1;
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
if (char === "'" || char === '"') {
|
|
203
|
-
index = skipQuotedLiteral(code, index) - 1;
|
|
204
|
-
canEnd = true;
|
|
205
|
-
canStartRegex = false;
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
if (char === "`") {
|
|
209
|
-
index = skipTemplateLiteral(code, index) - 1;
|
|
210
|
-
canEnd = true;
|
|
211
|
-
canStartRegex = false;
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (char === "/" && canStartRegex) {
|
|
215
|
-
index = skipRegexLiteral(code, index) - 1;
|
|
216
|
-
canEnd = true;
|
|
217
|
-
canStartRegex = false;
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
if (isIdentifierStart(char)) {
|
|
221
|
-
const end = readIdentifier(code, index);
|
|
222
|
-
const token = code.slice(index, end);
|
|
223
|
-
canEnd = !EXPRESSION_PREFIX_KEYWORDS.has(token);
|
|
224
|
-
canStartRegex = REGEX_PREFIX_KEYWORDS.has(token);
|
|
225
|
-
index = end - 1;
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
if (isDecimalDigit(char)) {
|
|
229
|
-
index = skipNumberLiteral(code, index) - 1;
|
|
230
|
-
canEnd = true;
|
|
231
|
-
canStartRegex = false;
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
if (char === "(") {
|
|
235
|
-
round += 1;
|
|
236
|
-
canEnd = false;
|
|
237
|
-
canStartRegex = true;
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
if (char === ")") {
|
|
241
|
-
round = Math.max(0, round - 1);
|
|
242
|
-
canEnd = true;
|
|
243
|
-
canStartRegex = false;
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
if (char === "[") {
|
|
247
|
-
square += 1;
|
|
248
|
-
canEnd = false;
|
|
249
|
-
canStartRegex = true;
|
|
250
|
-
continue;
|
|
251
|
-
}
|
|
252
|
-
if (char === "]") {
|
|
253
|
-
square = Math.max(0, square - 1);
|
|
254
|
-
canEnd = true;
|
|
255
|
-
canStartRegex = false;
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
if (char === "{") {
|
|
259
|
-
curly += 1;
|
|
260
|
-
canEnd = false;
|
|
261
|
-
canStartRegex = true;
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (char === "}") {
|
|
265
|
-
curly = Math.max(0, curly - 1);
|
|
266
|
-
canEnd = true;
|
|
267
|
-
canStartRegex = false;
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
if (char === ";" && round === 0 && square === 0 && curly === 0) return index + 1;
|
|
271
|
-
if (char === "," && round === 0 && square === 0 && curly === 0) {
|
|
272
|
-
canEnd = false;
|
|
273
|
-
canStartRegex = true;
|
|
274
|
-
continue;
|
|
275
|
-
}
|
|
276
|
-
if (isLineTerminator(char) && round === 0 && square === 0 && curly === 0 && canEnd) {
|
|
277
|
-
const nextIndex = nextSignificantIndex(code, index + 1);
|
|
278
|
-
if (nextIndex >= code.length || !isDeclarationContinuation(code[nextIndex])) return index;
|
|
279
|
-
}
|
|
280
|
-
if (!/\s/u.test(char)) {
|
|
281
|
-
canEnd = false;
|
|
282
|
-
canStartRegex = isExpressionOperator(char);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return code.length;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function rewriteDeclaration(code, declarationStart, start, end, keyword) {
|
|
289
|
-
const source = code.slice(declarationStart, end);
|
|
290
|
-
const assignments = [];
|
|
291
|
-
const preserveDeclaration = source.includes("//") || source.includes("/*");
|
|
292
|
-
for (const [segmentStart, segmentEnd] of splitDeclarators(code, start, end)) {
|
|
293
|
-
const segment = code.slice(segmentStart, segmentEnd);
|
|
294
|
-
if (!segment.trim()) return undefined;
|
|
295
|
-
const initializerStart = findTopLevelEquals(segment);
|
|
296
|
-
if (initializerStart < 0 && keyword === "const") return undefined;
|
|
297
|
-
const pattern = trimPattern(initializerStart < 0 ? segment : segment.slice(0, initializerStart));
|
|
298
|
-
const bindings = [];
|
|
299
|
-
collectPatternNames(pattern, bindings);
|
|
300
|
-
if (bindings.length === 0) return undefined;
|
|
301
|
-
if (preserveDeclaration) {
|
|
302
|
-
for (const name of bindings) assignments.push(`${persistTarget(name)} = ${name};`);
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
const target = rewriteBindingPattern(pattern);
|
|
306
|
-
if (target === undefined) return undefined;
|
|
307
|
-
const initializer = initializerStart < 0 ? "undefined" : segment.slice(initializerStart + 1).trim();
|
|
308
|
-
assignments.push(
|
|
309
|
-
`${target.startsWith("{") || target.startsWith("[") ? `;(${target} = ${initializer})` : `${target} = ${initializer}`};`,
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
if (assignments.length === 0) return undefined;
|
|
313
|
-
return preserveDeclaration ? `${source}\n${assignments.join("\n")}` : assignments.join("\n");
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function rewriteBindingPattern(source) {
|
|
317
|
-
const pattern = trimPattern(source);
|
|
318
|
-
if (!pattern) return undefined;
|
|
319
|
-
const defaultStart = findTopLevelEquals(pattern);
|
|
320
|
-
if (defaultStart >= 0) {
|
|
321
|
-
const target = rewriteBindingPattern(pattern.slice(0, defaultStart));
|
|
322
|
-
return target === undefined ? undefined : `${target} = ${pattern.slice(defaultStart + 1).trim()}`;
|
|
323
|
-
}
|
|
324
|
-
if (IDENTIFIER_RE.test(pattern)) return persistTarget(pattern);
|
|
325
|
-
if (pattern.startsWith("{") && pattern.endsWith("}")) {
|
|
326
|
-
const properties = splitPatternElements(pattern.slice(1, -1)).map((property) => rewriteObjectBinding(property));
|
|
327
|
-
if (properties.some((property) => property === undefined)) return undefined;
|
|
328
|
-
return `{${properties.filter((property) => property !== undefined).join(", ")}}`;
|
|
329
|
-
}
|
|
330
|
-
if (pattern.startsWith("[") && pattern.endsWith("]")) {
|
|
331
|
-
const elements = splitPatternElements(pattern.slice(1, -1)).map((element) => {
|
|
332
|
-
if (!element.trim()) return "";
|
|
333
|
-
const rest = trimPattern(element).startsWith("...");
|
|
334
|
-
const target = rewriteBindingPattern(rest ? trimPattern(element).slice(3) : element);
|
|
335
|
-
return target === undefined ? undefined : rest ? `...${target}` : target;
|
|
336
|
-
});
|
|
337
|
-
if (elements.some((element) => element === undefined)) return undefined;
|
|
338
|
-
return `[${elements.join(", ")}]`;
|
|
339
|
-
}
|
|
340
|
-
return undefined;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function rewriteObjectBinding(source) {
|
|
344
|
-
const property = trimPattern(source);
|
|
345
|
-
if (!property) return "";
|
|
346
|
-
if (property.startsWith("...")) {
|
|
347
|
-
const target = rewriteBindingPattern(property.slice(3));
|
|
348
|
-
return target === undefined ? undefined : `...${target}`;
|
|
349
|
-
}
|
|
350
|
-
const initializerStart = findTopLevelEquals(property);
|
|
351
|
-
const colon = findTopLevelColon(property);
|
|
352
|
-
if (colon >= 0 && (initializerStart < 0 || colon < initializerStart)) {
|
|
353
|
-
const target = rewriteBindingPattern(property.slice(colon + 1));
|
|
354
|
-
return target === undefined ? undefined : `${property.slice(0, colon + 1).trim()} ${target}`;
|
|
355
|
-
}
|
|
356
|
-
if (initializerStart >= 0) {
|
|
357
|
-
const target = rewriteBindingPattern(property.slice(0, initializerStart));
|
|
358
|
-
return target === undefined ? undefined : `${property.slice(0, initializerStart).trim()}: ${target} = ${property.slice(initializerStart + 1).trim()}`;
|
|
359
|
-
}
|
|
360
|
-
const target = rewriteBindingPattern(property);
|
|
361
|
-
return target === undefined ? undefined : `${property}: ${target}`;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
function collectPatternNames(source, bindings) {
|
|
365
|
-
const pattern = trimPattern(source);
|
|
366
|
-
if (!pattern) return;
|
|
367
|
-
const defaultStart = findTopLevelEquals(pattern);
|
|
368
|
-
if (defaultStart >= 0) {
|
|
369
|
-
collectPatternNames(pattern.slice(0, defaultStart), bindings);
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
if (pattern.startsWith("{") && pattern.endsWith("}")) {
|
|
373
|
-
for (const property of splitPatternElements(pattern.slice(1, -1))) {
|
|
374
|
-
const trimmed = trimPattern(property);
|
|
375
|
-
if (!trimmed) continue;
|
|
376
|
-
if (trimmed.startsWith("...")) {
|
|
377
|
-
collectPatternNames(trimmed.slice(3), bindings);
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
const equals = findTopLevelEquals(trimmed);
|
|
381
|
-
const colon = findTopLevelColon(trimmed);
|
|
382
|
-
if (colon >= 0 && (equals < 0 || colon < equals)) collectPatternNames(trimmed.slice(colon + 1), bindings);
|
|
383
|
-
else collectPatternNames(equals < 0 ? trimmed : trimmed.slice(0, equals), bindings);
|
|
384
|
-
}
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
if (pattern.startsWith("[") && pattern.endsWith("]")) {
|
|
388
|
-
for (const element of splitPatternElements(pattern.slice(1, -1))) {
|
|
389
|
-
const trimmed = trimPattern(element);
|
|
390
|
-
if (!trimmed) continue;
|
|
391
|
-
collectPatternNames(trimmed.startsWith("...") ? trimmed.slice(3) : trimmed, bindings);
|
|
392
|
-
}
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
const equals = findTopLevelEquals(pattern);
|
|
396
|
-
const name = trimPattern(equals < 0 ? pattern : pattern.slice(0, equals));
|
|
397
|
-
if (IDENTIFIER_RE.test(name)) bindings.push(name);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
function splitDeclarators(code, start, end) {
|
|
401
|
-
const ranges = [];
|
|
402
|
-
let segmentStart = start;
|
|
403
|
-
let round = 0;
|
|
404
|
-
let square = 0;
|
|
405
|
-
let curly = 0;
|
|
406
|
-
let canStartRegex = true;
|
|
407
|
-
for (let index = start; index < end; index += 1) {
|
|
408
|
-
const char = code[index];
|
|
409
|
-
const next = code[index + 1];
|
|
410
|
-
if (char === "/" && next === "/") {
|
|
411
|
-
index = Math.min(end, skipLineComment(code, index)) - 1;
|
|
412
|
-
continue;
|
|
413
|
-
}
|
|
414
|
-
if (char === "/" && next === "*") {
|
|
415
|
-
index = Math.min(end, skipBlockComment(code, index)) - 1;
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
|
-
if (char === "'" || char === '"') {
|
|
419
|
-
index = Math.min(end, skipQuotedLiteral(code, index)) - 1;
|
|
420
|
-
canStartRegex = false;
|
|
421
|
-
continue;
|
|
422
|
-
}
|
|
423
|
-
if (char === "`") {
|
|
424
|
-
index = Math.min(end, skipTemplateLiteral(code, index)) - 1;
|
|
425
|
-
canStartRegex = false;
|
|
426
|
-
continue;
|
|
427
|
-
}
|
|
428
|
-
if (char === "/" && canStartRegex) {
|
|
429
|
-
index = Math.min(end, skipRegexLiteral(code, index)) - 1;
|
|
430
|
-
canStartRegex = false;
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
if (char === "(") round += 1;
|
|
434
|
-
else if (char === ")") round = Math.max(0, round - 1);
|
|
435
|
-
else if (char === "[") square += 1;
|
|
436
|
-
else if (char === "]") square = Math.max(0, square - 1);
|
|
437
|
-
else if (char === "{") curly += 1;
|
|
438
|
-
else if (char === "}") curly = Math.max(0, curly - 1);
|
|
439
|
-
else if (char === "," && round === 0 && square === 0 && curly === 0) {
|
|
440
|
-
ranges.push([segmentStart, index]);
|
|
441
|
-
segmentStart = index + 1;
|
|
442
|
-
}
|
|
443
|
-
if (!/\s/u.test(char)) canStartRegex = isExpressionOperator(char) || char === "(" || char === "[" || char === "{";
|
|
444
|
-
}
|
|
445
|
-
if (segmentStart < end && code.slice(segmentStart, end).trim() !== ";") ranges.push([segmentStart, end - (code[end - 1] === ";" ? 1 : 0)]);
|
|
446
|
-
else if (ranges.length > 0 && code.slice(segmentStart, end).trim() !== ";") ranges.push([end, end]);
|
|
447
|
-
return ranges;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
function splitPatternElements(source) {
|
|
451
|
-
const elements = [];
|
|
452
|
-
let start = 0;
|
|
453
|
-
let round = 0;
|
|
454
|
-
let square = 0;
|
|
455
|
-
let curly = 0;
|
|
456
|
-
for (let index = 0; index < source.length; index += 1) {
|
|
457
|
-
const char = source[index];
|
|
458
|
-
const next = source[index + 1];
|
|
459
|
-
if (char === "/" && next === "/") {
|
|
460
|
-
index = skipLineComment(source, index) - 1;
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
if (char === "/" && next === "*") {
|
|
464
|
-
index = skipBlockComment(source, index) - 1;
|
|
465
|
-
continue;
|
|
466
|
-
}
|
|
467
|
-
if (char === "'" || char === '"') {
|
|
468
|
-
index = skipQuotedLiteral(source, index) - 1;
|
|
469
|
-
continue;
|
|
470
|
-
}
|
|
471
|
-
if (char === "`") {
|
|
472
|
-
index = skipTemplateLiteral(source, index) - 1;
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
if (char === "(") round += 1;
|
|
476
|
-
else if (char === ")") round = Math.max(0, round - 1);
|
|
477
|
-
else if (char === "[") square += 1;
|
|
478
|
-
else if (char === "]") square = Math.max(0, square - 1);
|
|
479
|
-
else if (char === "{") curly += 1;
|
|
480
|
-
else if (char === "}") curly = Math.max(0, curly - 1);
|
|
481
|
-
else if (char === "," && round === 0 && square === 0 && curly === 0) {
|
|
482
|
-
elements.push(source.slice(start, index));
|
|
483
|
-
start = index + 1;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
elements.push(source.slice(start));
|
|
487
|
-
return elements;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
function findTopLevelEquals(source) {
|
|
491
|
-
return findTopLevelCharacter(source, "=");
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
function findTopLevelColon(source) {
|
|
495
|
-
return findTopLevelCharacter(source, ":");
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function findTopLevelCharacter(source, target) {
|
|
499
|
-
let round = 0;
|
|
500
|
-
let square = 0;
|
|
501
|
-
let curly = 0;
|
|
502
|
-
for (let index = 0; index < source.length; index += 1) {
|
|
503
|
-
const char = source[index];
|
|
504
|
-
const next = source[index + 1];
|
|
505
|
-
if (char === "/" && next === "/") {
|
|
506
|
-
index = skipLineComment(source, index) - 1;
|
|
507
|
-
continue;
|
|
508
|
-
}
|
|
509
|
-
if (char === "/" && next === "*") {
|
|
510
|
-
index = skipBlockComment(source, index) - 1;
|
|
511
|
-
continue;
|
|
512
|
-
}
|
|
513
|
-
if (char === "'" || char === '"') {
|
|
514
|
-
index = skipQuotedLiteral(source, index) - 1;
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
if (char === "`") {
|
|
518
|
-
index = skipTemplateLiteral(source, index) - 1;
|
|
519
|
-
continue;
|
|
520
|
-
}
|
|
521
|
-
if (char === "(") round += 1;
|
|
522
|
-
else if (char === ")") round = Math.max(0, round - 1);
|
|
523
|
-
else if (char === "[") square += 1;
|
|
524
|
-
else if (char === "]") square = Math.max(0, square - 1);
|
|
525
|
-
else if (char === "{") curly += 1;
|
|
526
|
-
else if (char === "}") curly = Math.max(0, curly - 1);
|
|
527
|
-
else if (char === target && round === 0 && square === 0 && curly === 0) return index;
|
|
528
|
-
}
|
|
529
|
-
return -1;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function applyTextEdits(code, edits) {
|
|
533
|
-
let output = code;
|
|
534
|
-
for (const edit of edits.toSorted((left, right) => right.start - left.start)) {
|
|
535
|
-
output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);
|
|
536
|
-
}
|
|
537
|
-
return output;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function trimPattern(source) {
|
|
541
|
-
let start = 0;
|
|
542
|
-
while (start < source.length) {
|
|
543
|
-
if (/\s/u.test(source[start])) {
|
|
544
|
-
start += 1;
|
|
545
|
-
continue;
|
|
546
|
-
}
|
|
547
|
-
if (source.startsWith("//", start)) {
|
|
548
|
-
start = skipLineComment(source, start);
|
|
549
|
-
continue;
|
|
550
|
-
}
|
|
551
|
-
if (source.startsWith("/*", start)) {
|
|
552
|
-
start = skipBlockComment(source, start);
|
|
553
|
-
continue;
|
|
554
|
-
}
|
|
555
|
-
break;
|
|
556
|
-
}
|
|
557
|
-
let end = start;
|
|
558
|
-
for (let index = start; index < source.length; index += 1) {
|
|
559
|
-
const char = source[index];
|
|
560
|
-
const next = source[index + 1];
|
|
561
|
-
if (/\s/u.test(char)) continue;
|
|
562
|
-
if (char === "/" && next === "/") {
|
|
563
|
-
index = skipLineComment(source, index) - 1;
|
|
564
|
-
continue;
|
|
565
|
-
}
|
|
566
|
-
if (char === "/" && next === "*") {
|
|
567
|
-
index = skipBlockComment(source, index) - 1;
|
|
568
|
-
continue;
|
|
569
|
-
}
|
|
570
|
-
if (char === "'" || char === '"') {
|
|
571
|
-
index = skipQuotedLiteral(source, index) - 1;
|
|
572
|
-
end = index + 1;
|
|
573
|
-
continue;
|
|
574
|
-
}
|
|
575
|
-
if (char === "`") {
|
|
576
|
-
index = skipTemplateLiteral(source, index) - 1;
|
|
577
|
-
end = index + 1;
|
|
578
|
-
continue;
|
|
579
|
-
}
|
|
580
|
-
end = index + 1;
|
|
581
|
-
}
|
|
582
|
-
return source.slice(start, end);
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function nextSignificantIndex(code, start) {
|
|
586
|
-
for (let index = start; index < code.length; index += 1) {
|
|
587
|
-
if (/\s/u.test(code[index])) continue;
|
|
588
|
-
if (code.startsWith("//", index)) {
|
|
589
|
-
index = skipLineComment(code, index) - 1;
|
|
590
|
-
continue;
|
|
591
|
-
}
|
|
592
|
-
if (code.startsWith("/*", index)) {
|
|
593
|
-
index = skipBlockComment(code, index) - 1;
|
|
594
|
-
continue;
|
|
595
|
-
}
|
|
596
|
-
return index;
|
|
597
|
-
}
|
|
598
|
-
return code.length;
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
function isDeclarationContinuation(char) {
|
|
602
|
-
return char !== undefined && /[.[(,+\-*/%&|^?:<>=!~]/u.test(char);
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
function isExpressionOperator(char) {
|
|
606
|
-
return /[=,+\-*/%&|^?:<>!~]/u.test(char);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
function isIdentifierStart(char) {
|
|
610
|
-
return char !== undefined && IDENTIFIER_START_RE.test(char);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
function readIdentifier(code, start) {
|
|
614
|
-
let index = start + 1;
|
|
615
|
-
while (index < code.length && IDENTIFIER_CONTINUE_RE.test(code[index])) index += 1;
|
|
616
|
-
return index;
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
function isDecimalDigit(char) {
|
|
620
|
-
return char !== undefined && /[0-9]/u.test(char);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
function skipNumberLiteral(code, start) {
|
|
624
|
-
let index = start + 1;
|
|
625
|
-
while (index < code.length && /[0-9A-Fa-f_xXn.eE]/u.test(code[index])) index += 1;
|
|
626
|
-
return index;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
function skipQuotedLiteral(code, start) {
|
|
630
|
-
const quote = code[start];
|
|
631
|
-
for (let index = start + 1; index < code.length; index += 1) {
|
|
632
|
-
if (code[index] === "\\") {
|
|
633
|
-
index += 1;
|
|
634
|
-
continue;
|
|
635
|
-
}
|
|
636
|
-
if (code[index] === quote) return index + 1;
|
|
637
|
-
}
|
|
638
|
-
return code.length;
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
function skipTemplateLiteral(code, start) {
|
|
642
|
-
for (let index = start + 1; index < code.length; index += 1) {
|
|
643
|
-
const char = code[index];
|
|
644
|
-
if (char === "\\") {
|
|
645
|
-
index += 1;
|
|
646
|
-
continue;
|
|
647
|
-
}
|
|
648
|
-
if (char === "`") return index + 1;
|
|
649
|
-
if (char === "$" && code[index + 1] === "{") {
|
|
650
|
-
index = skipTemplateExpression(code, index + 2) - 1;
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
return code.length;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
function skipTemplateExpression(code, start) {
|
|
657
|
-
let curly = 1;
|
|
658
|
-
let canStartRegex = true;
|
|
659
|
-
for (let index = start; index < code.length; index += 1) {
|
|
660
|
-
const char = code[index];
|
|
661
|
-
const next = code[index + 1];
|
|
662
|
-
if (char === "/" && next === "/") {
|
|
663
|
-
index = skipLineComment(code, index) - 1;
|
|
664
|
-
continue;
|
|
665
|
-
}
|
|
666
|
-
if (char === "/" && next === "*") {
|
|
667
|
-
index = skipBlockComment(code, index) - 1;
|
|
668
|
-
continue;
|
|
669
|
-
}
|
|
670
|
-
if (char === "'" || char === '"') {
|
|
671
|
-
index = skipQuotedLiteral(code, index) - 1;
|
|
672
|
-
canStartRegex = false;
|
|
673
|
-
continue;
|
|
674
|
-
}
|
|
675
|
-
if (char === "`") {
|
|
676
|
-
index = skipTemplateLiteral(code, index) - 1;
|
|
677
|
-
canStartRegex = false;
|
|
678
|
-
continue;
|
|
679
|
-
}
|
|
680
|
-
if (char === "/" && canStartRegex) {
|
|
681
|
-
index = skipRegexLiteral(code, index) - 1;
|
|
682
|
-
canStartRegex = false;
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
if (isIdentifierStart(char)) {
|
|
686
|
-
const end = readIdentifier(code, index);
|
|
687
|
-
canStartRegex = REGEX_PREFIX_KEYWORDS.has(code.slice(index, end));
|
|
688
|
-
index = end - 1;
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
if (char === "{") curly += 1;
|
|
692
|
-
else if (char === "}" && --curly === 0) return index + 1;
|
|
693
|
-
canStartRegex = char === "(" || char === "[" || char === "{" || isExpressionOperator(char) || char === ",";
|
|
694
|
-
}
|
|
695
|
-
return code.length;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
function skipRegexLiteral(code, start) {
|
|
699
|
-
let inClass = false;
|
|
700
|
-
for (let index = start + 1; index < code.length; index += 1) {
|
|
701
|
-
const char = code[index];
|
|
702
|
-
if (char === "\\") {
|
|
703
|
-
index += 1;
|
|
704
|
-
continue;
|
|
705
|
-
}
|
|
706
|
-
if (char === "[") {
|
|
707
|
-
inClass = true;
|
|
708
|
-
continue;
|
|
709
|
-
}
|
|
710
|
-
if (char === "]") {
|
|
711
|
-
inClass = false;
|
|
712
|
-
continue;
|
|
713
|
-
}
|
|
714
|
-
if (char === "/" && !inClass) {
|
|
715
|
-
let end = index + 1;
|
|
716
|
-
while (end < code.length && /[A-Za-z]/u.test(code[end])) end += 1;
|
|
717
|
-
return end;
|
|
718
|
-
}
|
|
719
|
-
if (isLineTerminator(char)) return index;
|
|
720
|
-
}
|
|
721
|
-
return code.length;
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
function skipLineComment(code, start) {
|
|
725
|
-
for (let index = start + 2; index < code.length; index += 1) {
|
|
726
|
-
if (isLineTerminator(code[index])) return index;
|
|
727
|
-
}
|
|
728
|
-
return code.length;
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
function isLineTerminator(char) {
|
|
732
|
-
return char === "\n" || char === "\r" || char === "\u2028" || char === "\u2029";
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
function skipBlockComment(code, start) {
|
|
736
|
-
const end = code.indexOf("*/", start + 2);
|
|
737
|
-
return end < 0 ? code.length : end + 2;
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
function captureLastExpression(code) {
|
|
741
|
-
const start = scanTopLevelStatements(code).lastStatementStart;
|
|
742
|
-
const head = code.slice(0, start);
|
|
743
|
-
const tail = code.slice(start).trim();
|
|
744
|
-
if (!tail || isStatementOnly(tail)) return code;
|
|
745
|
-
return `${head}return ${tail.replace(/;+$/u, "")};`;
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
// oxlint-disable-next-line eslint/no-misleading-character-class -- ECMAScript identifiers permit ZWNJ and ZWJ as individual continuation characters.
|
|
749
|
-
const LABEL_PREFIX_RE = /^[\p{ID_Start}$_][\p{ID_Continue}\u200c\u200d$]*\s*:/u;
|
|
750
|
-
|
|
751
|
-
function isStatementOnly(source) {
|
|
752
|
-
if (
|
|
753
|
-
/^(?:const|let|var|if|for|while|switch|try|catch|finally|class|function|import|export|throw|return|do|break|continue|debugger)\b/u.test(
|
|
754
|
-
source,
|
|
755
|
-
)
|
|
756
|
-
)
|
|
757
|
-
return true;
|
|
758
|
-
return LABEL_PREFIX_RE.test(source);
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
const CONTROL_PAREN_KEYWORDS = new Set(["catch", "for", "if", "switch", "while", "with"]);
|
|
762
|
-
|
|
763
|
-
function scanTopLevelStatements(code) {
|
|
764
|
-
let start = 0;
|
|
765
|
-
let round = 0;
|
|
766
|
-
let square = 0;
|
|
767
|
-
let curly = 0;
|
|
768
|
-
let canStartRegex = true;
|
|
769
|
-
let lastSignificant = "";
|
|
770
|
-
let pendingControlParen = false;
|
|
771
|
-
let hasTopLevelReturn = false;
|
|
772
|
-
const controlParens = [];
|
|
773
|
-
for (let index = 0; index < code.length; index += 1) {
|
|
774
|
-
const char = code[index];
|
|
775
|
-
const next = code[index + 1];
|
|
776
|
-
if (char === "/" && next === "/") {
|
|
777
|
-
index = skipLineComment(code, index) - 1;
|
|
778
|
-
continue;
|
|
779
|
-
}
|
|
780
|
-
if (char === "/" && next === "*") {
|
|
781
|
-
index = skipBlockComment(code, index) - 1;
|
|
782
|
-
continue;
|
|
783
|
-
}
|
|
784
|
-
if (char === "'" || char === '"' || char === "`") {
|
|
785
|
-
index = (char === "`" ? skipTemplateLiteral(code, index) : skipQuotedLiteral(code, index)) - 1;
|
|
786
|
-
canStartRegex = false;
|
|
787
|
-
lastSignificant = char;
|
|
788
|
-
pendingControlParen = false;
|
|
789
|
-
continue;
|
|
790
|
-
}
|
|
791
|
-
if (char === "/" && canStartRegex) {
|
|
792
|
-
index = skipRegexLiteral(code, index) - 1;
|
|
793
|
-
canStartRegex = false;
|
|
794
|
-
lastSignificant = char;
|
|
795
|
-
pendingControlParen = false;
|
|
796
|
-
continue;
|
|
797
|
-
}
|
|
798
|
-
if (isIdentifierStart(char)) {
|
|
799
|
-
const end = readIdentifier(code, index);
|
|
800
|
-
const token = code.slice(index, end);
|
|
801
|
-
const isPropertyName = lastSignificant === ".";
|
|
802
|
-
const atTopLevel = round === 0 && square === 0 && curly === 0;
|
|
803
|
-
if (token === "return" && atTopLevel && !isPropertyName) hasTopLevelReturn = true;
|
|
804
|
-
canStartRegex = !isPropertyName && REGEX_PREFIX_KEYWORDS.has(token);
|
|
805
|
-
pendingControlParen = !isPropertyName && CONTROL_PAREN_KEYWORDS.has(token);
|
|
806
|
-
lastSignificant = code[end - 1];
|
|
807
|
-
index = end - 1;
|
|
808
|
-
continue;
|
|
809
|
-
}
|
|
810
|
-
if (isDecimalDigit(char)) {
|
|
811
|
-
index = skipNumberLiteral(code, index) - 1;
|
|
812
|
-
canStartRegex = false;
|
|
813
|
-
lastSignificant = char;
|
|
814
|
-
pendingControlParen = false;
|
|
815
|
-
continue;
|
|
816
|
-
}
|
|
817
|
-
if (char === "(") {
|
|
818
|
-
round += 1;
|
|
819
|
-
controlParens.push(pendingControlParen);
|
|
820
|
-
pendingControlParen = false;
|
|
821
|
-
canStartRegex = true;
|
|
822
|
-
lastSignificant = char;
|
|
823
|
-
continue;
|
|
824
|
-
}
|
|
825
|
-
if (char === ")") {
|
|
826
|
-
round = Math.max(0, round - 1);
|
|
827
|
-
canStartRegex = controlParens.pop() === true;
|
|
828
|
-
lastSignificant = char;
|
|
829
|
-
continue;
|
|
830
|
-
}
|
|
831
|
-
if (char === "[" || char === "{") {
|
|
832
|
-
if (char === "[") square += 1;
|
|
833
|
-
else curly += 1;
|
|
834
|
-
canStartRegex = true;
|
|
835
|
-
lastSignificant = char;
|
|
836
|
-
pendingControlParen = false;
|
|
837
|
-
continue;
|
|
838
|
-
}
|
|
839
|
-
if (char === "]" || char === "}") {
|
|
840
|
-
if (char === "]") square = Math.max(0, square - 1);
|
|
841
|
-
else curly = Math.max(0, curly - 1);
|
|
842
|
-
canStartRegex = false;
|
|
843
|
-
lastSignificant = char;
|
|
844
|
-
pendingControlParen = false;
|
|
845
|
-
continue;
|
|
846
|
-
}
|
|
847
|
-
if (char === ";" && round === 0 && square === 0 && curly === 0) {
|
|
848
|
-
const nextIndex = nextSignificantIndex(code, index + 1);
|
|
849
|
-
if (nextIndex < code.length && !isContinuationKeyword(code, nextIndex)) start = nextIndex;
|
|
850
|
-
canStartRegex = true;
|
|
851
|
-
lastSignificant = char;
|
|
852
|
-
pendingControlParen = false;
|
|
853
|
-
continue;
|
|
854
|
-
}
|
|
855
|
-
if (isLineTerminator(char) && round === 0 && square === 0 && curly === 0) {
|
|
856
|
-
if (!canStartRegex) {
|
|
857
|
-
const nextIndex = nextSignificantIndex(code, index + 1);
|
|
858
|
-
if (nextIndex < code.length && !isStatementContinuation(code, nextIndex, lastSignificant)) start = nextIndex;
|
|
859
|
-
}
|
|
860
|
-
canStartRegex = true;
|
|
861
|
-
continue;
|
|
862
|
-
}
|
|
863
|
-
if (!/\s/u.test(char)) {
|
|
864
|
-
canStartRegex = isExpressionOperator(char) || char === ",";
|
|
865
|
-
lastSignificant = char;
|
|
866
|
-
pendingControlParen = false;
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
return { lastStatementStart: start, hasTopLevelReturn };
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
const STATEMENT_CONTINUATION_KEYWORDS = new Set(["catch", "else", "finally"]);
|
|
873
|
-
|
|
874
|
-
function isContinuationKeyword(code, index) {
|
|
875
|
-
if (!isIdentifierStart(code[index])) return false;
|
|
876
|
-
return STATEMENT_CONTINUATION_KEYWORDS.has(code.slice(index, readIdentifier(code, index)));
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
function isStatementContinuation(code, index, previousChar) {
|
|
880
|
-
const char = code[index];
|
|
881
|
-
if (char === "(" || char === "[" || char === "`") return previousChar !== "}" && previousChar !== "";
|
|
882
|
-
if (char === "!" || char === "~") return false;
|
|
883
|
-
if (char !== undefined && isDeclarationContinuation(char)) return true;
|
|
884
|
-
return isContinuationKeyword(code, index);
|
|
885
|
-
}
|