|
| 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 | +const re = /^Stability: \d/; |
| 17 | + |
| 18 | +for (const file of docList) { |
| 19 | + const fileURL = new URL(file, docURL); |
| 20 | + const tree = unified() |
| 21 | + .use(remarkParse) |
| 22 | + .parse(fs.readFileSync(fileURL)); |
| 23 | + |
| 24 | + // Traverse first-level nodes, ignoring comment blocks |
| 25 | + const nodes = tree.children.filter((node) => node.type !== 'html'); |
| 26 | + |
| 27 | + for (let i = 0; i < nodes.length; i++) { |
| 28 | + const { [i]: node, [i - 1]: previousNode } = nodes; |
| 29 | + if (node.type !== 'blockquote' || !node.children.length) continue; |
| 30 | + |
| 31 | + const paragraph = node.children[0]; |
| 32 | + if (paragraph.type !== 'paragraph' || !paragraph.children.length) continue; |
| 33 | + |
| 34 | + const text = paragraph.children[0]; |
| 35 | + if (text.type !== 'text' || !re.exec(text.value)) continue; |
| 36 | + |
| 37 | + // Check that previous node type (excluding comment blocks) is one of: |
| 38 | + // * 'heading' |
| 39 | + // * 'paragraph' with a leading 'strong' node (pseudo-heading, eg. assert.equal) |
| 40 | + try { |
| 41 | + assert(previousNode.type === 'heading' || |
| 42 | + (previousNode.type === 'paragraph' && previousNode.children[0]?.type === 'strong'), |
| 43 | + 'Stability block must be the first content element under heading'); |
| 44 | + } catch (error) { |
| 45 | + const { line, column } = node.position.start; |
| 46 | + error.stack = error.stack.split('\n') |
| 47 | + .toSpliced(1, 0, ` at ${fileURL}:${line}:${column}`) |
| 48 | + .join('\n'); |
| 49 | + throw error; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments