-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathinvalid-change-version.mjs
More file actions
67 lines (62 loc) · 2.26 KB
/
invalid-change-version.mjs
File metadata and controls
67 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { LINT_MESSAGES } from '../constants.mjs';
import { valid, parse } from 'semver';
import { env } from 'node:process';
const NODE_RELEASED_VERSIONS = env.NODE_RELEASED_VERSIONS?.split(',');
/**
* Checks if the given version is "REPLACEME" and the array length is 1.
*
* @param {string} version - The version to check.
* @param {number} length - Length of the version array.
* @returns {boolean} True if conditions match, otherwise false.
*/
const isValidReplaceMe = (version, length) =>
length === 1 && version === 'REPLACEME';
/**
* Checks if a given semantic version should be ignored.
* A version is considered ignored if its major version is 0 and minor version is less than 2.
*
* @param {string} version - The version to check.
* @returns {boolean} Returns true if the version is ignored, false otherwise.
*/
const isIgnoredVersion = version => {
const { major, minor } = parse(version) || {};
return major === 0 && minor < 2;
};
/**
* Determines if a given version is invalid.
*
* @param {string} version - The version to check.
* @param {unknown} _ - Unused parameter.
* @param {{ length: number }} context - Array containing the length property.
* @returns {boolean} True if the version is invalid, otherwise false.
*/
const isInvalid = NODE_RELEASED_VERSIONS
? (version, _, { length }) =>
!(
isValidReplaceMe(version, length) ||
isIgnoredVersion(version) ||
NODE_RELEASED_VERSIONS.includes(version.replace(/^v/, ''))
)
: (version, _, { length }) =>
!(isValidReplaceMe(version, length) || valid(version));
/**
* Identifies invalid change versions from metadata entries.
*
* @param {ApiDocMetadataEntry[]} entries - Metadata entries to check.
* @returns {import('../types').LintIssue[]} List of detected lint issues.
*/
export const invalidChangeVersion = entries =>
entries.flatMap(({ changes, api_doc_source, yaml_position }) =>
changes.flatMap(({ version }) =>
(Array.isArray(version) ? version : [version])
.filter(isInvalid)
.map(version => ({
level: 'error',
message: LINT_MESSAGES.invalidChangeVersion.replace(
'{{version}}',
version
),
location: { path: api_doc_source, position: yaml_position },
}))
)
);