All files / gogocode-core/src Ast.js

96.01% Statements 458/477
90.4% Branches 311/344
100% Functions 68/68
96.39% Lines 455/472

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 84136x 36x 36x 36x 36x 36x 36x 36x 36x   36x                                 1495x 867x       1495x 1495x 1495x     189x     11x     86x     407x     1698x     710x     170x   170x     16x 16x 38x   16x     91x 91x 91x 218x 218x 218x 218x 218x   91x     288x 2x   286x     286x       286x 286x                 286x 286x 11x   286x   534x 343x 343x 2160x 343x 5x   343x   1817x     534x   286x 28x   286x     90x 90x 12x   90x 1x     89x     89x   11x 11x 10x 10x     10x         89x 1x 1x 11x 3x         89x 89x 77x 79x   77x   12x       8x 1x     7x   7x 7x 7x 31x   7x     77x 2x   75x 75x 75x 75x 12x   12x     12x           75x     21x     14x 1x     13x   13x 13x 13x 52x   13x     10x 1x     9x   9x 9x 9x 12x   9x     10x 1x     9x   9x 9x 9x 9x       10x 1x     9x   9x 9x 9x 8x   9x     10x 1x     9x   9x 9x 9x 9x     14x 14x 14x 14x 14x     104x 1x   103x 103x   22x 21x   1x     81x   79x 2x 2x     23x 23x     2x     2x 2x 2x 2x   2x 2x 1x 1x   2x 8x 8x 8x 8x 8x 8x             8x 8x     2x 2x           11x 2x       9x 4x 4x 4x 4x 4x 4x           1006x 3566x 196x 3370x 830x 82x 250x     748x           503x 1783x 98x 98x 1685x 415x 41x 125x     374x             5x   5x                   5x             9x 9x 9x 9x     132x   1x   131x 131x 131x     21x 1x   20x 2x   20x 18x   20x 2x   20x 20x 20x 20x     20x     43x 1x   42x 3x     39x   4x       4x 4x 4x       4x     35x   35x   18x 18x 14x 14x   4x   4x 4x 29x 4x       18x 9x   9x     17x 17x 14x 8x   6x   14x   3x 3x 3x 3x 3x 5x   5x 3x 3x 3x 3x     3x 3x         5x 5x         28x 6x   22x 13x   22x 4x   22x 4x 4x 4x   4x         18x 18x   18x 18x   17x     24x 5x   19x 10x   19x 2x 2x   17x 3x   17x 5x 5x 5x   5x         12x 11x   12x 13x   12x     35x 1x   34x 34x 34x           34x 9x 9x 25x 14x 11x   3x 3x     11x 11x 11x 1x           34x 6x 6x   34x 34x 24x   10x         18x 3x   15x 3x 3x   15x 6x 6x   15x 10x   15x 2x   15x 12x   15x 21x   15x       9x 2x 2x   9x 7x 7x   9x 4x   9x 1x   7x 7x   7x 7x   7x     11x 13x 7x 7x   6x 6x   6x 5x       11x     17x 1x   16x 9x 9x 9x 9x         9x     7x 7x 7x 6x       15x     289x 1x   288x 198x   90x           628x 628x     628x       79x 79x 79x 79x 158x 152x 152x     79x 73x   6x         333x 199x 199x   134x   333x       49x 17x 17x 71x             17x 71x 71x   32x 32x 17x   15x 15x 15x 15x 15x 15x 15x 15x 18x   18x 15x 15x 15x 15x 56x 56x   15x   41x 41x 24x   17x     56x   15x 15x 15x   18x 18x         23x 23x 23x 23x 23x 39x 23x 16x 16x           36x  
const generate = require('./js-core/generate');
const htmlGenerate = require('./html-core/serialize-node');
const vueGenerate = require('./vue-core/generate');
const core = require('./js-core/core');
const htmlCore = require('./html-core/core')
const vueCore = require('./vue-core/core')
const NodePath = require('./NodePath');
const filterProp = require('./js-core/filter-prop')
const { isObject } = require('./util')
 
const languageMap = {
    'js': { 
        generate,
        core
    },
    'html': { 
        generate: htmlGenerate,
        core: htmlCore
    },
    'vue': { 
        generate: vueGenerate,
        core: vueCore
    }
}
 
class AST {
    constructor(nodePath, { parseOptions, match, rootNode } = {}) {
        if (nodePath) {
            this[0] = {
                nodePath, match
            }
        }
        this.rootNode = rootNode;
        this.expando = 'g' + ('' + Math.random()).replace( /\D/g, "" ) + 'g';
        this.parseOptions = parseOptions;
    }
    get node() {
        return this[0] ? this[0].nodePath.node : null
    }
    get value() {
        return this[0] ? this[0].nodePath.value : null
    }
    get match() {
        return this[0] ? this[0].match : []
    }
    get isHtml() {
        return this.parseOptions && (this.parseOptions.html || this.parseOptions.language == 'html');
    }
    get language() {
        return (this.parseOptions && this.parseOptions.language) || 'js';
    }
    get core() {
        return languageMap[this.language].core
    }
    get _index() {
        initParent(this);
        // todo js
        return this[0]._index;
    }
    get length() {
        let i = 0;
        while(this[i]) {
            i++
        }
        return i;
    }
    each(callback) {
        let i = 0;
        const newAST = cloneAST(this)
        while (this[i]) {
            const { nodePath, match } = this[i]
            const eachNode = new AST(nodePath, { parseOptions: this.parseOptions, match, rootNode: this.rootNode})
            callback(eachNode, i);
            newAST[i] = eachNode[0] || null
            i++;
        }
        return newAST
    }
    find(selector, options = {}) {
        if (!selector) {
            throw new Error('find failed! first argument should not be null!')
        }
        Iif (!this[0]) {
            return this;
        }
        const { nodePath } = this[0];
        // if (typeof selector !== 'string' && !Array.isArray(selector)) {
        //     throw new Error('find failed! Nodepath is null!');
        // }
        const pOptions = options.parseOptions || this.parseOptions;
        const {nodePathList, matchWildCardList, extra = {} } = this.core.getAstsBySelector(
            nodePath.node,
            selector, {
                strictSequence: options.ignoreSequence === false,
                parseOptions: pOptions,
                expando: this.expando,
                deep: options.deep
            }
        );
        const newAST = cloneAST(this)
        if (!newAST.rootNode) {
            newAST.rootNode = this[0].nodePath;
        }
        nodePathList.forEach((nodePath, i) => {
            // 把this里的parentPath接到nodePath上
            if (this.language == 'js') {
                let theNodePath = nodePath;
                while(theNodePath.parentPath) {
                    if (theNodePath.parentPath && theNodePath.parentPath.name == 'root') {
                        if (theNodePath.parentPath.node.type != 'File') {
                            theNodePath.parentPath = this[0].nodePath;
                        }
                        break;
                    }
                    theNodePath = theNodePath.parentPath;
                }
            }
            newAST[i] = { nodePath, parseOptions: extra.parseOptions || pOptions, match: matchWildCardList[i] };
        })
        if (extra.parseOptions) {
            newAST.parseOptions = extra.parseOptions
        }
        return newAST;
    }
    parent(option) {
        let level = 0;
        if (typeof option == 'number') {
            level = option
        }
        if (!this[0]) {
            return this;
        }
        // if (!this[0].parentList) {
        initParent(this)
        // }
 
        let parent = [this[0].parentList[level]]
        function parentMatch (full, partial) {
            return Object.keys(partial).every(prop => {
                if (!full || !partial) return false
                Iif (!full[prop]) return false;
                Iif (isObject(partial[prop])) {
                    return parentMatch(full[prop], partial[prop])
                } else {
                    return full[prop] == partial[prop]
                }
            })
        }
 
        if (isObject(option)) {
            parent = [];
            this[0].parentList.forEach(p => {
                if (parentMatch(p.node, option)) {
                    parent.push(p)
                }
            })
        }
        
        const newAST = cloneAST(this)
        if (parent[0]) {
            parent.forEach((p, i) => {
                newAST[i] = { nodePath: p, parseOptions: this.parseOptions };
            })
            return newAST;
        } else {
            return this;
        }
    }
    parents() {
        if (!this[0]) {
            return this;
        }
        // if (!this[0].parentList) {
        initParent(this)
        // }
        const { parentList } = this[0];
        const newAST = cloneAST(this)
        parentList.forEach((nodePath, i) => {
            newAST[i] = { nodePath, parseOptions: this.parseOptions, match: null };
        })
        return newAST;
    }
    root(option) {
        if (!this.rootNode) {
            return this;
        }
        const newAST = cloneAST(this)
        newAST[0] = { nodePath: this.rootNode }
        newAST.rootNode = null;
        if (this.parseOptions && this.parseOptions.rootLanguage == 'vue') {
            Iif (option == 'template') {
                newAST[0] = { nodePath: this.rootNode.node.templateAst }
            } else Iif (option == 'script') {
                newAST[0] = { nodePath: this.rootNode.node.scriptAst }
            } else {
                newAST.parseOptions = Object.assign(
                    {}, 
                    this.parseOptions, 
                    { language: 'vue', rootLanguage: undefined });
            }
        }
        return newAST;
    }
    has(selector, options) {
        return !!this.find(selector, options)[0]
    }
    siblings() {
        if (!this[0]) {
            return this;
        }
        // if (!Array.isArray(this[0].siblings)) {
        initSiblings(this);
        // }
        const siblings = this[0].siblings || [];
        const newAST = cloneAST(this)
        siblings.forEach((sibling, i) => {
            newAST[i] = sibling
        });
        return newAST;
    }
    prevAll() {
        if (!this[0]) {
            return this;
        }
        // if (!Array.isArray(this[0].siblings)) {
        initSiblings(this);
        // }
        const prevAll = this[0].prevAll || [];
        const newAST = cloneAST(this)
        prevAll.forEach((prev, i) => {
            newAST[i] = prev
        });
        return newAST;
    }
    prev() {
        if (!this[0]) {
            return this;
        }
        // if (!Array.isArray(this[0].siblings)) {
        initSiblings(this);
        // }
        const prevAll = this[0].prevAll || [];
        const newAST = cloneAST(this)
        newAST[0] = prevAll[prevAll.length - 1];
        return newAST;
    }
 
    nextAll() {
        if (!this[0]) {
            return this;
        }
        // if (!Array.isArray(this[0].siblings)) {
        initSiblings(this);
        // }
        const nextAll = this[0].nextAll || [];
        const newAST = cloneAST(this)
        nextAll.forEach((next, i) => {
            newAST[i] = next
        });
        return newAST;
    }
    next() {
        if (!this[0]) {
            return this;
        }
        // if (!Array.isArray(this[0].siblings)) {
        initSiblings(this);
        // }
        const nextAll = this[0].nextAll || [];
        const newAST = cloneAST(this)
        newAST[0] = nextAll[0];
        return newAST;
    }
    eq(index) {
        index = index || 0;
        const { nodePath, match } = this[index] || {}
        const newAST = cloneAST(this)
        newAST[0] = { nodePath, parseOptions: this.parseOptions, match }
        return newAST;
    }
    attr(arg1, arg2) {
        if (!this[0] || !this[0].nodePath || !this[0].nodePath.node) {
            return this;
        }
        let attrMap = {};
        if (arg2) {
            // arg1是key arg2是value
            if (typeof arg1 == 'string') {
                attrMap = { [arg1]: arg2 }
            } else {
                throw new Error('attr failed! args[0] should be string!')
            }
        } else {
            if (typeof arg1 == 'string') {
                // 取某个属性
                return getAttrValue(this[0].nodePath.node, arg1);
            } else Eif (typeof arg1 == 'object') {
                attrMap = arg1;
            }
        }
        setAttrValue(this[0].nodePath.node, attrMap);
        return this;
    }
    child(attrName) {
        Iif (!this[0] || !this[0].nodePath || !this[0].nodePath.node) {
            return this;
        }
        const keyList = attrName.split('.');
        let currentNode = this.node;
        let nodePath = this[0].nodePath;
        let parentPath = this[0].nodePath.parentPath;
        let newNodeAST;
        let deep = 0;
        if (this.node.program) {
            nodePath = nodePath.get('program', 'body', '0')
            currentNode = currentNode.program.body[0];
        }
        keyList.forEach(attr => {
            const node = currentNode[attr];
            Eif (node) {
                if (this.language == 'js') {
                    newNodeAST = cloneAST(this);
                    nodePath = nodePath.get(attr)
                    newNodeAST[0] = { nodePath, parseOptions: this.parseOptions }
                } else E{
                    newNodeAST = cloneAST(this);
                    parentPath = nodePath
                    nodePath = new NodePath(currentNode[attr], nodePath, nodePath)
                    newNodeAST[0] = { nodePath, parseOptions: this.parseOptions }
                }
                currentNode = node
                deep++
            }
        })
        if (deep == keyList.length) {
            return newNodeAST;
        } else E{
            return null
        }
    }
    clone() {
        if (!this[0]) {
            return this;
        }
        let nodePath;
        // html深度克隆时需要忽略parentRef
        if (this.isHtml) {
            const parentRefList = []
            markParent(this[0].nodePath.node)
            const newNode = JSON.parse(JSON.stringify(this[0].nodePath.node));
            resetParent(newNode);
            resetParent(this[0].nodePath.node);
            nodePath = new NodePath(
                newNode, 
                this[0].nodePath.parent, 
                this[0].nodePath.parentPath 
            )
            function resetParent(node) {
                for (let key in node) {
                    if (key == 'parentRef') {
                        node[key] = parentRefList[node[key]]
                    } else if (isObject(node[key])) {
                        if (Array.isArray(node[key])) {
                            node[key].forEach(n => {
                                resetParent(n)
                            });
                        } else {
                            resetParent(node[key]);
                        }
                    }
                }
            }
            function markParent(node) {
                for (let key in node) {
                    if (key == 'parentRef') {
                        parentRefList.push(node[key]);
                        node[key] = parentRefList.length - 1;
                    } else if (isObject(node[key])) {
                        if (Array.isArray(node[key])) {
                            node[key].forEach(n => {
                                markParent(n)
                            });
                        } else {
                            markParent(node[key]);
                        }
                    }
                }
            }
            
        } else {
            const node = {};
            // js需要做一层属性过滤,否则会有环形依赖
            filterProp(this[0].nodePath.node, node, [
                'computed',
                'range',
                'leadingComments',
                'shorthand',
                'extra',
                'static',
                'typeParameters',
                'tokens'
            ]);
            nodePath = new NodePath(
                // JSON.parse(JSON.stringify(this[0].nodePath.node)), 
                JSON.parse(JSON.stringify(node)), 
                this[0].nodePath.parent, 
                this[0].nodePath.parentPath
            )
        }
        const { match } = this[0]
        const newAST = cloneAST(this)
        newAST[0] = { nodePath, parseOptions: this.parseOptions, match }
        return newAST;
    }
    replace(selector, replacer, { ignoreSequence, parseOptions } = {}) {
        if (!this[0]) {
            // throw new Error('replace failed! Nodepath is null!');
            return this;
        }
        parseOptions = parseOptions || this.parseOptions
        this.core.replaceSelBySel(this[0].nodePath, selector, replacer, ignoreSequence === false, parseOptions, this.expando)
        return this;
    }
    replaceBy(replacer) {
        if (!this[0]) {
            return this.root();
        }
        if (replacer[0] && replacer[0].nodePath) {
            replacer = replacer[0].nodePath.node
        }
        if (typeof replacer == 'string') {
            replacer = this.core.buildAstByAstStr(replacer);
        }
        if (replacer.type == 'File') {
            replacer = replacer.program.body[0]
        }
        let i = 0;
        while(this[i]) {
            this.core.replaceAstByAst(this[i].nodePath, replacer, this._index)
            i++
        }
        
        return this;
    }
    insertSiblingNode(node, type) {
        if (!this[0]) {
            return this;
        }
        if (!node.type && !node.nodeType) {
            throw new Error('insert failed! Unexpected node for insert!')
        }
 
        if (node.type && node.type.match('Comment')) {
            // 处理注释
            let targetNode = this;
            // if (this.node.type == 'File') {
            //     targetNode = $(this.node.program.body)
            // }
            node.trailing = type == 'after';
            node.leading = type == 'before';
            this.insertChildNode(
                'comments', 
                node, 
                type == 'after' ? 'append' : 'prepend')
            return;
        }
        // if (!this[0].parentList) {
        initParent(this)
        // }
        if (this.isHtml) {
            let p;
            let index = -1
            if (this.node.nodeType == 'document') {
                p = this.node.content.children;
                index = type == 'before' ? 0 : p.length - 1
            } else {
                const parent = this.parent();
                // todotodo
                p = parent.attr('content.children') || [];
                p.forEach((item, i) => {
                    if (item == this.node) {
                        index = i
                    }
                })
            }
            if (type == 'before') {
                p.splice(index, 0, node)
            } else {
                p.splice(index + 1, 0, node)
            }
        } else {
            const parentList = this[0].parentList;
            if ((!parentList || parentList.length == 0) && this.node.type == 'File') {
                if (type == 'before') {
                    this.attr('program.body').unshift(node)
                } else {
                    this.attr('program.body').push(node)
                }
                return;
            }
            let getArrayParent = false;
            let i = 0;
            let selfPathNode = this[0].nodePath.value;
            let selfIndex = -1;
            while(!getArrayParent) {
                Iif (!parentList[i] || !parentList[i].value) {
                    getArrayParent = true;
                } else if (Array.isArray(parentList[i].value)) {
                    getArrayParent = true;
                    parentList[i].value.forEach((nodePath, index) => {
                        Eif (nodePath == selfPathNode) {
                            selfIndex = index;
                        }
                    })
                    if (type == 'after') {
                        parentList[i].value.splice(selfIndex + 1, 0, node)
                    } else E{
                        parentList[i].value.splice(selfIndex, 0, node)
                    }
                }
                selfPathNode = parentList[i].value;
                i++
            }
        }
    }
    after(node) {
        if (!node) {
            throw new Error('after failed! Unexpected node for insert!')
        }
        if (typeof node == 'string') {
            node = this.core.buildAstByAstStr(node)
        }
        if (node[0] && node[0].nodePath) {
            node = node[0].nodePath.value
        }
        if (node.type == 'File') {
            if (node.program.body.length > 0) {
                node.program.body.forEach(item => {
                    this.insertSiblingNode(item, 'after');
                })
                return this;
            } else E{
                return this;
            }
        }
        Eif (!Array.isArray(node)) {
            node = [node]
        }
        node.forEach(n => {
            this.insertSiblingNode(n, 'after');
        })
        return this;
    }
    before(node) {
        if (!node) {
            throw new Error('before failed! Unexpected node for insert!')
        }
        if (typeof node == 'string') {
            node = this.core.buildAstByAstStr(node)
        }
        if (node[0] && node[0].type == 'Decorator') {
            this.node.decorators = (this.node.decorators || []).concat(node);
            return this;
        }
        if (node[0] && node[0].nodePath) {
            node = node[0].nodePath.value
        }
        if (node.type == 'File') {
            if (node.program.body.length > 0) {
                node.program.body.reverse().forEach(item => {
                    this.insertSiblingNode(item, 'before');
                })
                return this;
            } else E{
                return this;
            }
        }
        if (!Array.isArray(node)) {
            node = [node]
        }
        node.reverse().forEach(n => {
            this.insertSiblingNode(n, 'before');
        })
        return this;
    }
    insertChildNode(attr, node, type) {
        if (!this[0] || !this[0].nodePath) {
            return;
        }
        let selfNode = this[0].nodePath.value;
        let bodyIndex = selfNode.program && type == 'append' ? selfNode.program.body.length - 1 : 0
        Eif (!Array.isArray(selfNode)) {
            // for(let key in selfNode) {
            //     if (Array.isArray(selfNode[key])) {
            //         selfNode = selfNode[key]
            //     }
            // }
            if (attr == 'content.children') {
                selfNode.content.children = selfNode.content.children || [];
                selfNode = selfNode.content.children;
            } else if (selfNode.program && selfNode.program.body) {
                if (attr == 'program.body') {
                    selfNode = selfNode.program.body;
                } else {
                    selfNode.program.body[bodyIndex][attr] = selfNode.program.body[bodyIndex][attr] || []
                    selfNode = selfNode.program.body[bodyIndex][attr]
                }
            } else {
                selfNode[attr] = selfNode[attr] || [];
                selfNode = selfNode[attr];
                if (!Array.isArray(selfNode)) {
                    selfNode = selfNode.body
                }
            }
            
        }
 
        if (node.type == 'File' && node.program.body) {
            node = node.program.body[bodyIndex]
            Iif (!node) return;
        }
        Eif (selfNode) {
            if (type == 'append') {
                selfNode.push(node);
            } else {
                selfNode.unshift(node);
            }
        }
    }
    append(attr, node) {
        if (!attr) {
            return this;
        }
        if (this.isHtml) {
            node = attr;
            attr = 'content.children';
        }
        if (!node) {
            node = attr;
            attr = 'program.body'
        }
        if (typeof node == 'string') {
            node = this.core.buildAstByAstStr(node)
        }
        if (node[0] && node[0].nodePath) {
            node = node[0].nodePath.value
        }
        if (!Array.isArray(node)) {
            node = [node]
        }
        node.forEach(n => {
            this.insertChildNode(attr, n, 'append');
        })
        return this;
 
    }
    prepend(attr, node) {
        if (this.isHtml) {
            node = attr;
            attr = 'content.children';
        }
        if (!node) {
            node = attr;
            attr = 'program.body'
        }
        if (typeof node == 'string') {
            node = this.core.buildAstByAstStr(node)
        }
        if (node[0] && node[0].nodePath) {
            node = node[0].nodePath.value
        }
        Eif (!Array.isArray(node)) {
            node = [node]
        }
        node.reverse().forEach(n => {
            this.insertChildNode(attr, n, 'prepend');
        })
        return this;
    }
    empty() {
        this.each(item => {
            if (item.language == 'html') {
                Eif (Array.isArray(item.attr('content.children'))) {
                    item.attr('content.children', []);
                }
            } else Eif (item.language == 'js') {
                Iif (Array.isArray(item[0].nodePath.value)) {
                    item[0].nodePath.value = [];
                } else if (item.node.type == 'File') {
                    item.attr('program.body', [])
                }
            }
        })
        return this
    }
    remove(selector, options = {}) {
        if (!this[0]) {
            return this.root()
        }
        if (typeof selector == 'string' || Array.isArray(selector)) {
            const pOptions = options.parseOptions || this.parseOptions;
            let i = 0;
            while(this[i]) {
                this.core.removeAst(this.node, selector, { 
                    strictSequence: options.ignoreSequence === false, 
                    parseOptions: pOptions, 
                    expando: this.expando
                });
                i++
            }
        } else {
            let i = 0;
            while(this[i]) {
                this.core.remove(this[i].nodePath)
                i++
            }
            
        }
        return this.root()
    }
    generate({ isPretty = false } = {}) {
        if (!this[0]) {
            return '';
        }
        if (this.language == 'js') {
            return generate(this[0].nodePath.node, isPretty)
        } else {
            return (languageMap[this.language].generate)(this[0].nodePath.value);
        }
    }
}
 
function cloneAST(ast) {
    const newAST = new AST('', { parseOptions: ast.parseOptions, rootNode: ast.rootNode})
    Iif (ast.sfc) {
        newAST.sfc = ast.sfc
    }
    return newAST
}
 
function getAttrValue(node, attr) {
    const keyList = attr.split('.');
    let currentNode = node;
    let deep = 0;
    keyList.forEach(attr => {
        if (currentNode[attr]) {
            currentNode = currentNode[attr];
            deep++
        }
    })
    if (deep == keyList.length) {
        return currentNode;
    } else {
        return null
    }
}
 
function initParent(ast) {
    if (ast.isHtml) {
        ast[0].parentList =  ast.core.getParentListByAst(ast[0].nodePath)
        ast[0]._index = ast[0].parentList[0] ? ast[0].parentList[0].node.content.children.indexOf(ast[0].nodePath.node) : 0;
    } else {
        ast[0].parentList =  core.getParentListByAst(ast[0].nodePath)
    }
    return ast[0].parentList
}
 
function initSiblings(ast) {
    if (ast.language == 'html') {
        const parent = ast.parent();
        const siblings = (parent.attr('content.children') || []).map((node, index) => {
            return {
                _index: index,
                nodePath: new NodePath(node, parent[0].nodePath, parent[0].nodePath),
                parseOptions: ast.parseOptions
            }
        });
        
        ast[0].siblings = siblings;
        ast[0].prevAll = siblings.filter(s => s._index < ast._index);
        ast[0].nextAll = siblings.filter(s => s._index > ast._index);
    } else {
        const parentList = initParent(ast);
        if (!parentList || parentList.length == 0) {
            return;
        }
        const parseOptions = ast.parseOptions;
        let getArrayParent = false;
        let i = 0;
        const siblings = [];
        const prevAll = [];
        const nextAll = [];
        let selfPathNode = ast[0].nodePath.value;
        while(!getArrayParent) {
            Iif (!parentList[i] || !parentList[i].value) {
                getArrayParent = true;
            } else if (Array.isArray(parentList[i].value)) {
                getArrayParent = true;
                let isPrev = true;
                let childIndex = 0
                while (parentList[i].__childCache[childIndex]) {
                    const nodePath = parentList[i].__childCache[childIndex]
                    if (nodePath.value == selfPathNode) {
                        // find self
                        isPrev = false;
                    } else {
                        siblings.push({ nodePath, parseOptions })
                        if (isPrev) {
                            prevAll.push({ nodePath, parseOptions })
                        } else {
                            nextAll.push({ nodePath, parseOptions })
                        }
                    }
                    childIndex++
                }
                ast[0].siblings = siblings;
                ast[0].prevAll = prevAll;
                ast[0].nextAll = nextAll;
            }
            selfPathNode = parentList[i].value;
            i++
        }
    }   
}
function setAttrValue(node, attrMap) {
    for(const key in attrMap) {
        const value = attrMap[key];
        const keyList = key.split('.');
        let currentNode = node;
        keyList.forEach((attr, index) => {
            if (index == keyList.length - 1) {
                currentNode[attr] = value;
            } else Eif (currentNode[attr]) {
                currentNode = currentNode[attr]
            }
        })
    }
    
}
module.exports = AST;