Skip to content

Commit 8a53a39

Browse files
Fix a number of typescript errors (#32773)
Fixes 96 typescript errors. Behaviour changes are commented below. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent e619384 commit 8a53a39

19 files changed

+81
-80
lines changed

web_src/js/features/repo-branch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function initRepoCreateBranchButton() {
1111
for (const el of document.querySelectorAll('.show-create-branch-modal')) {
1212
el.addEventListener('click', () => {
1313
const modalFormName = el.getAttribute('data-modal-form') || '#create-branch-form';
14-
const modalForm = document.querySelector(modalFormName);
14+
const modalForm = document.querySelector<HTMLFormElement>(modalFormName);
1515
if (!modalForm) return;
1616
modalForm.action = `${modalForm.getAttribute('data-base-action')}${el.getAttribute('data-branch-from-urlcomponent')}`;
1717

@@ -29,7 +29,7 @@ function initRepoRenameBranchButton() {
2929
const target = el.getAttribute('data-modal');
3030
const modal = document.querySelector(target);
3131
const oldBranchName = el.getAttribute('data-old-branch-name');
32-
modal.querySelector('input[name=from]').value = oldBranchName;
32+
modal.querySelector<HTMLInputElement>('input[name=from]').value = oldBranchName;
3333

3434
// display the warning that the branch which is chosen is the default branch
3535
const warn = modal.querySelector('.default-branch-warning');

web_src/js/features/repo-code.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {toAbsoluteUrl} from '../utils.ts';
88
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
99
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
1010

11-
function changeHash(hash) {
11+
function changeHash(hash: string) {
1212
if (window.history.pushState) {
1313
window.history.pushState(null, null, hash);
1414
} else {
@@ -24,7 +24,7 @@ function getLineEls() {
2424
return document.querySelectorAll(`.code-view td.lines-code${isBlame() ? '.blame-code' : ''}`);
2525
}
2626

27-
function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
27+
function selectRange($linesEls, $selectionEndEl, $selectionStartEls?) {
2828
for (const el of $linesEls) {
2929
el.closest('tr').classList.remove('active');
3030
}
@@ -34,15 +34,15 @@ function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
3434
const copyPermalink = document.querySelector('a.copy-line-permalink');
3535
const viewGitBlame = document.querySelector('a.view_git_blame');
3636

37-
const updateIssueHref = function (anchor) {
37+
const updateIssueHref = function (anchor: string) {
3838
if (!refInNewIssue) return;
3939
const urlIssueNew = refInNewIssue.getAttribute('data-url-issue-new');
4040
const urlParamBodyLink = refInNewIssue.getAttribute('data-url-param-body-link');
4141
const issueContent = `${toAbsoluteUrl(urlParamBodyLink)}#${anchor}`; // the default content for issue body
4242
refInNewIssue.setAttribute('href', `${urlIssueNew}?body=${encodeURIComponent(issueContent)}`);
4343
};
4444

45-
const updateViewGitBlameFragment = function (anchor) {
45+
const updateViewGitBlameFragment = function (anchor: string) {
4646
if (!viewGitBlame) return;
4747
let href = viewGitBlame.getAttribute('href');
4848
href = `${href.replace(/#L\d+$|#L\d+-L\d+$/, '')}`;
@@ -52,7 +52,7 @@ function selectRange($linesEls, $selectionEndEl, $selectionStartEls) {
5252
viewGitBlame.setAttribute('href', href);
5353
};
5454

55-
const updateCopyPermalinkUrl = function (anchor) {
55+
const updateCopyPermalinkUrl = function (anchor: string) {
5656
if (!copyPermalink) return;
5757
let link = copyPermalink.getAttribute('data-url');
5858
link = `${link.replace(/#L\d+$|#L\d+-L\d+$/, '')}#${anchor}`;
@@ -142,13 +142,7 @@ export function initRepoCodeView() {
142142
});
143143
}
144144
selectRange($(linesEls), $(selectedEls), from ? $(from) : null);
145-
146-
if (window.getSelection) {
147-
window.getSelection().removeAllRanges();
148-
} else {
149-
document.selection.empty();
150-
}
151-
145+
window.getSelection().removeAllRanges();
152146
showLineButton();
153147
});
154148

web_src/js/features/repo-common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function initRepoCloneLink() {
6464
});
6565
}
6666

67-
export function initRepoCommonBranchOrTagDropdown(selector) {
67+
export function initRepoCommonBranchOrTagDropdown(selector: string) {
6868
$(selector).each(function () {
6969
const $dropdown = $(this);
7070
$dropdown.find('.reference.column').on('click', function () {
@@ -75,7 +75,7 @@ export function initRepoCommonBranchOrTagDropdown(selector) {
7575
});
7676
}
7777

78-
export function initRepoCommonFilterSearchDropdown(selector) {
78+
export function initRepoCommonFilterSearchDropdown(selector: string) {
7979
const $dropdown = $(selector);
8080
if (!$dropdown.length) return;
8181

web_src/js/features/repo-diff-commit.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {hideElem, showElem, toggleElem} from '../utils/dom.ts';
22
import {GET} from '../modules/fetch.ts';
33

4-
async function loadBranchesAndTags(area, loadingButton) {
4+
async function loadBranchesAndTags(area: Element, loadingButton: Element) {
55
loadingButton.classList.add('disabled');
66
try {
77
const res = await GET(loadingButton.getAttribute('data-fetch-url'));
@@ -15,15 +15,15 @@ async function loadBranchesAndTags(area, loadingButton) {
1515
}
1616
}
1717

18-
function addTags(area, tags) {
18+
function addTags(area: Element, tags: Array<Record<string, any>>) {
1919
const tagArea = area.querySelector('.tag-area');
2020
toggleElem(tagArea.parentElement, tags.length > 0);
2121
for (const tag of tags) {
2222
addLink(tagArea, tag.web_link, tag.name);
2323
}
2424
}
2525

26-
function addBranches(area, branches, defaultBranch) {
26+
function addBranches(area: Element, branches: Array<Record<string, any>>, defaultBranch: string) {
2727
const defaultBranchTooltip = area.getAttribute('data-text-default-branch-tooltip');
2828
const branchArea = area.querySelector('.branch-area');
2929
toggleElem(branchArea.parentElement, branches.length > 0);
@@ -33,7 +33,7 @@ function addBranches(area, branches, defaultBranch) {
3333
}
3434
}
3535

36-
function addLink(parent, href, text, tooltip) {
36+
function addLink(parent: Element, href: string, text: string, tooltip?: string) {
3737
const link = document.createElement('a');
3838
link.classList.add('muted', 'tw-px-1');
3939
link.href = href;

web_src/js/features/repo-graph.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function initRepoGraphGit() {
2222
for (const link of document.querySelectorAll('.pagination a')) {
2323
const href = link.getAttribute('href');
2424
if (!href) continue;
25-
const url = new URL(href, window.location);
25+
const url = new URL(href, window.location.href);
2626
const params = url.searchParams;
2727
params.set('mode', 'monochrome');
2828
url.search = `?${params.toString()}`;
@@ -38,7 +38,7 @@ export function initRepoGraphGit() {
3838
for (const link of document.querySelectorAll('.pagination a')) {
3939
const href = link.getAttribute('href');
4040
if (!href) continue;
41-
const url = new URL(href, window.location);
41+
const url = new URL(href, window.location.href);
4242
const params = url.searchParams;
4343
params.delete('mode');
4444
url.search = `?${params.toString()}`;
@@ -53,7 +53,7 @@ export function initRepoGraphGit() {
5353
window.history.replaceState({}, '', window.location.pathname);
5454
}
5555
});
56-
const url = new URL(window.location);
56+
const url = new URL(window.location.href);
5757
const params = url.searchParams;
5858
const updateGraph = () => {
5959
const queryString = params.toString();
@@ -103,15 +103,15 @@ export function initRepoGraphGit() {
103103
},
104104
onAdd(toAdd) {
105105
if (toAdd === '...flow-hide-pr-refs') {
106-
params.set('hide-pr-refs', true);
106+
params.set('hide-pr-refs', 'true');
107107
} else {
108108
params.append('branch', toAdd);
109109
}
110110
updateGraph();
111111
},
112112
});
113113

114-
graphContainer.addEventListener('mouseenter', (e) => {
114+
graphContainer.addEventListener('mouseenter', (e: MouseEvent & {target: HTMLElement}) => {
115115
if (e.target.matches('#rev-list li')) {
116116
const flow = e.target.getAttribute('data-flow');
117117
if (flow === '0') return;
@@ -132,7 +132,7 @@ export function initRepoGraphGit() {
132132
}
133133
});
134134

135-
graphContainer.addEventListener('mouseleave', (e) => {
135+
graphContainer.addEventListener('mouseleave', (e: MouseEvent & {target: HTMLElement}) => {
136136
if (e.target.matches('#rev-list li')) {
137137
const flow = e.target.getAttribute('data-flow');
138138
if (flow === '0') return;

web_src/js/features/repo-home.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {stripTags} from '../utils.ts';
22
import {hideElem, queryElemChildren, showElem} from '../utils/dom.ts';
33
import {POST} from '../modules/fetch.ts';
4-
import {showErrorToast} from '../modules/toast.ts';
4+
import {showErrorToast, type Toast} from '../modules/toast.ts';
55
import {fomanticQuery} from '../modules/fomantic/base.ts';
66

77
const {appSubUrl} = window.config;
@@ -13,7 +13,7 @@ export function initRepoTopicBar() {
1313
const editDiv = document.querySelector('#topic_edit');
1414
const viewDiv = document.querySelector('#repo-topics');
1515
const topicDropdown = editDiv.querySelector('.ui.dropdown');
16-
let lastErrorToast;
16+
let lastErrorToast: Toast;
1717

1818
mgrBtn.addEventListener('click', () => {
1919
hideElem(viewDiv);

web_src/js/features/repo-issue-pr-status.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function initRepoPullRequestCommitStatus() {
22
for (const btn of document.querySelectorAll('.commit-status-hide-checks')) {
33
const panel = btn.closest('.commit-status-panel');
4-
const list = panel.querySelector('.commit-status-list');
4+
const list = panel.querySelector<HTMLElement>('.commit-status-list');
55
btn.addEventListener('click', () => {
66
list.style.maxHeight = list.style.maxHeight ? '' : '0px'; // toggle
77
btn.textContent = btn.getAttribute(list.style.maxHeight ? 'data-show-all' : 'data-hide-all');

web_src/js/features/repo-issue.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function initRepoIssueFilterItemLabel() {
124124

125125
export function initRepoIssueCommentDelete() {
126126
// Delete comment
127-
document.addEventListener('click', async (e) => {
127+
document.addEventListener('click', async (e: MouseEvent & {target: HTMLElement}) => {
128128
if (!e.target.matches('.delete-comment')) return;
129129
e.preventDefault();
130130

@@ -143,7 +143,7 @@ export function initRepoIssueCommentDelete() {
143143
const counter = document.querySelector('#review-box .review-comments-counter');
144144
let num = parseInt(counter?.getAttribute('data-pending-comment-number')) - 1 || 0;
145145
num = Math.max(num, 0);
146-
counter.setAttribute('data-pending-comment-number', num);
146+
counter.setAttribute('data-pending-comment-number', String(num));
147147
counter.textContent = String(num);
148148
}
149149

@@ -199,7 +199,7 @@ export function initRepoIssueDependencyDelete() {
199199

200200
export function initRepoIssueCodeCommentCancel() {
201201
// Cancel inline code comment
202-
document.addEventListener('click', (e) => {
202+
document.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
203203
if (!e.target.matches('.cancel-code-comment')) return;
204204

205205
const form = e.target.closest('form');
@@ -268,12 +268,14 @@ export function initRepoPullRequestMergeInstruction() {
268268
export function initRepoPullRequestAllowMaintainerEdit() {
269269
const wrapper = document.querySelector('#allow-edits-from-maintainers');
270270
if (!wrapper) return;
271-
const checkbox = wrapper.querySelector('input[type="checkbox"]');
271+
const checkbox = wrapper.querySelector<HTMLInputElement>('input[type="checkbox"]');
272272
checkbox.addEventListener('input', async () => {
273273
const url = `${wrapper.getAttribute('data-url')}/set_allow_maintainer_edit`;
274274
wrapper.classList.add('is-loading');
275275
try {
276-
const resp = await POST(url, {data: new URLSearchParams({allow_maintainer_edit: checkbox.checked})});
276+
const resp = await POST(url, {data: new URLSearchParams({
277+
allow_maintainer_edit: String(checkbox.checked),
278+
})});
277279
if (!resp.ok) {
278280
throw new Error('Failed to update maintainer edit permission');
279281
}
@@ -322,7 +324,7 @@ export function initRepoIssueWipTitle() {
322324

323325
const $issueTitle = $('#issue_title');
324326
$issueTitle.trigger('focus');
325-
const value = $issueTitle.val().trim().toUpperCase();
327+
const value = ($issueTitle.val() as string).trim().toUpperCase();
326328

327329
const wipPrefixes = $('.title_wip_desc').data('wip-prefixes');
328330
for (const prefix of wipPrefixes) {
@@ -338,7 +340,7 @@ export function initRepoIssueWipTitle() {
338340
export function initRepoIssueComments() {
339341
if (!$('.repository.view.issue .timeline').length) return;
340342

341-
document.addEventListener('click', (e) => {
343+
document.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
342344
const urlTarget = document.querySelector(':target');
343345
if (!urlTarget) return;
344346

@@ -490,7 +492,7 @@ export function initRepoPullRequestReview() {
490492

491493
export function initRepoIssueReferenceIssue() {
492494
// Reference issue
493-
$(document).on('click', '.reference-issue', function (event) {
495+
$(document).on('click', '.reference-issue', function (e) {
494496
const target = this.getAttribute('data-target');
495497
const content = document.querySelector(`#${target}`)?.textContent ?? '';
496498
const poster = this.getAttribute('data-poster-username');
@@ -500,7 +502,7 @@ export function initRepoIssueReferenceIssue() {
500502
const textarea = modal.querySelector('textarea[name="content"]');
501503
textarea.value = `${content}\n\n_Originally posted by @${poster} in ${reference}_`;
502504
$(modal).modal('show');
503-
event.preventDefault();
505+
e.preventDefault();
504506
});
505507
}
506508

@@ -584,7 +586,7 @@ export function initRepoIssueTitleEdit() {
584586
}
585587

586588
export function initRepoIssueBranchSelect() {
587-
document.querySelector('#branch-select')?.addEventListener('click', (e) => {
589+
document.querySelector('#branch-select')?.addEventListener('click', (e: MouseEvent & {target: HTMLElement}) => {
588590
const el = e.target.closest('.item[data-branch]');
589591
if (!el) return;
590592
const pullTargetBranch = document.querySelector('#pull-target-branch');

web_src/js/features/repo-migration.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {hideElem, showElem, toggleElem} from '../utils/dom.ts';
22

3-
const service = document.querySelector('#service_type');
4-
const user = document.querySelector('#auth_username');
5-
const pass = document.querySelector('#auth_password');
6-
const token = document.querySelector('#auth_token');
7-
const mirror = document.querySelector('#mirror');
8-
const lfs = document.querySelector('#lfs');
9-
const lfsSettings = document.querySelector('#lfs_settings');
10-
const lfsEndpoint = document.querySelector('#lfs_endpoint');
11-
const items = document.querySelectorAll('#migrate_items input[type=checkbox]');
3+
const service = document.querySelector<HTMLInputElement>('#service_type');
4+
const user = document.querySelector<HTMLInputElement>('#auth_username');
5+
const pass = document.querySelector<HTMLInputElement>('#auth_password');
6+
const token = document.querySelector<HTMLInputElement>('#auth_token');
7+
const mirror = document.querySelector<HTMLInputElement>('#mirror');
8+
const lfs = document.querySelector<HTMLInputElement>('#lfs');
9+
const lfsSettings = document.querySelector<HTMLElement>('#lfs_settings');
10+
const lfsEndpoint = document.querySelector<HTMLElement>('#lfs_endpoint');
11+
const items = document.querySelectorAll<HTMLInputElement>('#migrate_items input[type=checkbox]');
1212

1313
export function initRepoMigration() {
1414
checkAuth();
@@ -25,11 +25,11 @@ export function initRepoMigration() {
2525
});
2626
lfs?.addEventListener('change', setLFSSettingsVisibility);
2727

28-
const cloneAddr = document.querySelector('#clone_addr');
28+
const cloneAddr = document.querySelector<HTMLInputElement>('#clone_addr');
2929
cloneAddr?.addEventListener('change', () => {
30-
const repoName = document.querySelector('#repo_name');
30+
const repoName = document.querySelector<HTMLInputElement>('#repo_name');
3131
if (cloneAddr.value && !repoName?.value) { // Only modify if repo_name input is blank
32-
repoName.value = cloneAddr.value.match(/^(.*\/)?((.+?)(\.git)?)$/)[3];
32+
repoName.value = /^(.*\/)?((.+?)(\.git)?)$/.exec(cloneAddr.value)[3];
3333
}
3434
});
3535
}
@@ -41,8 +41,8 @@ function checkAuth() {
4141
checkItems(serviceType !== 1);
4242
}
4343

44-
function checkItems(tokenAuth) {
45-
let enableItems;
44+
function checkItems(tokenAuth: boolean) {
45+
let enableItems = false;
4646
if (tokenAuth) {
4747
enableItems = token?.value !== '';
4848
} else {

web_src/js/features/repo-new.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function initRepoNew() {
77
const gitignores = $('input[name="gitignores"]').val();
88
const license = $('input[name="license"]').val();
99
if (gitignores || license) {
10-
document.querySelector('input[name="auto_init"]').checked = true;
10+
document.querySelector<HTMLInputElement>('input[name="auto_init"]').checked = true;
1111
}
1212
});
1313
}

web_src/js/features/repo-projects.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function createNewColumn(url, columnTitle, projectColorInput) {
2525
}
2626
}
2727

28-
async function moveIssue({item, from, to, oldIndex}) {
28+
async function moveIssue({item, from, to, oldIndex}: {item: HTMLElement, from: HTMLElement, to: HTMLElement, oldIndex: number}) {
2929
const columnCards = to.querySelectorAll('.issue-card');
3030
updateIssueCount(from);
3131
updateIssueCount(to);
@@ -97,14 +97,14 @@ export function initRepoProject() {
9797
return;
9898
}
9999

100-
const _promise = initRepoProjectSortable();
100+
initRepoProjectSortable(); // no await
101101

102102
for (const modal of document.querySelectorAll('.edit-project-column-modal')) {
103-
const projectHeader = modal.closest('.project-column-header');
104-
const projectTitleLabel = projectHeader?.querySelector('.project-column-title-label');
105-
const projectTitleInput = modal.querySelector('.project-column-title-input');
106-
const projectColorInput = modal.querySelector('#new_project_column_color');
107-
const boardColumn = modal.closest('.project-column');
103+
const projectHeader = modal.closest<HTMLElement>('.project-column-header');
104+
const projectTitleLabel = projectHeader?.querySelector<HTMLElement>('.project-column-title-label');
105+
const projectTitleInput = modal.querySelector<HTMLInputElement>('.project-column-title-input');
106+
const projectColorInput = modal.querySelector<HTMLInputElement>('#new_project_column_color');
107+
const boardColumn = modal.closest<HTMLElement>('.project-column');
108108
modal.querySelector('.edit-project-column-button')?.addEventListener('click', async function (e) {
109109
e.preventDefault();
110110
try {
@@ -119,7 +119,7 @@ export function initRepoProject() {
119119
} finally {
120120
projectTitleLabel.textContent = projectTitleInput?.value;
121121
projectTitleInput.closest('form')?.classList.remove('dirty');
122-
const dividers = boardColumn.querySelectorAll(':scope > .divider');
122+
const dividers = boardColumn.querySelectorAll<HTMLElement>(':scope > .divider');
123123
if (projectColorInput.value) {
124124
const color = contrastColor(projectColorInput.value);
125125
boardColumn.style.setProperty('background', projectColorInput.value, 'important');

0 commit comments

Comments
 (0)