Skip to content

Commit

Permalink
prefer-keyboard-event-key: Improve logic (#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 9, 2021
1 parent b564ff1 commit a32d8f8
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions rules/prefer-keyboard-event-key.js
Expand Up @@ -80,20 +80,29 @@ const fix = node => fixer => {
return;
}

const {right = {}, operator} = nearestIf.test;
const isTestingEquality = operator === '==' || operator === '===';
const isRightValid = isTestingEquality && right.type === 'Literal' && typeof right.value === 'number';
const {type, operator, right} = nearestIf.test;
if (
!(
type === 'BinaryExpression'
&& (operator === '==' || operator === '===')
&& right.type === 'Literal'
&& typeof right.value === 'number'
)
) {
return;
}

// Either a meta key or a printable character
const keyCode = translateToKey[right.value] || String.fromCharCode(right.value);
const key = translateToKey[right.value] || String.fromCodePoint(right.value);
// And if we recognize the `.keyCode`
if (!isRightValid || !keyCode) {
if (!key) {
return;
}

// Apply fixes
return [
fixer.replaceText(node, 'key'),
fixer.replaceText(right, quoteString(keyCode)),
fixer.replaceText(right, quoteString(key)),
];
};

Expand Down

0 comments on commit a32d8f8

Please sign in to comment.