Skip to content

Commit 97d9f7f

Browse files
committed
Fix some outstanding bugs in the docs linter.
1 parent f5a4d84 commit 97d9f7f

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

_lint-docs/lint-http-link.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function checkKnownCorrectRequestFail(href, headers, status, body) {
2828

2929
/**
3030
* Returns `undefined` if no error, a string if an error does occur.
31-
* @param {(message?: string)} callback
31+
* @param {(message?: string) => void} callback
3232
*/
33-
function checkHttp(href, callback) {
33+
function checkHttpInner(href, callback) {
3434
// Prefer https: > http: where possible, but allow http: when https: is inaccessible.
3535

3636
const url = new URL(href)
@@ -67,6 +67,39 @@ function checkHttp(href, callback) {
6767
})
6868
}
6969

70+
// Kill the remaining duplication by using a global cache.
71+
const urlCache = new Map()
72+
73+
/**
74+
* Returns `undefined` if no error, a string if an error does occur.
75+
* @param {(message?: string) => void} callback
76+
*/
77+
function checkHttp(href, callback) {
78+
if (href.includes("#")) {
79+
process.exitCode = 1
80+
callback(`Expected href to be sanitized of hashes, but found ${href}`)
81+
}
82+
83+
if (urlCache.has(href)) {
84+
const message = urlCache.get(href)
85+
if (Array.isArray(message)) {
86+
message.push(callback)
87+
} else {
88+
process.nextTick(callback, message)
89+
}
90+
} else {
91+
const queue = []
92+
urlCache.set(href, queue)
93+
checkHttpInner(href, (message) => {
94+
urlCache.set(href, message)
95+
process.nextTick(callback, message)
96+
for (const callback of queue) {
97+
process.nextTick(callback, message)
98+
}
99+
})
100+
}
101+
}
102+
70103
module.exports = {
71104
checkHttp,
72105
}

_lint-docs/task-queue.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let running = 0
99
function runTask(task, callback) {
1010
process.nextTick(task, (...args) => {
1111
process.nextTick(callback, ...args)
12-
if (running === maxConcurrency) {
12+
if (running === maxConcurrency && queue.length !== 0) {
1313
const [nextTask, nextCallback] = queue.splice(0, 2)
1414
runTask(nextTask, nextCallback)
1515
}
@@ -22,6 +22,14 @@ function runTask(task, callback) {
2222
* @param {(...args: A) => void} callback
2323
*/
2424
function submitTask(task, callback) {
25+
if (typeof task !== "function") {
26+
throw new TypeError("`task` must be a function")
27+
}
28+
29+
if (typeof callback !== "function") {
30+
throw new TypeError("`callback` must be a function")
31+
}
32+
2533
if (running < maxConcurrency) {
2634
running++
2735
runTask(task, callback)

0 commit comments

Comments
 (0)