ember-cli / eslint-plugin-ember

An ESLint plugin that provides set of rules for Ember applications based on commonly known good practices.
MIT License
257 stars 198 forks source link

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

Closed davidtaylorhq closed 1 month ago

davidtaylorhq commented 3 months ago

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.

patricklx commented 3 months ago

Does _item also get reported as unused?

davidtaylorhq commented 3 months ago

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.

patricklx commented 3 months ago

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

davidtaylorhq commented 3 months ago

Same issue in both gjs and gts 😓

NullVoxPopuli commented 3 months ago

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

davidtaylorhq commented 3 months ago

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.

NullVoxPopuli commented 3 months ago

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