* Extract the string value from an OXC string literal or template literal node. * * If `consts` is provided, interpolated template literals (e.g. * `\`hello ${NAME}\``) are resolved when every `${...}` expression is a bare * `Identifier` whose name is present in the map. This lets metadata field
(node: any, consts?: Map<string, string>)
| 37 | * Returns `null` if the node is not statically resolvable. |
| 38 | */ |
| 39 | function stringValue(node: any, consts?: Map<string, string>): string | null { |
| 40 | if (node?.type === 'StringLiteral') return node.value; |
| 41 | if (node?.type === 'Literal' && typeof node.value === 'string') |
| 42 | return node.value; |
| 43 | if (node?.type === 'TemplateLiteral') { |
| 44 | const quasis = node.quasis ?? []; |
| 45 | const expressions = node.expressions ?? []; |
| 46 | if (expressions.length === 0) { |
| 47 | return quasis[0]?.value?.cooked ?? null; |
| 48 | } |
| 49 | if (!consts) return null; |
| 50 | let result = ''; |
| 51 | for (let i = 0; i < quasis.length; i++) { |
| 52 | const cooked = quasis[i]?.value?.cooked; |
| 53 | if (cooked == null) return null; |
| 54 | result += cooked; |
| 55 | if (i < expressions.length) { |
| 56 | const expr = expressions[i]; |
| 57 | if (expr?.type !== 'Identifier') return null; |
| 58 | const resolved = consts.get(expr.name); |
| 59 | if (resolved == null) return null; |
| 60 | result += resolved; |
| 61 | } |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | /** Check if an OXC node is a string-like literal. */ |
| 69 | function isStringLike(node: any, consts?: Map<string, string>): boolean { |
no outgoing calls
no test coverage detected