New Rule: svelte/kit-parent-function-at-bottom rule
#438This PR introduces the getSvelteContext
function. This function will be invoked at the beginning of every rule before the release of eslint-plugin-svelte v3. It enables the conditional execution of linting based on factors such as the Svelte version SvelteKit route etc. By doing so, it allows users can use recommended
config regardless of the Svelte version they are using. As a result, unnecessary rules will be effectively disabled automatically.
After merging this PR and applying the changes to all rules, I plan to update the recommended config and release v3. Consequently, I will close #957.
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 }
};
}
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.
/** @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 }
};
}
I need to think about better rule name.😅
New rule sound good to me! We need a better rule name, as you say, but I don't have any ideas 😓
How about svelte/avoid-waterfall-in-kit-loads
?
Hmm. I'm not familiar with English, so I'm not sure what "waterfall" means. So I'm not sure if that naming of the rule makes sense 😓. I would like someone's opinion on that name.
waterfall
is used in the kit doc. So this word itself is natural.
But to protect waterfall
, possibly we need to take care something more in the future, so I'd like to use a name that has a specific check purpose.
I mean "This is is just check the place of parent
function. Not check everything regarding avoiding waterfall"