3 ms·
That won't be possible without a lot of work. You'll need to tweak the TypeScript compiler to make the parser comprehend exported variables. For example, im
by maxloh 2y ago
That won't be possible without a lot of work. You'll need to tweak the TypeScript compiler to make the parser comprehend exported variables.
For example,
import { contacts } from "db"
const jane = contacts.one(c => c.email === janeEmail)
Marco-like magic can be confusing, as we learned from Svelte. Especially when it's half-baked like the one you are suggesting.
Another drawback is that you'll need to compile the code twice to run it: once with the custom parser and once with the TypeScript compiler.
- jeswin 2y ago> Marco-like magic can be confusing, as we learned from Svelte. Especially when it's half-baked like the one you are suggesting. When I'm giving examples on a forum thread, I try to provide one that's simpler to understand and captures the essence of the argument. I am not making any claims on completeness. > Marco-like magic can be confusing, as we learned from Svelte. Especially when it's half-baked like the one you are suggesting. Svelte invents new syntax, which in my view isn't great. This is pure JS, and it could even run in a browser against arrays (for tables). Add: > That won't be possible without a lot of work. You'll need to tweak the TypeScript compiler to make the parser comprehend exported variables. Not required. I was suggesting that the expression could be parsed at runtime. There are various options, with different tradeoffs.
- maxloh 2y ago> Not required. I was suggesting that the expression could be parsed at runtime. There are various options, with different tradeoffs. Good idea. That might be possible, though not every JavaScript runtime implements Function.prototype.toString. I'm not sure if Node does. const aFunction = arg => arg.a === arg.b const aFunctionSrc = aFunction.toString() console.log(aFunctionSrc) // 'arg => arg.a === arg.b' const transformedFunctionSrc = transform(aFunctionSrc) const transformedFunction = new Function(transformedFunctionSrc) console.log(transformedFunction) // [Function: anonymous] // Cache the transformed function in a WeakMap // WeakMap values are garbage collected when their keys are garbage collected const functionMap = new WeakMap() functionMap.set(aFunction, transformedFunction) console.log(functionMap.get(aFunction)) // [Function: anonymous]