* 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>() )
| 162 | * @param vars Record of variable name to variable type |
| 163 | */ |
| 164 | function 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 | |
| 205 | interface IsStructurallySameResult { |
| 206 | error?: Error |
no test coverage detected