43081j / 43081j/eslint-plugin-lit

`markVariableAsUsed` for the `customElement` decorator

Đang mở
#195 11 bình luận 2 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
138
Fork
26
Merge trung bình
23 giờ 29 phút
Pull request đã merge (30 ngày)
4

Mô tả

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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.