export function parseVariantsTag()

in compiler/src/model/utils.ts [1088:1137]


export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
  const tags = parseJsDocTags(jsDoc)
  if (tags.variants == null) {
    return undefined
  }

  const nonExhaustive = (typeof tags.non_exhaustive === 'string') ? true : undefined

  const [type, ...values] = tags.variants.split(' ')
  if (type === 'external') {
    return {
      kind: 'external_tag',
      nonExhaustive: nonExhaustive
    }
  }

  if (type === 'container') {
    return {
      kind: 'container',
      nonExhaustive: nonExhaustive
    }
  }

  if (type === 'internal') {
    const pairs = parseKeyValues(jsDoc, values, 'tag', 'default')
    assert(jsDoc, typeof pairs.tag === 'string', 'Internal variant requires a tag definition')
    return {
      kind: 'internal_tag',
      nonExhaustive: nonExhaustive,
      tag: pairs.tag,
      defaultTag: pairs.default
    }
  }

  if (type === 'untagged') {
    const pairs = parseKeyValues(jsDoc, values, 'untyped')
    assert(jsDoc, typeof pairs.untyped === 'string', 'Untagged variant requires an untyped definition')
    const fqn = pairs.untyped.split('.')
    return {
      kind: 'untagged',
      nonExhaustive: nonExhaustive,
      untypedVariant: {
        namespace: fqn.slice(0, fqn.length - 1).join('.'),
        name: fqn[fqn.length - 1]
      }
    }
  }

  assert(jsDoc, false, `Bad variant type: ${type}`)
}