@elementumai/edk 0.9.11 → 0.9.13
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/README.md +39 -5
- package/dist-sdk/src/catalog/factories.d.ts +35 -14
- package/dist-sdk/src/catalog/factories.js +64 -13
- package/dist-sdk/src/catalog/from-authoring.js +4 -4
- package/dist-sdk/src/catalog/index.d.ts +1 -1
- package/dist-sdk/src/catalog/org.js +3 -2
- package/dist-sdk/src/catalog/token.js +4 -0
- package/dist-sdk/src/catalog/types.d.ts +13 -3
- package/dist-sdk/src/core/triggers/on-demand.d.ts +4 -28
- package/dist-sdk/src/core/triggers/on-demand.js +2 -4
- package/dist-sdk/src/engine/entity.d.ts +2 -0
- package/dist-sdk/src/engine/logging.d.ts +90 -0
- package/dist-sdk/src/engine/logging.js +124 -0
- package/dist-sdk/src/engine/progress.d.ts +122 -0
- package/dist-sdk/src/engine/progress.js +685 -0
- package/dist-sdk/src/engine/pull/result-output.d.ts +24 -0
- package/dist-sdk/src/engine/pull/result-output.js +59 -0
- package/dist-sdk/src/entities/agent/tools.d.ts +2 -2
- package/dist-sdk/src/entities/app/build.d.ts +8 -1
- package/dist-sdk/src/entities/app/from-tf.js +4 -4
- package/dist-sdk/src/entities/app/to-tf.js +9 -13
- package/dist-sdk/src/entities/automation/build.d.ts +11 -16
- package/dist-sdk/src/entities/automation/build.js +60 -16
- package/dist-sdk/src/entities/automation/catalog-render.js +1 -1
- package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +1 -2
- package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +23 -5
- package/dist-sdk/src/entities/automation/deserializer/print-automation.js +247 -35
- package/dist-sdk/src/entities/automation/from-tf.js +181 -24
- package/dist-sdk/src/entities/automation/index.d.ts +2 -2
- package/dist-sdk/src/entities/automation/module.js +2 -2
- package/dist-sdk/src/entities/automation/output-field-types.js +25 -13
- package/dist-sdk/src/entities/automation/script-code.d.ts +12 -4
- package/dist-sdk/src/entities/automation/script-code.js +659 -101
- package/dist-sdk/src/entities/automation/task-label.js +25 -2
- package/dist-sdk/src/entities/automation/tasks.d.ts +60 -28
- package/dist-sdk/src/entities/automation/tasks.js +31 -15
- package/dist-sdk/src/entities/automation/to-tf.js +443 -64
- package/dist-sdk/src/entities/automation/triggers.d.ts +15 -9
- package/dist-sdk/src/entities/automation/triggers.js +4 -2
- package/dist-sdk/src/entities/chart/build.d.ts +15 -3
- package/dist-sdk/src/entities/chart/build.js +6 -1
- package/dist-sdk/src/entities/chart/from-tf.d.ts +8 -2
- package/dist-sdk/src/entities/chart/from-tf.js +71 -8
- package/dist-sdk/src/entities/chart/index.d.ts +1 -1
- package/dist-sdk/src/entities/chart/index.js +1 -1
- package/dist-sdk/src/entities/chart/printer.js +27 -7
- package/dist-sdk/src/entities/chart/to-tf.js +56 -2
- package/dist-sdk/src/entities/datamine/from-tf.js +11 -1
- package/dist-sdk/src/entities/element/build.d.ts +6 -5
- package/dist-sdk/src/entities/element/from-tf.js +3 -2
- package/dist-sdk/src/entities/element/module.js +1 -0
- package/dist-sdk/src/entities/element/to-tf.d.ts +3 -0
- package/dist-sdk/src/entities/element/to-tf.js +9 -1
- package/dist-sdk/src/entities/field/from-tf.d.ts +24 -0
- package/dist-sdk/src/entities/field/from-tf.js +73 -1
- package/dist-sdk/src/entities/field/metadata.d.ts +18 -0
- package/dist-sdk/src/entities/field/metadata.js +34 -0
- package/dist-sdk/src/entities/relationship/build.d.ts +17 -6
- package/dist-sdk/src/entities/relationship/column-contract.d.ts +24 -0
- package/dist-sdk/src/entities/relationship/column-contract.js +41 -0
- package/dist-sdk/src/entities/relationship/from-tf.js +19 -34
- package/dist-sdk/src/entities/relationship/to-tf.js +15 -2
- package/dist-sdk/src/entities/shared/context.d.ts +6 -3
- package/dist-sdk/src/entities/shared/context.js +19 -1
- package/dist-sdk/src/entities/shared/filters/index.d.ts +7 -0
- package/dist-sdk/src/entities/shared/filters/index.js +36 -1
- package/dist-sdk/src/entities/shared/filters/print.js +2 -0
- package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +8 -1
- package/dist-sdk/src/entities/shared/filters/tf-codec.js +10 -6
- package/dist-sdk/src/entities/shared/filters/types.d.ts +2 -1
- package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +5 -0
- package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +36 -23
- package/dist-sdk/src/entities/skill/module.js +1 -0
- package/dist-sdk/src/entities/skill/to-tf.d.ts +1 -1
- package/dist-sdk/src/entities/skill/to-tf.js +17 -0
- package/dist-sdk/src/entities/skill/tools.d.ts +38 -12
- package/dist-sdk/src/entities/skill/tools.js +2 -1
- package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +8 -12
- package/dist-sdk/src/entities/skill-tool/from-tf.js +28 -44
- package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +4 -0
- package/dist-sdk/src/entities/skill-tool/to-tf.js +45 -7
- package/dist-sdk/src/entities/table/build.js +10 -2
- package/dist-sdk/src/entities/view/to-tf.js +5 -7
- package/dist-sdk/src/entities/widget/build.d.ts +26 -0
- package/dist-sdk/src/entities/widget/from-tf.js +30 -5
- package/dist-sdk/src/entities/widget/to-tf.js +111 -7
- package/dist-sdk/src/graph/types.d.ts +5 -0
- package/package.json +1 -1
- package/dist-sdk/src/core/types/platform.d.ts +0 -2
- package/dist-sdk/src/core/types/platform.js +0 -1
|
@@ -1,42 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
* Function-form `code:` for executeScript (authoring v3).
|
|
3
|
-
*
|
|
4
|
-
* The platform's script contract (Hydra AddExecuteScriptActionInternalToolProvider):
|
|
5
|
-
* the code is a top-level script body with `input` in scope — inputs are read
|
|
6
|
-
* via `input.parameters.<name>`, and the script MUST `return` a plain object;
|
|
7
|
-
* downstream refs read `result.<key>`. So an authored
|
|
8
|
-
*
|
|
9
|
-
* code: (input) => { const { X } = input.parameters; return { OUT: X }; }
|
|
10
|
-
*
|
|
11
|
-
* ships as the function's BODY. `code:` is ALWAYS a real function — #125
|
|
12
|
-
* deliberately banned a raw-string alternative so pulled automations stay
|
|
13
|
-
* editable TypeScript, not opaque blobs.
|
|
14
|
-
*
|
|
15
|
-
* Two authoring hazards, handled here:
|
|
16
|
-
* - CLOSURES CANNOT SURVIVE: `const x = 5; code: () => ({v: x})` serializes the
|
|
17
|
-
* reference, not the value — on the platform `x` is undefined. A best-effort
|
|
18
|
-
* free-identifier scan throws at def() time on obvious captures (per the
|
|
19
|
-
* tranche decision: best effort, not a guarantee).
|
|
20
|
-
* - WHAT YOU AUTHOR IS TYPE-STRIPPED FIRST: Node erases type annotations by
|
|
21
|
-
* overwriting them with spaces, so `Function.prototype.toString()` returns
|
|
22
|
-
* the original spacing but blanks wherever a type used to be. For a
|
|
23
|
-
* HAND-authored script that is usually harmless. For a PULLED script it is
|
|
24
|
-
* lossy: the platform's exact text stops matching after one `edk build`,
|
|
25
|
-
* producing a spurious plan diff forever.
|
|
26
|
-
*
|
|
27
|
-
* `elementum build` avoids that loss for pulled scripts WITHOUT relying on
|
|
28
|
-
* `toString()` at all: `src/engine/build/build.ts` wraps each automation
|
|
29
|
-
* file's import with that file's own on-disk text (engine-side,
|
|
30
|
-
* entity-agnostic — `source-context.ts`), and `tryExtractFromCurrentSource`
|
|
31
|
-
* here locates the matching `.executeScript("name", { code: (input) => {...}
|
|
32
|
-
* })` call at any fluent-chain depth by a
|
|
33
|
-
* lightweight source scan — the same string/comment-aware tokenizing already
|
|
34
|
-
* used by {@link inferOutputSchema} — and slices the body's ORIGINAL text directly out
|
|
35
|
-
* of the file, never touching the loaded function. `extractScriptBody`
|
|
36
|
-
* (the `toString()` path) remains the fallback for contexts with no tracked
|
|
37
|
-
* source (tests constructing automations directly).
|
|
38
|
-
*/
|
|
1
|
+
import { createRequire } from "node:module";
|
|
39
2
|
import { currentSourceText } from "../../engine/build/source-context.js";
|
|
3
|
+
let tsModule;
|
|
4
|
+
function getTs() {
|
|
5
|
+
if (tsModule === undefined) {
|
|
6
|
+
try {
|
|
7
|
+
tsModule = createRequire(import.meta.url)("typescript");
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
tsModule = null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return tsModule ?? undefined;
|
|
14
|
+
}
|
|
40
15
|
/** Globals a script body may legitimately reference on the platform runtime. */
|
|
41
16
|
const SCRIPT_GLOBALS = new Set([
|
|
42
17
|
"input",
|
|
@@ -79,6 +54,7 @@ const KEYWORDS = new Set([
|
|
|
79
54
|
"in", "of", "try", "catch", "finally", "throw", "void", "this", "null", "true", "false",
|
|
80
55
|
"async", "await", "yield", "static", "get", "set", "extends", "super",
|
|
81
56
|
]);
|
|
57
|
+
const CONTROL_FLOW_KEYWORDS = new Set(["if", "for", "while", "switch", "catch", "with"]);
|
|
82
58
|
export class ScriptCodeError extends Error {
|
|
83
59
|
constructor(message) {
|
|
84
60
|
super(message);
|
|
@@ -222,6 +198,27 @@ function skipNonCode(src, i) {
|
|
|
222
198
|
}
|
|
223
199
|
return i;
|
|
224
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Keywords that an expression (and thus a regex literal) may legitimately follow.
|
|
203
|
+
* When `/` follows an identifier or keyword, it is division unless the preceding
|
|
204
|
+
* word is one of these keywords (and not a property access like `obj.return`).
|
|
205
|
+
*/
|
|
206
|
+
const REGEX_PRECEDING_KEYWORDS = new Set([
|
|
207
|
+
"return",
|
|
208
|
+
"typeof",
|
|
209
|
+
"case",
|
|
210
|
+
"throw",
|
|
211
|
+
"yield",
|
|
212
|
+
"void",
|
|
213
|
+
"delete",
|
|
214
|
+
"in",
|
|
215
|
+
"of",
|
|
216
|
+
"instanceof",
|
|
217
|
+
"new",
|
|
218
|
+
"do",
|
|
219
|
+
"else",
|
|
220
|
+
"await",
|
|
221
|
+
]);
|
|
225
222
|
/**
|
|
226
223
|
* Characters a `/` may follow and still begin a regex literal rather than a
|
|
227
224
|
* division. After an identifier, number, `)` or `]` — or a closing quote — the
|
|
@@ -230,16 +227,43 @@ function skipNonCode(src, i) {
|
|
|
230
227
|
const REGEX_MAY_START_AFTER = /[({[=,:;!?&|+\-*%^~<>]/;
|
|
231
228
|
/**
|
|
232
229
|
* True when the `/` at `i` opens a regex literal, judged by the nearest
|
|
233
|
-
* preceding significant
|
|
230
|
+
* preceding significant token. Looks BACKWARD so the balancing scans below
|
|
234
231
|
* need not each thread a "previous token" through their branches; it reaches
|
|
235
232
|
* the same verdicts as {@link maskNonCodeForIdentifierScan}'s forward state.
|
|
236
233
|
*/
|
|
237
234
|
function startsRegexLiteralAt(src, i) {
|
|
238
235
|
let k = i - 1;
|
|
239
|
-
while (k >= 0
|
|
240
|
-
|
|
236
|
+
while (k >= 0) {
|
|
237
|
+
const c = src[k];
|
|
238
|
+
if (/\s/.test(c)) {
|
|
239
|
+
k -= 1;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
// Backward block comment: */ ... /*
|
|
243
|
+
if (c === "/" && k > 0 && src[k - 1] === "*") {
|
|
244
|
+
k -= 2;
|
|
245
|
+
while (k > 0 && !(src[k - 1] === "/" && src[k] === "*"))
|
|
246
|
+
k -= 1;
|
|
247
|
+
k -= 2;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
if (k < 0)
|
|
253
|
+
return true;
|
|
241
254
|
const previous = src[k];
|
|
242
|
-
|
|
255
|
+
if (REGEX_MAY_START_AFTER.test(previous))
|
|
256
|
+
return true;
|
|
257
|
+
if (/[A-Za-z0-9_$]/.test(previous)) {
|
|
258
|
+
let start = k;
|
|
259
|
+
while (start >= 0 && /[A-Za-z0-9_$]/.test(src[start]))
|
|
260
|
+
start -= 1;
|
|
261
|
+
if (start >= 0 && src[start] === ".")
|
|
262
|
+
return false;
|
|
263
|
+
const word = typeof src === "string" ? src.slice(start + 1, k + 1) : src.slice(start + 1, k + 1).join("");
|
|
264
|
+
return REGEX_PRECEDING_KEYWORDS.has(word);
|
|
265
|
+
}
|
|
266
|
+
return false;
|
|
243
267
|
}
|
|
244
268
|
/**
|
|
245
269
|
* Skip a regex literal starting at `src[i] === "/"`; returns the index just
|
|
@@ -393,8 +417,44 @@ function classifyValueExpr(expr) {
|
|
|
393
417
|
return "number";
|
|
394
418
|
if (expr === "true" || expr === "false")
|
|
395
419
|
return "boolean";
|
|
420
|
+
if (expr.startsWith("[") && expr.endsWith("]")) {
|
|
421
|
+
const inner = expr.slice(1, -1).trim();
|
|
422
|
+
if (inner === "")
|
|
423
|
+
return { type: "array", items: "text" };
|
|
424
|
+
const itemTypes = splitTopLevelExpressions(inner).map(classifyValueExpr);
|
|
425
|
+
const first = itemTypes[0];
|
|
426
|
+
return {
|
|
427
|
+
type: "array",
|
|
428
|
+
items: itemTypes.every((item) => JSON.stringify(item) === JSON.stringify(first)) ? first : "text",
|
|
429
|
+
};
|
|
430
|
+
}
|
|
396
431
|
return "text";
|
|
397
432
|
}
|
|
433
|
+
/** Split a simple array literal without treating nested arrays/objects as separators. */
|
|
434
|
+
function splitTopLevelExpressions(value) {
|
|
435
|
+
const parts = [];
|
|
436
|
+
let start = 0;
|
|
437
|
+
let depth = 0;
|
|
438
|
+
let i = 0;
|
|
439
|
+
while (i < value.length) {
|
|
440
|
+
const c = value[i];
|
|
441
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
442
|
+
i = skipString(value, i);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (c === "[" || c === "{" || c === "(")
|
|
446
|
+
depth += 1;
|
|
447
|
+
else if (c === "]" || c === "}" || c === ")")
|
|
448
|
+
depth -= 1;
|
|
449
|
+
else if (c === "," && depth === 0) {
|
|
450
|
+
parts.push(value.slice(start, i).trim());
|
|
451
|
+
start = i + 1;
|
|
452
|
+
}
|
|
453
|
+
i += 1;
|
|
454
|
+
}
|
|
455
|
+
parts.push(value.slice(start).trim());
|
|
456
|
+
return parts.filter((part) => part.length > 0);
|
|
457
|
+
}
|
|
398
458
|
/** Strip the common leading indentation (the flush-left fixed point survives ei's heredoc round trip). */
|
|
399
459
|
function dedent(body) {
|
|
400
460
|
const lines = body.split("\n");
|
|
@@ -403,67 +463,346 @@ function dedent(body) {
|
|
|
403
463
|
return lines.map((l) => (l.trim() === "" ? "" : l.slice(min))).join("\n");
|
|
404
464
|
}
|
|
405
465
|
/**
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
466
|
+
* AST-based free-identifier scan using the real TypeScript compiler parser.
|
|
467
|
+
* Resolves grammar constructs natively and enforces strict lexical scoping
|
|
468
|
+
* so that inner parameters/locals do not leak and hide outer closure captures.
|
|
469
|
+
*/
|
|
470
|
+
function freeIdentifiersAST(body, params, ts) {
|
|
471
|
+
const sf = ts.createSourceFile("script.ts", body, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
472
|
+
const free = new Set();
|
|
473
|
+
// Lexical scope stack: root scope is a function-level scope seeded with parameters.
|
|
474
|
+
const scopeStack = [
|
|
475
|
+
{ kind: "function", names: new Set(params) },
|
|
476
|
+
];
|
|
477
|
+
const currentScope = () => scopeStack[scopeStack.length - 1].names;
|
|
478
|
+
const functionScope = () => {
|
|
479
|
+
for (let i = scopeStack.length - 1; i >= 0; i--) {
|
|
480
|
+
if (scopeStack[i].kind === "function")
|
|
481
|
+
return scopeStack[i].names;
|
|
482
|
+
}
|
|
483
|
+
return scopeStack[0].names;
|
|
484
|
+
};
|
|
485
|
+
const isDeclared = (name) => {
|
|
486
|
+
for (let i = scopeStack.length - 1; i >= 0; i--) {
|
|
487
|
+
if (scopeStack[i].names.has(name))
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
return SCRIPT_GLOBALS.has(name);
|
|
491
|
+
};
|
|
492
|
+
const collectBindings = (node, targetSet) => {
|
|
493
|
+
if (!node)
|
|
494
|
+
return;
|
|
495
|
+
if (ts.isIdentifier(node)) {
|
|
496
|
+
targetSet.add(node.text);
|
|
497
|
+
}
|
|
498
|
+
else if (ts.isObjectBindingPattern(node) || ts.isArrayBindingPattern(node)) {
|
|
499
|
+
for (const element of node.elements) {
|
|
500
|
+
if (ts.isBindingElement(element)) {
|
|
501
|
+
collectBindings(element.name, targetSet);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
const precollectDeclarations = (container, isFn) => {
|
|
507
|
+
ts.forEachChild(container, (child) => {
|
|
508
|
+
if (ts.isVariableStatement(child)) {
|
|
509
|
+
const isBlockScoped = (child.declarationList.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) !== 0;
|
|
510
|
+
const target = isBlockScoped ? currentScope() : functionScope();
|
|
511
|
+
for (const decl of child.declarationList.declarations) {
|
|
512
|
+
collectBindings(decl.name, target);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
else if (ts.isFunctionDeclaration(child)) {
|
|
516
|
+
if (child.name && ts.isIdentifier(child.name)) {
|
|
517
|
+
functionScope().add(child.name.text);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
else if (ts.isClassDeclaration(child)) {
|
|
521
|
+
if (child.name && ts.isIdentifier(child.name)) {
|
|
522
|
+
(isFn ? functionScope() : currentScope()).add(child.name.text);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
};
|
|
527
|
+
// Pre-collect top-level declarations into the root scope.
|
|
528
|
+
precollectDeclarations(sf, true);
|
|
529
|
+
const isTypeContext = (node) => {
|
|
530
|
+
let curr = node;
|
|
531
|
+
while (curr) {
|
|
532
|
+
if (ts.isTypeNode(curr) ||
|
|
533
|
+
ts.isTypeAliasDeclaration(curr) ||
|
|
534
|
+
ts.isInterfaceDeclaration(curr) ||
|
|
535
|
+
ts.isTypeQueryNode(curr)) {
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
curr = curr.parent;
|
|
539
|
+
}
|
|
540
|
+
return false;
|
|
541
|
+
};
|
|
542
|
+
const walk = (node) => {
|
|
543
|
+
let pushedScope = false;
|
|
544
|
+
if (ts.isFunctionDeclaration(node) ||
|
|
545
|
+
ts.isFunctionExpression(node) ||
|
|
546
|
+
ts.isArrowFunction(node) ||
|
|
547
|
+
ts.isMethodDeclaration(node) ||
|
|
548
|
+
ts.isConstructorDeclaration(node) ||
|
|
549
|
+
ts.isGetAccessor(node) ||
|
|
550
|
+
ts.isSetAccessor(node)) {
|
|
551
|
+
pushedScope = true;
|
|
552
|
+
const fnScope = new Set();
|
|
553
|
+
for (const param of node.parameters) {
|
|
554
|
+
collectBindings(param.name, fnScope);
|
|
555
|
+
}
|
|
556
|
+
if (node.name && ts.isIdentifier(node.name) && (ts.isFunctionExpression(node) || ts.isClassExpression(node))) {
|
|
557
|
+
fnScope.add(node.name.text);
|
|
558
|
+
}
|
|
559
|
+
scopeStack.push({ kind: "function", names: fnScope });
|
|
560
|
+
if (node.body) {
|
|
561
|
+
precollectDeclarations(node.body, true);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
else if (ts.isBlock(node) && !ts.isFunctionLike(node.parent)) {
|
|
565
|
+
pushedScope = true;
|
|
566
|
+
const blockScope = new Set();
|
|
567
|
+
scopeStack.push({ kind: "block", names: blockScope });
|
|
568
|
+
precollectDeclarations(node, false);
|
|
569
|
+
}
|
|
570
|
+
else if (ts.isCatchClause(node)) {
|
|
571
|
+
pushedScope = true;
|
|
572
|
+
const catchScope = new Set();
|
|
573
|
+
if (node.variableDeclaration) {
|
|
574
|
+
collectBindings(node.variableDeclaration.name, catchScope);
|
|
575
|
+
}
|
|
576
|
+
scopeStack.push({ kind: "block", names: catchScope });
|
|
577
|
+
if (node.block) {
|
|
578
|
+
precollectDeclarations(node.block, false);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
else if (ts.isForStatement(node) || ts.isForInStatement(node) || ts.isForOfStatement(node)) {
|
|
582
|
+
pushedScope = true;
|
|
583
|
+
const forScope = new Set();
|
|
584
|
+
if (node.initializer && ts.isVariableDeclarationList(node.initializer)) {
|
|
585
|
+
const isBlockScoped = (node.initializer.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) !== 0;
|
|
586
|
+
const target = isBlockScoped ? forScope : functionScope();
|
|
587
|
+
for (const decl of node.initializer.declarations) {
|
|
588
|
+
collectBindings(decl.name, target);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
scopeStack.push({ kind: "block", names: forScope });
|
|
592
|
+
}
|
|
593
|
+
if (ts.isIdentifier(node)) {
|
|
594
|
+
const parent = node.parent;
|
|
595
|
+
if (!isTypeContext(node)) {
|
|
596
|
+
if (parent && ts.isPropertyAccessExpression(parent) && parent.name === node) {
|
|
597
|
+
// obj.prop -> prop is property access, not an identifier reference in scope
|
|
598
|
+
}
|
|
599
|
+
else if (parent && ts.isPropertyAssignment(parent) && parent.name === node) {
|
|
600
|
+
// { prop: val } -> prop is property key, not an identifier reference in scope
|
|
601
|
+
}
|
|
602
|
+
else if (parent && ts.isBindingElement(parent) && (parent.name === node || parent.propertyName === node)) {
|
|
603
|
+
// { a: b } in destructuring pattern
|
|
604
|
+
}
|
|
605
|
+
else if (parent && ((ts.isMethodDeclaration(parent) && parent.name === node) ||
|
|
606
|
+
(ts.isPropertyDeclaration(parent) && parent.name === node) ||
|
|
607
|
+
(ts.isGetAccessor(parent) && parent.name === node) ||
|
|
608
|
+
(ts.isSetAccessor(parent) && parent.name === node) ||
|
|
609
|
+
(ts.isVariableDeclaration(parent) && parent.name === node) ||
|
|
610
|
+
(ts.isParameter(parent) && parent.name === node) ||
|
|
611
|
+
(ts.isFunctionDeclaration(parent) && parent.name === node) ||
|
|
612
|
+
(ts.isFunctionExpression(parent) && parent.name === node) ||
|
|
613
|
+
(ts.isClassDeclaration(parent) && parent.name === node) ||
|
|
614
|
+
(ts.isClassExpression(parent) && parent.name === node) ||
|
|
615
|
+
(ts.isLabeledStatement(parent) && parent.label === node) ||
|
|
616
|
+
(ts.isBreakOrContinueStatement(parent) && parent.label === node))) {
|
|
617
|
+
// declaration or label name
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
if (!isDeclared(node.text)) {
|
|
621
|
+
free.add(node.text);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
ts.forEachChild(node, walk);
|
|
627
|
+
if (pushedScope) {
|
|
628
|
+
scopeStack.pop();
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
walk(sf);
|
|
632
|
+
return [...free].sort();
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Free-identifier scan: uses the TypeScript compiler parser when available
|
|
636
|
+
* for full AST-level grammar fidelity, falling back to a tokenizing lexer when
|
|
637
|
+
* typescript is not present in the runtime environment.
|
|
409
638
|
*/
|
|
410
639
|
export function freeIdentifiers(body, params) {
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
// catch (e) / for (const x of ...) already covered by const/let/var; arrow params inside:
|
|
415
|
-
for (const m of stripped.matchAll(/\(([^)]*)\)\s*=>/g)) {
|
|
416
|
-
for (const id of (m[1] ?? "").matchAll(/[A-Za-z_$][\w$]*/g))
|
|
417
|
-
declared.add(id[0]);
|
|
418
|
-
}
|
|
419
|
-
// Arrow functions may omit parentheses for a single parameter (`row => …`).
|
|
420
|
-
// Without this pass, common callbacks such as `items.map(row => row.value)`
|
|
421
|
-
// are incorrectly reported as closure captures.
|
|
422
|
-
for (const m of stripped.matchAll(/(?:\basync\s+)?([A-Za-z_$][\w$]*)\s*=>/g)) {
|
|
423
|
-
declared.add(m[1]);
|
|
424
|
-
}
|
|
425
|
-
// function / class names AND their formal parameters (nested helpers in
|
|
426
|
-
// pulled scripts are common — Sanofi getSuppliers_1's levenshtein helpers).
|
|
427
|
-
for (const m of stripped.matchAll(/\b(?:function|class)\s+([A-Za-z_$][\w$]*)/g))
|
|
428
|
-
declared.add(m[1]);
|
|
429
|
-
for (const m of stripped.matchAll(/\bfunction(?:\s+[A-Za-z_$][\w$]*)?\s*\(([^)]*)\)/g)) {
|
|
430
|
-
for (const id of (m[1] ?? "").matchAll(/[A-Za-z_$][\w$]*/g))
|
|
431
|
-
declared.add(id[0]);
|
|
432
|
-
}
|
|
433
|
-
for (const m of stripped.matchAll(/\bcatch\s*\(([^)]*)\)/g)) {
|
|
434
|
-
for (const id of (m[1] ?? "").matchAll(/[A-Za-z_$][\w$]*/g))
|
|
435
|
-
declared.add(id[0]);
|
|
640
|
+
const ts = getTs();
|
|
641
|
+
if (ts !== undefined) {
|
|
642
|
+
return freeIdentifiersAST(body, params, ts);
|
|
436
643
|
}
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
unknown.add(id);
|
|
644
|
+
return freeIdentifiersLexer(body, params);
|
|
645
|
+
}
|
|
646
|
+
function findMatchingBrace(source, openIndex) {
|
|
647
|
+
let depth = 1;
|
|
648
|
+
for (let i = openIndex + 1; i < source.length; i += 1) {
|
|
649
|
+
const c = source[i];
|
|
650
|
+
if (c === "{")
|
|
651
|
+
depth += 1;
|
|
652
|
+
else if (c === "}") {
|
|
653
|
+
depth -= 1;
|
|
654
|
+
if (depth === 0)
|
|
655
|
+
return i;
|
|
656
|
+
}
|
|
451
657
|
}
|
|
452
|
-
return
|
|
658
|
+
return source.length;
|
|
659
|
+
}
|
|
660
|
+
function findExpressionEnd(source, startIndex) {
|
|
661
|
+
let i = startIndex;
|
|
662
|
+
let parenDepth = 0;
|
|
663
|
+
let bracketDepth = 0;
|
|
664
|
+
let braceDepth = 0;
|
|
665
|
+
while (i < source.length) {
|
|
666
|
+
const c = source[i];
|
|
667
|
+
if (c === "(")
|
|
668
|
+
parenDepth += 1;
|
|
669
|
+
else if (c === ")") {
|
|
670
|
+
if (parenDepth === 0)
|
|
671
|
+
return i;
|
|
672
|
+
parenDepth -= 1;
|
|
673
|
+
}
|
|
674
|
+
else if (c === "[")
|
|
675
|
+
bracketDepth += 1;
|
|
676
|
+
else if (c === "]") {
|
|
677
|
+
if (bracketDepth === 0)
|
|
678
|
+
return i;
|
|
679
|
+
bracketDepth -= 1;
|
|
680
|
+
}
|
|
681
|
+
else if (c === "{")
|
|
682
|
+
braceDepth += 1;
|
|
683
|
+
else if (c === "}") {
|
|
684
|
+
if (braceDepth === 0)
|
|
685
|
+
return i;
|
|
686
|
+
braceDepth -= 1;
|
|
687
|
+
}
|
|
688
|
+
else if (parenDepth === 0 && bracketDepth === 0 && braceDepth === 0) {
|
|
689
|
+
if (c === ";" || c === ",")
|
|
690
|
+
return i;
|
|
691
|
+
if (c === "\n" || c === "\r") {
|
|
692
|
+
let next = i + 1;
|
|
693
|
+
while (next < source.length && /\s/.test(source[next]))
|
|
694
|
+
next += 1;
|
|
695
|
+
const rest = source.slice(next);
|
|
696
|
+
if (next >= source.length ||
|
|
697
|
+
/^(?:const|let|var|return|if|for|while|switch|throw|try|function|class|export|import)\b/.test(rest) ||
|
|
698
|
+
source[next] === "}") {
|
|
699
|
+
return i;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
i += 1;
|
|
704
|
+
}
|
|
705
|
+
return source.length;
|
|
706
|
+
}
|
|
707
|
+
function extractParameterDeclarations(paramsText, paramsBaseOffset) {
|
|
708
|
+
const result = [];
|
|
709
|
+
let i = 0;
|
|
710
|
+
while (i < paramsText.length) {
|
|
711
|
+
while (i < paramsText.length && /\s/.test(paramsText[i]))
|
|
712
|
+
i += 1;
|
|
713
|
+
if (i >= paramsText.length)
|
|
714
|
+
break;
|
|
715
|
+
let depth = 0;
|
|
716
|
+
const paramStart = i;
|
|
717
|
+
let colonIndex = -1;
|
|
718
|
+
let equalsIndex = -1;
|
|
719
|
+
while (i < paramsText.length) {
|
|
720
|
+
const c = paramsText[i];
|
|
721
|
+
if (c === "(" || c === "[" || c === "{")
|
|
722
|
+
depth += 1;
|
|
723
|
+
else if (c === ")" || c === "]" || c === "}")
|
|
724
|
+
depth -= 1;
|
|
725
|
+
else if (c === ":" && depth === 0 && colonIndex === -1 && equalsIndex === -1) {
|
|
726
|
+
colonIndex = i;
|
|
727
|
+
}
|
|
728
|
+
else if (c === "=" && depth === 0 && equalsIndex === -1) {
|
|
729
|
+
equalsIndex = i;
|
|
730
|
+
}
|
|
731
|
+
else if (c === "," && depth <= 0) {
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
i += 1;
|
|
735
|
+
}
|
|
736
|
+
const endBinding = colonIndex !== -1 ? colonIndex : (equalsIndex !== -1 ? equalsIndex : i);
|
|
737
|
+
const bindingPart = paramsText.slice(paramStart, endBinding);
|
|
738
|
+
for (const m of bindingPart.matchAll(/[A-Za-z_$][\w$]*/g)) {
|
|
739
|
+
if (!KEYWORDS.has(m[0])) {
|
|
740
|
+
result.push({ name: m[0], offset: paramsBaseOffset + paramStart + m.index });
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
if (i < paramsText.length && paramsText[i] === ",") {
|
|
744
|
+
i += 1;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return result;
|
|
453
748
|
}
|
|
454
749
|
/**
|
|
455
|
-
* Collect binding names from `const`/`let`/`var` statements
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
* name — false-positive "closure captures" on every later declarator.
|
|
459
|
-
*
|
|
460
|
-
* Initializers can contain commas (arrays, calls, objects), so this walks
|
|
461
|
-
* depth-aware from the keyword to the terminating `;` (or EOF).
|
|
750
|
+
* Collect binding names from `const`/`let`/`var` statements into their appropriate
|
|
751
|
+
* lexical scopes (enclosing function for `var`, enclosing block or loop header for `let`/`const`,
|
|
752
|
+
* or global script scope when at top-level).
|
|
462
753
|
*/
|
|
463
|
-
function collectLexicalDeclarations(source,
|
|
754
|
+
function collectLexicalDeclarations(source, declaredGlobal, declarationIndices, addScoped, getEnclosingBlock, getEnclosingFunction) {
|
|
464
755
|
const keywordRe = /\b(?:const|let|var)\b/g;
|
|
465
756
|
let km;
|
|
466
757
|
while ((km = keywordRe.exec(source)) !== null) {
|
|
758
|
+
const kind = km[0];
|
|
759
|
+
const keywordIndex = km.index;
|
|
760
|
+
const beforeKeyword = source.slice(Math.max(0, keywordIndex - 20), keywordIndex);
|
|
761
|
+
const isForHeader = /\bfor\s*\(\s*$/.test(beforeKeyword);
|
|
762
|
+
let forBodyEnd = -1;
|
|
763
|
+
if (isForHeader) {
|
|
764
|
+
let parenDepth = 1;
|
|
765
|
+
let j = keywordIndex;
|
|
766
|
+
while (j < source.length && parenDepth > 0) {
|
|
767
|
+
if (source[j] === "(")
|
|
768
|
+
parenDepth += 1;
|
|
769
|
+
else if (source[j] === ")")
|
|
770
|
+
parenDepth -= 1;
|
|
771
|
+
j += 1;
|
|
772
|
+
}
|
|
773
|
+
while (j < source.length && /\s/.test(source[j]))
|
|
774
|
+
j += 1;
|
|
775
|
+
if (source[j] === "{") {
|
|
776
|
+
forBodyEnd = findMatchingBrace(source, j) + 1;
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
forBodyEnd = findExpressionEnd(source, j);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
const registerBinding = (name, offset) => {
|
|
783
|
+
declarationIndices.add(offset);
|
|
784
|
+
if (isForHeader && forBodyEnd !== -1 && kind !== "var") {
|
|
785
|
+
addScoped(name, keywordIndex, forBodyEnd);
|
|
786
|
+
}
|
|
787
|
+
else if (kind === "var") {
|
|
788
|
+
const fn = getEnclosingFunction(keywordIndex);
|
|
789
|
+
if (fn) {
|
|
790
|
+
addScoped(name, fn.start, fn.end);
|
|
791
|
+
}
|
|
792
|
+
else {
|
|
793
|
+
declaredGlobal.add(name);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
const blk = getEnclosingBlock(keywordIndex);
|
|
798
|
+
if (blk) {
|
|
799
|
+
addScoped(name, blk.start, blk.end);
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
declaredGlobal.add(name);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
};
|
|
467
806
|
let i = km.index + km[0].length;
|
|
468
807
|
while (i < source.length) {
|
|
469
808
|
while (i < source.length && /\s/.test(source[i]))
|
|
@@ -486,14 +825,19 @@ function collectLexicalDeclarations(source, declared) {
|
|
|
486
825
|
depth -= 1;
|
|
487
826
|
i += 1;
|
|
488
827
|
}
|
|
489
|
-
for (const id of source.slice(start, i).matchAll(/[A-Za-z_$][\w$]*/g))
|
|
490
|
-
|
|
828
|
+
for (const id of source.slice(start, i).matchAll(/[A-Za-z_$][\w$]*/g)) {
|
|
829
|
+
if (!KEYWORDS.has(id[0])) {
|
|
830
|
+
registerBinding(id[0], start + id.index);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
491
833
|
}
|
|
492
834
|
else {
|
|
493
835
|
const m = /^[A-Za-z_$][\w$]*/.exec(source.slice(i));
|
|
494
836
|
if (m === null)
|
|
495
837
|
break;
|
|
496
|
-
|
|
838
|
+
if (!KEYWORDS.has(m[0])) {
|
|
839
|
+
registerBinding(m[0], i);
|
|
840
|
+
}
|
|
497
841
|
i += m[0].length;
|
|
498
842
|
}
|
|
499
843
|
while (i < source.length && /\s/.test(source[i]))
|
|
@@ -522,6 +866,224 @@ function collectLexicalDeclarations(source, declared) {
|
|
|
522
866
|
}
|
|
523
867
|
}
|
|
524
868
|
}
|
|
869
|
+
/**
|
|
870
|
+
* Best-effort fallback tokenizing scan: strip comments/strings/templates, collect
|
|
871
|
+
* identifier tokens that are not property accesses, object keys, declarations,
|
|
872
|
+
* params, keywords, or known script globals. Scope-aware for nested methods,
|
|
873
|
+
* functions, catch clauses, and arrow function parameter blocks.
|
|
874
|
+
*/
|
|
875
|
+
export function freeIdentifiersLexer(body, params) {
|
|
876
|
+
const stripped = maskNonCodeForIdentifierScan(body);
|
|
877
|
+
const declaredGlobal = new Set(params);
|
|
878
|
+
const scopedDeclarations = new Map();
|
|
879
|
+
const declarationIndices = new Set();
|
|
880
|
+
const addScoped = (name, start, end) => {
|
|
881
|
+
let spans = scopedDeclarations.get(name);
|
|
882
|
+
if (!spans) {
|
|
883
|
+
spans = [];
|
|
884
|
+
scopedDeclarations.set(name, spans);
|
|
885
|
+
}
|
|
886
|
+
spans.push({ start, end });
|
|
887
|
+
};
|
|
888
|
+
// 1. Build all block spans from code braces
|
|
889
|
+
const allBlocks = [];
|
|
890
|
+
const blockStack = [];
|
|
891
|
+
for (let i = 0; i < stripped.length; i += 1) {
|
|
892
|
+
const c = stripped[i];
|
|
893
|
+
if (c === "{") {
|
|
894
|
+
blockStack.push(i);
|
|
895
|
+
}
|
|
896
|
+
else if (c === "}") {
|
|
897
|
+
const open = blockStack.pop();
|
|
898
|
+
if (open !== undefined) {
|
|
899
|
+
allBlocks.push({ start: open, end: i });
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
// 2. Identify function blocks and register function/method scopes and parameters
|
|
904
|
+
const functionOpenBraces = new Set();
|
|
905
|
+
// Arrow functions with parenthesized parameters: `(a, b) => { ... }` or `(a, b) => expr`
|
|
906
|
+
const arrowParensRe = /(?:\basync\s+)?\(([^)]*)\)\s*=>/dg;
|
|
907
|
+
for (const m of stripped.matchAll(arrowParensRe)) {
|
|
908
|
+
const afterArrow = m.index + m[0].length;
|
|
909
|
+
let bodyStart = afterArrow;
|
|
910
|
+
while (bodyStart < stripped.length && /\s/.test(stripped[bodyStart]))
|
|
911
|
+
bodyStart += 1;
|
|
912
|
+
let bodyEnd;
|
|
913
|
+
if (stripped[bodyStart] === "{") {
|
|
914
|
+
functionOpenBraces.add(bodyStart);
|
|
915
|
+
bodyEnd = findMatchingBrace(stripped, bodyStart) + 1;
|
|
916
|
+
}
|
|
917
|
+
else {
|
|
918
|
+
bodyEnd = findExpressionEnd(stripped, bodyStart);
|
|
919
|
+
}
|
|
920
|
+
const paramsText = m[1] ?? "";
|
|
921
|
+
const paramsOffset = m.indices && m.indices[1] ? m.indices[1][0] : m.index + m[0].indexOf(paramsText);
|
|
922
|
+
for (const p of extractParameterDeclarations(paramsText, paramsOffset)) {
|
|
923
|
+
declarationIndices.add(p.offset);
|
|
924
|
+
addScoped(p.name, bodyStart, bodyEnd);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
// Arrow functions with single unparenthesized parameter: `x => { ... }` or `x => expr`
|
|
928
|
+
const arrowSingleRe = /(?:\basync\s+)?([A-Za-z_$][\w$]*)\s*=>/dg;
|
|
929
|
+
for (const m of stripped.matchAll(arrowSingleRe)) {
|
|
930
|
+
const name = m[1];
|
|
931
|
+
if (!KEYWORDS.has(name)) {
|
|
932
|
+
const nameOffset = m.indices && m.indices[1] ? m.indices[1][0] : m.index;
|
|
933
|
+
declarationIndices.add(nameOffset);
|
|
934
|
+
const afterArrow = m.index + m[0].length;
|
|
935
|
+
let bodyStart = afterArrow;
|
|
936
|
+
while (bodyStart < stripped.length && /\s/.test(stripped[bodyStart]))
|
|
937
|
+
bodyStart += 1;
|
|
938
|
+
let bodyEnd;
|
|
939
|
+
if (stripped[bodyStart] === "{") {
|
|
940
|
+
functionOpenBraces.add(bodyStart);
|
|
941
|
+
bodyEnd = findMatchingBrace(stripped, bodyStart) + 1;
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
bodyEnd = findExpressionEnd(stripped, bodyStart);
|
|
945
|
+
}
|
|
946
|
+
addScoped(name, bodyStart, bodyEnd);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
// Function declarations/expressions
|
|
950
|
+
const fnRe = /\bfunction\*?(?:\s+([A-Za-z_$][\w$]*))?\s*\(([^)]*)\)\s*\{/dg;
|
|
951
|
+
for (const m of stripped.matchAll(fnRe)) {
|
|
952
|
+
const openBrace = m.index + m[0].length - 1;
|
|
953
|
+
functionOpenBraces.add(openBrace);
|
|
954
|
+
const closeBrace = findMatchingBrace(stripped, openBrace);
|
|
955
|
+
const paramsText = m[2] ?? "";
|
|
956
|
+
const paramsOffset = m.indices && m.indices[2] ? m.indices[2][0] : m.index + m[0].indexOf(paramsText);
|
|
957
|
+
for (const p of extractParameterDeclarations(paramsText, paramsOffset)) {
|
|
958
|
+
declarationIndices.add(p.offset);
|
|
959
|
+
addScoped(p.name, openBrace, closeBrace);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
// Method shorthand in objects and classes: `helper(a, b) { ... }`,
|
|
963
|
+
// `async helper(a, b) { ... }`, `*helper(a, b) { ... }`, `get foo() { ... }`, `set foo(v) { ... }`.
|
|
964
|
+
// Distinguish from control flow (`if (cond) {`, `while (cond) {`, etc.) by filtering keywords.
|
|
965
|
+
const methodRe = /(?:^|[^\w$.])(?:async\s+|\*\s*|get\s+|set\s+)?([A-Za-z_$][\w$]*)\s*\(([^)]*)\)\s*\{/dg;
|
|
966
|
+
for (const m of stripped.matchAll(methodRe)) {
|
|
967
|
+
const name = m[1];
|
|
968
|
+
if (!CONTROL_FLOW_KEYWORDS.has(name) && !KEYWORDS.has(name)) {
|
|
969
|
+
if (m.indices && m.indices[1]) {
|
|
970
|
+
declarationIndices.add(m.indices[1][0]);
|
|
971
|
+
}
|
|
972
|
+
const openBrace = m.index + m[0].length - 1;
|
|
973
|
+
functionOpenBraces.add(openBrace);
|
|
974
|
+
const closeBrace = findMatchingBrace(stripped, openBrace);
|
|
975
|
+
const paramsText = m[2] ?? "";
|
|
976
|
+
const paramsOffset = m.indices && m.indices[2] ? m.indices[2][0] : m.index + m[0].indexOf(paramsText);
|
|
977
|
+
for (const p of extractParameterDeclarations(paramsText, paramsOffset)) {
|
|
978
|
+
declarationIndices.add(p.offset);
|
|
979
|
+
addScoped(p.name, openBrace, closeBrace);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
// Scope helpers
|
|
984
|
+
const getEnclosingBlock = (pos) => {
|
|
985
|
+
let innermost = null;
|
|
986
|
+
for (let i = 0; i < allBlocks.length; i += 1) {
|
|
987
|
+
const b = allBlocks[i];
|
|
988
|
+
if (b.start < pos && pos < b.end) {
|
|
989
|
+
if (!innermost || b.start > innermost.start) {
|
|
990
|
+
innermost = b;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return innermost;
|
|
995
|
+
};
|
|
996
|
+
const getEnclosingFunction = (pos) => {
|
|
997
|
+
let innermost = null;
|
|
998
|
+
for (let i = 0; i < allBlocks.length; i += 1) {
|
|
999
|
+
const b = allBlocks[i];
|
|
1000
|
+
if (functionOpenBraces.has(b.start) && b.start < pos && pos < b.end) {
|
|
1001
|
+
if (!innermost || b.start > innermost.start) {
|
|
1002
|
+
innermost = b;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return innermost;
|
|
1007
|
+
};
|
|
1008
|
+
// Register function names (hoisted to enclosing function or global)
|
|
1009
|
+
for (const m of stripped.matchAll(fnRe)) {
|
|
1010
|
+
const fnName = m[1];
|
|
1011
|
+
if (fnName && !KEYWORDS.has(fnName)) {
|
|
1012
|
+
if (m.indices && m.indices[1])
|
|
1013
|
+
declarationIndices.add(m.indices[1][0]);
|
|
1014
|
+
const parentFn = getEnclosingFunction(m.index);
|
|
1015
|
+
if (parentFn) {
|
|
1016
|
+
addScoped(fnName, parentFn.start, parentFn.end);
|
|
1017
|
+
}
|
|
1018
|
+
else {
|
|
1019
|
+
declaredGlobal.add(fnName);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
// Catch blocks
|
|
1024
|
+
const catchRe = /\bcatch\s*\(([^)]*)\)\s*\{/dg;
|
|
1025
|
+
for (const m of stripped.matchAll(catchRe)) {
|
|
1026
|
+
const openBrace = m.index + m[0].length - 1;
|
|
1027
|
+
const closeBrace = findMatchingBrace(stripped, openBrace);
|
|
1028
|
+
const paramsText = m[1] ?? "";
|
|
1029
|
+
const paramsOffset = m.indices && m.indices[1] ? m.indices[1][0] : m.index + m[0].indexOf(paramsText);
|
|
1030
|
+
for (const p of extractParameterDeclarations(paramsText, paramsOffset)) {
|
|
1031
|
+
declarationIndices.add(p.offset);
|
|
1032
|
+
addScoped(p.name, openBrace, closeBrace);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
// Class declarations (block-scoped or global)
|
|
1036
|
+
for (const m of stripped.matchAll(/\bclass\s+([A-Za-z_$][\w$]*)/dg)) {
|
|
1037
|
+
const className = m[1];
|
|
1038
|
+
if (!KEYWORDS.has(className)) {
|
|
1039
|
+
if (m.indices && m.indices[1])
|
|
1040
|
+
declarationIndices.add(m.indices[1][0]);
|
|
1041
|
+
const blk = getEnclosingBlock(m.index);
|
|
1042
|
+
if (blk) {
|
|
1043
|
+
addScoped(className, blk.start, blk.end);
|
|
1044
|
+
}
|
|
1045
|
+
else {
|
|
1046
|
+
declaredGlobal.add(className);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
// Collect const / let / var declarations into proper block, function, or global scopes
|
|
1051
|
+
collectLexicalDeclarations(stripped, declaredGlobal, declarationIndices, addScoped, getEnclosingBlock, getEnclosingFunction);
|
|
1052
|
+
const isDeclaredAt = (id, pos) => {
|
|
1053
|
+
if (declaredGlobal.has(id))
|
|
1054
|
+
return true;
|
|
1055
|
+
const spans = scopedDeclarations.get(id);
|
|
1056
|
+
if (spans) {
|
|
1057
|
+
for (let i = 0; i < spans.length; i += 1) {
|
|
1058
|
+
const span = spans[i];
|
|
1059
|
+
if (pos >= span.start && pos < span.end) {
|
|
1060
|
+
return true;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
return false;
|
|
1065
|
+
};
|
|
1066
|
+
const unknown = new Set();
|
|
1067
|
+
const idRe = /[A-Za-z_$][\w$]*/g;
|
|
1068
|
+
let match;
|
|
1069
|
+
while ((match = idRe.exec(stripped)) !== null) {
|
|
1070
|
+
const id = match[0];
|
|
1071
|
+
if (declarationIndices.has(match.index))
|
|
1072
|
+
continue;
|
|
1073
|
+
const before = stripped.slice(0, match.index);
|
|
1074
|
+
const after = stripped.slice(match.index + id.length);
|
|
1075
|
+
if (/[.]\s*$/.test(before))
|
|
1076
|
+
continue; // property access: a.b
|
|
1077
|
+
if (/^\s*:/.test(after) && !/\?\s*$/.test(before))
|
|
1078
|
+
continue; // object key: {b: ...} (not ternary)
|
|
1079
|
+
if (KEYWORDS.has(id) || SCRIPT_GLOBALS.has(id))
|
|
1080
|
+
continue;
|
|
1081
|
+
if (isDeclaredAt(id, match.index))
|
|
1082
|
+
continue;
|
|
1083
|
+
unknown.add(id);
|
|
1084
|
+
}
|
|
1085
|
+
return [...unknown].sort();
|
|
1086
|
+
}
|
|
525
1087
|
/**
|
|
526
1088
|
* Replace comments, strings, template literals, and regex literals with
|
|
527
1089
|
* spaces while preserving line breaks and character offsets. A regex-only
|
|
@@ -541,7 +1103,6 @@ function maskNonCodeForIdentifierScan(source) {
|
|
|
541
1103
|
chars[i] = " ";
|
|
542
1104
|
};
|
|
543
1105
|
let i = 0;
|
|
544
|
-
let previousSignificant = "";
|
|
545
1106
|
while (i < source.length) {
|
|
546
1107
|
const c = source[i];
|
|
547
1108
|
const next = source[i + 1];
|
|
@@ -581,18 +1142,15 @@ function maskNonCodeForIdentifierScan(source) {
|
|
|
581
1142
|
i += 1;
|
|
582
1143
|
}
|
|
583
1144
|
mask(start, i);
|
|
584
|
-
previousSignificant = '"';
|
|
585
1145
|
continue;
|
|
586
1146
|
}
|
|
587
|
-
const regexContext =
|
|
1147
|
+
const regexContext = startsRegexLiteralAt(chars, i);
|
|
588
1148
|
if (c === "/" && next !== "/" && next !== "*" && regexContext) {
|
|
589
1149
|
const start = i;
|
|
590
1150
|
i = skipRegexLiteral(source, i);
|
|
591
1151
|
mask(start, i);
|
|
592
|
-
previousSignificant = "/";
|
|
593
1152
|
continue;
|
|
594
1153
|
}
|
|
595
|
-
previousSignificant = c;
|
|
596
1154
|
i += 1;
|
|
597
1155
|
}
|
|
598
1156
|
return chars.join("");
|