Skip to content

Commit

Permalink
fix(sveltekit): Wrap load when typed explicitly (#8049)
Browse files Browse the repository at this point in the history
When `load` is explicitly typed (using `export const load: LayoutLoad = ...`), auto-instrumentation failed. This patch extends the regex to handle this case and adds a test for it.
  • Loading branch information
sreetamdas authored May 5, 2023
1 parent 2cc7708 commit 194130e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/sveltekit/src/vite/autoInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export async function canWrapLoad(id: string, debug: boolean): Promise<boolean>
debug && console.log(`Skipping wrapping ${id} because it already contains Sentry code`);
}

const hasLoadDeclaration = /((const|let|var|function)\s+load\s*(=|\())|as\s+load\s*(,|})/gm.test(codeWithoutComments);
const hasLoadDeclaration = /((const|let|var|function)\s+load\s*(=|\(|:))|as\s+load\s*(,|})/gm.test(
codeWithoutComments,
);
if (!hasLoadDeclaration) {
// eslint-disable-next-line no-console
debug && console.log(`Skipping wrapping ${id} because it doesn't declare a \`load\` function`);
Expand Down
6 changes: 6 additions & 0 deletions packages/sveltekit/test/vite/autoInstrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ describe('canWrapLoad', () => {
async function somethingElse(){};
export { somethingElse as load, foo }`,
],

[
'export variable declaration - inline function with assigned type',
`import type { LayoutLoad } from './$types';
export const load : LayoutLoad = async () => { return { props: { msg: "hi" } } }`,
],
])('returns `true` if a load declaration (%s) exists and no Sentry code was found', async (_, code) => {
fileContent = code;
expect(await canWrapLoad('+page.ts', false)).toEqual(true);
Expand Down

0 comments on commit 194130e

Please sign in to comment.