You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Functions in JS and TS can be called with template strings as their arguments, which passes the literal parts as an array and the variables inserted into the pattern as vaargs.
I've been using this in LÖVE2D for shaders, with this simple function that pieces together the bits of the shader code and the variables evaluated at runtime into a single string then passed into the newShader function.
exportconstglsl=(code: TemplateStringsArray, ...vars: unknown[])=>{constlen=code.lengthif(len===1)returncode[0]leti=0letbuff=''do{buff+=code[i]+tostring(vars[i])}while(++i<len-1)buff+=code[i]returnbuff}constcode=glsl` vec4 effect(vec4 colour, Image tex, vec2 texpos, vec2 scrpos) { vec4 r; // shader logic ... // Since this is a template string, can conveniently pull variables from the scope with ${} return r }`constshader=love.graphics.newShader()
It's great because it pairs with glsl-literal to provide syntactic highlighting inside my TS sources.
Wanting to experiment with this pattern, I tried to use it for simple tile map creation with a factory that would return a function that works similarly. Something along the lines of
constbuildMap=(displaySize: WH,cellSize: WH)=>{if(displaySize.width%cellSize.width!==0)error('Cell width is not an exact denominator of display width')if(displaySize.height%cellSize.height!==0)error('Cell height is not an exact denominator of display height')return((template: TemplateStringsArray)=>{constcanvas=love.graphics.newCanvas()conststr=template.raw.join().replace('%s','')//strip all whitespacesconstmap: Nullable<Rectangle>[][]=[]// Build the tile map and stuffconstupdate=()=>{/* ... */}constdraw=()=>{/* ... */}return{
map,
update,
draw,}})}const{ map, update, draw }=buildMap({width: 10,height: 10},{width: 2,height: 2})/* CRASHES HERE */` +++++ +...+ +.#.+ +...+ +++++`
But TSTL doesn't seem to like immediately calling the returned function. It does however work if you take the function returned by the factory and then use the template literal call patten with it
declarefunctionprint(...msg: string[]): void// Note: crashes regardless of using the `function` keyword or arrow pattern.constfactory=()=>{return(template: TemplateStringsArray)=>{for(letpartoftemplate)print(part)}}factory()`Some string template`
Stack trace:
PS [REDACTED]> pnpm tstl
[REDACTED]\node_modules\.pnpm\typescript@5.8.3\node_modules\typescript\lib\typescript.js:126991
throw e;
^
Error: Unsupported LeftHandSideExpression kind: CallExpression
at transformContextualCallExpression ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\visitors\call.js:120:15)
at transformTaggedTemplateExpression ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\visitors\template.js:71:61)
at TransformationContext.transformNodeRaw ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:67:24)
at TransformationContext.transformExpression ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:83:29)
at transformExpressionStatement ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\visitors\expression-statement.js:18:36)
at TransformationContext.transformNodeRaw ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:67:24)
at TransformationContext.transformNode ([REDACTED]\repro\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:47:56)
at [REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:102:37
at Array.flatMap (<anonymous>)
at TransformationContext.transformStatements ([REDACTED]\node_modules\.pnpm\typescript-to-lua@1.31.1_typescript@5.8.3\node_modules\typescript-to-lua\dist\transformation\context\context.js:100:45)
Node.js v24.0.2
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Issue description
Functions in JS and TS can be called with template strings as their arguments, which passes the literal parts as an array and the variables inserted into the pattern as vaargs.
I've been using this in LÖVE2D for shaders, with this simple function that pieces together the bits of the shader code and the variables evaluated at runtime into a single string then passed into the
newShader
function.It's great because it pairs with glsl-literal to provide syntactic highlighting inside my TS sources.
Wanting to experiment with this pattern, I tried to use it for simple tile map creation with a factory that would return a function that works similarly. Something along the lines of
But TSTL doesn't seem to like immediately calling the returned function. It does however work if you take the function returned by the factory and then use the template literal call patten with it
Minimal reproduction
Here's a minimal reproduction setup:
package.json
:reproduction.ts
Stack trace:
The text was updated successfully, but these errors were encountered: