eslint-plugin-svelte3 icon indicating copy to clipboard operation
eslint-plugin-svelte3 copied to clipboard

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

Open johnnysprinkles opened this issue 3 years ago • 4 comments

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 avatar Mar 30 '22 03:03 johnnysprinkles

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

johnnysprinkles avatar Mar 30 '22 03:03 johnnysprinkles

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}

kling90 avatar May 19 '22 06:05 kling90

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

stephane-vanraes avatar May 19 '22 06:05 stephane-vanraes

<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'}

molily avatar Apr 15 '23 19:04 molily