New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add prefer-event-key
rule
#226
Conversation
key
. Fixes #118prefer-key-over-key-code
rule
5e9e6fd
to
3c24b81
Compare
I think my approach isn't very good here, trying to first narrow down to Following up with a better one where in I directly select |
One foreseeable gotcha with const something = undefined;
element.addEventListener('keyup', event => {
console.log(something.keyCode); // This `.keyCode` is "inside an event callback"
}); I think this can be correctly solved by targeting the callback function in the visitor, then getting it's scope and checking all the references to the first argument to see if they are |
I'll have the CallbackExpression in the Also I assume we only want to target |
We might as well target all events, since accessing @sindresorhus Any thoughts? @ankeetmaini If we indeed want to target only |
Hey, thanks for the pull request 🙌 I have gotten a huge response on the IssueHunt bounty issues and I now have a very long backlog of pull requests to review, so it might take some time until I will be able to review this. However, you can help move this along by helping out and reviewing the other submissions. If everyone helps everyone we can manage this 👌. Some things to look for when reviewing:
|
👍 |
prefer-key-over-key-code
ruleprefer-key-over-key-code
rule
@futpib this version shows my current approach. It also takes care of the edge case you suggested. Will poke a bit more before making it final. |
fed4239
to
c3d57a4
Compare
I am happy with the current approach and have added a lot of tests for valid/invalid code. The rule also covers the destructuring cases too. Please take a look :) The thing that I am not convinced on is the fixing part. How do we fix if the case is like bar.addEventListener('keyup', e => {
sendThisToAnOtherModule(e.keyCode)
}) In this even if I try to fix it, it'll break the contract as the the receiving party might not accept a string representation of the key. And in other case like const keys = {
'enter': 34
}
bar.addEventListener('keyup', e => {
if (e.keyCode === keys.enter) return;
}) I am not sure if we should try to fix as we might step over and break things. Please advise! |
I think best we can do is fix only comparisons against literals (like |
a916e74
to
4464281
Compare
Update: @futpib I've added the fixer code which takes care of direct if conditions. if (e.keyCode === 27) -> if (e.key === 'Escape') but the case const {keyCode} = e;
if (keyCode === 27) // do something needs to be looked into. |
@ankeetmaini Would be nice to handle destructurings too, but I think we can give it a pass, as other rules like this ( Unless @sindresorhus disagrees. |
@futpib I'm planning to give |
Hi @futpib, this is ready for review now. Also I couldn't figure out a way to fix destructured variables in I tried multiple ways, only one handler for all all keyCode, which etc (currently I target const {keyCode} = event Here this generates into two identifiers, where both the key and value of Even if I fix this, when the above variable is referenced again I wasn't able to find out if it is resolved from the destructured variable, as the AST walk function is called separately for them. I also tried using |
index.js
Outdated
@@ -56,7 +56,8 @@ module.exports = { | |||
'unicorn/prefer-node-append': 'error', | |||
'unicorn/prefer-query-selector': 'error', | |||
'unicorn/prefer-node-remove': 'error', | |||
'unicorn/prefer-text-content': 'error' | |||
'unicorn/prefer-text-content': 'error', | |||
'unicorn/prefer-key-over-key-code': 'error' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sindresorhus Should this rule be named prefer-key
or prefer-event-key
, without the "over something else" part, like the other rules, or is the current name fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be prefer-event-key
.
prefer-key-over-key-code
ruleprefer-key-over-key-code
rule
I guess you were looking for |
prefer-key-over-key-code
ruleprefer-key-over-key-code
rule
91dca31
to
15b5e5f
Compare
@futpib thanks a lot for a thorough review! I've fixed and updated the implementation as per your comments. Let me know! |
|
||
Enforces the use of [`KeyboardEvent#key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) over [`KeyboardEvent#keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) which is deprecated. The `key` property is also more semantic and readable. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please describe that it's fixable and what is fixable. See the other docs for how to write it.
@ankeetmaini Can you fix the merge conflict and resolve the last feedback? |
@sindresorhus I am so sorry for not getting to it I'll update this shortly. Thanks for updating this PR. Will also address @futpib's comment. :) |
@sindresorhus all requested changes have been pushed! |
// @MrHen |
Looks good to me now. Thanks for contributing, @ankeetmaini :) |
Thanks so much! Yay! |
Fixes #118