Skip to content

Support hoisted functions and function declarations #22

@lxsmnsyc

Description

@lxsmnsyc

Hoisted functions are a partially supported, except for the following:

  • Function declarations (i.e. a function declared as a statement). Due to the quirky behavior of functions, it isn't currently optimized by Forgetti. Function declarations can be declared regardless of the function's flow (e.g. return, branches, etc.)
  • Functions with hoisted references.
function Example(props) {
  const hello = () => {
    console.log(a, b);
  }
  const a = 'hello';
  const b = 'world';
  hello();
  hello();
}

If hello is only accessed once, it works however if it is accessed more than once, the compiler doesn't know what to do:

import { useMemo as _useMemo } from "react";
import { $$cache as _$$cache } from "forgetti/runtime";
import { $$equals as _$$equals } from "forgetti/runtime";
function Example(props) {
  let _c = _$$cache(_useMemo, 5),
    _eq = _$$equals(_c, 0, a),
    _v = _eq ? _c[0] : _c[0] = a,
    _eq2 = _$$equals(_c, 1, b),
    _v2 = _eq2 ? _c[1] : _c[1] = b;
  const hello = _eq && _eq2 ? _c[2] : _c[2] = () => {
    console.log(a, b);
  };
  const a = 'hello';
  const b = 'world';
  3 in _c ? _c[3] : _c[3] = hello();
  4 in _c ? _c[4] : _c[4] = hello();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions