|
| 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 | +}; |
0 commit comments