MCPcopy
hub / github.com/formatjs/formatjs / processDescriptor

Function processDescriptor

packages/unplugin/transform.ts:241–343  ·  view source on GitHub ↗
(
    descriptor: MessageDescriptor,
    locations: DescriptorLocation,
    isJSX: boolean
  )

Source from the content-addressed store, hash-verified

239 }
240
241 function processDescriptor(
242 descriptor: MessageDescriptor,
243 locations: DescriptorLocation,
244 isJSX: boolean
245 ): void {
246 let {defaultMessage, description} = descriptor
247 let existingId = descriptor.id
248
249 // Normalize whitespace on defaultMessage
250 if (defaultMessage && !preserveWhitespace) {
251 defaultMessage = normalizeMessageWhitespace(defaultMessage)
252 }
253
254 // Apply flatten transformation (hoist selectors + normalize ICU format via printAST)
255 // This must happen before ID generation so the ID matches formatjs extract and babel-plugin-formatjs
256 if (flatten && defaultMessage) {
257 try {
258 defaultMessage = printAST(hoistSelectors(parse(defaultMessage)))
259 } catch {
260 // If parsing fails, keep the original message
261 }
262 }
263
264 // Generate ID
265 let newId = existingId
266 if (overrideIdFn) {
267 newId = overrideIdFn(existingId, defaultMessage, description, id)
268 } else if (!existingId && idInterpolationPattern && defaultMessage) {
269 newId = interpolateName(
270 {resourcePath: id} as any,
271 idInterpolationPattern,
272 {
273 content: description
274 ? `${defaultMessage}#${description}`
275 : defaultMessage,
276 }
277 )
278 }
279
280 if (!newId && !defaultMessage) return
281
282 if (!s) s = new MagicString(code)
283
284 // Insert/update id
285 if (newId) {
286 if (locations.id) {
287 // Replace existing id value
288 s.overwrite(locations.id.start, locations.id.end, JSON.stringify(newId))
289 } else if (locations.insertionPoint != null) {
290 // Insert id at a stable position (after `{` for objects, before first attr for JSX)
291 if (isJSX) {
292 s.appendLeft(locations.insertionPoint, ` id=${JSON.stringify(newId)}`)
293 } else {
294 s.appendRight(
295 locations.insertionPoint,
296 `id: ${JSON.stringify(newId)}, `
297 )
298 }

Callers 2

handleCallExpressionFunction · 0.85
handleJSXOpeningElementFunction · 0.85

Calls 8

parseFunction · 0.90
interpolateNameFunction · 0.90
printASTFunction · 0.85
hoistSelectorsFunction · 0.85
overrideIdFnFunction · 0.85
removeJSXAttributeFunction · 0.85
removeObjectPropertyFunction · 0.85

Tested by

no test coverage detected