Skip to content

Commit

Permalink
manually fix eslint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Jun 27, 2024
1 parent 51e82da commit 6e2d2dd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
32 changes: 15 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ async function ready(): Promise<void> {
});
}

(async function () {
await ready();
await ready();

const startTime = performance.now();
const errors = await scan(document.body);
const endTime = performance.now();
const startTime = performance.now();
const errors = await scan(document.body);
const endTime = performance.now();

for (const accessbilityError of errors) {
accessbilityError.element.setAttribute("style", "border: 5px solid red;");
console.log(
accessbilityError.text,
accessbilityError.element,
accessbilityError.url,
);
}
for (const accessbilityError of errors) {
accessbilityError.element.setAttribute("style", "border: 5px solid red;");
console.log(
`Took ${(endTime - startTime).toPrecision(
2,
)}ms to execute accessbility scans`,
accessbilityError.text,
accessbilityError.element,
accessbilityError.url,
);
})();
}
console.log(
`Took ${(endTime - startTime).toPrecision(
2,
)}ms to execute accessbility scans`,
);
7 changes: 5 additions & 2 deletions src/rules/aria-valid-attr-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ function valid(element: Element, attribute: string, info: Info) {
return false;
}
return true;
} else if (info.type === "decimal" && !isNaN(Number.parseFloat(value))) {
} else if (
info.type === "decimal" &&
!Number.isNaN(Number.parseFloat(value))
) {
return true;
} else if (info.type === "int" && !isNaN(Number.parseInt(value))) {
} else if (info.type === "int" && !Number.isNaN(Number.parseInt(value))) {
if (info.minValue != undefined) {
return info.minValue <= Number.parseInt(value);
}
Expand Down
2 changes: 2 additions & 0 deletions src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ export async function requestIdleScan(
return new Promise((resolve) => {
requestIdleCallback(async function executeScan(deadline: IdleDeadline) {
while (
// eslint-disable-next-line tscompat/tscompat
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
rules.length > 0
) {
// eslint-disable-next-line tscompat/tscompat
logger.log(deadline.timeRemaining(), deadline.didTimeout);
const rule = rules.shift()!;
logger.log(`Executing ${rule.name}`);
Expand Down
36 changes: 22 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export function labelReadableText(label: HTMLElement): boolean {
if (!label?.textContent?.trim()) return false;

// NOTE: This is expensive and we should look into ways to not do this any more.
const hasDisplayNone =
window.getComputedStyle(label, null).display === "none";
const hasDisplayNone = window.getComputedStyle(label).display === "none";
if (hasDisplayNone) return false;

const copiedNode = label.cloneNode(true) as Element;
Expand All @@ -42,15 +41,19 @@ type Container = HTMLElement | Element | Document | ShadowRoot;
export function querySelector<T extends Element>(
selector: string,
container: Container,
options = { depth: Number.POSITIVE_INFINITY },
): T | null {
const els = querySelectorAll<T>(selector, container, options);
options,
): T | undefined {
const els = querySelectorAll<T>(
selector,
container,
options || { depth: Number.POSITIVE_INFINITY },
);

if (Array.isArray(els) && els.length > 0) {
return els[0];
}

return null;
return undefined;
}

/**
Expand All @@ -68,9 +71,12 @@ export function querySelector<T extends Element>(
export function querySelectorAll<T extends Element>(
selector: string,
container: Container,
options = { depth: Number.POSITIVE_INFINITY },
options,
): T[] {
const elements = getAllElementsAndShadowRoots(container, options);
const elements = getAllElementsAndShadowRoots(
container,
options || { depth: Number.POSITIVE_INFINITY },
);

const queriedElements = elements.flatMap((element) => [
...element.querySelectorAll<T>(selector),
Expand All @@ -80,22 +86,24 @@ export function querySelectorAll<T extends Element>(

// This could probably get really slow and memory intensive in large DOMs,
// maybe an infinite generator in the future?
export function getAllElementsAndShadowRoots(
container: Container,
options = { depth: Number.POSITIVE_INFINITY },
) {
export function getAllElementsAndShadowRoots(container: Container, options) {
const selector = "*";
return recurse(container, selector, options);
return recurse(
container,
selector,
options || { depth: Number.POSITIVE_INFINITY },
);
}

function recurse(
container: Container,
selector: string,
options = { depth: Number.POSITIVE_INFINITY },
options,
elementsToProcess: Array<Element | ShadowRoot | Document> = [],
elements: Array<Element | ShadowRoot | Document> = [],
currentDepth = 1,
) {
options = options || { depth: Number.POSITIVE_INFINITY };
// if "document" is passed in, it will also pick up "<html>" causing the query to run twice.
if (container instanceof Document) {
container = container.documentElement;
Expand Down

0 comments on commit 6e2d2dd

Please sign in to comment.