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

no-unused-vars firing incorrectly on args inside template-tag

Open davidtaylorhq opened this issue 10 months ago • 7 comments

Given this snippet:

const array = [];

<template>
  {{#each array as |item index|}}
    {{index}}
  {{/each}}
</template>

Eslint will fail with

error  'item' is defined but never used  no-unused-vars

In the same file, I can do someFunction((item, index) => console.log(index)); in regular javascript with no errors about 'item' being unused.

So it seems that the default 'args: after-used' behaviour is not being applied properly inside template tags.

davidtaylorhq avatar Mar 28 '24 09:03 davidtaylorhq

Does _item also get reported as unused?

patricklx avatar Mar 28 '24 09:03 patricklx

Underscores don't seem to make any difference out-the-box. Calling it _item, or _ doesn't make any difference

If I configure an eslint argsIgnorePattern, then underscored variables can be ignored:

/*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/

const array = [];

<template>
  {{#each array as |_item index|}}
    {{index}}
  {{/each}}
</template>

^^ this passes ✅

But this argsIgnorePattern is not default eslint behaviour, so I imagine most projects rely on the default 'args: after-used'' behaviour.

davidtaylorhq avatar Mar 28 '24 10:03 davidtaylorhq

Oh, right. Does it make a difference of its a gts or gjs file?

patricklx avatar Mar 28 '24 10:03 patricklx

Same issue in both gjs and gts 😓

davidtaylorhq avatar Mar 28 '24 10:03 davidtaylorhq

Isn't this correct behavior tho? (Am i missing something? 😅)

NullVoxPopuli avatar Mar 28 '24 12:03 NullVoxPopuli

This fails :x:

<template>
  {{#each array as |item index|}}
    {{index}}
  {{/each}}
</template>

This passes ✅

someFunction((item, index) => console.log(index));

Conceptually, they are the same, so the eslint result should be the same for both examples.

Eslint docs say

unused positional arguments that occur before the last used argument will not be checked, but all named arguments and all positional arguments after the last used argument will be checked.

davidtaylorhq avatar Mar 28 '24 12:03 davidtaylorhq

Ah! Thanks for that clarification! I didn't know they differentiate with prior positionals!

NullVoxPopuli avatar Mar 28 '24 12:03 NullVoxPopuli