https://github.com/sindresorhus/eslint-plugin-unicorn/issues/118Go to Github

FundedView inPrefer `event.key` over `event.keyCode`#118

sindresorhuscreated this issue 13 days ago

For readability. Got the idea when doing https://github.com/wulkano/kap/commit/71be04f7411aa33d0b393c05077c619de2ab07be.

Should trigger when in an addEventListener callback.

Fail

foo.addEventListener('click', event => {
    if (event.keyCode === '27') {
        …
    }
});

Pass

foo.addEventListener('click', event => {
    if (event.key === 'Escape') {
        …
    }
});

More info: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key


For the first implementation, we can just naively look for the same variable name as defined as the parameter. Usually event or e.

Later on, we could add code path support and support having the function outside the addEventListener call:

const handler = event => {
    if (event.key === 'Escape') {
        …
    }
};

…

foo.addEventListener('click', handler);

@kevva @SamVerschueren Thoughts?

Logs

Amount funded

$80.00

Resolve this issue and earn $72.00

Fund this issue

Fund as

Payment method

Amount(USD)

Please sign in first to fund this issue

Sign in with

Pull requests

0

    Submit

    URL of your pull request

    Submit your pull request

    Please sign in first to submit a pull request

    Sign in with