action/delete.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteAction = void 0;
const action_1 = require("../action");
/**
 * DeleteAction to delete child node.
 * @extends BaseAction
 */
class DeleteAction extends action_1.BaseAction {
    /**
     * Create a DeleteAction
     * @param {T} node
     * @param {string|string[]} selectors - name of child nodes
     * @param {DeleteOptions} options
     * @param options.adapter - adapter to parse the node
     */
    constructor(node, selectors, options) {
        super(node, "", { adapter: options.adapter });
        this.selectors = Array.isArray(selectors) ? selectors : Array(selectors);
        this.type = "delete";
        this.options = options;
    }
    /**
     * Calculate the begin and end positions.
     * @protected
     */
    calculatePositions() {
        this.start = Math.min(...this.selectors.map((selector) => this.adapter.childNodeRange(this.node, selector).start));
        this.end = Math.max(...this.selectors.map((selector) => this.adapter.childNodeRange(this.node, selector).end));
        if (this.options.andComma) {
            this.removeComma();
        }
        this.squeezeSpaces();
        this.removeSpace();
        if (this.options.wholeLine) {
            this.removeNewLine();
            this.squeezeLines();
        }
    }
    /**
     * The rewritten code, always empty string.
     */
    get newCode() {
        return "";
    }
    /**
     * Remove the whole line.
     * @private
     */
    removeNewLine() {
        const lines = this.source().split("\n");
        const beginLine = this.beginLine();
        const endLine = this.endLine();
        this.start =
            lines.slice(0, beginLine - 1).join("\n").length +
                (beginLine === 1 ? 0 : "\n".length);
        this.end = lines.slice(0, endLine).join("\n").length;
        if (lines.length > endLine) {
            this.end = this.end + "\n".length;
        }
    }
    beginLine() {
        return this.adapter.fileContent(this.node).slice(0, this.start).split("\n").length;
    }
    endLine() {
        return this.adapter.fileContent(this.node).slice(0, this.end).split("\n").length;
    }
}
exports.DeleteAction = DeleteAction;
exports.default = DeleteAction;