3 ms·
Well, Function.toString() should print the code of a lambda in JavaScript. So I think you could do it without a pre-processor: use Babel as a library to parse t
by fiddlerwoaroof 4y ago
Well, Function.toString() should print the code of a lambda in JavaScript. So I think you could do it without a pre-processor: use Babel as a library to parse the body of the function; run each sub-expression separately and display the results.
- tobyhinloopen 4y agoCan you actually do that, since you don’t have the context the function is running in? You can inspect the source, but functions don’t run in isolation so you cannot extract the source and run it somewhere else. Simple example: var x = 0; var y = function() { x++; }; You can’t reliably execute function y with just y’s function body. Maybe if you replace the function with another by injecting code into a copy of the source, but I’m not sure if that’s even possible.
- fiddlerwoaroof 4y agoYou can just document a constraint that these function must use the purely functional subset of JavaScript, which is enough for the sorts of assertions I typically write. Alternatively, you could constrain it to the subset that has no unbound variables or side effects.