Skip to content

Commit b74556c

Browse files
authored
feat: allowlist for version numbers in YAML comments (#172)
* feat: allowlist for version numbers in YAML comments If an environment variable `NODE_RELEASED_VERSIONS` is detected, it is used to validate the version numbers in the YAML comments. At the notable exception of version numbers 0.0.x and 0.1.x, and the `REPLACEME` placeholder, any value not in the list is reported as invalid. The idea is to use this in a GitHub Action. It will generally not affect running the linter locally.
1 parent 37f0097 commit b74556c

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

‎README.md‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,34 @@ npm test
2020

2121
### Adding the language to the documentation style guide
2222

23-
1. PR the [nodejs/node](https://github.com/nodejs/node) repo adding the language/grammar to the [documentation style guide](https://github.com/nodejs/node/blob/master/doc/guides/doc-style-guide.md)
23+
1. PR the [nodejs/node](https://github.com/nodejs/node) repo adding the
24+
language/grammar to the
25+
[documentation style guide](https://github.com/nodejs/node/blob/master/doc/guides/doc-style-guide.md).
2426

2527
### Adding the language to the linter
2628

27-
1. PR this repo adding the language/grammar
28-
1. Bump this package version, publish it
29-
1. In [node-lint-md-cli-rollup](https://github.com/nodejs/node/tree/master/tools/node-lint-md-cli-rollup), bump the `remark-preset-lint-node` dependency
30-
1. In the `nodejs/node` repo, rebuild the Markdown linter (`make lint-md-rollup`)
31-
1. PR the `nodejs/node` repo with the updated linter
29+
1. PR this repo adding the language/grammar.
30+
1. Bump this package version, publish it.
31+
1. In
32+
[node-lint-md-cli-rollup](https://github.com/nodejs/node/tree/master/tools/node-lint-md-cli-rollup),
33+
bump the `remark-preset-lint-node` dependency.
34+
1. In the `nodejs/node` repo, rebuild the Markdown linter
35+
(`make lint-md-rollup`).
36+
1. PR the `nodejs/node` repo with the updated linter.
37+
38+
## Environment variables
39+
40+
#### `NODE_RELEASED_VERSIONS`
41+
42+
On runtime, the linter will check the environment if the
43+
`NODE_RELEASED_VERSIONS` variable is defined; if it's there, it will use the
44+
content of the variable as a comma-separated list of allowed version numbers.
45+
This list is supposed to be built from the changelog(s), and validates the
46+
version numbers for the `nodejs-yaml-comments` rule.
47+
48+
For better compatibility with the nodejs/node changelogs, there are a few
49+
exceptions:
50+
51+
- Version numbers `^0.0.0 || ^0.1.0` are not validated using the provided list,
52+
they are validating using the `vx.x.x` pattern.
53+
- `REPLACEME` placeholder is always valid, regardless it's in the list or not.

‎remark-lint-nodejs-yaml-comments.js‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ const validVersionNumberRegex = /^v\d+\.\d+\.\d+$/;
2222
const prUrlRegex = new RegExp("^https://github.com/nodejs/node/pull/\\d+$");
2323
const privatePRUrl = "https://github.com/nodejs-private/node-private/pull/";
2424

25+
let releasedVersions;
26+
let invalidVersionMessage = "version(s) must respect the pattern `vx.x.x` or";
27+
if (process.env.NODE_RELEASED_VERSIONS) {
28+
console.log("Using release list from env...");
29+
releasedVersions = process.env.NODE_RELEASED_VERSIONS.split(",").map(
30+
(v) => `v${v}`
31+
);
32+
invalidVersionMessage = `version not listed in the changelogs, `;
33+
}
34+
invalidVersionMessage += `use the placeholder \`${VERSION_PLACEHOLDER}\``;
35+
2536
const kContainsIllegalKey = Symbol("illegal key");
2637
const kWrongKeyOrder = Symbol("Wrong key order");
2738
function unorderedKeys(meta) {
@@ -41,11 +52,16 @@ function containsInvalidVersionNumber(version) {
4152
return version.some(containsInvalidVersionNumber);
4253
}
4354

44-
return (
45-
version !== undefined &&
46-
version !== VERSION_PLACEHOLDER &&
47-
!validVersionNumberRegex.test(version)
48-
);
55+
if (version === undefined || version === VERSION_PLACEHOLDER) return false;
56+
57+
if (
58+
releasedVersions &&
59+
// Always ignore 0.0.x and 0.1.x release numbers:
60+
(version[1] !== "0" || (version[3] !== "0" && version[3] !== "1"))
61+
)
62+
return !releasedVersions.includes(version);
63+
64+
return !validVersionNumberRegex.test(version);
4965
}
5066
const getValidSemver = (version) =>
5167
version === VERSION_PLACEHOLDER ? MAX_SAFE_SEMVER_VERSION : version;
@@ -122,11 +138,7 @@ function validateChanges(file, node, changes) {
122138
}
123139

124140
if (containsInvalidVersionNumber(change.version)) {
125-
file.message(
126-
`changes[${index}]: version(s) must respect the pattern \`vx.x.x\` ` +
127-
`or use the placeholder \`${VERSION_PLACEHOLDER}\``,
128-
node
129-
);
141+
file.message(`changes[${index}]: ${invalidVersionMessage}`, node);
130142
} else if (areVersionsUnordered(change.version)) {
131143
file.message(`changes[${index}]: list of versions is not in order`, node);
132144
}
@@ -180,31 +192,22 @@ function validateMeta(node, file, meta) {
180192
}
181193

182194
if (containsInvalidVersionNumber(meta.added)) {
183-
file.message(
184-
"Invalid `added` value: version(s) must respect the pattern `vx.x.x` " +
185-
`or use the placeholder \`${VERSION_PLACEHOLDER}\``,
186-
node
187-
);
195+
file.message(`Invalid \`added\` value: ${invalidVersionMessage}`, node);
188196
} else if (areVersionsUnordered(meta.added)) {
189197
file.message("Versions in `added` list are not in order", node);
190198
}
191199

192200
if (containsInvalidVersionNumber(meta.deprecated)) {
193201
file.message(
194-
"Invalid `deprecated` value: version(s) must respect the pattern `vx.x.x` " +
195-
`or use the placeholder \`${VERSION_PLACEHOLDER}\``,
202+
`Invalid \`deprecated\` value: ${invalidVersionMessage}`,
196203
node
197204
);
198205
} else if (areVersionsUnordered(meta.deprecated)) {
199206
file.message("Versions in `deprecated` list are not in order", node);
200207
}
201208

202209
if (containsInvalidVersionNumber(meta.removed)) {
203-
file.message(
204-
"Invalid `removed` value: version(s) must respect the pattern `vx.x.x` " +
205-
`or use the placeholder \`${VERSION_PLACEHOLDER}\``,
206-
node
207-
);
210+
file.message(`Invalid \`removed\` value: ${invalidVersionMessage}`, node);
208211
} else if (areVersionsUnordered(meta.removed)) {
209212
file.message("Versions in `removed` list are not in order", node);
210213
}

0 commit comments

Comments
 (0)