Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 0 additions & 144 deletions add-examples-to-dts.ts

This file was deleted.

16 changes: 3 additions & 13 deletions collector.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
/** @file This needs to be in a separate file so it can bee tree-shaken before being published, while still being importable by tests */

export const testableUrls = new Map<string, string[]>();

export function addTests(test: string, urls: string[]): void {
TEST: testableUrls.set(test, urls);
}
import examples from 'virtual:examples';

export function getTests(detectName: string): string[] {
if (detectName === 'combinedTestOnly') {
return ['combinedTestOnly'];
}

return testableUrls.get(detectName)?.flatMap(url => url.startsWith('http') ? url : getTests(url) ?? []) ?? [];
return [...new Set(examples[detectName]?.flatMap(url => url.startsWith('http') ? url : getTests(url)))];
}

export function getAllUrls(): Set<string> {
return new Set<string>([...testableUrls.values()].flat().filter(url => url.startsWith('http')));
return new Set(Object.values(examples).flat().filter(url => url.startsWith('http')));
}
4 changes: 1 addition & 3 deletions demo/Detections.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
{#if !name.startsWith('_') && name !== 'utils'}
<h2 id={name}><a href="#{name}">{name}</a></h2>
{@const urls = getTests(name)}
{#if urls[0] === 'combinedTestOnly'}
<p><em>Demo URLs missing</em></p>
{:else if urls.length === 0}
{#if urls.length === 0}
<p><em>Undeterminable via URL</em></p>
{:else}
<pre><code>
Expand Down
2 changes: 1 addition & 1 deletion demo/Index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import parseUrl from './parse-url.js';
import * as urlDetection from '../index';
import { getAllUrls, testableUrls } from '../collector';
import { getAllUrls } from '../collector';

const defaultUrl = 'https://github.com/refined-github/github-url-detection';
const urlParameter = new URLSearchParams(location.search);
Expand Down
3 changes: 2 additions & 1 deletion demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {resolve} from 'node:path';
import * as url from 'node:url';
import {defineConfig} from 'vite';
import {svelte} from '@sveltejs/vite-plugin-svelte';
import {examples} from '../vite-plugin-examples.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export default defineConfig({
base: '',
plugins: [svelte()],
plugins: [svelte(), examples()],
build: {
rollupOptions: {
input: {
Expand Down
6 changes: 6 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
interface RegExp {
test(s: string | undefined): boolean;
}

declare module 'virtual:examples' {
// Detection name => URLs or other detections
const examples: Record<string, string[]>;
export default examples;
}
17 changes: 12 additions & 5 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import * as pageDetect from './index.js';
clearTimeout(id);
};

const allUrls = getAllUrls();
// Can't be tested via URLs alone
const skipped = new Set([
'isRepoFile404', // Depends on `document.title`
'hasRepoHeader', // `isRepo` except `isRepoSearch`
]);

const skippedUrls = new Set([...skipped].flatMap(name => getTests(name)));
const allUrls = new Set([...getAllUrls()].filter(url => !skippedUrls.has(url)));

for (const [detectName, detect] of Object.entries(pageDetect)) {
if (typeof detect !== 'function') {
Expand All @@ -24,14 +31,14 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {

const validURLs = getTests(detectName);

if (validURLs[0] === 'combinedTestOnly' || String(detect).startsWith('() =>')) {
if (skipped.has(detectName) || String(detect).startsWith('() =>')) {
continue;
}

test(detectName + ' has tests', () => {
assert.ok(
Array.isArray(validURLs),
`The function \`${detectName}\` doesn’t have any tests. Set them via \`addTests()\``,
`The function \`${detectName}\` doesn’t have any tests. Add \`@example\` tags to its JSDoc`,
);
});

Expand All @@ -48,7 +55,7 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
${url.replace('https://github.com', '')}
• Yes? The \`${detectName}\` detection is wrong and should be fixed.
• No? Remove it from its \`addTests()\` array.
• No? Remove it from the \`@example\` tags of its JSDoc.
`),
);
});
Expand All @@ -64,7 +71,7 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
Is this URL \`${detectName}\`?
${url.replace('https://github.com', '')}
• Yes? Add it to the \`addTests()\` array.
• Yes? Add it to the \`@example\` tags of its JSDoc.
• No? The \`${detectName}\` detection is wrong and should be fixed.
`),
);
Expand Down
Loading
Loading