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 | 33x 33x 33x 33x 33x 33x 2887x 4503x 25x 25x 17x 17x 9x 9x 9x 6x 6x 4497x 172x 4325x 2126x 2126x 339x 2126x 339x 328x 296x 296x 340x 43x 43x 340x 184x 296x 11x 1787x 1787x 1787x 172x 1787x 2126x 2199x 18x 2181x 335x 335x 335x 335x 335x 196x 196x 61x 61x 61x 25x 25x 53x 53x 335x 335x 1846x 1846x 25x 25x 1846x 339x 339x 309x 846x 846x 6x 6x 6x 339x 333x 6x 6x 8x 6x 2x 2x 8x 2x 6x 6x 140x 140x 140x 140x 140x 754x 754x 752x 2x 754x 185x 185x 754x 1x 2x 749x 749x 2x 140x 33x | 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')
let Expando = 'g123o456g789o';
function checkIsMatch(full, partial, extraData, strictSequence) {
return Object.keys(partial).every((prop) => {
if (prop == 'body') {
// 匹配一块代码
try {
const bodyContent = partial.body[0] || partial.body.body[0];
Eif (bodyContent && bodyContent.expression.name && bodyContent.expression.name.match) {
if (bodyContent.expression.name.match(Expando)) {
const expandoKey = bodyContent.expression.name.replace(Expando, '') || '0';
extraData[expandoKey] = extraData[expandoKey] || [];
// 去掉首尾花括号
const bodyStr = generate(full.body).slice(1, -2);
extraData[expandoKey].push({ node: full.body, value: bodyStr });
return true;
}
}
} catch (e) {
// console.log(e)
}
}
if (!full || !partial) {
// full没有
return false;
} else if (isObject(partial[prop])) {
let res = false;
if (Array.isArray(partial[prop])) {
// 处理$$$这种情况
find$$$(partial[prop], full[prop], extraData, strictSequence);
}
if (Array.isArray(partial[prop]) && !strictSequence) {
if (hasOwn(full, prop)) {
res = partial[prop].every((p) => {
let a = false;
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) {
Iif (partial[prop] && partial[prop].name && partial[prop].name.match(Expando)) {
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;
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;
// partial.splice(i, 1);
// // 存疑🤨
break;
}
}
})
if (!key$$$) {
return;
}
const extraNodeList = 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);
}
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;
}
}
});
return { nodePathList, matchWildCardList };
}
module.exports = { find, visit };
|