export function modelBehaviors()

in compiler/src/model/utils.ts [417:452]


export function modelBehaviors (node: ExpressionWithTypeArguments, jsDocs: JSDoc[]): model.Behavior {
  const behaviorName = node.getExpression().getText()
  const generics = node.getTypeArguments().map(node => modelType(node))

  let meta: Map<string, string> | undefined
  const tags = parseJsDocTagsAllowDuplicates(jsDocs)
  if (tags.behavior_meta !== undefined) {
    // Extracts whitespace/comma-separated key-value-pairs with a "=" delimiter and handles double-quotes
    const re = /(?<key>[^=\s,]+)=(?<value>"([^"]*)"|([^\s,]+))/g

    for (const tag of tags.behavior_meta) {
      const id = tag.split(' ')
      if (id[0].trim() !== behaviorName) {
        continue
      }
      const matches = [...id.slice(1).join(' ').matchAll(re)]
      meta = new Map<string, string>()
      for (const match of matches) {
        if (match.groups == null) {
          continue
        }
        meta.set(match.groups.key, match.groups.value.replace(/^"(.+(?="$))"$/, '$1'))
      }
      break
    }
  }

  return {
    type: {
      name: behaviorName,
      namespace: getNameSpace(node)
    },
    ...(generics.length > 0 && { generics }),
    meta: (meta === undefined) ? undefined : Object.fromEntries(meta)
  }
}