# All files / gogocode-core/src/js-core find.js

93.28% Statements 125/134
93.01% Branches 133/143
100% Functions 10/10
93.98% Lines 125/133

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
    24835x

    35x
    35x
    35x
    35x
    35x

    35x

    6437x
    7887x
    4189x

    7662x

    114x
    114x
    114x
    58x
    56x
    52x

    86x
    86x
    65x

    86x
    57x
    19x
    19x

    19x
    11x
    11x

    7651x

    7651x
    4542x
    4542x
    4542x

    879x

    4542x
    728x
    708x
    663x
    663x
    14x

    649x

    867x

    161x
    161x

    867x

    419x

    649x

    20x

    3814x

    3814x
    3814x
    344x

    54x

    3814x

    225x

    4542x

    3109x
    208x

    2901x
    734x

    734x
    734x
    734x

    734x

    413x
    413x

    20x
    20x

    143x
    143x
    143x

    45x
    45x

    11x
    11x

    102x
    102x

    734x
    734x
    2167x

    2167x

    13x
    13x

    2167x

    879x
    879x
    847x
    1455x
    1455x
    173x
    173x

    173x

    879x
    710x

    169x
    169x
    285x
    169x

    116x
    116x
    194x
    55x

    139x

    169x
    169x

    338x
    338x
    338x
    338x
    338x

    1564x
    1564x
    1562x

    2x

    1564x
    519x
    519x

    1564x

    1x

    2x

    1559x
    1559x

    2x

    12x

    338x

    35x
     const { isObject, hasOwn } = require('../util')
    // 通过简单ast结构查找ast节点

    const recast = require('recast');
    const visit = recast.types.visit;
    const filterProps = require('./filter-prop.js');
    const generate = require('./generate')
    const strictSequenceAttrList = ['arguments', 'params'];

    let Expando = 'g123o456g789o';

    function checkIsMatch(full, partial, extraData, strictSequence) {
        return Object.keys(partial).every((prop) => {
            if (full[prop] && isObject(full[prop])) {
                full[prop]._parentNode = full;
            }
            if (prop == 'body') {
                // 匹配一块代码
                try {
                    let bodyContent = partial.body;
                    if (Array.isArray(partial.body)) {
                        bodyContent = partial.body[0] || partial.body.body[0];
                    } else if (partial.body && partial.body.body) {
                        bodyContent = partial.body.body[0]
                    }
                    let name = ''
                    if (bodyContent) {
                        name = bodyContent.expression ? bodyContent.expression.name : bodyContent.name ? bodyContent.name : ''
                    }
                    if (name && name.match) {
                        if (name.match(Expando)) {
                            const expandoKey = name.replace(Expando, '') || '0';
                            extraData[expandoKey] = extraData[expandoKey] || [];
                            // 去掉首尾花括号
                            const bodyStr = generate(full.body) ? generate(full.body).slice(1, -2) : '';
                            extraData[expandoKey].push({ node: full.body, value: bodyStr });
                            return true;
                        }
                    }
                } catch (e) {
                    // console.log(e)
                }
            }
            Iif (!full || !partial) {
                // full没有
                return false;
            } else if (isObject(partial[prop])) {
                let res = false;
                let has$$$ = false;
                if (Array.isArray(partial[prop])) {
                    // 处理$$$这种情况
                    has$$$ = find$$$(partial[prop], full[prop], extraData, strictSequence);
                }
                if (Array.isArray(partial[prop]) && !strictSequence && strictSequenceAttrList.indexOf(prop) == -1) {
                    if (hasOwn(full, prop)) {
                        res = partial[prop].every((p) => {
                            let a = false;
                            if (!full[prop].length && partial[prop].length == 1 && has$$$) {
                                return true
                            }
                            full[prop] &&
                                full[prop].forEach((f) => {
                                    if (f && f.type == 'ObjectProperty') {
                                        // 兼容 { a: 1 } 匹配 { 'a': 1 } 这种情况
                                        f.key.name && (f.key.value = f.key.name);
                                        f.key.value && (f.key.name = f.key.value);
                                    }
                                    if (
                                        checkIsMatch(
                                            f,
                                            p,
                                            extraData,
                                            strictSequence
                                        )
                                    ) {
                                        a = true;
                                    }
                                });
                            return a;
                        });
                    } else {
                        res = false;
                    }
                } else {
                    try {
                        // 兼容某些情况例如 使用{ $_$: $_$ }匹配{ a() {} }
                        let fullProp = full[prop];
                        if (!fullProp) {
                            if (partial[prop] && typeof partial[prop].name == 'string' &&
                                (partial[prop].name.match(Expando) || partial[prop].name.match(new RegExp(Expando.slice(0, -1) + '\\$3')))
                            ) {
                                fullProp = full;
                            }
                        }
                        res =
                            // hasOwn(full, prop)
                            // &&
                            checkIsMatch(
                                fullProp,
                                partial[prop],
                                extraData,
                                strictSequence
                            );
                    } catch (e) {
                        console.log(e);
                    }
                }
                return res;
            } else {
                if (partial[prop].match && partial[prop].match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                    return true;
                }
                if (partial[prop].match && partial[prop].match(Expando)) {
                    let extra = {
                        node: full
                    };
                    const expandoKey = partial[prop].replace(Expando, '') || '0';
                    extraData[expandoKey] = extraData[expandoKey] || [];
                    Iif (!full) return;

                    switch (full.type) {
                    case 'Identifier':
                        extra.value = full.name;
                        break;
                    case 'ThisExpression':
                        extra.value = 'this';
                        break;
                    case 'StringLiteral':
                        extra.raw = `'${full.value}'`;
                        extra.value = full.value
                        break;
                    case 'NumericLiteral':
                    case 'BooleanLiteral':
                        extra.value = full.value;
                        break;
                    case 'NullLiteral':
                        extra.value = null;
                        break;
                    case 'CommentLine':
                    case 'CommentBlock':
                        extra.value = full.value;
                        break;
                    default:
                        try {
                            extra.value = generate(full);
                        } catch(e) {
                            if (full[prop]) {
                                extra.value = full[prop];
                            } else {
                                extra.value = {};
                                filterProps(full, extra.value);
                            }
                        }
                    }
                    extraData[expandoKey].push(extra);
                    return true;
                } else Eif (partial[prop]) {
                    // const reg = /^(?:\$\[).*(?=\]\$)/;
                }
                if (full && full.type == 'ObjectProperty') {
                    // 兼容 { a: 1 } 匹配 { 'a': 1 } 这种情况
                    full.key.name && (full.key.value = full.key.name);
                    full.key.value && (full.key.name = full.key.value);
                }
                return full ? full[prop] == partial[prop] : false;
            }
        });
    }

    function find$$$(partial, full, extraData, strictSequence) {
        // 先考虑strctSequence = false的情况
        let key$$$;
        let index$$$ = -1;
        partial.forEach((p, i) => {
            for (const key in p) {
                const value = p[key] ? (p[key].name || p[key].value || p[key]) : null;
                if (value && value.match && value.match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                    key$$$ = value.replace(new RegExp(Expando.slice(0, -1) + '\\$3'), '') || '$'
                    index$$$ = i;

                    break;
                }
            }
        })
        if (!key$$$) {
            return false;
        }
        const extraNodeList = full ? full.slice(0) : [];
        partial.forEach((p, i) => {
            if (i == index$$$) {
                return;
            }
            let fi = 0;
            while(extraNodeList[fi]) {
                if (checkIsMatch(extraNodeList[fi], p, {}, strictSequence)) {
                    extraNodeList.splice(fi, 1);
                } else {
                    fi++;
                }
            }
        })
        extraData[`$$$${key$$$}`] = (extraData[`$$$${key$$$}`] || []).concat(extraNodeList);
        return true;
    }

    function find(nodeType, structure, strictSequence, deep = 'nn', expando = 'g123o456g789o') {
        const nodePathList = [];
        const matchWildCardList = [];
        let isMatch = false;
        Expando = expando
        visit(this, {
            [`visit${nodeType}`](path) {
                const extraData = {};
                if (deep != 'n' || path.parent.name == 'program') {
                    isMatch = checkIsMatch(
                        path.value,
                        structure,
                        extraData,
                        strictSequence
                    );
                } else {
                    isMatch = false;
                }
                if (isMatch) {
                    nodePathList.push(path);
                    matchWildCardList.push(extraData);
                }
                switch (deep) {
                case '1':
                    this.abort();
                    break;
                case 'n':
                    return false;
                case 'nn':
                    this.traverse(path);
                    break;
                default:
                    return false;
                }
            },
            visitComment() {
                return false;
            },
        });
        return { nodePathList, matchWildCardList };
    }
    module.exports = { find, visit };