| 231 | * Eine Datei lesen: die Fundstellen der Muster und die stummen Flaechen. |
| 232 | */ |
| 233 | export function scanFile(absolute) { |
| 234 | const source = readFileSync(absolute, 'utf8'); |
| 235 | const file = ts.createSourceFile(absolute, source, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX); |
| 236 | const relativePath = relative(ROOT, absolute); |
| 237 | const promises = []; |
| 238 | const dead = []; |
| 239 | |
| 240 | const at = (pos) => file.getLineAndCharacterOfPosition(pos).line + 1; |
| 241 | |
| 242 | // ---------------------------------------------------- (1) die Saetze |
| 243 | const lines = source.split('\n'); |
| 244 | for (const spec of PROMISE_PATTERNS.filter((entry) => entry.scope === 'text')) { |
| 245 | lines.forEach((line, index) => { |
| 246 | if (spec.pattern.test(line)) { |
| 247 | promises.push({ |
| 248 | file: relativePath, |
| 249 | line: index + 1, |
| 250 | pattern: spec.name, |
| 251 | scope: spec.scope, |
| 252 | text: line.trim().slice(0, 140), |
| 253 | }); |
| 254 | } |
| 255 | }); |
| 256 | } |
| 257 | |
| 258 | const visiblePatterns = PROMISE_PATTERNS.filter((entry) => entry.scope === 'visible'); |
| 259 | |
| 260 | const checkVisible = (text, pos) => { |
| 261 | for (const spec of visiblePatterns) { |
| 262 | if (spec.pattern.test(text)) { |
| 263 | promises.push({ |
| 264 | file: relativePath, |
| 265 | line: at(pos), |
| 266 | pattern: spec.name, |
| 267 | scope: spec.scope, |
| 268 | text: text.replace(/\s+/g, ' ').trim().slice(0, 140), |
| 269 | }); |
| 270 | } |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | // ------------------------------------------- (2) der Baum, fuer beides |
| 275 | const visit = (node) => { |
| 276 | if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) { |
| 277 | checkVisible(node.text, node.getStart()); |
| 278 | } |
| 279 | if (ts.isTemplateHead(node) || ts.isTemplateMiddle(node) || ts.isTemplateTail(node)) { |
| 280 | checkVisible(node.text, node.getStart()); |
| 281 | } |
| 282 | if (ts.isJsxText(node)) { |
| 283 | checkVisible(node.text, node.getStart()); |
| 284 | } |
| 285 | |
| 286 | const opening = ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node) ? node : undefined; |
| 287 | if (opening !== undefined) { |
| 288 | const tag = tagNameOf(opening); |
| 289 | const attributes = opening.attributes.properties; |
| 290 | const named = attributes.filter((entry) => ts.isJsxAttribute(entry)); |