Skip to content

Commit

Permalink
Simplify making the last statement an expression a return
Browse files Browse the repository at this point in the history
From the conversation on discord `replace` can be used to simplify
making the last statement in an expression a return.

Note:

- This version doesn't do any trimming.
- This does not fix #497

Ran tests and build step locally, but wasn't sure if PRs
should only be the code change (rather than artefacts).
  • Loading branch information
andersmurphy committed Jan 20, 2025
1 parent 8b43f29 commit 50161d2
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions library/src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,9 @@ export class Engine {
ctx: RuntimeContext,
...argNames: string[]
): RuntimeExpressionFunction {
const stmts = ctx.value
.split(/;|\n/)
.map((s) => s.trim())
.filter((s) => s !== '')
const lastIdx = stmts.length - 1
const last = stmts[lastIdx]
if (!last.startsWith('return')) {
stmts[lastIdx] = `return (${stmts[lastIdx]});`
}
let userExpression = stmts.join(';\n').trim()

// Make last statement in an expression a return
let userExpression = ctx.value.replace(/([^;\n]*$)/, 'return ($1);')

// Ingore any escaped values
const escaped = new Map<string, string>()
Expand Down

0 comments on commit 50161d2

Please sign in to comment.