devanshj/sthir

Evaluate the iife produced by `pa` macro when used with `p`

devanshj opened this issue · 2 comments

import { p, pa } from "@sthir/macro"

pa(x, p("!==", undefined))

Gets transformed to...

(t => t !== undefined)(x)

This is correct because pa could have even be used with some other predicate not just p...

pa(x, somePredicate)

But in cases where it's used with p the iife can be further evaluated....

x !== undefined

We can't do the proposed if x has a call expression anywhere in the subtree, or even property accesses
because of possibility of effectful getters and proxies. (That is to say an expression can be impure and evaluating the iife can evaluate the expression more than once). So given we'd have to do a lot more work to micro-optimize only a small set of cases, I think the current behavior is fine.

I think the feature is attractive and we can evalute only when x is an IdentifierExpression (and maybe even MemberExpression). Or perhaps develop a heuristic that checks if an expression is most likely pure or not. Actually we can support impure expressions too if it's only required once in the predicate expression.