function compileTypesInvariant()

in packages/sqrl/src/ast/AstTypes.ts [207:238]


function compileTypesInvariant(fnAst: CallAst, types: ArgumentCheck[]) {
  const { args } = fnAst;

  const providedArgs = args.length;
  const repeatedLast = types.length && types[types.length - 1].isRepeated;
  const optArgs = types.filter((t) => t.isOptional).length;
  const maxArgs = repeatedLast ? providedArgs : types.length;
  const minArgs = types.length - optArgs;

  let lengthMessage: string;
  if (repeatedLast) {
    lengthMessage = `at least ${pluralize("argument", minArgs)}`;
  } else if (minArgs === maxArgs) {
    lengthMessage = `${pluralize("argument", minArgs)}`;
  } else {
    lengthMessage = `${minArgs} to ${pluralize("argument", maxArgs)}`;
  }

  sqrlInvariant(
    fnAst,
    providedArgs >= minArgs && providedArgs <= maxArgs,
    `Argument count to call of ${fnAst.func} did not match. ` +
      `Expected ${lengthMessage} but got ${providedArgs}.`
  );

  for (let i = 0; i < types.length; i++) {
    // The last field may have a repeated checker on it
    const checker: ArgumentCheck = types[Math.min(i, types.length - 1)];

    checker.compileTimeCheck(args[i], fnAst);
  }
}