Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
13
Indexable
const fs = require('fs');
const { parse } = require('qmlweb-parser');
const acorn = require('acorn');

// Read the QML file
const qmlContent = fs.readFileSync('./YouZanApi.qml', 'utf8');

// Parse the QML content
const qmlAst = parse(qmlContent);

// Function to recursively search for JavaScript blocks in the QML AST
function findJavaScriptBlocks(node) {
    let jsBlocks = [];

    if (node.type === 'JavaScriptBlock') {
        jsBlocks.push(node.value);
    }

    if (node.children) {
        node.children.forEach(child => {
            jsBlocks = jsBlocks.concat(findJavaScriptBlocks(child));
        });
    }

    return jsBlocks;
}

// Find all JavaScript blocks in the QML AST
const jsBlocks = findJavaScriptBlocks(qmlAst);

// Parse each JavaScript block with acorn
const parsedJSBlocks = jsBlocks.map(jsCode => {
    try {
        return acorn.parse(jsCode, { sourceType: 'module' });
    } catch (error) {
        console.error('Error parsing JavaScript block:', error);
        return null;
    }
});

// Combine the QML AST and parsed JavaScript blocks into one object
const combinedAst = {
    qml: qmlAst,
    javascript: parsedJSBlocks
};

// Write the combined AST to a JSON file
fs.writeFileSync('./combined-ast.json', JSON.stringify(combinedAst, null, 2), 'utf8');

console.log('Combined AST has been written to combined-ast.json');
Editor is loading...
Leave a Comment