| 11 | |
| 12 | // Helper function to parse errors documentation and return a Map |
| 13 | function getErrorsInDoc() { |
| 14 | const lines = doc.split('\n'); |
| 15 | let currentHeader; |
| 16 | const errors = new Set(); |
| 17 | const legacyErrors = new Set(); |
| 18 | const codePattern = /^### `([^`]+)`$/; |
| 19 | const anchorPattern = /^<a id="([^"]+)"><\/a>$/; |
| 20 | |
| 21 | let previousAnchor; |
| 22 | |
| 23 | function parse(line, legacy, lineNumber) { |
| 24 | const anchorMatch = anchorPattern.exec(line); |
| 25 | if (anchorMatch) { |
| 26 | const code = anchorMatch[1]; |
| 27 | if (previousAnchor != null && previousAnchor > code) { |
| 28 | throw new Error(`Unordered error anchor in ${docPath}:${lineNumber}`, { cause: `${previousAnchor} ≤ ${code}` }); |
| 29 | } |
| 30 | previousAnchor = code; |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | const codeMatch = codePattern.exec(line); |
| 35 | if (codeMatch == null) return; |
| 36 | |
| 37 | const code = codeMatch[1]; |
| 38 | if (previousAnchor == null) { |
| 39 | throw new Error(`Missing error anchor in ${docPath}:${lineNumber}`, { cause: code }); |
| 40 | } |
| 41 | assert.strictEqual(code, previousAnchor, `Error anchor do not match with error code in ${docPath}:${lineNumber}`); |
| 42 | |
| 43 | if (legacy && errors.has(code)) { |
| 44 | throw new Error(`Error is documented both as legacy and non-legacy in ${docPath}:${lineNumber}`, { cause: code }); |
| 45 | } |
| 46 | (legacy ? legacyErrors : errors).add(code); |
| 47 | } |
| 48 | |
| 49 | let lineNumber = 0; |
| 50 | for (const line of lines) { |
| 51 | lineNumber++; |
| 52 | if (line.startsWith('## ')) currentHeader = line.substring(3); |
| 53 | if (currentHeader === 'Node.js error codes') parse(line, false, lineNumber); |
| 54 | if (line === '## Legacy Node.js error codes') previousAnchor = null; |
| 55 | if (currentHeader === 'Legacy Node.js error codes') parse(line, true, lineNumber); |
| 56 | } |
| 57 | |
| 58 | return { errors, legacyErrors }; |
| 59 | } |
| 60 | |
| 61 | // Main rule export |
| 62 | module.exports = { |