# All files / gogocode-core/src/js-core filter-prop.js

100% Statements 21/21
94.12% Branches 16/17
100% Functions 2/2
100% Lines 21/21

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

    37x

    37x

    37x
    1928x
    1928x

    13384x
    5029x
    1646x
    520x
    520x
    426x
    426x

    1126x
    1126x

    3383x
    12x
    12x

    12x

    3371x

    37xconst { isObject } = require('../util')

    // 查找时 ast 无用属性，这些属性的保留（如行数信息）会干扰节点的匹配，所以需要过滤掉
    const Props = [
        'computed',
        'range',
        'loc',
        'type',
        'raw',
        'start',
        'end',
        'shorthand',
        'extra',
        'static',
        'trailing',
        'leading',
        'method'
    ]

    const ignoreTypeList = [
        'Super',
        'Import',
        'ImportSpecifier', 'ImportDefaultSpecifier', 'ImportNamespaceSpecifier',
        'exportSpecifier', 'exportDefaultSpecifier', 'exportNamespaceSpecifier',
        'ThisExpression'
    ];
    const filterProps = function (node, structure, propList, expando) {
        const props = propList || Props;
        for (const key in node) {
            // 过滤值为空的字段
            if ((key === 'type' && ignoreTypeList.indexOf(node[key]) > -1) || (props.indexOf(key) == -1 && (node[key] !== undefined && node[key] !== null))) {
                if (isObject(node[key])) {
                    if (Array.isArray(node[key])) {
                        structure[key] = [];
                        node[key].forEach((n, i) => {
                            structure[key][i] = {};
                            filterProps(n, structure[key][i], props);
                        });
                    } else {
                        structure[key] = {};
                        filterProps(node[key], structure[key], props);
                    }
                } else if (expando) {
                    Eif (typeof node[key] == 'string') {
                        node[key] = node[key].replace(/\$_\$/g, expando)
                            .replace(/\$\$\$/g, expando.slice(0, -1) + '$3')
                            .replace(/\/\$_\/\$/g, '$_$')
                            .replace(/\/\$\/\$\/\$/g, '$$$$$$')
                        structure[key] = node[key]
                    }
                }
                else {
                    structure[key] = node[key];
                }
            }
        }
    }

    module.exports = filterProps;