"use strict";
/*
 * edge-parser
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAST = void 0;
const edge_error_1 = require("edge-error");
const acorn_1 = require("acorn");
/**
 * Patch the acorn loc to point to the correct line number
 * inside the original template engine
 */
function patchLoc(loc, lexerLoc) {
    /**
     * Patch the column also, when it's the first line. The reason we do this, since
     * the first line in the actual edge file may contain the Javascript expression
     * at a different column all together
     */
    if (loc.start.line === 1) {
        loc.start.column = loc.start.column + lexerLoc.start.col;
    }
    loc.start.line = loc.start.line + lexerLoc.start.line - 1;
    loc.end.line = loc.end.line + lexerLoc.start.line - 1;
}
/**
 * Generates and returns the acorn AST for a given Javascript expression. Assuming
 * the Javascript expression is embedded into the edge lexer token, this method
 * expects you to pass the token loc and the filename.
 */
function generateAST(jsArg, lexerLoc, filename) {
    const acornOptions = {
        locations: true,
        ecmaVersion: 2020,
        allowAwaitOutsideFunction: true,
        onToken: (token) => patchLoc(token.loc, lexerLoc),
    };
    try {
        const ast = (0, acorn_1.parse)(jsArg, acornOptions);
        return ast['body'][0];
    }
    catch (error) {
        /**
         * The error loc is not passed via `onToken` event, so need
         * to patch is here seperately
         */
        const line = error.loc.line + lexerLoc.start.line - 1;
        const col = error.loc.line === 1 ? error.loc.column + lexerLoc.start.col : error.loc.column;
        throw new edge_error_1.EdgeError(error.message.replace(/\(\d+:\d+\)/, ''), 'E_ACORN_ERROR', {
            line,
            col,
            filename,
        });
    }
}
exports.generateAST = generateAST;
