New Rule: svelte/valid-context-access
close: #448
This is came from sveltejs/svelte#7864
According to the docs, getContext needs to call during component initialization.
Svelte team tried to implement this in the compiler, but this is a bit difficult, so I thought that we will implement this as ESLint rule.
https://svelte.dev/docs#run-time-svelte-getcontext
Retrieves the context that belongs to the closest parent component with the specified key. Must be called during component initialisation.
<script>
import { getContext } from 'svelte';
let test = getContext('test');
</script>
<!-- ✓ GOOD -->
<a href={test}>xxx</a>
<!-- ✗ BAD -->
<a href={getContext('test')}>xxx</a>
No response
I have read the documentation. It seems that getContext isn't the only thing that has to be called during component initialization. So other functions such as setContext, hasContext should also be checked. Also, it seems necessary to warn when used inside $:, not just in templates.
Additionally, I think the rule name should be something like valid-context-access.