-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathinvalid-deprecations.mjs
More file actions
69 lines (55 loc) · 1.86 KB
/
invalid-deprecations.mjs
File metadata and controls
69 lines (55 loc) · 1.86 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
import { lintRule } from 'unified-lint-rule';
import { visit } from 'unist-util-visit';
const DEPRECATION_COMMENT = /^<!-- md-lint skip-deprecation (DEP\d{4}) -->$/;
const DEPRECATION_HEADING = /^(DEP\d{4}):/;
const DEPRECATION_YAML = /^<!-- YAML\r?\nchanges:\r?\n/;
const generateDeprecation = code => `DEP${code.toString().padStart(4, '0')}`;
/**
* Ensures all needed metadata exists
* @type {import('unified-lint-rule').Rule}
*/
const invalidDeprecations = (tree, vfile) => {
if (vfile.stem !== 'deprecations') {
// Skip non-deprecation files
return;
}
let expectedDeprecationCode = 1;
visit(tree, ['heading', 'html'], (node, idx, parent) => {
// Get the current deprecation
const deprecationCode =
node.type === 'html'
? node.value.match(DEPRECATION_COMMENT)?.[1]
: node.children[0].value.match(DEPRECATION_HEADING)?.[1];
if (node.type === 'heading') {
const typeNode = parent.children[idx + 1]?.children[0];
if (!typeNode?.value?.startsWith('Type:')) {
vfile.message(
`Deprecation "${deprecationCode}" is missing a "Type"`,
node
);
}
const changesNode = parent.children[idx + 2];
if (!DEPRECATION_YAML.test(changesNode?.value)) {
vfile.message(
`Deprecation "${deprecationCode}" is missing changes`,
node
);
}
}
// If not found, skip
if (!deprecationCode) {
return;
}
const generatedDeprecationCode = generateDeprecation(
expectedDeprecationCode
);
if (deprecationCode !== generatedDeprecationCode) {
vfile.message(
`Deprecation codes are out of order. Expected ${generatedDeprecationCode}, saw "${deprecationCode}"`,
node
);
}
expectedDeprecationCode++;
});
};
export default lintRule('node-core:invalid-deprecations', invalidDeprecations);