Skip to content

Commit a03132e

Browse files
lubiensindresorhus
andcommitted
Support pre-release versions in expiring-todo-comments rule (#435)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 1bc47a0 commit a03132e

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

docs/rules/expiring-todo-comments.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ Argument versions should be [semver](https://semver.org/) compatible such as: `1
9797

9898
Supported comparisons are `>` and `>=`. Comparison must have a `@` before such as `@>` and `@>=`.
9999

100+
### Pre-releases
101+
102+
TODO comments with rules for package.json and dependency versions support the semver pre-release format, such as `1.0.0-my.pre.release.1.2.3`. This means that if your TODO asks for version `>=1.0.0` and you're in `1.0.0-beta`, your TODO will **not** trigger as a pre-release comes first. When the version is at least `1.0.0`, it will properly trigger.
103+
104+
Keep in mind that pre-releases compare by number and alphabetical order. Example: `1.0.0-alpha` < `1.0.0-alpha.1` < `1.0.0-alpha.beta` < `1.0.0-beta` < `1.0.0-beta.2` < `1.0.0-beta.11` < `1.0.0-rc.1` < `1.0.0`.
105+
106+
You can read more about the semver pre-release format [here](https://semver.org/#spec-item-9) and semver precedence rules [here](https://semver.org/#spec-item-11).
107+
100108
### Combinations
101109

102110
Any combination of rules is possible as long as you separate them by commas. Each condition **triggers an individual report.**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"semver": "^6.3.0"
5151
},
5252
"devDependencies": {
53+
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
5354
"ava": "^2.4.0",
5455
"babel-eslint": "^10.0.3",
5556
"chalk": "^2.4.2",

rules/expiring-todo-comments.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const packageDependencies = {
2727
};
2828

2929
const DEPENDENCY_INCLUSION_RE = /^[+|-]\s*@?[\S+]\/?\S+/;
30-
const VERSION_COMPARISON_RE = /^(@?[\S+]\/?\S+)@(>|>=)([\d]+(\.\d+){0,2})/;
31-
const PKG_VERSION_RE = /^(>|>=)([\d]+(\.\d+){0,2})\s*$/;
30+
const VERSION_COMPARISON_RE = /^(@?[\S+]\/?\S+)@(>|>=)([\d]+(\.\d+){0,2}(-[\da-z-]+(\.[\da-z-]+)*)?(\+[\da-z-]+(\.[\da-z-]+)*)?)/i;
31+
const PKG_VERSION_RE = /^(>|>=)([\d]+(\.\d+){0,2}(-[\da-z-]+(\.[\da-z-]+)*)?(\+[\da-z-]+(\.[\da-z-]+)*)?)\s*$/;
3232
const ISO8601_DATE = /(\d{4})-(\d{2})-(\d{2})/;
3333

3434
function parseTodoWithArguments(string, {terms}) {
@@ -158,9 +158,37 @@ function reachedDate(past) {
158158
return Date.parse(past) < Date.parse(now);
159159
}
160160

161-
function tryToCoerceVersion(version) {
161+
function tryToCoerceVersion(rawVersion) {
162+
let version = rawVersion;
163+
164+
// Remove leading things like `^1.0.0`, `>1.0.0`
165+
const leadingNoises = [
166+
'>=',
167+
'<=',
168+
'>',
169+
'<',
170+
'~',
171+
'^'
172+
];
173+
const foundTrailingNoise = leadingNoises.find(noise => version.startsWith(noise));
174+
if (foundTrailingNoise) {
175+
version = version.slice(foundTrailingNoise.length);
176+
}
177+
178+
// Get only the first member for cases such as `1.0.0 - 2.9999.9999`
179+
const parts = version.split(' ');
180+
if (parts.length > 1) {
181+
version = parts[0];
182+
}
183+
184+
if (semver.valid(version)) {
185+
return version;
186+
}
187+
162188
try {
163-
return semver.coerce(version);
189+
// Try to semver.parse a perfect match while semver.coerce tries to fix errors
190+
// But coerce can't parse pre-releases.
191+
return semver.parse(version) || semver.coerce(version);
164192
} catch (_) {
165193
return false;
166194
}

test/expiring-todo-comments.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ ruleTester.run('expiring-todo-comments', rule, {
8181
'// TODO [+popura]: I think we wont need a broken package.',
8282
'// TODO [semver@>1000]: Welp hopefully we wont get at that.',
8383
'// TODO [semver@>=1000]: Welp hopefully we wont get at that.',
84+
'// TODO [@lubien/fixture-beta-package@>=1.0.0]: we are using a pre-release',
85+
'// TODO [@lubien/fixture-beta-package@>=1.0.0-gamma.1]: beta comes first from gamma',
86+
'// TODO [@lubien/fixture-beta-package@>=1.0.0-beta.2]: we are in beta.1',
8487
'// TODO [2200-12-12, -read-pkg-up]: Combo',
8588
'// TODO [2200-12-12, -read-pkg-up, +popura]: Combo',
8689
'// TODO [2200-12-12, -read-pkg-up, +popura, semver@>=1000]: Combo',
@@ -223,6 +226,18 @@ ruleTester.run('expiring-todo-comments', rule, {
223226
code: '// TODO [read-pkg-up@>=5.1.1]: when `read-pkg-up` version is >= 5.1.1',
224227
errors: [versionMatchesError('read-pkg-up >= 5.1.1', 'when `read-pkg-up` version is >= 5.1.1')]
225228
},
229+
{
230+
code: '// TODO [@lubien/fixture-beta-package@>=1.0.0-alfa.1]: when `@lubien/fixture-beta-package` version is >= 1.0.0-alfa.1',
231+
errors: [versionMatchesError('@lubien/fixture-beta-package >= 1.0.0-alfa.1', 'when `@lubien/fixture-beta-package` version is >= 1.0.0-alfa.1')]
232+
},
233+
{
234+
code: '// TODO [@lubien/fixture-beta-package@>=1.0.0-beta.1]: when `@lubien/fixture-beta-package` version is >= 1.0.0-beta.1',
235+
errors: [versionMatchesError('@lubien/fixture-beta-package >= 1.0.0-beta.1', 'when `@lubien/fixture-beta-package` version is >= 1.0.0-beta.1')]
236+
},
237+
{
238+
code: '// TODO [@lubien/fixture-beta-package@>=1.0.0-beta.0]: when `@lubien/fixture-beta-package` version is >= 1.0.0-beta.0',
239+
errors: [versionMatchesError('@lubien/fixture-beta-package >= 1.0.0-beta.0', 'when `@lubien/fixture-beta-package` version is >= 1.0.0-beta.0')]
240+
},
226241
{
227242
code: '// TODO [semver>1]: Missing @.',
228243
errors: [missingAtSymbolError('semver>1', 'semver@>1', 'Missing @.')]

0 commit comments

Comments
 (0)