Skip to content

Commit 4bc0567

Browse files
Copilotfregante
andcommitted
Add strict validation to exists() and demonstrate with invalid selector
- Added function overloads to exists() with StrictlyParseSelector - Returns never when selector is invalid, boolean when valid - Intentionally broke isLoggedIn selector to demonstrate validation - Selector 'body.logged-in >' returns never (invalid trailing combinator) Co-authored-by: fregante <1402241+fregante@users.noreply.github.com>
1 parent 5412d1b commit 4bc0567

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function $<Selected extends Element>(selector: string): Selected | undefined {
1212
return document.querySelector<Selected>(selector) ?? undefined;
1313
}
1414

15+
function exists<Selector extends string, Selected extends Element = StrictlyParseSelector<Selector>>(
16+
selector: Selector,
17+
): Selected extends never ? never : boolean;
18+
function exists(selector: string): boolean;
1519
function exists(selector: string): boolean {
1620
return Boolean(document.querySelector(selector));
1721
}
@@ -31,7 +35,7 @@ export const is500 = (): boolean => document.title === 'Server Error · GitHub'
3135

3236
export const isPasswordConfirmation = (): boolean => document.title === 'Confirm password' || document.title === 'Confirm access';
3337

34-
export const isLoggedIn = (): boolean => exists('body.logged-in');
38+
export const isLoggedIn = (): boolean => exists('body.logged-in >');
3539

3640
export const isBlame = (url: URL | HTMLAnchorElement | Location = location): boolean => Boolean(getRepo(url)?.path.startsWith('blame/'));
3741
TEST: addTests('isBlame', [

0 commit comments

Comments
 (0)