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

Add rule no-unused-props #1028

Open
oekazuma opened this issue Jan 15, 2025 · 2 comments · May be fixed by #1061
Open

Add rule no-unused-props #1028

oekazuma opened this issue Jan 15, 2025 · 2 comments · May be fixed by #1061
Labels
enhancement New feature or request new rule

Comments

@oekazuma
Copy link

oekazuma commented Jan 15, 2025

Motivation

This rule aims to ensure consistency between the defined Props type and the actual use of $props().
This prevents mistakes due to overlooking properties and improves code readability and maintainability.
Enforcing this rule helps developers understand and reduces the risk of potential bugs.

Description

Issue a warning if the type of the defined Props does not match the variable destructuring assignment in $props().

Examples

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

  interface Props {
    a: string;
    b: number;
  }

  let { a, b }: Props = $props();

  /* ✗ BAD */

  interface Props {
    a: string;
    b: number;
  }

  let { a }: Props = $props();
</script>

Additional comments

No response

@oekazuma oekazuma added enhancement New feature or request new rule labels Jan 15, 2025
@ota-meshi
Copy link
Member

Thank you for proposing the rule!
That rule sounds good to me!

If possible, it would be even better if it could check the usage of unnamed props as well.

<script lang="ts">
	interface Props {
		a: number;
		[key: string]: unknown; // unused
	}

	let {a}: Props = $props();
        // We can guess that the following would be correct:
	// let {a, ...other}: Props = $props();
</script>

And it would be even better if the rule could check member access as well.

<script lang="ts">
  interface Props {
    a: string;
    b: number; // unused
  }

  let props: Props = $props();
</script>

{props.a}
<script lang="ts">
  interface Props {
    a: string;
    b: number;
    c: number; // unused
  }

  let { a, ...otherProps }: Props = $props();
</script>

{otherProps.b}

I think the rule name might be better, something like no-unused-props.
What do you think?

@oekazuma
Copy link
Author

Thanks for the good additional suggestions!

I agree with the rule name, no-unused-props.

@oekazuma oekazuma changed the title Add rule consistent-props-usage Add rule no-unused-props Jan 16, 2025
@baseballyama baseballyama linked a pull request Feb 8, 2025 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