action/insert.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InsertAction = void 0;
const action_1 = require("../action");
/**
 * InsertAction to add code to the node.
 * @extends BaseAction
 */
class InsertAction extends action_1.BaseAction {
    /**
     * Create an InsertAction
     * @param {T} node
     * @param {string} code - new code to be inserted
     * @param {InsertOptions} options
     * @param options.adapter - adapter to parse the node
     */
    constructor(node, code, options) {
        super(node, code, { adapter: options.adapter });
        this.selector = options.to;
        this.options = options;
        this.type = "insert";
        if (options.conflictPosition) {
            this.conflictPosition = options.conflictPosition;
        }
    }
    /**
     * Calculate the begin and end positions.
     * @protected
     */
    calculatePositions() {
        const range = this.selector
            ? this.adapter.childNodeRange(this.node, this.selector)
            : { start: this.adapter.getStart(this.node), end: this.adapter.getEnd(this.node) };
        this.start = this.options.at === "beginning" ? range.start : range.end;
        this.end = this.start;
    }
    /**
     * The rewritten source code.
     * @returns {string} rewritten code.
     */
    get newCode() {
        if (this.options.andComma) {
            return this.options.at === "end" ? `, ${this.rewrittenSource()}` : `${this.rewrittenSource()}, `;
        }
        if (this.options.andSpace) {
            return this.options.at === "end" ? ` ${this.rewrittenSource()}` : `${this.rewrittenSource()} `;
        }
        return this.rewrittenSource();
    }
}
exports.InsertAction = InsertAction;