| 244 | |
| 245 | /** Eine Datei lesen und die sichtbaren hartkodierten Strings darin melden. */ |
| 246 | export function scanFile(relativePath, root = ROOT) { |
| 247 | const absolute = join(root, relativePath); |
| 248 | const source = readFileSync(absolute, 'utf8'); |
| 249 | const file = ts.createSourceFile(absolute, source, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX); |
| 250 | const findings = []; |
| 251 | |
| 252 | const report = (rule, text, pos) => { |
| 253 | const { line, character } = file.getLineAndCharacterOfPosition(pos); |
| 254 | findings.push({ |
| 255 | file: relative(root, absolute), |
| 256 | line: line + 1, |
| 257 | column: character + 1, |
| 258 | rule, |
| 259 | text: text.replace(/\s+/g, ' ').trim().slice(0, 120), |
| 260 | }); |
| 261 | }; |
| 262 | |
| 263 | const visit = (node) => { |
| 264 | // 1. JSX-Textknoten. |
| 265 | if (ts.isJsxText(node) && isVisibleLiteral(node.text)) { |
| 266 | report('jsx-text', node.text, node.getStart()); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * 2. Literale in einer JSX-Klammer, auch durch eine Bedingung hindurch. |
| 271 | * |
| 272 | * Ohne das Hindurchgehen waere `{last ? 'finish' : 'next'}` keine |
| 273 | * Fundstelle, und das ist die haeufigste Form, in der ein Wort in |
| 274 | * dieser Oberflaeche auf den Bildschirm kommt. |
| 275 | */ |
| 276 | if (ts.isJsxExpression(node) && node.expression !== undefined |
| 277 | && (node.parent === undefined || !ts.isJsxAttribute(node.parent))) { |
| 278 | for (const found of literalsOf(node.expression)) { |
| 279 | if (isVisibleLiteral(found.text)) { |
| 280 | report('jsx-expression', found.text, found.pos); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // 3. Sichtbare Attribute. |
| 286 | if (ts.isJsxAttribute(node)) { |
| 287 | const attribute = nameOf(node.name); |
| 288 | if (VISIBLE_ATTRIBUTES.has(attribute) && node.initializer !== undefined) { |
| 289 | const expression = ts.isJsxExpression(node.initializer) |
| 290 | ? node.initializer.expression |
| 291 | : node.initializer; |
| 292 | for (const found of literalsOf(expression)) { |
| 293 | if (isVisibleLiteral(found.text)) { |
| 294 | report(`attribute:${attribute}`, found.text, found.pos); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // 4. Beschriftende Eigenschaften eines Objektliterals. |
| 301 | if (ts.isPropertyAssignment(node)) { |
| 302 | const property = nameOf(node.name); |
| 303 | if (VISIBLE_PROPERTIES.has(property)) { |