File size: 2,891 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { tsPlugin } from "@sveltejs/acorn-typescript";
import { Parser } from "acorn";
// The engine transpiles scripts as TypeScript, so annotated scripts must parse here too.
const ScriptParser = Parser.extend(tsPlugin());
function record(value) {
    return value !== null && typeof value === "object" && !Array.isArray(value);
}
function bindsDiscovery(value) {
    if (!record(value))
        return false;
    if (value.type === "Identifier")
        return value.name === "tools" || value.name === "search";
    if (value.type === "Property")
        return bindsDiscovery(value.value);
    if (value.type === "AssignmentPattern")
        return bindsDiscovery(value.left);
    return Object.values(value).some(entry => Array.isArray(entry) ? entry.some(bindsDiscovery) : bindsDiscovery(entry));
}
/** The pinned engine has a bare search() intrinsic, but no tools.search alias.
 * Rewrite syntax before execution, never retry a script that may have written.
 * Source ranges leave strings, comments, and unrelated tool paths unchanged. */
export function nativeDiscoveryCode(code) {
    if (!code.includes("search"))
        return code;
    let tree;
    try {
        tree = ScriptParser.parse(code, { ecmaVersion: "latest", locations: true, allowAwaitOutsideFunction: true, allowReturnOutsideFunction: true });
    }
    catch {
        // Let the engine own syntax diagnostics.
        return code;
    }
    const ranges = [];
    let shadowed = false;
    function visit(value) {
        if (!record(value))
            return;
        if ((value.type === "VariableDeclarator" || value.type === "FunctionDeclaration" || value.type === "FunctionExpression" || value.type === "ClassDeclaration" || value.type === "ClassExpression") && bindsDiscovery(value.id)
            || value.type === "CatchClause" && bindsDiscovery(value.param)
            || Array.isArray(value.params) && value.params.some(bindsDiscovery))
            shadowed = true;
        if (value.type === "MemberExpression" && record(value.object) && value.object.type === "Identifier" && value.object.name === "tools"
            && record(value.property) && (value.computed === true ? value.property.type === "Literal" && value.property.value === "search"
            : value.property.type === "Identifier" && value.property.name === "search")
            && typeof value.start === "number" && typeof value.end === "number")
            ranges.push({ start: value.start, end: value.end });
        for (const entry of Object.values(value)) {
            if (Array.isArray(entry))
                entry.forEach(visit);
            else
                visit(entry);
        }
    }
    visit(tree);
    if (shadowed)
        return code;
    for (const { start, end } of ranges.sort((left, right) => right.start - left.start))
        code = code.slice(0, start) + "search" + code.slice(end);
    return code;
}