-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
Expand file tree
/
Copy pathdocumented-errors.js
More file actions
86 lines (73 loc) Β· 2.66 KB
/
documented-errors.js
File metadata and controls
86 lines (73 loc) Β· 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { isDefiningError } = require('./rules-utils.js');
// Load the errors documentation file once
const docPath = path.resolve(__dirname, '../../doc/api/errors.md');
const doc = fs.readFileSync(docPath, 'utf8');
// Helper function to parse errors documentation and return a Map
function getErrorsInDoc() {
const lines = doc.split('\n');
let currentHeader;
const errors = new Set();
const legacyErrors = new Set();
const codePattern = /^### `([^`]+)`$/;
const anchorPattern = /^<a id="([^"]+)"><\/a>$/;
let previousAnchor;
function parse(line, legacy, lineNumber) {
const anchorMatch = anchorPattern.exec(line);
if (anchorMatch) {
const code = anchorMatch[1];
if (previousAnchor != null && previousAnchor > code) {
throw new Error(`Unordered error anchor in ${docPath}:${lineNumber}`, { cause: `${previousAnchor} β€ ${code}` });
}
previousAnchor = code;
return;
}
const codeMatch = codePattern.exec(line);
if (codeMatch == null) return;
const code = codeMatch[1];
if (previousAnchor == null) {
throw new Error(`Missing error anchor in ${docPath}:${lineNumber}`, { cause: code });
}
assert.strictEqual(code, previousAnchor, `Error anchor do not match with error code in ${docPath}:${lineNumber}`);
if (legacy && errors.has(code)) {
throw new Error(`Error is documented both as legacy and non-legacy in ${docPath}:${lineNumber}`, { cause: code });
}
(legacy ? legacyErrors : errors).add(code);
}
let lineNumber = 0;
for (const line of lines) {
lineNumber++;
if (line.startsWith('## ')) currentHeader = line.substring(3);
if (currentHeader === 'Node.js error codes') parse(line, false, lineNumber);
if (line === '## Legacy Node.js error codes') previousAnchor = null;
if (currentHeader === 'Legacy Node.js error codes') parse(line, true, lineNumber);
}
return { errors, legacyErrors };
}
// Main rule export
module.exports = {
create(context) {
const { errors, legacyErrors } = getErrorsInDoc();
return {
ExpressionStatement(node) {
if (!isDefiningError(node)) return;
const code = node.expression.arguments?.[0]?.value;
if (!code) return;
if (legacyErrors.has(code)) {
context.report({
node,
message: `"${code}" is marked as legacy, yet it is used in lib/.`,
});
} else if (!errors.has(code)) {
context.report({
node,
message: `"${code}" is not documented in doc/api/errors.md`,
});
}
},
};
},
};