fix(compiler): reserve api as a runtime binding to avoid client-scope collision

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-19 15:45:09 +05:30
co-authored by Claude Opus 5
parent bd2f6ac5e3
commit 70777e4a45
2 changed files with 29 additions and 0 deletions
+1
View File
@@ -61,6 +61,7 @@ const RUNTIME_BINDINGS = new Set([
"server",
"props",
"refs",
"api",
"event",
"payload",
]);
@@ -80,3 +80,31 @@ test("a block without an error section still emits its response body", () => {
expect(generated).toContain("plainUsers");
expect(generated).toContain("data.users");
});
test("a state field named api does not collide with the emitted api object", () => {
const generated = generateTargets(
parse(`page Repro {
state {
api = ""
}
client {
${BLOCK}
}
functions {
client async function run(): Promise<void> {
const users = await api.searchUsers({ name: "Ajay" })
console.log(users)
}
}
view { <main><button @click="run()">go</button></main> }
}
`),
).browser;
expect(() => {
new Function(generated.replace(/^\s*import[^\n]*$/gm, "").replace(/\bexport\s+/g, ""));
}).not.toThrow();
});