function expression()

in js/src/fluent-serialize.ts [47:88]


function expression(expr: Expression | Markup) {
  if ('fn' in expr && isIdentifier(expr.fn)) {
    const options: string[] = []
    if (expr.opt) {
      for (const [name, value] of Object.entries(expr.opt)) {
        if (typeof value !== 'string') {
          const error = `fluent: Unsupported option value for ${name}`
          throw new SerializeError(error)
        }
        options.push(`${name}: ${literal(value)}`)
      }
    }
    switch (expr.fn) {
      case 'message': {
        if (expr._ !== undefined) {
          const id = expr._
          if (id[0] === '-' && isIdentifier(id.substring(1))) {
            return options.length ? `${id}(${options.join(', ')})` : id
          }
          if (isMsgRef(id) && options.length === 0) return id
        }
        const error = 'fluent: Unsupported message or term reference'
        throw new SerializeError(error)
      }
      case 'number':
        if (options.length === 0 && isNumber(expr._)) return expr._
      // fallthrough
      default: {
        if ('_' in expr && expr._ !== undefined) {
          options.unshift(literal(expr._))
        } else if ('$' in expr && isIdentifier(expr.$)) {
          options.unshift('$' + expr.$)
        }
        return expr.fn.toUpperCase() + '(' + options.join(', ') + ')'
      }
    }
  }
  if ('_' in expr && expr._ !== undefined) return literal(expr._)
  if ('$' in expr && isIdentifier(expr.$)) return '$' + expr.$
  const error = `fluent: Unsupported pattern part ${JSON.stringify(expr)}`
  throw new SerializeError(error)
}