MCPcopy
hub / github.com/formatjs/formatjs / extractMessageDescriptor

Function extractMessageDescriptor

packages/ts-transformer/transform.ts:296–641  ·  view source on GitHub ↗
(
  ts: TypeScript,
  node:
    | typescript.ObjectLiteralExpression
    | typescript.JsxOpeningElement
    | typescript.JsxSelfClosingElement,
  {
    overrideIdFn,
    extractSourceLocation,
    preserveWhitespace,
    flatten,
    throws = true,
    onMsgError,
  }: Opts,
  sf: typescript.SourceFile
)

Source from the content-addressed store, hash-verified

294}
295
296function extractMessageDescriptor(
297 ts: TypeScript,
298 node:
299 | typescript.ObjectLiteralExpression
300 | typescript.JsxOpeningElement
301 | typescript.JsxSelfClosingElement,
302 {
303 overrideIdFn,
304 extractSourceLocation,
305 preserveWhitespace,
306 flatten,
307 throws = true,
308 onMsgError,
309 }: Opts,
310 sf: typescript.SourceFile
311): MessageDescriptor | undefined {
312 let extractionError: Error | null = null
313
314 // Helper to handle errors based on throws option
315 function handleError(errorMsg: string, errorNode?: typescript.Node): void {
316 let locationMsg = errorMsg
317 if (errorNode) {
318 const {line, character} = ts.getLineAndCharacterOfPosition(
319 sf,
320 errorNode.getStart(sf)
321 )
322 locationMsg = `${sf.fileName}:${line + 1}:${character + 1} ${errorMsg}`
323 }
324 const error = new Error(locationMsg)
325 if (throws) {
326 throw error
327 }
328 extractionError = error
329 if (onMsgError) {
330 onMsgError(sf.fileName, error)
331 }
332 }
333 let properties:
334 | typescript.NodeArray<typescript.ObjectLiteralElement>
335 | typescript.NodeArray<typescript.JsxAttributeLike>
336 | undefined = undefined
337 if (ts.isObjectLiteralExpression(node)) {
338 properties = node.properties
339 } else if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
340 properties = node.attributes.properties
341 }
342 const msg: MessageDescriptor = {id: ''}
343 if (!properties) {
344 return
345 }
346
347 properties.forEach(prop => {
348 const {name} = prop
349 const initializer:
350 | typescript.Expression
351 | typescript.JsxExpression
352 | undefined =
353 ts.isPropertyAssignment(prop) || ts.isJsxAttribute(prop)

Calls 10

parseFunction · 0.90
handleErrorFunction · 0.85
evaluateStringConcatFunction · 0.85
printASTFunction · 0.85
hoistSelectorsFunction · 0.85
interpolateNameFunction · 0.85
overrideIdFnFunction · 0.85

Tested by

no test coverage detected