Skip to content

Commit

Permalink
build: upgrade to xo 0.58 (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 8, 2024
1 parent 501fc4c commit b010033
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 421 deletions.
785 changes: 408 additions & 377 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,7 +49,7 @@
"sinon": "^17.0.0",
"strip-ansi": "^7.0.1",
"typescript": "^5.0.0",
"xo": "^0.56.0"
"xo": "^0.58.0"
},
"engines": {
"node": ">=18"
Expand Down
24 changes: 5 additions & 19 deletions src/cli.ts
Expand Up @@ -166,10 +166,6 @@ async function main() {
logger.info(`${state} ${chalk.gray(link.url)}`);
break;
}

default: {
throw new Error('Unexpected link state.');
}
}

if (format === Format.CSV) {
Expand Down Expand Up @@ -247,14 +243,12 @@ async function main() {
// }
// eslint-disable-next-line unicorn/no-array-reduce
const parents = result.links.reduce<Record<string, LinkResult[]>>(
(acc, curr) => {
const parent = curr.parent || '';
if (!acc[parent]) {
acc[parent] = [];
}
(accumulator, current) => {
const parent = current.parent || '';
accumulator[parent] ||= [];

acc[parent].push(curr);
return acc;
accumulator[parent].push(current);
return accumulator;
},
{},
);
Expand Down Expand Up @@ -306,10 +300,6 @@ async function main() {
logger.info(` ${state} ${chalk.gray(link.url)}`);
break;
}

default: {
throw new Error('Unexpected link state.');
}
}
}
}
Expand Down Expand Up @@ -398,10 +388,6 @@ function shouldShowResult(link: LinkResult, verbosity: LogLevel) {
case LinkState.SKIPPED: {
return verbosity <= LogLevel.INFO;
}

default: {
throw new Error('Unexpected link state.');
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -71,7 +71,7 @@ export class LinkChecker extends EventEmitter {
on(event: 'link', listener: (result: LinkResult) => void): this;
on(event: 'pagestart', listener: (link: string) => void): this;
on(event: 'retry', listener: (details: RetryInfo) => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this {
on(event: string | symbol, listener: (...arguments_: any[]) => void): this {
return super.on(event, listener);
}

Expand All @@ -86,7 +86,7 @@ export class LinkChecker extends EventEmitter {
options.path = [options.path];
}

options.linksToSkip = options.linksToSkip || [];
options.linksToSkip ||= [];
let server: http.Server | undefined;
const hasHttpPaths = options.path.find((x) => x.startsWith('http'));
if (!hasHttpPaths) {
Expand Down
23 changes: 12 additions & 11 deletions src/links.ts
Expand Up @@ -2,7 +2,7 @@ import {type Readable} from 'node:stream';
import {WritableStream} from 'htmlparser2/lib/WritableStream';
import {parseSrcset} from 'srcset';

const linksAttr: Record<string, string[]> = {
const linksAttribute: Record<string, string[]> = {
background: ['body'],
cite: ['blockquote', 'del', 'ins', 'q'],
data: ['object'],
Expand All @@ -29,11 +29,11 @@ const linksAttr: Record<string, string[]> = {
srcset: ['img', 'source'],
};
// Create lookup table for tag name to attribute that contains URL:
const tagAttr: Record<string, string[]> = {};
for (const attr of Object.keys(linksAttr)) {
for (const tag of linksAttr[attr]) {
if (!tagAttr[tag]) tagAttr[tag] = [];
tagAttr[tag].push(attr);
const tagAttribute: Record<string, string[]> = {};
for (const attribute of Object.keys(linksAttribute)) {
for (const tag of linksAttribute[attribute]) {
tagAttribute[tag] ||= [];
tagAttribute[tag].push(attribute);
}
}

Expand All @@ -59,6 +59,7 @@ export async function getLinks(
}

// ignore href properties for link tags where rel is likely to fail
// eslint-disable-next-line unicorn/prevent-abbreviations
const relValuesToIgnore = ['dns-prefetch', 'preconnect'];
if (tag === 'link' && relValuesToIgnore.includes(attributes.rel)) {
return;
Expand All @@ -75,11 +76,11 @@ export async function getLinks(
}
}

if (tagAttr[tag]) {
for (const attr of tagAttr[tag]) {
const linkString = attributes[attr];
if (tagAttribute[tag]) {
for (const attribute of tagAttribute[tag]) {
const linkString = attributes[attribute];
if (linkString) {
for (const link of parseAttr(attr, linkString)) {
for (const link of parseAttribute(attribute, linkString)) {
links.push(parseLink(link, realBaseUrl));
}
}
Expand Down Expand Up @@ -114,7 +115,7 @@ function isAbsoluteUrl(url: string): boolean {
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
}

function parseAttr(name: string, value: string): string[] {
function parseAttribute(name: string, value: string): string[] {
switch (name) {
case 'srcset': {
// The swapping of any multiple spaces into a single space is here to
Expand Down
4 changes: 1 addition & 3 deletions src/options.ts
Expand Up @@ -75,9 +75,7 @@ export async function processOptions(
);
}

if (options.serverRoot) {
options.serverRoot = path.normalize(options.serverRoot);
}
options.serverRoot &&= path.normalize(options.serverRoot);

// Expand globs into paths
if (!isUrlType) {
Expand Down
6 changes: 3 additions & 3 deletions src/queue.ts
Expand Up @@ -33,15 +33,15 @@ export class Queue extends EventEmitter {
}

on(event: 'done', listener: () => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this {
on(event: string | symbol, listener: (...arguments_: any[]) => void): this {
return super.on(event, listener);
}

add(fn: AsyncFunction, options?: QueueItemOptions) {
add(function_: AsyncFunction, options?: QueueItemOptions) {
const delay = options?.delay || 0;
const timeToRun = Date.now() + delay;
this.q.push({
fn,
fn: function_,
timeToRun,
});
setTimeout(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Expand Up @@ -76,12 +76,12 @@ async function handleRequest(
const isDirectory = stats.isDirectory();
if (isDirectory) {
// This means we got a path with no / at the end!
const doc = "<html><body>Redirectin'</body></html>";
const document = "<html><body>Redirectin'</body></html>";
response.statusCode = 301;
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.setHeader('Content-Length', Buffer.byteLength(doc));
response.setHeader('Content-Length', Buffer.byteLength(document));
response.setHeader('Location', request.url + '/');
response.end(doc);
response.end(document);
return;
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions test/test.cli.ts
Expand Up @@ -12,10 +12,10 @@ describe('cli', function () {
let server: http.Server;
this.timeout(20_000);

const pkg = JSON.parse(
const package_ = JSON.parse(
fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf8'),
) as {bin: {linkinator: string}};
const {linkinator} = pkg.bin;
const {linkinator} = package_.bin;
const node = 'node';

afterEach(async () => {
Expand Down

0 comments on commit b010033

Please sign in to comment.