1
1
mirror of https://github.com/go-gitea/gitea synced 2025-12-07 05:18:29 +00:00

Enable TypeScript strictNullChecks (#35843)

A big step towards enabling strict mode in Typescript.

There was definitely a good share of potential bugs while refactoring
this. When in doubt, I opted to keep the potentially broken behaviour.
Notably, the `DOMEvent` type is gone, it was broken and we're better of
with type assertions on `e.target`.

---------

Signed-off-by: silverwind <me@silverwind.io>
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2025-12-03 03:13:16 +01:00
committed by GitHub
parent 9f268edd2f
commit 46d7adefe0
108 changed files with 686 additions and 658 deletions

View File

@@ -12,7 +12,7 @@ export type DiffTreeEntry = {
DiffStatus: DiffStatus,
EntryMode: string,
IsViewed: boolean,
Children: DiffTreeEntry[],
Children: DiffTreeEntry[] | null,
FileIcon: string,
ParentEntry?: DiffTreeEntry,
};
@@ -25,7 +25,7 @@ type DiffFileTree = {
folderIcon: string;
folderOpenIcon: string;
diffFileTree: DiffFileTreeData;
fullNameMap?: Record<string, DiffTreeEntry>
fullNameMap: Record<string, DiffTreeEntry>
fileTreeIsVisible: boolean;
selectedItem: string;
};

View File

@@ -10,8 +10,8 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']);
// which will automatically set an appropriate headers. For json content, only object
// and array types are currently supported.
export function request(url: string, {method = 'GET', data, headers = {}, ...other}: RequestOpts = {}): Promise<Response> {
let body: string | FormData | URLSearchParams;
let contentType: string;
let body: string | FormData | URLSearchParams | undefined;
let contentType: string | undefined;
if (data instanceof FormData || data instanceof URLSearchParams) {
body = data;
} else if (isObject(data) || Array.isArray(data)) {

View File

@@ -7,7 +7,7 @@ export function initFomanticTab() {
const tabName = elBtn.getAttribute('data-tab');
if (!tabName) continue;
elBtn.addEventListener('click', () => {
const elTab = document.querySelector(`.ui.tab[data-tab="${tabName}"]`);
const elTab = document.querySelector(`.ui.tab[data-tab="${tabName}"]`)!;
queryElemSiblings(elTab, `.ui.tab`, (el) => el.classList.remove('active'));
queryElemSiblings(elBtn, `[data-tab]`, (el) => el.classList.remove('active'));
elBtn.classList.add('active');

View File

@@ -42,7 +42,7 @@ export function registerGlobalInitFunc<T extends HTMLElement>(name: string, hand
}
function callGlobalInitFunc(el: HTMLElement) {
const initFunc = el.getAttribute('data-global-init');
const initFunc = el.getAttribute('data-global-init')!;
const func = globalInitFuncs[initFunc];
if (!func) throw new Error(`Global init function "${initFunc}" not found`);
@@ -66,7 +66,7 @@ function attachGlobalEvents() {
});
}
export function initGlobalSelectorObserver(perfTracer?: InitPerformanceTracer): void {
export function initGlobalSelectorObserver(perfTracer: InitPerformanceTracer | null): void {
if (globalSelectorObserverInited) throw new Error('initGlobalSelectorObserver() already called');
globalSelectorObserverInited = true;

View File

@@ -9,12 +9,12 @@ export async function createSortable(el: Element, opts: {handle?: string} & Sort
animation: 150,
ghostClass: 'card-ghost',
onChoose: (e: SortableEvent) => {
const handle = opts.handle ? e.item.querySelector(opts.handle) : e.item;
const handle = opts.handle ? e.item.querySelector(opts.handle)! : e.item;
handle.classList.add('tw-cursor-grabbing');
opts.onChoose?.(e);
},
onUnchoose: (e: SortableEvent) => {
const handle = opts.handle ? e.item.querySelector(opts.handle) : e.item;
const handle = opts.handle ? e.item.querySelector(opts.handle)! : e.item;
handle.classList.remove('tw-cursor-grabbing');
opts.onUnchoose?.(e);
},

View File

@@ -68,7 +68,7 @@ export function createTippy(target: Element, opts: TippyOpts = {}): Instance {
*
* Note: "tooltip" doesn't equal to "tippy". "tooltip" means a auto-popup content, it just uses tippy as the implementation.
*/
function attachTooltip(target: Element, content: Content = null): Instance {
function attachTooltip(target: Element, content: Content | null = null): Instance | null {
switchTitleToTooltip(target);
content = content ?? target.getAttribute('data-tooltip-content');
@@ -125,7 +125,7 @@ function switchTitleToTooltip(target: Element): void {
* The tippy by default uses "mouseenter" event to show, so we use "mouseover" event to switch to tippy
*/
function lazyTooltipOnMouseHover(this: HTMLElement, e: Event): void {
e.target.removeEventListener('mouseover', lazyTooltipOnMouseHover, true);
(e.target as HTMLElement).removeEventListener('mouseover', lazyTooltipOnMouseHover, true);
attachTooltip(this);
}
@@ -184,7 +184,7 @@ export function initGlobalTooltips(): void {
export function showTemporaryTooltip(target: Element, content: Content): void {
// if the target is inside a dropdown or tippy popup, the menu will be hidden soon
// so display the tooltip on the "aria-controls" element or dropdown instead
let refClientRect: DOMRect;
let refClientRect: DOMRect | undefined;
const popupTippyId = target.closest(`[data-tippy-root]`)?.id;
if (popupTippyId) {
// for example, the "Copy Permalink" button in the "File View" page for the selected lines

View File

@@ -52,7 +52,7 @@ function showToast(message: string, level: Intent, {gravity, position, duration,
if (preventDuplicates) {
const toastEl = parent.querySelector(`:scope > .toastify.on[data-toast-unique-key="${CSS.escape(duplicateKey)}"]`);
if (toastEl) {
const toastDupNumEl = toastEl.querySelector('.toast-duplicate-number');
const toastDupNumEl = toastEl.querySelector('.toast-duplicate-number')!;
showElem(toastDupNumEl);
toastDupNumEl.textContent = String(Number(toastDupNumEl.textContent) + 1);
animateOnce(toastDupNumEl, 'pulse-1p5-200');
@@ -77,9 +77,10 @@ function showToast(message: string, level: Intent, {gravity, position, duration,
});
toast.showToast();
toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast());
toast.toastElement.setAttribute('data-toast-unique-key', duplicateKey);
(toast.toastElement as ToastifyElement)._giteaToastifyInstance = toast;
const el = toast.toastElement as ToastifyElement;
el.querySelector('.toast-close')!.addEventListener('click', () => toast.hideToast());
el.setAttribute('data-toast-unique-key', duplicateKey);
el._giteaToastifyInstance = toast;
return toast;
}