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

Function collectVariables

packages/icu-messageformat-parser/manipulator.ts:164–203  ·  view source on GitHub ↗

* Collect all variables in an AST to Record * @param ast AST to collect variables from * @param vars Record of variable name to variable type

(
  ast: MessageFormatElement[],
  vars: Map<string, TYPE> = new Map<string, TYPE>()
)

Source from the content-addressed store, hash-verified

162 * @param vars Record of variable name to variable type
163 */
164function collectVariables(
165 ast: MessageFormatElement[],
166 vars: Map<string, TYPE> = new Map<string, TYPE>()
167): void {
168 ast.forEach(el => {
169 if (
170 isArgumentElement(el) ||
171 isDateElement(el) ||
172 isTimeElement(el) ||
173 isNumberElement(el)
174 ) {
175 // If the variable was already registered as a plural/select, it's normal
176 // for it to also appear inside as number/date/time/argument — not a conflict.
177 if (vars.has(el.value)) {
178 const existingType = vars.get(el.value)!
179 if (
180 existingType !== el.type &&
181 existingType !== TYPE.plural &&
182 existingType !== TYPE.select
183 ) {
184 throw new Error(`Variable ${el.value} has conflicting types`)
185 }
186 } else {
187 vars.set(el.value, el.type)
188 }
189 }
190
191 if (isPluralElement(el) || isSelectElement(el)) {
192 vars.set(el.value, el.type)
193 Object.keys(el.options).forEach(k => {
194 collectVariables(el.options[k].value, vars)
195 })
196 }
197
198 if (isTagElement(el)) {
199 vars.set(el.value, el.type)
200 collectVariables(el.children, vars)
201 }
202 })
203}
204
205interface IsStructurallySameResult {
206 error?: Error

Callers 1

isStructurallySameFunction · 0.85

Calls 9

isArgumentElementFunction · 0.85
isDateElementFunction · 0.85
isTimeElementFunction · 0.85
isNumberElementFunction · 0.85
isPluralElementFunction · 0.85
isSelectElementFunction · 0.85
isTagElementFunction · 0.85
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected