Skip to content

Commit 6bbc799

Browse files
committed
fix(function): keep an exhausted JavaScript sentinel space a user compile error
User code can occupy every sentinel a short placeholder could take, so running out of them is the code's fault, not a compiler invariant.
1 parent e3c1523 commit 6bbc799

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎apps/sim/lib/execution/code-placeholders/compiler.test.ts‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,20 +1549,28 @@ describe('python true positives survive the dot guard', () => {
15491549
})
15501550
})
15511551

1552-
describe('compiler invariant failures', () => {
1552+
describe('compiler failure classification', () => {
15531553
it('reports overlapping source edits as a compiler invariant, not a user compile error', () => {
1554-
const thrown = (() => {
1555-
try {
1556-
applySourceEdits('return value', [
1557-
{ start: 0, end: 6, text: 'yield' },
1558-
{ start: 3, end: 9, text: 'x' },
1559-
])
1560-
} catch (error) {
1561-
return error
1562-
}
1563-
})()
1564-
1565-
expect(thrown).toBeInstanceOf(CodePlaceholderInvariantError)
1566-
expect(thrown).not.toBeInstanceOf(CodePlaceholderCompileError)
1554+
const applyOverlappingEdits = () =>
1555+
applySourceEdits('return value', [
1556+
{ start: 0, end: 6, text: 'yield' },
1557+
{ start: 3, end: 9, text: 'x' },
1558+
])
1559+
1560+
expect(applyOverlappingEdits).toThrow(CodePlaceholderInvariantError)
1561+
expect(applyOverlappingEdits).not.toThrow(CodePlaceholderCompileError)
1562+
})
1563+
1564+
it('reports an exhausted JavaScript sentinel space as a user compile error', async () => {
1565+
/** Every `$xyz$` sentinel a five-character `{{a}}` could take, packed under the code cap. */
1566+
const alphabet = [...'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ']
1567+
const payloads = alphabet.flatMap((a) =>
1568+
alphabet.flatMap((b) => alphabet.map((c) => `${a}${b}${c}`))
1569+
)
1570+
const code = `const taken = '$${payloads.join('$')}$'\nreturn {{a}}`
1571+
1572+
await expect(
1573+
compileCodePlaceholders({ code, language: CodeLanguage.JavaScript, params: { a: 1 } })
1574+
).rejects.toBeInstanceOf(CodePlaceholderCompileError)
15671575
})
15681576
})

‎apps/sim/lib/execution/code-placeholders/javascript.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import ts from '@typescript/typescript6'
22
import {
33
applySourceEdits,
44
CodePlaceholderCompileError,
5-
CodePlaceholderInvariantError,
65
createCodePlaceholderCompilationContext,
76
isOffsetInRanges,
87
type SourceEdit,
@@ -262,7 +261,7 @@ function createSentinel(
262261
return sentinel
263262
}
264263
}
265-
throw new CodePlaceholderInvariantError('Unable to allocate a collision-free parser sentinel')
264+
throw new CodePlaceholderCompileError('Unable to allocate a collision-free parser sentinel')
266265
}
267266

268267
function createSentinelSource(

0 commit comments

Comments
 (0)