Skip to content

Commit c41bd3e

Browse files
committed
test: check stability block position in API markdown
1 parent 641653b commit c41bd3e

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

doc/api/buffer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,13 +3491,13 @@ changes:
34913491
calculations with them.
34923492
-->
34933493

3494+
> Stability: 0 - Deprecated: Use [`buf.subarray`][] instead.
3495+
34943496
* `start` {integer} Where the new `Buffer` will start. **Default:** `0`.
34953497
* `end` {integer} Where the new `Buffer` will end (not inclusive).
34963498
**Default:** [`buf.length`][].
34973499
* Returns: {Buffer}
34983500

3499-
> Stability: 0 - Deprecated: Use [`buf.subarray`][] instead.
3500-
35013501
Returns a new `Buffer` that references the same memory as the original, but
35023502
offset and cropped by the `start` and `end` indexes.
35033503

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import '../common/index.mjs';
2+
3+
import fs from 'fs';
4+
import assert from 'assert';
5+
6+
import {
7+
remarkParse,
8+
unified,
9+
} from '../../tools/doc/deps.mjs';
10+
11+
const ignore = ['deprecations.md', 'documentation.md'];
12+
13+
const docURL = new URL('../../doc/api/', import.meta.url);
14+
const docList = fs.readdirSync(docURL).filter((filename) => !ignore.includes(filename));
15+
16+
for (const file of docList) {
17+
const fileURL = new URL(file, docURL);
18+
const tree = unified()
19+
.use(remarkParse)
20+
.parse(fs.readFileSync(fileURL));
21+
22+
for (let i = 0; i < tree.children.length; i++) {
23+
const { [i]: node, [i - 1]: previousNode } = tree.children;
24+
if (node.type !== 'blockquote' || !node.children.length) continue;
25+
26+
const paragraph = node.children[0];
27+
if (paragraph.type !== 'paragraph' || !paragraph.children.length) continue;
28+
29+
const text = paragraph.children[0];
30+
if (text.type !== 'text' || !text.value.match(/^Stability: \d/)) continue;
31+
32+
// Check that previous node type is one of:
33+
// * 'heading'
34+
// * 'html' (YAML block)
35+
// * 'paragraph' with a leading 'strong' node (pseudo-heading, eg. assert.equal)
36+
try {
37+
assert(previousNode.type === 'heading' || previousNode.type === 'html' ||
38+
(previousNode.type === 'paragraph' && previousNode.children[0]?.type === 'strong'),
39+
'Stability block must be the first content element under heading');
40+
} catch (error) {
41+
const { line, column } = node.position.start;
42+
error.stack = error.stack.split('\n')
43+
.toSpliced(1, 0, ` at ${fileURL}:${line}:${column}`)
44+
.join('\n');
45+
throw error;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)