action/prepend.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrependAction = void 0;
const action_1 = require("../action");
const node_mutation_1 = __importDefault(require("../node-mutation"));
/**
 * PrependAction to prepend code to the top of node body.
 * @extends BaseAction
 */
class PrependAction extends action_1.BaseAction {
    /**
     * Create an AppendAction
     * @param {T} node
     * @param {string} code
     * @param {object} options
     * @param {Adapter<T>} options.adapter - adapter to parse the node
     */
    constructor(node, code, { adapter }) {
        super(node, code, { adapter });
        this.type = "insert";
    }
    /**
     * Calculate the begin and end positions.
     * @protected
     */
    calculatePositions() {
        this.start = this.adapter.getStart(this.node) + this.adapter.getSource(this.node).indexOf("{") + "{\n".length;
        this.end = this.start;
    }
    /**
     * The rewritten source code.
     * @returns {string} rewritten code.
     */
    get newCode() {
        const source = this.rewrittenSource();
        const indent = this.adapter.getIndent(this.node) + node_mutation_1.default.tabWidth;
        return this.addIndentToCode(source, indent);
    }
}
exports.PrependAction = PrependAction;