Skip to content

New Rule: svelte/kit-parent-function-at-bottom rule #438

Open
@baseballyama

Description

@baseballyama

Motivation

I would like to recognize performance overhead regarding parent.

/** @type {import('./$types').PageLoad} */
export async function load({ params, parent }) {
  const parentData = await parent();
  //                       ^ `parent` function can execute later to avoid a waterfall
  const data = await getData(params);

  return {
    ...data
    meta: { ...parentData.meta, ...data.meta }
  };
}

Description

SvelteKit has parent function. And according to the document, parent should use at the bottom of load function as much as possible in terms of performance.

https://kit.svelte.dev/docs/load#using-parent-data

But it's too difficult to recognize by human code review, so I would like to recognize this automatically.

Examples

/** @type {import('./$types').PageLoad} */
export async function load({ params, parent }) {
  // NG
  const parentData = await parent();
  const data = await getData(params);
  // OK
  const parentData = await parent();
  // OK (Because `doSomething` needs to use the return value of `parent`.)
  const foo = await doSomething(parentData);

  return {
    ...data
    meta: { ...parentData.meta, ...data.meta }
  };
}

Additional comments

I need to think about better rule name.😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions