* Get the string value of a JSX attribute (extracts from t() calls) * Used for 'name' attribute where we want just the identifier
(
node: ts.JsxElement | ts.JsxSelfClosingElement,
attributeName: string
)
| 346 | * Used for 'name' attribute where we want just the identifier |
| 347 | */ |
| 348 | private getJsxAttribute( |
| 349 | node: ts.JsxElement | ts.JsxSelfClosingElement, |
| 350 | attributeName: string |
| 351 | ): string | null { |
| 352 | const attributes = ts.isJsxElement(node) |
| 353 | ? node.openingElement.attributes |
| 354 | : node.attributes; |
| 355 | |
| 356 | for (const attr of attributes.properties) { |
| 357 | if ( |
| 358 | ts.isJsxAttribute(attr) && |
| 359 | ts.isIdentifier(attr.name) && |
| 360 | attr.name.text === attributeName |
| 361 | ) { |
| 362 | if (attr.initializer) { |
| 363 | // String literal: name="firstName" |
| 364 | if (ts.isStringLiteral(attr.initializer)) { |
| 365 | return attr.initializer.text; |
| 366 | } |
| 367 | // JSX expression: name={someName} or name={t('Label')} |
| 368 | if (ts.isJsxExpression(attr.initializer) && attr.initializer.expression) { |
| 369 | const expr = attr.initializer.expression; |
| 370 | // String literal: name={'firstName'} |
| 371 | if (ts.isStringLiteral(expr)) { |
| 372 | return expr.text; |
| 373 | } |
| 374 | // t() call: label={t('Name')} |
| 375 | if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression)) { |
| 376 | if (expr.expression.text === 't' && expr.arguments.length > 0) { |
| 377 | const firstArg = expr.arguments[0]; |
| 378 | if (firstArg && ts.isStringLiteral(firstArg)) { |
| 379 | return firstArg.text; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } else { |
| 385 | // Boolean shorthand: required (no value) |
| 386 | return ''; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return null; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Get the expression text of a JSX attribute as-is (preserves t() calls) |
no outgoing calls
no test coverage detected