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

@const variables used in style directives give "no-unused-vars" #174

Open
johnnysprinkles opened this issue Mar 30, 2022 · 4 comments
Open

Comments

@johnnysprinkles
Copy link

I have code like this:

        {#each items as item}
          {@const left = (<some expression>) * 100 + '%'}
          {@const width = (<some expression>) * 100 + '%'}
          {#if !item.secondary}
            <div class="item-container" style:left style:width>
              <Item {item} />
            </div>
          {/if}
        {/each}

But eslint with eslint-plugin-svelte3 report no-unused-vars, apparently not accounting for "left" and "width" being referenced in the style directives.

@johnnysprinkles
Copy link
Author

johnnysprinkles commented Mar 30, 2022

Note that I'm not seeing the same issue when used in a class directive. Also this is version 3.4.1.

@kling90
Copy link

kling90 commented May 19, 2022

Was about to create an issue about the same problem, though I was using @const with destructuring.
A silly example but eslint outputs a no-unused-vars error:

<script>
  const images = [
    { src: 'https://placeimg.com/500/500/any', width: '250px' },
    { src: 'https://placeimg.com/500/500/any', width: '250px' },
    { src: 'https://placeimg.com/500/500/any', width: '250px' },
  ];
</script>

{#each images as image}
  {@const { src, width } = image}
  <img {src} style:width alt />
{/each}

@stephane-vanraes
Copy link

To note that this does not give errors:

<script>
  const images = [
    { src: 'https://placeimg.com/500/500/any', w: '250px' },
    { src: 'https://placeimg.com/500/500/any', w: '250px' },
    { src: 'https://placeimg.com/500/500/any', w: '250px' }
  ];
</script>

{#each images as image}
  {@const { src, w } = image}
  <img {src} style:width={w} alt />
{/each}

So the problem seems to be with the shorthand

@molily
Copy link

molily commented Apr 15, 2023

<svelte:element this={variable}> triggers this problem as well:

<script>
const numbers = [1, 2, 3, 4];
</script>

{#each numbers as number}
  {@const tag = number === 1 ? 'h1' : 'p'}
  <svelte:element this={tag}>{number}</svelte:element>
{/each}

Error message: 'tag' is assigned a value but never used. eslint(no-unused-vars)

Workaround:

{@const tag = /* eslint-disable-line no-unused-vars */ number === 1 ? 'h1' : 'p'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants