43081j / 43081j/eslint-plugin-lit

`markVariableAsUsed` for the `customElement` decorator

オープン
#195 コメント 11 件 リアクション 2 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
138
フォーク
26
平均マージ
23時間 29分
マージ済み PR(30日)
4

説明

Hello! Thank you for such a cool plugin!

There is currently an issue where if you use a custom component declaration through the `customElement` decorator on a class, ESLint with the `no-unused-vars` rule will complain that the class is not used, even though it is actually being used. This can be circumvented by declaring the custom component through the `customElements.define` function, but this is a workaround. Of course, it is preferable to use decorators.

In the **typescript-eslint** repository, you can find the [following comment](https://github.com/typescript-eslint/typescript-eslint/issues/6260#issuecomment-1463068196):

> [bradzacher](https://github.com/bradzacher) commented [on Mar 10, 2023](https://github.com/typescript-eslint/typescript-eslint/issues/6260#issuecomment-1463068196)
We do not encode semantics for framework-specific side-effects in our lint rules.
We purely work on the code you provide.
If the class is not referenced, but is indirectly used by a side-effect in the generator - then it's unused.
If you'd like to encode information about your specific framework's side-effects into the rule - you can freely do that by creating a new rule that marks the variable as used using ESLint's standard API, context.markVariableAsUsed:
https://eslint.org/docs/latest/extend/custom-rules#:~:text=markVariableAsUsed(name)%20%2D%20marks%20a%20variable%20with%20the%20given%20name%20in%20the%20current%20scope%20as%20used.%20This%20affects%20the%20no%2Dunused%2Dvars%20rule.%20Returns%20true%20if%20a%20variable%20with%20the%20given%20name%20was%20found%20and%20marked%20as%20used%2C%20otherwise%20false.
Here are two example rules that do exactly that:
https://github.com/jsx-eslint/eslint-plugin-react/blob/master/lib/rules/jsx-uses-react.js
https://github.com/jsx-eslint/eslint-plugin-react/blob/master/lib/rules/jsx-uses-vars.js

It seems reasonable to agree with it that frameworks should handle this themselves.

Another example is when I had to work with the ESLint plugin **eslint-plugin-jsdoc**, which exhibits similar behavior. By default, ESLint complains if you only use a type in comments, as it does not mark such values as `markVariableAsUsed`. To fix this, the **eslint-plugin-jsdoc** team created their own rule called [no-undefined-types](https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/no-undefined-types.md), which marks all values as used that are only used in comments via `markVariableAsUsed`.

I feel like something similar should emerge in **eslint-plugin-lit**. Or maybe I haven't found a better way to fix it. I'd be glad to hear any of your thoughts.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。