Skip to content

Javascript - visitors for space get info on locations #5459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

greg-at-moderne
Copy link
Contributor

@greg-at-moderne greg-at-moderne commented May 20, 2025

What's changed?

In Javascript visitors for spaces adding Location information. Similar to what we have in Java (but not copying over the exact names of the Location constants).

What's your motivation?

  • That will be needed for some of the autoformatting visitors.

How

This is ChatGPT's useful script, plus a lot of manual work to glue things together.

const fs = require("fs");
const path = require("path");

const FILES_TO_PATCH = [
    "java/visitor.ts",
    "java/rpc.ts",
    "javascript/rpc.ts",
    "javascript/visitor.ts",
    "javascript/print.ts"
];

function toUpperUnderscore(name) {
    return name
        .replace(/([a-z])([A-Z])/g, "$1_$2")
        .replace(/[\W]+/g, "_")
        .replace(/^_+|_+$/g, "")
        .toUpperCase();
}

function extractPropertyFromLine(line) {
    // Case 1: match property from lambda: el => el.property
    let match = line.match(/el\s*=>\s*el\.([a-zA-Z0-9_]+)/);
    if (match) return match[1];

    // Case 2: match assignment target like: draft.valueType = await ...
    match = line.match(/\b([a-zA-Z0-9_]+)\.([a-zA-Z0-9_]+)\s*=.*\s*await\b/);
    if (match) return match[2];

    // Case 3: match: getAndSend(<any>, <anyVar> => <anyVar>.<property>, ...
    match = line.match(/getAndSend(?:List)?\([^,]+,\s*(\w+)\s*=>\s*\1\.(\w+)/);
    if (match) return match[2];

    // Case 4: match: visitXYZ(...object.propertyName..., ...)
    match = line.match(/await this.visit\w+\s*\(\s*[\w\d_]+\.(\w+)/);
    if (match) return match[1];

    // Case 5: match: produceJava
    match = line.match(/produceJava(?:Script)?<(?:J|JS)\.([A-Za-z0-9_.]+)>/)
    if (match) return "PREFIX";

    return "UNKNOWN";
}

function addMiddleArgToVisitContainer(content) {
    const lines = content.split("\n");
    const updatedLines = [];

    let currentMethod = null;

    for (let i = 0; i < lines.length; i++) {
        let line = lines[i];

        const methodMatch = line.match(/^ +(?:public|protected|private)?\s*(?:override\s+)?(?:async\s+)?(visit[A-Z][A-Za-z0-9_]*)\s*\(/);
        if (methodMatch) {
           const fullMethod = methodMatch[1]; // e.g. visitExpressionWithTypeArguments
           // Strip the "visit" prefix and convert to UPPER_UNDERSCORE
           currentMethod = fullMethod.startsWith("visit") ? fullMethod.slice(5) : fullMethod;
        }

        const visitCallMatch = line.match(/(\bproduceJava(?:Script)?[<][^>]+[>]\s*\()\s*([^,]+)\s*,\s*([^)\n]+)/);
        if (visitCallMatch) {
            const objectArg = visitCallMatch[2].trim();
            const restArg = visitCallMatch[3].trim();

            const property = extractPropertyFromLine(line);
            const method = currentMethod || "UNKNOWN";

            const locationValue = `"${toUpperUnderscore(method)}_${toUpperUnderscore(property)}"`;

            line = line.replace(visitCallMatch[0], `${visitCallMatch[1]}${objectArg}, ${locationValue}, ${restArg})`);
        }

        updatedLines.push(line);
    }

    return updatedLines.join("\n");
}


function main() {
    for (const file of FILES_TO_PATCH) {
        const filePath = path.resolve(__dirname, file);
        if (!fs.existsSync(filePath)) {
            console.warn(`⚠️ File not found: ${filePath}`);
            continue;
        }

        const original = fs.readFileSync(filePath, "utf8");
        const updated = addMiddleArgToVisitContainer(original);
        fs.writeFileSync(filePath, updated, "utf8");
        console.log(`✅ visitContainer calls amended in: ${file}`);
    }
}

main();

@greg-at-moderne greg-at-moderne self-assigned this May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

1 participant