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

New Rule: svelte/valid-context-access #448

Open
baseballyama opened this issue Apr 26, 2023 · 1 comment · May be fixed by #480
Open

New Rule: svelte/valid-context-access #448

baseballyama opened this issue Apr 26, 2023 · 1 comment · May be fixed by #480
Labels
enhancement New feature or request new rule

Comments

@baseballyama
Copy link
Member

Motivation

This is came from sveltejs/svelte#7864

Description

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.

Examples

<script>
  import { getContext } from 'svelte';

  let test = getContext('test');
</script>



<!-- ✓ GOOD -->
<a href={test}>xxx</a>

<!-- ✗ BAD -->
<a href={getContext('test')}>xxx</a>

Additional comments

No response

@baseballyama baseballyama added enhancement New feature or request new rule labels Apr 26, 2023
@ota-meshi
Copy link
Member

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.

@baseballyama baseballyama changed the title New Rule: svelte/no-getContext-in-template New Rule: svelte/valid-context-access May 1, 2023
@baseballyama baseballyama linked a pull request May 14, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants