Enable @typescript-eslint/explicit-function-return-type in /src (#9788)

* Enable `@typescript-eslint/explicit-member-accessibility` on /src

* Prettier

* Enable `@typescript-eslint/explicit-function-return-type` in /src

* Fix types

* tsc strict fixes

* Delint

* Fix test

* Fix bad merge
This commit is contained in:
Michael Telatynski 2023-01-12 13:25:14 +00:00 committed by GitHub
parent 7a36ba0fde
commit 030b7e90bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
683 changed files with 3459 additions and 3013 deletions

View file

@ -23,7 +23,7 @@ import { Part, Type } from "./parts";
export type Caret = Range | DocumentPosition;
export function setSelection(editor: HTMLDivElement, model: EditorModel, selection: Range | IPosition) {
export function setSelection(editor: HTMLDivElement, model: EditorModel, selection: Range | IPosition): void {
if (selection instanceof Range) {
setDocumentRangeSelection(editor, model, selection);
} else {
@ -31,7 +31,7 @@ export function setSelection(editor: HTMLDivElement, model: EditorModel, selecti
}
}
function setDocumentRangeSelection(editor: HTMLDivElement, model: EditorModel, range: Range) {
function setDocumentRangeSelection(editor: HTMLDivElement, model: EditorModel, range: Range): void {
const sel = document.getSelection();
sel.removeAllRanges();
const selectionRange = document.createRange();
@ -42,7 +42,7 @@ function setDocumentRangeSelection(editor: HTMLDivElement, model: EditorModel, r
sel.addRange(selectionRange);
}
export function setCaretPosition(editor: HTMLDivElement, model: EditorModel, caretPosition: IPosition) {
export function setCaretPosition(editor: HTMLDivElement, model: EditorModel, caretPosition: IPosition): void {
if (model.isEmpty) return; // selection can't possibly be wrong, so avoid a reflow
const range = document.createRange();
@ -69,7 +69,14 @@ export function setCaretPosition(editor: HTMLDivElement, model: EditorModel, car
sel.addRange(range);
}
function getNodeAndOffsetForPosition(editor: HTMLDivElement, model: EditorModel, position: IPosition) {
function getNodeAndOffsetForPosition(
editor: HTMLDivElement,
model: EditorModel,
position: IPosition,
): {
node: Node;
offset: number;
} {
const { offset, lineIndex, nodeIndex } = getLineAndNodePosition(model, position);
const lineNode = editor.childNodes[lineIndex];
@ -87,7 +94,14 @@ function getNodeAndOffsetForPosition(editor: HTMLDivElement, model: EditorModel,
return { node: focusNode, offset };
}
export function getLineAndNodePosition(model: EditorModel, caretPosition: IPosition) {
export function getLineAndNodePosition(
model: EditorModel,
caretPosition: IPosition,
): {
offset: number;
lineIndex: number;
nodeIndex: number;
} {
const { parts } = model;
const partIndex = caretPosition.index;
const lineResult = findNodeInLineForPart(parts, partIndex);
@ -106,7 +120,7 @@ export function getLineAndNodePosition(model: EditorModel, caretPosition: IPosit
return { lineIndex, nodeIndex, offset };
}
function findNodeInLineForPart(parts: Part[], partIndex: number) {
function findNodeInLineForPart(parts: Part[], partIndex: number): { lineIndex: number; nodeIndex: number } {
let lineIndex = 0;
let nodeIndex = -1;
@ -142,7 +156,12 @@ function findNodeInLineForPart(parts: Part[], partIndex: number) {
return { lineIndex, nodeIndex };
}
function moveOutOfUnselectablePart(parts: Part[], partIndex: number, nodeIndex: number, offset: number) {
function moveOutOfUnselectablePart(
parts: Part[],
partIndex: number,
nodeIndex: number,
offset: number,
): { offset: number; nodeIndex: number } {
// move caret before or after unselectable part
const part = parts[partIndex];
if (part && !part.acceptsCaret) {

View file

@ -127,7 +127,7 @@ function parseHeader(n: Node, pc: PartCreator, opts: IParseOptions): Part[] {
return [prefix, ...parseChildren(n, pc, opts)];
}
function checkIgnored(n) {
function checkIgnored(n: Node): boolean {
if (n.nodeType === Node.TEXT_NODE) {
// Element adds \n text nodes in a lot of places,
// which should be ignored
@ -138,7 +138,7 @@ function checkIgnored(n) {
return true;
}
function prefixLines(parts: Part[], prefix: string, pc: PartCreator) {
function prefixLines(parts: Part[], prefix: string, pc: PartCreator): void {
parts.unshift(pc.plain(prefix));
for (let i = 0; i < parts.length; i++) {
if (parts[i].type === Type.Newline) {
@ -283,7 +283,7 @@ export function parsePlainTextMessage(body: string, pc: PartCreator, opts: IPars
}, [] as Part[]);
}
export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }) {
export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }): Part[] {
const content = event.getContent();
let parts: Part[];
const isEmote = content.msgtype === MsgType.Emote;

View file

@ -23,7 +23,7 @@ import Range from "./range";
type Predicate = (node: Node) => boolean;
type Callback = (node: Node) => void;
export function walkDOMDepthFirst(rootNode: Node, enterNodeCallback: Predicate, leaveNodeCallback: Callback) {
export function walkDOMDepthFirst(rootNode: Node, enterNodeCallback: Predicate, leaveNodeCallback: Callback): void {
let node = rootNode.firstChild;
while (node && node !== rootNode) {
const shouldDescend = enterNodeCallback(node);
@ -45,12 +45,24 @@ export function walkDOMDepthFirst(rootNode: Node, enterNodeCallback: Predicate,
}
}
export function getCaretOffsetAndText(editor: HTMLDivElement, sel: Selection) {
export function getCaretOffsetAndText(
editor: HTMLDivElement,
sel: Selection,
): {
caret: DocumentOffset;
text: string;
} {
const { offset, text } = getSelectionOffsetAndText(editor, sel.focusNode, sel.focusOffset);
return { caret: offset, text };
}
function tryReduceSelectionToTextNode(selectionNode: Node, selectionOffset: number) {
function tryReduceSelectionToTextNode(
selectionNode: Node,
selectionOffset: number,
): {
node: Node;
characterOffset: number;
} {
// if selectionNode is an element, the selected location comes after the selectionOffset-th child node,
// which can point past any childNode, in which case, the end of selectionNode is selected.
// we try to simplify this to point at a text node with the offset being
@ -87,7 +99,14 @@ function tryReduceSelectionToTextNode(selectionNode: Node, selectionOffset: numb
};
}
function getSelectionOffsetAndText(editor: HTMLDivElement, selectionNode: Node, selectionOffset: number) {
function getSelectionOffsetAndText(
editor: HTMLDivElement,
selectionNode: Node,
selectionOffset: number,
): {
offset: DocumentOffset;
text: string;
} {
const { node, characterOffset } = tryReduceSelectionToTextNode(selectionNode, selectionOffset);
const { text, offsetToNode } = getTextAndOffsetToNode(editor, node);
const offset = getCaret(node, offsetToNode, characterOffset);
@ -96,7 +115,7 @@ function getSelectionOffsetAndText(editor: HTMLDivElement, selectionNode: Node,
// gets the caret position details, ignoring and adjusting to
// the ZWS if you're typing in a caret node
function getCaret(node: Node, offsetToNode: number, offsetWithinNode: number) {
function getCaret(node: Node, offsetToNode: number, offsetWithinNode: number): DocumentOffset {
// if no node is selected, return an offset at the start
if (!node) {
return new DocumentOffset(0, false);
@ -119,12 +138,12 @@ function getCaret(node: Node, offsetToNode: number, offsetWithinNode: number) {
// gets the text of the editor as a string,
// and the offset in characters where the selectionNode starts in that string
// all ZWS from caret nodes are filtered out
function getTextAndOffsetToNode(editor: HTMLDivElement, selectionNode: Node) {
function getTextAndOffsetToNode(editor: HTMLDivElement, selectionNode: Node): { offsetToNode: number; text: string } {
let offsetToNode = 0;
let foundNode = false;
let text = "";
function enterNodeCallback(node: HTMLElement) {
function enterNodeCallback(node: HTMLElement): boolean {
if (!foundNode) {
if (node === selectionNode) {
foundNode = true;
@ -150,7 +169,7 @@ function getTextAndOffsetToNode(editor: HTMLDivElement, selectionNode: Node) {
return true;
}
function leaveNodeCallback(node: HTMLElement) {
function leaveNodeCallback(node: HTMLElement): void {
// if this is not the last DIV (which are only used as line containers atm)
// we don't just check if there is a nextSibling because sometimes the caret ends up
// after the last DIV and it creates a newline if you type then,

View file

@ -47,7 +47,7 @@ export default class HistoryManager {
this.removedSinceLastPush = false;
}
private shouldPush(inputType, diff) {
private shouldPush(inputType, diff): boolean {
// right now we can only push a step after
// the input has been applied to the model,
// so we can't push the state before something happened.
@ -85,7 +85,7 @@ export default class HistoryManager {
}
}
private pushState(model: EditorModel, caret: Caret) {
private pushState(model: EditorModel, caret: Caret): void {
// remove all steps after current step
while (this.currentIndex < this.stack.length - 1) {
this.stack.pop();

View file

@ -131,7 +131,7 @@ export function replaceRangeAndAutoAdjustCaret(
});
}
const isFormattable = (_index: number, offset: number, part: Part) => {
const isFormattable = (_index: number, offset: number, part: Part): boolean => {
return part.text[offset] !== " " && part.type === Type.Plain;
};
@ -217,7 +217,7 @@ export function formatRangeAsCode(range: Range): void {
replaceRangeAndExpandSelection(range, parts);
}
export function formatRangeAsLink(range: Range, text?: string) {
export function formatRangeAsLink(range: Range, text?: string): void {
const { model } = range;
const { partCreator } = model;
const linkRegex = /\[(.*?)]\(.*?\)/g;
@ -233,8 +233,8 @@ export function formatRangeAsLink(range: Range, text?: string) {
}
// parts helper methods
const isBlank = (part) => !part.text || !/\S/.test(part.text);
const isNL = (part) => part.type === Type.Newline;
const isBlank = (part: Part): boolean => !part.text || !/\S/.test(part.text);
const isNL = (part: Part): boolean => part.type === Type.Newline;
export function toggleInlineFormat(range: Range, prefix: string, suffix = prefix): void {
const { model, parts } = range;

View file

@ -426,7 +426,7 @@ class RoomPillPart extends PillPart {
return Type.RoomPill;
}
protected get className() {
protected get className(): string {
return "mx_Pill " + (this.room?.isSpaceRoom() ? "mx_SpacePill" : "mx_RoomPill");
}
}
@ -457,7 +457,7 @@ class UserPillPart extends PillPart {
return Type.UserPill;
}
protected get className() {
protected get className(): string {
return "mx_UserPill mx_Pill";
}

View file

@ -47,7 +47,7 @@ export default class Range {
return this._initializedEmpty;
}
public setWasEmpty(value: boolean) {
public setWasEmpty(value: boolean): void {
this._initializedEmpty = value;
}