Skip to content
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

[ESLint] Prefer KeyboardEvent#key over KeyboardEvent#keyCode #3126

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-rice-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/react': patch
---

Prefer KeyboardEvent#key over KeyboardEvent#keyCode
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ module.exports = {
message:
"`useMemo` with an empty dependency array can't provide a stable reference, use `useRef` instead.",
},
{
// ❌ event.keyCode
selector: 'MemberExpression > .property[type=Identifier][name=keyCode]',
message: 'Use `.key` instead of `.keyCode`',
},
],
'no-ternary': 0,
'no-underscore-dangle': 0,
Expand Down Expand Up @@ -279,6 +284,8 @@ module.exports = {
'unicorn/prefer-node-protocol': 'error',
'import/no-unresolved': ['error', { ignore: ['^node:'] }],
'unicorn/prefer-string-replace-all': 'error',
// doesn't catch a lot of cases; we use ESLint builtin `no-restricted-syntax` to forbid `.keyCode`
'unicorn/prefer-keyboard-event-key': 'off',

'unicorn/prefer-switch': 'error',
// TODO: Fix all errors for the following rules included in recommended config
Expand Down
11 changes: 4 additions & 7 deletions packages/graphiql-react/src/editor/header-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,10 @@ export function useHeaderEditor(
});

newEditor.on('keyup', (editorInstance, event) => {
const code = event.keyCode;
if (
(code >= 65 && code <= 90) || // letters
(!event.shiftKey && code >= 48 && code <= 57) || // numbers
(event.shiftKey && code === 189) || // underscore
(event.shiftKey && code === 222) // "
) {
const { keyCode, key, shiftKey } = event;
const isLetter = keyCode >= 65 && keyCode <= 90;
const isNumber = keyCode >= 48 && keyCode <= 57;
if (isLetter || (!shiftKey && isNumber) || key === '_' || key === '"') {
editorInstance.execCommand('autocomplete');
}
});
Expand Down
11 changes: 4 additions & 7 deletions packages/graphiql-react/src/editor/variable-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,10 @@ export function useVariableEditor(
});

newEditor.on('keyup', (editorInstance, event) => {
const code = event.keyCode;
if (
(code >= 65 && code <= 90) || // letters
(!event.shiftKey && code >= 48 && code <= 57) || // numbers
(event.shiftKey && code === 189) || // underscore
(event.shiftKey && code === 222) // "
) {
const { keyCode, key, shiftKey } = event;
const isLetter = keyCode >= 65 && keyCode <= 90;
const isNumber = keyCode >= 48 && keyCode <= 57;
if (isLetter || (!shiftKey && isNumber) || key === '_' || key === '"') {
editorInstance.execCommand('autocomplete');
}
});
Expand Down
5 changes: 3 additions & 2 deletions packages/graphiql-react/src/explorer/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ export function Search() {

useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.metaKey && event.keyCode === 75 && inputRef.current) {
inputRef.current.focus();
if (event.metaKey && event.key === 'k') {
inputRef.current?.focus();
}
}

window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, []);
Expand Down
6 changes: 2 additions & 4 deletions packages/graphiql-react/src/history/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ export function HistoryItem(props: QueryHistoryItemProps) {
defaultValue={props.item.label}
ref={inputRef}
onKeyDown={e => {
if (e.keyCode === 27) {
// Escape
if (e.key === 'Esc') {
setIsEditable(false);
} else if (e.keyCode === 13) {
// Enter
} else if (e.key === 'Enter') {
setIsEditable(false);
editLabel({ ...props.item, label: e.currentTarget.value });
}
Expand Down
4 changes: 3 additions & 1 deletion packages/graphiql-react/src/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
const counter = ++counterRef.current;

const maybeIntrospectionData = props.schema;

async function fetchIntrospectionData() {
if (maybeIntrospectionData) {
// No need to introspect if we already have the data
Expand Down Expand Up @@ -317,10 +318,11 @@ export function SchemaContextProvider(props: SchemaContextProviderProps) {
*/
useEffect(() => {
function triggerIntrospection(event: KeyboardEvent) {
if (event.keyCode === 82 && event.shiftKey && event.ctrlKey) {
if (event.ctrlKey && event.key === 'R') {
Comment on lines -320 to +321
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to check event.shiftKey because key: 'R' is always with shift

introspect();
}
}

window.addEventListener('keydown', triggerIntrospection);
return () => window.removeEventListener('keydown', triggerIntrospection);
});
Expand Down