Procházet zdrojové kódy

ref: eslint explicit function return type

Sv443 před 1 měsícem
rodič
revize
207a2ca8cb

+ 6 - 0
eslint.config.mjs

@@ -25,6 +25,7 @@ const config = [
       "dist/**/*",
       "**/dev/**/*",
       "**/test.ts",
+      "test/**/*",
     ],
   }, ...compat.extends(
     "eslint:recommended",
@@ -73,6 +74,10 @@ const config = [
         allowTaggedTemplates: true,
       }],
       "@typescript-eslint/no-unsafe-declaration-merging": "off",
+      "@typescript-eslint/explicit-function-return-type": ["error", {
+        allowExpressions: true,
+        allowIIFEs: true,
+      }],
       "comma-dangle": ["error", "only-multiline"],
       "no-misleading-character-class": "off",
     },
@@ -80,6 +85,7 @@ const config = [
     files: ["**/*.js", "**/*.mjs", "**/*.cjs"],
     rules: {
       "@typescript-eslint/no-var-requires": "off",
+      "@typescript-eslint/explicit-function-return-type": "off",
       quotes: ["error", "double"],
       semi: ["error", "always"],
       "eol-last": ["error", "always"],

+ 2 - 2
lib/Debouncer.ts

@@ -120,14 +120,14 @@ export class Debouncer<TFunc extends AnyFunc> extends NanoEmitter<DebouncerEvent
   /** Use this to call the debouncer with the specified arguments that will be passed to all listener functions registered with {@linkcode addListener()} */
   public call(...args: Parameters<TFunc>): void {
     /** When called, calls all registered listeners */
-    const cl = (...a: Parameters<TFunc>) => {
+    const cl = (...a: Parameters<TFunc>): void => {
       this.queuedCall = undefined;
       this.emit("call", ...a);
       this.listeners.forEach((l) => l.apply(this, a));
     };
 
     /** Sets a timeout that will call the latest queued call and then set another timeout if there was a queued call */
-    const setRepeatTimeout = () => {
+    const setRepeatTimeout = (): void => {
       this.activeTimeout = setTimeout(() => {
         if(this.queuedCall) {
           this.queuedCall();

+ 2 - 4
lib/SelectorObserver.ts

@@ -4,13 +4,11 @@
  */
 
 import { Debouncer, debounce, type DebouncerType } from "./Debouncer.js";
+import { isDomLoaded } from "./dom.js";
 import type { Prettify } from "./types.js";
 
 void ["type only", Debouncer];
 
-let domLoaded = false;
-document.addEventListener("DOMContentLoaded", () => domLoaded = true);
-
 /** Options for the `onSelector()` method of {@linkcode SelectorObserver} */
 export type SelectorListenerOptions<TElem extends Element = HTMLElement> = Prettify<SelectorOptionsOne<TElem> | SelectorOptionsAll<TElem>>;
 
@@ -113,7 +111,7 @@ export class SelectorObserver {
 
   /** Call to check all selectors in the {@linkcode listenerMap} using {@linkcode checkSelector()} */
   protected checkAllSelectors(): void {
-    if(!this.enabled || !domLoaded)
+    if(!this.enabled || !isDomLoaded())
       return;
 
     for(const [selector, listeners] of this.listenerMap.entries())

+ 1 - 1
lib/colors.ts

@@ -30,7 +30,7 @@ export function hexToRgb(hex: string): [red: number, green: number, blue: number
 
 /** Converts RGB or RGBA number values to a hex color string in the format `#RRGGBB` or `#RRGGBBAA` */
 export function rgbToHex(red: number, green: number, blue: number, alpha?: number, withHash = true, upperCase = false): string {
-  const toHexVal = (n: number) =>
+  const toHexVal = (n: number): string =>
     clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0")[(upperCase ? "toUpperCase" : "toLowerCase")]();
   return `${withHash ? "#" : ""}${toHexVal(red)}${toHexVal(green)}${toHexVal(blue)}${alpha ? toHexVal(alpha * 255) : ""}`;
 }

+ 3 - 3
lib/translation.ts

@@ -371,9 +371,9 @@ const templateLiteralTransform: TransformTuple<string> = [
 
     let str = String(trValue);
 
-    const eachKeyInTrString = (keys: string[]) => keys.every((key) => trValue.includes(`${patternStart}${key}${patternEnd}`));
+    const eachKeyInTrString = (keys: string[]): boolean => keys.every((key) => trValue.includes(`${patternStart}${key}${patternEnd}`));
 
-    const namedMapping = () => {
+    const namedMapping = (): void => {
       if(!str.includes(patternStart) || typeof trArgs[0] === "undefined" || typeof trArgs[0] !== "object" || !eachKeyInTrString(Object.keys(trArgs[0] ?? {})))
         return;
       for(const match of matches) {
@@ -383,7 +383,7 @@ const templateLiteralTransform: TransformTuple<string> = [
       }
     };
 
-    const positionalMapping = () => {
+    const positionalMapping = (): void => {
       if(!(patternRegex.test(str)) || !trArgs[0])
         return;
       let matchNum = -1;

+ 2 - 2
tools/fix-dts.mts

@@ -5,7 +5,7 @@ import k from "kleur";
 const dtsPath = resolve("./dist/lib/");
 
 /** Adds two spaces to the end of each line in JSDoc comments to preserve the meticulous line breaks */
-async function addTrailingSpaces(filePath: string) {
+async function addTrailingSpaces(filePath: string): Promise<void> {
   const content = String(await readFile(filePath, "utf8"));
 
   const fixedContent = content.replace(/\/\*\*[\s\S]*?\*\//g, (m) =>
@@ -18,7 +18,7 @@ async function addTrailingSpaces(filePath: string) {
 }
 
 /** Recursively processes all files in the given directory */
-async function processRecursive(directory: string) {
+async function processRecursive(directory: string): Promise<void> {
   directory = resolve(directory);
   const files = await readdir(directory);
 

+ 2 - 2
tools/post-build-global.mts

@@ -8,7 +8,7 @@ const { exit } = process;
 /** Path to the global / IIFE bundle built by tsup */
 const iifeBundlePath = resolve("dist/index.global.js");
 
-async function run() {
+async function run(): Promise<void> {
   if(!await exists(iifeBundlePath)) {
     console.error(`No global script found at path '${iifeBundlePath}'`);
     setImmediate(() => exit(1));
@@ -50,7 +50,7 @@ async function run() {
 }
 
 /** Checks if a path exists / is readable and writable (by default) */
-async function exists(path: string, mode = fsconstants.R_OK | fsconstants.W_OK) {
+async function exists(path: string, mode = fsconstants.R_OK | fsconstants.W_OK): Promise<boolean> {
   try {
     await access(path, mode);
     return true;

+ 1 - 1
tools/update-jsr-version.mts

@@ -1,6 +1,6 @@
 import { readFile, writeFile } from "node:fs/promises";
 
-async function run() {
+async function run(): Promise<void> {
   try {
     const pkgJson = JSON.parse(String(await readFile("package.json", "utf8")));
     const { version } = pkgJson;