MCPcopy
hub / github.com/formatjs/formatjs / evaluateMessageDescriptor

Function evaluateMessageDescriptor

packages/babel-plugin-formatjs/utils.ts:93–153  ·  view source on GitHub ↗
(
  descriptorPath: MessageDescriptorPath,
  isJSXSource = false,
  filename: string | undefined,
  idInterpolationPattern?: string,
  overrideIdFn?: Options['overrideIdFn'],
  preserveWhitespace?: Options['preserveWhitespace'],
  flatten?: Options['flatten']
)

Source from the content-addressed store, hash-verified

91}
92
93export function evaluateMessageDescriptor(
94 descriptorPath: MessageDescriptorPath,
95 isJSXSource = false,
96 filename: string | undefined,
97 idInterpolationPattern?: string,
98 overrideIdFn?: Options['overrideIdFn'],
99 preserveWhitespace?: Options['preserveWhitespace'],
100 flatten?: Options['flatten']
101): MessageDescriptor {
102 let id = getMessageDescriptorValue(descriptorPath.id)
103 let defaultMessage = getICUMessageValue(
104 descriptorPath.defaultMessage,
105 {
106 isJSXSource,
107 },
108 preserveWhitespace
109 )
110 const description = getMessageDescriptorValue(descriptorPath.description)
111
112 // GH #3537: Apply flatten transformation before calling overrideIdFn
113 // so that the ID generation sees the same message format as the final output
114 if (flatten && defaultMessage) {
115 try {
116 defaultMessage = printAST(hoistSelectors(parse(defaultMessage)))
117 } catch (e: any) {
118 const loc = descriptorPath.defaultMessage?.node.loc
119 const locationStr = loc
120 ? ` at line ${loc.start.line}, column ${loc.start.column + 1}`
121 : ''
122 throw new Error(
123 `[formatjs] Cannot flatten message in file "${filename}"${locationStr}${id ? ` with id "${id}"` : ''}: ${e.message}\nMessage: ${defaultMessage}`
124 )
125 }
126 }
127
128 if (overrideIdFn) {
129 id = overrideIdFn(id, defaultMessage, description, filename)
130 } else if (!id && idInterpolationPattern && defaultMessage) {
131 id = interpolateName(
132 {resourcePath: filename} as any,
133 idInterpolationPattern,
134 {
135 content: description
136 ? `${defaultMessage}#${description}`
137 : defaultMessage,
138 }
139 )
140 }
141 const descriptor: MessageDescriptor = {
142 id,
143 }
144
145 if (description) {
146 descriptor.description = description
147 }
148 if (defaultMessage) {
149 descriptor.defaultMessage = defaultMessage
150 }

Callers 2

visitorFunction · 0.90
processMessageObjectFunction · 0.90

Calls 7

parseFunction · 0.90
interpolateNameFunction · 0.90
getICUMessageValueFunction · 0.85
printASTFunction · 0.85
hoistSelectorsFunction · 0.85
overrideIdFnFunction · 0.85

Tested by

no test coverage detected