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

95.9% Statements 117/122
95.83% Branches 115/120
100% Functions 10/10
96.69% Lines 117/121

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

    37x
    37x
    37x
    37x
    37x
    37x

    37x

    4889x
    8205x
    8205x
    15x

    8190x

    164x
    8026x
    3365x
    3365x
    3365x

    695x

    3365x
    568x
    551x
    492x
    492x
    9x

    483x

    642x

    118x
    118x

    642x

    334x

    483x

    17x

    2797x

    2797x
    2797x
    206x

    43x

    2x
    2x
    2x
    2x

    41x

    2795x

    3363x

    4661x
    142x

    4519x
    613x
    613x

    613x
    613x

    613x

    340x
    340x

    10x
    10x

    121x
    121x
    121x

    40x
    40x

    11x
    11x

    91x
    91x

    3x
    2x

    1x
    1x

    613x
    613x
    3906x

    3906x

    85x
    85x

    3906x

    695x
    695x
    623x
    1580x
    1580x
    111x
    111x

    111x

    695x
    588x

    107x
    107x
    171x
    107x

    64x
    64x
    108x
    33x

    75x

    107x
    107x

    373x
    373x
    373x
    373x
    373x

    1344x

    1344x

    1344x
    474x
    474x

    1344x

    1x

    4x

    1337x
    1337x

    2x

    10x

    373x

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

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

    let Expando = 'g123o456g789o';

    function checkIsMatch(full, partial, extraData, strictSequence) {
        return Object.keys(partial).every((prop) => {
            const { specific, result } = handleSpecific({ full, partial, prop, extraData, Expando });
            if (specific) {
                return result;
            }
            if (!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 && !Array.isArray(full)) {
                            if (partial[prop] && typeof partial[prop].name == 'string' &&
                                (partial[prop].name.match(Expando) || partial[prop].name.match(new RegExp(Expando.slice(0, -1) + '\\$3')))
                            ) {
                                if (full.type == 'VariableDeclarator' && prop == 'init' && partial[prop].name.match(Expando)) {
                                    // var $_$ = $_$匹配 var a这种;
                                    const expandoKey = partial[prop].name.replace(Expando, '') || '0';
                                    extraData[expandoKey] = extraData[expandoKey] || [];
                                    extraData[expandoKey].push({ node: null, value: null })
                                    return true;
                                } else {
                                    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)) {
                    Iif (!full) return;
                    let extra = {
                        node: full
                    };
                    const expandoKey = partial[prop].replace(Expando, '') || '0';
                    extraData[expandoKey] = extraData[expandoKey] || [];

                    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 if (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 = {};

                isMatch = checkIsMatch(
                    path.value,
                    structure,
                    extraData,
                    strictSequence
                );
                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 };