Skip to content

Commit 4512b5c

Browse files
committed
duplicate-stability-nodes
1 parent 4a1cde1 commit 4512b5c

File tree

3 files changed

+191
-119
lines changed

3 files changed

+191
-119
lines changed
Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1+
import createQueries from '../../utils/queries/index.mjs';
12
import { LINT_MESSAGES } from '../constants.mjs';
3+
import { visit } from 'unist-util-visit';
24

35
/**
46
* Checks if there are multiple stability nodes within a chain.
57
*
6-
* @param {ApiDocMetadataEntry[]} entries
7-
* @returns {Array<import('../types').LintIssue>}
8+
* @param {import('../types.d.ts').LintContext} context
9+
* @returns {void}
810
*/
9-
export const duplicateStabilityNodes = entries => {
11+
export const duplicateStabilityNodes = context => {
1012
const issues = [];
1113
let currentDepth = 0;
1214
let currentStability = -1;
15+
let currentHeaderDepth = 0;
1316

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]);
1739

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+
}
3458
}
35-
}
59+
});
3660

3761
return issues;
3862
};

src/linter/rules/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
// import { duplicateStabilityNodes } from './duplicate-stability-nodes.mjs';
3+
import { duplicateStabilityNodes } from './duplicate-stability-nodes.mjs';
44
import { invalidChangeVersion } from './invalid-change-version.mjs';
55
import { missingIntroducedIn } from './missing-introduced-in.mjs';
66
import { missingLlmDescription } from './missing-llm-description.mjs';
@@ -9,7 +9,7 @@ import { missingLlmDescription } from './missing-llm-description.mjs';
99
* @type {Record<string, import('../types').LintRule>}
1010
*/
1111
export default {
12-
// 'duplicate-stability-nodes': duplicateStabilityNodes,
12+
'duplicate-stability-nodes': duplicateStabilityNodes,
1313
'invalid-change-version': invalidChangeVersion,
1414
'missing-introduced-in': missingIntroducedIn,
1515
'missing-llm-description': missingLlmDescription,

0 commit comments

Comments
 (0)