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: require-strict-events #345

Closed
marekdedic opened this issue Jan 22, 2023 · 5 comments · Fixed by #365
Closed

New rule: require-strict-events #345

marekdedic opened this issue Jan 22, 2023 · 5 comments · Fixed by #365
Labels
enhancement New feature or request help wanted Extra attention is needed new rule

Comments

@marekdedic
Copy link
Contributor

Motivation

Hi,
when using TypeScript with svelte, component events aren't automatically typed - e. g. you can have an on:nonexistent={() => {}} handle for a non-existent event. However, svelte does check this, if the opening script tag of the child component has the strictEvents attribute. (<script lang="ts" strictEvents>).

I propose to add a rule that would enforce this.

Description

The rule should enforce that components have the strictEvents attribute present in their opening svelte tag.

I propose several an option to choose for which components the attribute should be enforced:

  1. Require the attribute on all components. This makes sense since for a component that does not dispatch any event, the attribute still makes svelte check that no event listeners are present. I propose that this should be the default.
  2. Only check components that actually dispatch an event - this would probably be more complicated to implement

Additional note: For some components that have event mixins, the strictEvents attribute does not work and you have to declare the $$Events interface. In that case, I think the rule shouldn't fire - so effectively, it would require either the strictEvents attribute or a manually declared interface $$Events for the component.

Examples

<!-- ✓ GOOD -->
<script lang="ts" strictEvents>
</script>

<script lang="ts">
  interface $$Events {}
</script>

<!-- ✗ BAD -->
<script lang="ts">
</script>

Additional comments

The relevant RFC.

@marekdedic marekdedic added enhancement New feature or request new rule labels Jan 22, 2023
@ota-meshi
Copy link
Member

Thanks for the rule suggestions.
That rule sounds good to me, but it looks like the RFCs haven't been merged yet.
New rule should be marked as experimental until the RFC is merged.
sveltejs/rfcs#38

@ota-meshi ota-meshi added the help wanted Extra attention is needed label Jan 23, 2023
@marekdedic
Copy link
Contributor Author

Hi,
I am honestly not sure how the RFC process works with svelte - the features mentioned in the RFC aren't mentioned in the docs for example, however, they do work for me with the current stable release.

If they're experimental, though, the rule should be too.

Thanks!

@marekdedic
Copy link
Contributor Author

Relevant PR: sveltejs/language-tools#1054

@marekdedic
Copy link
Contributor Author

Hi,
what exactly do you mean by "experimental" rule? I see no such option in eslint rule metadata...

Do you mean just a warning in the readme and a separate section in the rule list?

Thanks :)

@ota-meshi
Copy link
Member

I think it would be nice to prefix the rule name with "experimental-". When it becomes GA, we will change the rule name and release it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants