Skip to content

Commit 9ff422d

Browse files
committed
tmp
1 parent f51c583 commit 9ff422d

File tree

13 files changed

+125
-11
lines changed

13 files changed

+125
-11
lines changed

src/constants.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,6 @@ export const LINT_MESSAGES = {
425425
missingIntroducedIn: "Missing 'introduced_in' field in the API doc entry",
426426
missingChangeVersion: 'Missing version field in the API doc entry',
427427
invalidChangeVersion: 'Invalid version number: {{version}}',
428+
malformedDeprecationHeader: 'Malformed deprecation header',
429+
outOfOrderDeprecationCode: "Deprecation code '{{code}}' out of order",
428430
};

src/generators/legacy-json/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface HierarchizedEntry extends ApiDocMetadataEntry {
88
/**
99
* List of child entries that are part of this entry's hierarchy.
1010
*/
11-
hierarchyChildren: ApiDocMetadataEntry[];
11+
hierarchyChildren: HierarchizedEntry[];
1212
}
1313

1414
/**

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildHierarchy } from './buildHierarchy.mjs';
1+
import { buildHierarchy } from '../../../utils/buildHierarchy.mjs';
22
import { getRemarkRehype } from '../../../utils/remark.mjs';
33
import { transformNodesToString } from '../../../utils/unist.mjs';
44
import { parseList } from './parseList.mjs';

src/linter/constants.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
// Validates a deprecation header from doc/api/deprecation.md and captures the
4+
// code
5+
// For example, `DEP0001: `http.OutgoingMessage.prototype.flush` captures `0001`
6+
export const DEPRECATION_HEADER_REGEX = /DEP(\d{4}): .+?/;

src/linter/engine.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const createLinterEngine = rules => {
1616
const issues = [];
1717

1818
for (const rule of rules) {
19-
const ruleIssues = rule(entry);
19+
const ruleIssues = rule([entry]);
2020

2121
if (ruleIssues.length > 0) {
2222
issues.push(...ruleIssues);
@@ -35,8 +35,12 @@ const createLinterEngine = rules => {
3535
const lintAll = entries => {
3636
const issues = [];
3737

38-
for (const entry of entries) {
39-
issues.push(...lint(entry));
38+
for (const rule of rules) {
39+
const ruleIssues = rule(entries);
40+
41+
if (ruleIssues.length > 0) {
42+
issues.push(...ruleIssues);
43+
}
4044
}
4145

4246
return issues;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @ts-check
2+
'use strict';
3+
4+
import { LINT_MESSAGES } from '../../constants.mjs';
5+
import { buildHierarchy } from '../../utils/buildHierarchy.mjs';
6+
import { DEPRECATION_HEADER_REGEX } from '../constants.mjs';
7+
import getDeprecationEntries from './utils/getDeprecationEntries.mjs';
8+
9+
/**
10+
* @param {ApiDocMetadataEntry} deprecation
11+
* @param {number} expectedCode
12+
* @returns {Array<import('../types').LintIssue>}
13+
*/
14+
function lintDeprecation(deprecation, expectedCode) {
15+
// Try validating the header (`DEPXXXX: ...`) and extract the code for us to
16+
// look at
17+
const match = deprecation.heading.data.text.match(DEPRECATION_HEADER_REGEX);
18+
19+
if (!match) {
20+
// Malformed header
21+
return [
22+
{
23+
level: 'error',
24+
location: {
25+
path: deprecation.api_doc_source,
26+
position: deprecation.yaml_position,
27+
},
28+
message: LINT_MESSAGES.malformedDeprecationHeader,
29+
},
30+
];
31+
}
32+
33+
const code = Number(match[1]);
34+
35+
return code === expectedCode ? [] : [
36+
{
37+
level: 'error',
38+
location: {
39+
path: deprecation.api_doc_source,
40+
position: deprecation.yaml_position,
41+
},
42+
message: LINT_MESSAGES.outOfOrderDeprecationCode.replaceAll('{{code}}', match[1]),
43+
},
44+
];
45+
}
46+
47+
/**
48+
* Checks if any deprecation codes are out of order
49+
*
50+
* @type {import('../types').LintRule}
51+
*/
52+
export const deprecationCodeOrder = entries => {
53+
if (entries.length === 0 || entries[0].api !== 'deprecations') {
54+
// This is only relevant to doc/api/deprecations.md
55+
return [];
56+
}
57+
58+
const issues = [];
59+
60+
const hierarchy = buildHierarchy(entries);
61+
62+
hierarchy.forEach(root => {
63+
const deprecations = getDeprecationEntries(root.hierarchyChildren);
64+
65+
let expectedCode = 1;
66+
67+
deprecations?.forEach(deprecation => {
68+
issues.push(...lintDeprecation(deprecation, expectedCode));
69+
70+
expectedCode++;
71+
});
72+
});
73+
74+
return issues;
75+
};

src/linter/rules/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use strict';
22

3+
import { deprecationCodeOrder } from './deprecation-code-order.mjs';
34
import { invalidChangeVersion } from './invalid-change-version.mjs';
5+
import { malformedDeprecationHeader } from './malformed-deprecation-header.mjs';
46
import { missingChangeVersion } from './missing-change-version.mjs';
57
import { missingIntroducedIn } from './missing-introduced-in.mjs';
68

79
/**
810
* @type {Record<string, import('../types').LintRule>}
911
*/
1012
export default {
11-
'invalid-change-version': invalidChangeVersion,
12-
'missing-change-version': missingChangeVersion,
13-
'missing-introduced-in': missingIntroducedIn,
13+
// 'invalid-change-version': invalidChangeVersion,
14+
// 'missing-change-version': missingChangeVersion,
15+
// 'missing-introduced-in': missingIntroducedIn,
16+
'deprecation-code-order': deprecationCodeOrder
17+
// 'malformed-deprecation-header': malformedDeprecationHeader,
1418
};

src/linter/rules/invalid-change-version.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
import { LINT_MESSAGES } from '../../constants.mjs';
24
import { valid } from 'semver';
35

src/linter/rules/missing-change-version.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
/**
24
* Checks if any change version is missing
35
*

src/linter/rules/missing-introduced-in.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
import { LINT_MESSAGES } from '../../constants.mjs';
24

35
/**

0 commit comments

Comments
 (0)