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 | 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 532x 3223x 3223x 3223x 3223x 3223x 3223x 1x 1x 1x 1x 1x 1x 3223x 3223x 532x 532x 532x 532x 532x 532x 532x 3223x 3223x 3223x 3223x 3223x 12317x 12317x 12317x 12317x 12317x 12317x 12317x 124x 124x 461x 461x 461x 124x 124x 124x 12193x 12297x 19x 19x 104x 104x 104x 19x 19x 19x 12174x 12297x 5x 5x 17x 17x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 17x 17x 5x 5x 5x 12169x 12169x 12169x 12169x 12297x 4442x 4442x 4442x 4442x 4442x 2256x 2256x 4442x 1129x 4432x 4442x 4442x 4442x 2252x 2252x 4442x 4442x 10x 8x 10x 2x 2x 10x 4442x 4442x 3305x 3305x 4442x 4442x 4442x 4442x 7727x 12285x 97x 42x 42x 42x 55x 55x 7685x 12317x 575x 575x 7685x 7685x 7685x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x 3223x | /**
* @file identifiers.js
* @description Finds the root identifiers a template expression reads.
*
* Shared by template validation, which reports a root identifier the component
* does not declare, and by dependency extraction, which records the state a
* template's rendering depends on.
*/
/**
* Names that are never reported as undeclared: literals, keywords, the
* globals the expression language exposes, and the names every component's
* scope provides.
* @type {Set<string>}
*/
export const EXCLUDED_IDENTIFIERS = new Set([
'true',
'false',
'null',
'undefined',
'NaN',
'Infinity',
'this',
'let',
'const',
'var',
'function',
'return',
'if',
'else',
'for',
'in',
'of',
'new',
'typeof',
'instanceof',
'class',
'extends',
'try',
'catch',
'finally',
'throw',
'await',
'async',
'import',
'export',
'default',
'delete',
'void',
'do',
'while',
'switch',
'case',
'break',
'continue',
'debugger',
'yield',
'with',
'arguments',
'console',
'Math',
'JSON',
'window',
'document',
'Array',
'Object',
'String',
'Number',
'Boolean',
'Date',
'Error',
'Map',
'Set',
'Promise',
'props',
'state',
'styles',
'event',
'args',
]);
/**
* The parameter names bound by arrow functions in an expression.
*
* Covers `x => …` and `(x, y) => …`, which is the whole of what the template
* expression language admits -- a destructuring parameter is not an expression
* and is refused before it reaches here.
* @param {string} code - The expression source.
* @returns {Set<string>} The bound names.
*/
function arrowParameters(code) {
const names = new Set();
const arrow = /(?:\(([^)]*)\)|([A-Za-z_$][\w$]*))\s*=>/g;
let match;
while ((match = arrow.exec(code)) !== null) {
const list = match[1] !== undefined ? match[1] : match[2];
for (const part of list.split(',')) {
const name = part.trim();
if (/^[A-Za-z_$][\w$]*$/.test(name)) names.add(name);
}
}
return names;
}
/**
* Extracts root variable and method identifiers from a JS expression string.
* @param {string} code - The Javascript expression/statement.
* @returns {string[]} The list of root identifiers.
*/
export function extractRootIdentifiers(code) {
const identifiers = new Set();
let i = 0;
let hasQuestionMark = false;
while (i < code.length) {
const char = code[i];
if (char === '/' && code[i + 1] === '/') {
i += 2;
while (i < code.length && code[i] !== '\n') i++;
continue;
}
if (char === '/' && code[i + 1] === '*') {
i += 2;
while (i < code.length && !(code[i] === '*' && code[i + 1] === '/')) i++;
i += 2;
continue;
}
if (char === "'") {
i++;
while (i < code.length && code[i] !== "'") {
if (code[i] === '\\') i++;
i++;
}
i++;
continue;
}
if (char === '"') {
i++;
while (i < code.length && code[i] !== '"') {
if (code[i] === '\\') i++;
i++;
}
i++;
continue;
}
if (char === '`') {
i++;
while (i < code.length && code[i] !== '`') {
if (code[i] === '\\') i++;
if (code[i] === '$' && code[i + 1] === '{') {
let depth = 1;
let j = i + 2;
while (j < code.length && depth > 0) {
if (code[j] === '{') depth++;
else if (code[j] === '}') depth--;
j++;
}
const subExpr = code.substring(i + 2, j - 1);
extractRootIdentifiers(subExpr).forEach((id) => identifiers.add(id));
i = j - 1;
}
i++;
}
i++;
continue;
}
const idRegex = /^[A-Za-z_$][\w$]*/;
const sub = code.substring(i);
const match = sub.match(idRegex);
if (match) {
const name = match[0];
let isProperty = false;
let checkIdx = i - 1;
while (checkIdx >= 0 && /\s/.test(code[checkIdx])) {
checkIdx--;
}
if (checkIdx >= 0 && code[checkIdx] === '.') {
isProperty = true;
} else if (checkIdx >= 1 && code[checkIdx] === '.' && code[checkIdx - 1] === '?') {
isProperty = true;
}
let nextIdx = i + name.length;
while (nextIdx < code.length && /\s/.test(code[nextIdx])) {
nextIdx++;
}
let isObjectKey = false;
if (nextIdx < code.length && code[nextIdx] === ':') {
if (!hasQuestionMark) {
isObjectKey = true;
} else {
hasQuestionMark = false;
}
}
if (!isProperty && !isObjectKey) {
identifiers.add(name);
}
i += name.length;
continue;
}
if (char === '?') {
if (code[i + 1] === '.') {
i += 2;
continue;
}
hasQuestionMark = true;
}
if (char === ';' || char === ',' || char === '{' || char === '(' || char === '[') {
hasQuestionMark = false;
}
i++;
}
// An arrow function's parameters are bound by the arrow, not by the
// component. `rows.filter(r => r.done)` declares `r`, and reporting it as
// undeclared is a warning on correct code -- which is worse than no
// warning, because it teaches people to ignore the category.
//
// Previously invisible: a `<@for>` header containing an arrow never reached
// this function intact, because the header was truncated at the arrow
// before it got here.
for (const name of arrowParameters(code)) {
identifiers.delete(name);
}
return Array.from(identifiers);
}
|