`typeof 1` is removed when transformed
Description
When passing in typeof 1 to esbuild, it returns an empty string.
This leads to unexpected behavior when esbuild is used to transform input to a REPL: https://github.com/privatenumber/tsx/issues/327
Reproduction
https://esbuild.github.io/try/#dAAwLjE5LjcAAHR5cGVvZiAx
Part of this is because the input to esbuild is a module, not a code fragment, so it's assumed that a transformation like this has no effect on the run-time behavior of the code. REPLs make use of the weird behavior of eval that returns the completion of the "last value-producing item in the statement list" (as the specification puts it).
And part of this is because some amount of tree-shaking even when minification is disabled is desirable due to define substitutions. For example, esbuild has an isUnsightlyPrimitive function that trims top-level primitives since people have requested them to be removed.
I think the removal of top-level primitives is still reasonable here since the input to esbuild is a module, and the completion value of a module is not supposed to be observable. I don't think the right approach to this is necessarily to just disable this removal of top-level primitives. The more robust thing to do would be to add explicit support for eval semantics to esbuild so that esbuild is fully aware of the weird "completion" semantics that eval has.
I see, thank you for explaining. And that makes a lot of sense.
Would love to see a completion format or platform supported!