MCPcopy Index your code
hub / github.com/formatjs/formatjs / extractJSXDescriptor

Function extractJSXDescriptor

packages/unplugin/transform.ts:196–239  ·  view source on GitHub ↗
(
    elementNode: any
  )

Source from the content-addressed store, hash-verified

194 }
195
196 function extractJSXDescriptor(
197 elementNode: any
198 ):
199 | {descriptor: MessageDescriptor; locations: DescriptorLocation}
200 | undefined {
201 const attributes = elementNode.attributes || []
202 const descriptor: MessageDescriptor = {}
203 const locations: DescriptorLocation = {}
204
205 // Always insert after the element name so the id survives even when the
206 // first attribute (e.g. description) is removed.
207 locations.insertionPoint = elementNode.name.end
208
209 for (const attr of attributes) {
210 if (attr.type !== 'JSXAttribute') continue
211 const attrName = attr.name
212 if (!attrName || attrName.type !== 'JSXIdentifier') continue
213 const n = attrName.name
214 if (n !== 'id' && n !== 'defaultMessage' && n !== 'description') continue
215 const keyName: keyof MessageDescriptor = n
216
217 let val: string | undefined
218 const valueNode = attr.value
219 if (!valueNode) continue
220
221 if (valueNode.type === 'StringLiteral' || valueNode.type === 'Literal') {
222 val = decodeJSXAttributeValue(valueNode.value as string)
223 } else if (valueNode.type === 'JSXExpressionContainer') {
224 val = getStaticValue(valueNode.expression)
225 }
226
227 if (val === undefined) continue
228
229 descriptor[keyName] = val
230 locations[keyName] = {
231 start: attr.value.start,
232 end: attr.value.end,
233 value: val,
234 }
235 }
236
237 if (!descriptor.defaultMessage && !descriptor.id) return undefined
238 return {descriptor, locations}
239 }
240
241 function processDescriptor(
242 descriptor: MessageDescriptor,

Callers 1

handleJSXOpeningElementFunction · 0.85

Calls 2

decodeJSXAttributeValueFunction · 0.85
getStaticValueFunction · 0.85

Tested by

no test coverage detected