testImmediatelyInvokedFunctionsRunNow

tcmts.core.test.hash.testImmediatelyInvokedFunctionsRunNow · #t4tqvqww · term

A function called where it's written runs as its definition is initialized: what it uses is needed then (Effect's Base = (() => { … Base.prototype = proto … })()), unlike what a function body called later uses.

import { analyze } from "#codebase/tcmts/core/test/hash";
import assert from "node:assert/strict";

export const testImmediatelyInvokedFunctionsRunNow = () => {
  const a = analyze(`const proto = { a: 1 };
export const Base = (() => {
  const B = function () {};
  B.prototype = proto;
  return B;
})();
export const later = () => proto;
`);
  const proto = a.defs.find((d) => d.nameHint === "proto")!.hash;
  const strictOf = (name: string) =>
    a.defs.find((d) => d.nameHint === name)!.deps.find((d) => d.ref === `#${proto}`)?.strict;
  assert.equal(strictOf("Base"), true, "the IIFE reads proto as Base is made");
  assert.equal(strictOf("later"), false, "a function body reads it later");
}

uses