|
| 1 | +import createQueries from '../../utils/queries/index.mjs'; |
1 | 2 | import { LINT_MESSAGES } from '../constants.mjs'; |
| 3 | +import { visit } from 'unist-util-visit'; |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Checks if there are multiple stability nodes within a chain. |
5 | 7 | * |
6 | | - * @param {ApiDocMetadataEntry[]} entries |
7 | | - * @returns {Array<import('../types').LintIssue>} |
| 8 | + * @param {import('../types.d.ts').LintContext} context |
| 9 | + * @returns {void} |
8 | 10 | */ |
9 | | -export const duplicateStabilityNodes = entries => { |
| 11 | +export const duplicateStabilityNodes = context => { |
10 | 12 | const issues = []; |
11 | 13 | let currentDepth = 0; |
12 | 14 | let currentStability = -1; |
| 15 | + let currentHeaderDepth = 0; |
13 | 16 |
|
14 | | - for (const entry of entries) { |
15 | | - const { depth } = entry.heading.data; |
16 | | - const entryStability = entry.stability.children[0]?.data.index ?? -1; |
| 17 | + visit(context.tree, node => { |
| 18 | + // Track the current header depth |
| 19 | + if (node.type === 'heading') { |
| 20 | + currentHeaderDepth = node.depth; |
| 21 | + } |
| 22 | + |
| 23 | + // Process blockquotes to find stability nodes |
| 24 | + if (node.type === 'blockquote') { |
| 25 | + if (node.children && node.children.length > 0) { |
| 26 | + const paragraph = node.children[0]; |
| 27 | + if ( |
| 28 | + paragraph.type === 'paragraph' && |
| 29 | + paragraph.children && |
| 30 | + paragraph.children.length > 0 |
| 31 | + ) { |
| 32 | + const text = paragraph.children[0]; |
| 33 | + if (text.type === 'text') { |
| 34 | + const match = text.value.match( |
| 35 | + createQueries.QUERIES.stabilityIndex |
| 36 | + ); |
| 37 | + if (match) { |
| 38 | + const stability = parseFloat(match[1]); |
17 | 39 |
|
18 | | - if ( |
19 | | - depth > currentDepth && |
20 | | - entryStability >= 0 && |
21 | | - entryStability === currentStability |
22 | | - ) { |
23 | | - issues.push({ |
24 | | - level: 'warn', |
25 | | - message: LINT_MESSAGES.duplicateStabilityNode, |
26 | | - location: { |
27 | | - path: entry.api_doc_source, |
28 | | - position: entry.stability.children[0].children[0].position, |
29 | | - }, |
30 | | - }); |
31 | | - } else { |
32 | | - currentDepth = depth; |
33 | | - currentStability = entryStability; |
| 40 | + if ( |
| 41 | + currentHeaderDepth > currentDepth && |
| 42 | + stability >= 0 && |
| 43 | + stability === currentStability |
| 44 | + ) { |
| 45 | + issues.push({ |
| 46 | + level: 'warn', |
| 47 | + message: LINT_MESSAGES.duplicateStabilityNode, |
| 48 | + position: node.position, |
| 49 | + }); |
| 50 | + } else { |
| 51 | + currentDepth = currentHeaderDepth; |
| 52 | + currentStability = stability; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
34 | 58 | } |
35 | | - } |
| 59 | + }); |
36 | 60 |
|
37 | 61 | return issues; |
38 | 62 | }; |
0 commit comments