Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parent in load functions receive @typescript-eslint/unbound-method error #12622

Open
rakuzen25 opened this issue Aug 28, 2024 · 0 comments
Open

Comments

@rakuzen25
Copy link

rakuzen25 commented Aug 28, 2024

Describe the bug

My +page.ts looks like something like this:

export const load: PageLoad = async ({ fetch, parent }) => {
    const { uid } = (await parent()).session?.user ?? {};
    if (!uid) {
        throw new TypeError("User ID not found");
    }

    // Do something
};

However, eslint seems not very happy about the parent function reference and throws

Avoid referencing unbound methods which may cause unintentional scoping of `this`.
If your function does not access `this`, you can annotate it with `this: void`, or consider using an arrow function instead. eslint(@typescript-eslint/unbound-method)

Docs on unbound-method

Reproduction

MRE on Stackblitz

Run pnpm eslint ./src to see the errors.

Logs

/home/projects/stackblitz-starters-1y7qpk/src/routes/sum/+layout.ts
  3:42  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`.
If your function does not access `this`, you can annotate it with `this: void`, or consider using an arrow function instead  @typescript-eslint/unbound-method

/home/projects/stackblitz-starters-1y7qpk/src/routes/sum/+page.ts
  3:40  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`.
If your function does not access `this`, you can annotate it with `this: void`, or consider using an arrow function instead  @typescript-eslint/unbound-method

✖ 2 problems (2 errors, 0 warnings)

System Info

System:
  OS: Linux 5.0 undefined
  CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 0 Bytes / 0 Bytes
  Shell: 1.0 - /bin/jsh
Binaries:
  Node: 18.20.3 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.2.3 - /usr/local/bin/npm
  pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.2.4 
  @sveltejs/kit: ^2.0.0 => 2.5.25 
  @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.2 
  svelte: ^4.2.7 => 4.2.19 
  vite: ^5.0.3 => 5.4.2

Severity

annoyance

Additional Information

Patrick in the server mentioned that perhaps

/**
* `await parent()` returns data from parent `+layout.js` `load` functions.
* Implicitly, a missing `+layout.js` is treated as a `({ data }) => data` function, meaning that it will return and forward data from parent `+layout.server.js` files.
*
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
*/
parent(): Promise<ParentData>;

and

/**
* `await parent()` returns data from parent `+layout.server.js` `load` functions.
*
* Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
*/
parent(): Promise<ParentData>;

needs to be changed to parent(this: void): Promise<ParentData>; or parent: () => Promise<ParentData>;, but it seems like this file is auto-generated…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant