Skip to content
Merged

Lint #5845

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
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@cheap-glitch/mi-cron": "^1.0.1",
"@primer/octicons-react": "^17.3.0",
"array-union": "^3.0.1",
"code-tag": "^1.1.0",
"debounce-fn": "^5.1.2",
"delay": "^5.0.0",
"delegate-it": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions source/features/ci-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as pageDetect from 'github-url-detection';

import features from '.';
import * as api from '../github-helpers/api';
import {getRepo} from '../github-helpers';
import {buildRepoURL} from '../github-helpers';
import attachElement from '../helpers/attach-element';

async function getHead(): Promise<string> {
Expand All @@ -23,7 +23,7 @@ async function getHead(): Promise<string> {
}

function getCiDetails(commit: string): HTMLElement {
const endpoint = `/${getRepo()!.nameWithOwner}/commits/checks-statuses-rollups`;
const endpoint = buildRepoURL('commits/checks-statuses-rollups');
return (
// `span` also required by `attachElement`’s deduplicator
<span className="rgh-ci-link">
Expand Down
2 changes: 1 addition & 1 deletion source/features/clean-conversation-headers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function cleanIssueHeader(): Promise<void | false> {
}

async function cleanPrHeader(): Promise<void | false> {
const byline = await elementReady('.gh-header-meta .flex-auto:not(.rgh-clean-conversation-headers)');
const byline = await elementReady('.gh-header-meta > .flex-auto:not(.rgh-clean-conversation-headers)');
if (!byline) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion source/features/conversation-activity-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {CheckIcon, EyeClosedIcon, EyeIcon, XIcon} from '@primer/octicons-react';
import {wrap} from '../helpers/dom-utils';
import features from '.';
import onNewComments from '../github-events/on-new-comments';
import registerHotkey from '../github-helpers/register-hotkey';
import {registerHotkey} from '../github-helpers/hotkey';
import onConversationHeaderUpdate from '../github-events/on-conversation-header-update';

const expectedDropdownWidth = 270;
Expand Down
2 changes: 1 addition & 1 deletion source/features/create-release-shortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {addHotkey} from '../github-helpers';
import {addHotkey} from '../github-helpers/hotkey';
import addQuickSubmit from './submission-via-ctrl-enter-everywhere';

function init(): void {
Expand Down
2 changes: 1 addition & 1 deletion source/features/pagination-hotkey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {addHotkey} from '../github-helpers';
import {addHotkey} from '../github-helpers/hotkey';

const nextPageButtonSelectors = [
'a.next_page', // Issue/PR list, Search
Expand Down
2 changes: 1 addition & 1 deletion source/features/parse-backticks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {parseBackticks} from '../github-helpers/dom-formatters';

function init(): void {
const selectors = [
'.BorderGrid--spacious .f4.mt-3', // `isRepoHome` repository description
'.BorderGrid--spacious .f4.my-3', // `isRepoHome` repository description
'.js-commits-list-item pre', // `isCommitList` commit description
'.js-commit-group .pr-1 code', // `isPRConversation` commit message
'.js-commit-group pre', // `isPRConversation` commit description
Expand Down
2 changes: 1 addition & 1 deletion source/features/pull-request-hotkeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {addHotkey} from '../github-helpers';
import {addHotkey} from '../github-helpers/hotkey';

function init(): void {
const tabs = select.all('#partial-discussion-header + .tabnav a.tabnav-tab');
Expand Down
29 changes: 9 additions & 20 deletions source/features/restore-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,19 @@ async function restoreFile(progress: (message: string) => void, menuItem: Elemen
}
}

const filesRestored = new WeakSet<HTMLButtonElement>();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was needed because we kept the dropdown open, I think. Since the dropdown now closes on click, the user cannot double click. By the time they manage to open the menu and click again, it's probably done and the file is gone.

async function handleRestoreFileClick(event: DelegateEvent<MouseEvent, HTMLButtonElement>): Promise<void> {
const menuItem = event.delegateTarget;

// Only allow one click
if (filesRestored.has(menuItem)) {
return;
}

filesRestored.add(menuItem);

try {
await showToast(async progress => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

showToast already acts as its own try/catch, with errors being shown in the toast itself. No need for an extra try/catch as long as everything is inside.

const filePath = menuItem.closest<HTMLDivElement>('[data-path]')!.dataset.path!;
// Show toast while restoring
await showToast(async progress => restoreFile(progress!, menuItem, filePath), {
message: 'Restoring…',
doneMessage: 'Restored!',
});

// Hide file from view
menuItem.closest('.file')!.remove();
} catch (error) {
features.log.error(import.meta.url, error);
}
return restoreFile(progress!, menuItem, filePath);
}, {
message: 'Restoring…',
doneMessage: 'Restored!',
});

// Hide file from view
menuItem.closest('.file')!.remove();
}

function handleMenuOpening({delegateTarget: dropdown}: DelegateEvent): void {
Expand Down
2 changes: 1 addition & 1 deletion source/features/select-all-notifications-shortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import registerHotkey from '../github-helpers/register-hotkey';
import {registerHotkey} from '../github-helpers/hotkey';

function selectAllNotifications(): void {
select('.js-notifications-mark-all-prompt')!.click();
Expand Down
2 changes: 1 addition & 1 deletion source/features/selection-in-new-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import select from 'select-dom';
import onetime from 'onetime';

import features from '.';
import registerHotkey from '../github-helpers/register-hotkey';
import {registerHotkey} from '../github-helpers/hotkey';

function openInNewTab(): void {
const selected = select('.navigation-focus a.js-navigation-open[href]');
Expand Down
22 changes: 22 additions & 0 deletions source/github-helpers/hotkey.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'dom-chef';

export function registerHotkey(hotkey: string, action: VoidFunction | string): Deinit {
const element = typeof action === 'string'
? <a hidden href={action} data-hotkey={hotkey}/>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I also added support for strings, but haven't found any use for it where it would simplify the code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if we treat this string as a selector and do select(action).click()?

To emulate link behavior, just pass an action of () => location.assign(link).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What if we treat this string as a selector and do select(action).click()?

addHotkey already does that:

registerHotkey('ctrl-a', 'a[blah]') // Your suggestion
addHotkey(select('a[blah]'), 'ctrl-a') // Already-possible solution

To emulate link behavior, just pass an action of () => location.assign(link).

There's no substitute for real links (which can be handled by Turbo as well).

I do see two possible improvements here:

  • make the two functions consistent (always accept "hotkey" first), or
  • merge the two functions so registerHotkey accepts an element passed as "action"

: <button hidden type="button" data-hotkey={hotkey} onClick={action}/>;

document.body.append(element);

return () => {
element.remove();
};
}

/** Safely add a hotkey to an element, preserving any existing ones and avoiding duplicates */
export const addHotkey = (button: HTMLAnchorElement | HTMLButtonElement | undefined, hotkey: string): void => {
if (button) {
const hotkeys = new Set(button.dataset.hotkey?.split(','));
hotkeys.add(hotkey);
button.dataset.hotkey = [...hotkeys].join(',');
}
};
31 changes: 31 additions & 0 deletions source/github-helpers/hotkeys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {test, assert} from 'vitest';

import {
addHotkey,
} from './hotkey';

const testAddHotkey = (existing: string | undefined, added: string, final: string): void => {
const link = document.createElement('a');
if (existing) {
link.setAttribute('data-hotkey', existing);
}

addHotkey(link, added);
assert.equal(link.dataset.hotkey, final);
};

test('addHotkey if one is specified', testAddHotkey.bind(null,
'T-REX',
'CHICKEN',
'T-REX,CHICKEN',
));
test('addHotkey if the same is already specified', testAddHotkey.bind(null,
'CHICKEN',
'CHICKEN',
'CHICKEN',
));
test('addHotkey when none are specified', testAddHotkey.bind(null,
undefined,
'CHICKEN',
'CHICKEN',
));
27 changes: 0 additions & 27 deletions source/github-helpers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
compareNames,
getLatestVersionTag,
shouldFeatureRun,
addHotkey,
} from '.';

test('getConversationNumber', () => {
Expand Down Expand Up @@ -167,29 +166,3 @@ test('shouldFeatureRun', () => {
exclude: yesNo,
}), 'If any `exclude` is true, then it should not run, regardless of `asLongAs` and `include`');
});

const testAddHotkey = (existing: string | undefined, added: string, final: string): void => {
const link = document.createElement('a');
if (existing) {
link.setAttribute('data-hotkey', existing);
}

addHotkey(link, added);
assert.equal(link.dataset.hotkey, final);
};

test('addHotkey if one is specified', testAddHotkey.bind(null,
'T-REX',
'CHICKEN',
'T-REX,CHICKEN',
));
test('addHotkey if the same is already specified', testAddHotkey.bind(null,
'CHICKEN',
'CHICKEN',
'CHICKEN',
));
test('addHotkey when none are specified', testAddHotkey.bind(null,
undefined,
'CHICKEN',
'CHICKEN',
));
9 changes: 0 additions & 9 deletions source/github-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,3 @@ export function shouldFeatureRun({
}): boolean {
return asLongAs.every(c => c()) && include.some(c => c()) && exclude.every(c => !c());
}

/** Safely add a hotkey to an element, preserving any existing ones and avoiding duplicates */
export const addHotkey = (button: HTMLAnchorElement | HTMLButtonElement | undefined, hotkey: string): void => {
if (button) {
const hotkeys = new Set(button.dataset.hotkey?.split(','));
hotkeys.add(hotkey);
button.dataset.hotkey = [...hotkeys].join(',');
}
};
10 changes: 0 additions & 10 deletions source/github-helpers/register-hotkey.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion source/helpers/abort-controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function callHandle(handle: DeinitHandle): void {
if ('disconnect' in handle) { // Browser observers
handle.disconnect();
} else if ('abort' in handle) { // Selector observer
} else if ('abort' in handle) {
handle.abort();
} else if (typeof handle === 'function') {
handle();
Expand Down
2 changes: 1 addition & 1 deletion source/helpers/hotfix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'dom-chef';
import cache from 'webext-storage-cache';
import {isEnterprise} from 'github-url-detection';
import compareVersions from 'tiny-version-compare';
import {any as concatenateTemplateLiteralTag} from 'code-tag';

import {RGHOptions} from '../options-storage';
import isDevelopmentVersion from './is-development-version';
import {concatenateTemplateLiteralTag} from './template-literal';

function parseCsv(content: string): string[][] {
const lines = [];
Expand Down
13 changes: 0 additions & 13 deletions source/helpers/template-literal.ts

This file was deleted.