sveltejs / eslint-plugin-svelte

ESLint plugin for Svelte using AST
https://sveltejs.github.io/eslint-plugin-svelte/
MIT License
305 stars 38 forks source link

Add rule `require-event-prefix` #910

Open marekdedic opened 1 week ago

marekdedic commented 1 week ago

Motivation

In Svelte 5, all HTML element events are props starting with "on". Component events can have any name, so it is not clear, which props are events and which are not.

Description

I'd like to add a rule that requires that all props that are events (i.e. that are functions) start with "on"

Examples

<script lang="ts">
  /* ✓ GOOD */

  interface Props {
    regularProp: string;
    onclick(): void;
  }

  let { regularProp, onclick }: Props = $props;

  /* ✗ BAD */

  interface Props {
    click(): void;
  }

  let { click }: Props = $props;
</script>

Additional comments

I think this works even without TS, as the type would be in the parsed AST, right?

ota-meshi commented 1 day ago

Hmm. I think it would be useful to have that rule, but I'm worried about whether it's okay to judge all function props as event props 🤔.

What about implementing a rule that checks naming conventions for all props and provides examples for event names in the rule's documentation? The rule could also disallow the on prefix for non-function props.