invokedWhereWritten

tcmts.core.canonicalize.invokedWhereWritten · #5rgb5m4d · term · runs anywhere

Whether a function expression is called where it's written ((() => { … })(), (function () { … })()): its body runs as the code around it does.

import {
  isArrowFunction as ts_isArrowFunction, isCallExpression as ts_isCallExpression,
  isFunctionExpression as ts_isFunctionExpression, isParenthesizedExpression as ts_isParenthesizedExpression,
} from "typescript/ast";
import type { Node as ts_Node } from "typescript/ast";

export function invokedWhereWritten(fn: ts_Node): boolean {
  if (!ts_isArrowFunction(fn) && !ts_isFunctionExpression(fn)) return false;
  let n: ts_Node = fn;
  while (n.parent && ts_isParenthesizedExpression(n.parent)) n = n.parent;
  return !!n.parent && ts_isCallExpression(n.parent) && n.parent.expression === n;
}

used by