Skip to content

Commit

Permalink
Load with satisfies expression (HoudiniGraphql#793)
Browse files Browse the repository at this point in the history
* fix bug using satisfies expression with load functions

* changeset
  • Loading branch information
AlecAivazis authored and endigma committed Nov 10, 2024
1 parent 8c463e1 commit b146ce5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/brown-jobs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'houdini-svelte': patch
'houdini': patch
---

Fix erorr when using satisfies expression with load functions
31 changes: 31 additions & 0 deletions packages/houdini-svelte/src/plugin/transforms/kit/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1357,3 +1357,34 @@ test('onError hook', async function () {
}
`)
})

test('existing loads with parens', async function () {
const route = await route_test({
script: `
import type { LayoutServerLoad } from "./$types";
export const load = (() => ({ test: "Hello" })) satisfies LayoutServerLoad;
`,
component: `
<script>
const result = graphql\`
query TestQuery1 {
viewer {
id
}
}
\`
</script>
`,
})

// make sure we added the right stuff
expect(route.script).toMatchInlineSnapshot(`
import _TestQuery1Artifact from "$houdini/artifacts/TestQuery1";
import type { LayoutServerLoad } from "./$types";
export const load = (() => ({
test: "Hello"
})) satisfies LayoutServerLoad;
`)
})
27 changes: 27 additions & 0 deletions packages/houdini-svelte/src/plugin/transforms/kit/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ test('modifies existing load +layout.server.js - no return', async function () {
`)
})

test('modifies existing load +layout.server.js - satisfies operator', async function () {
const result = await test_transform_js(
'src/routes/+layout.server.js',
`
import type { LayoutServerLoad } from "./$types";
export const load = (() => ({ test: "Hello" })) satisfies LayoutServerLoad;
`
)

expect(result).toMatchInlineSnapshot(`
import { buildSessionObject } from "$houdini/plugins/houdini-svelte/runtime/session";
import type { LayoutServerLoad } from "./$types";
export const load = (event => {
const __houdini__vite__plugin__return__value__ = ({
test: "Hello"
});
return {
...buildSessionObject(event),
...__houdini__vite__plugin__return__value__
};
}) satisfies LayoutServerLoad;
`)
})

test('modifies existing load +layout.server.js - rest params', async function () {
const result = await test_transform_js(
'src/routes/+layout.server.js',
Expand Down
20 changes: 14 additions & 6 deletions packages/houdini/src/vite/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function find_exported_fn(
body: Statement[],
name: string
): FunctionDeclaration | FunctionExpression | ArrowFunctionExpression | null {
console.log(body)
for (const statement of body) {
console.log(statement)
if (statement.type !== 'ExportNamedDeclaration') {
continue
}
Expand All @@ -44,7 +46,6 @@ export function find_exported_fn(
// we also need to find exported variables that are functions or arrow functions
else if (exportDeclaration.declaration?.type === 'VariableDeclaration') {
const value = exportDeclaration.declaration as VariableDeclaration

// make sure that the declared value has a matching name
if (
value.declarations.length !== 1 ||
Expand All @@ -57,12 +58,19 @@ export function find_exported_fn(

// we only care about this exported thing if it's a function or arrow function
const declaration = value.declarations[0]
// grab the initialized value
let { init } = declaration
if (!init) {
continue
}

if (
declaration.init?.type === 'FunctionExpression' ||
declaration.init?.type === 'ArrowFunctionExpression'
) {
return declaration.init
// if the value is a satisfies expression, we need to use the actual expression
if (init.type === 'TSSatisfiesExpression') {
init = init.expression
}

if (init.type === 'FunctionExpression' || init.type === 'ArrowFunctionExpression') {
return init
}
}
// it wasn't something we care about, move along
Expand Down

0 comments on commit b146ce5

Please sign in to comment.