Skip to content

Commit

Permalink
chore: add lint rule for semver.validRange and semver.Range (#4407)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz authored Apr 28, 2022
1 parent 67ce47f commit 0eb915f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ module.exports = {
},
},
],
rules: {
'no-restricted-properties': [2,
{
object: `semver`,
property: `validRange`,
message: `Use 'semverUtils.validRange' instead`,
},
{
object: `semver`,
property: `Range`,
message: `Use 'semverUtils.validRange' instead`,
},
],
},
};
3 changes: 3 additions & 0 deletions .yarn/versions/e2989261.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declined:
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/core"
4 changes: 2 additions & 2 deletions packages/plugin-essentials/sources/suggestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Cache, DescriptorHash, Descriptor, Ident, Locator, Manifest, Project, ThrowReport, Workspace, FetchOptions, ResolveOptions, Configuration} from '@yarnpkg/core';
import {formatUtils, structUtils} from '@yarnpkg/core';
import {formatUtils, structUtils, semverUtils} from '@yarnpkg/core';
import {PortablePath, ppath, xfs} from '@yarnpkg/fslib';
import semver from 'semver';

Expand Down Expand Up @@ -209,7 +209,7 @@ export async function getSuggestedDescriptors(request: Descriptor, {project, wor
throw new Error(`Invalid maxResults (${maxResults})`);

const [requestRange, requestTag] = request.range !== `unknown`
? fixed || semver.validRange(request.range) || !request.range.match(/^[a-z0-9._-]+$/i)
? fixed || semverUtils.validRange(request.range) || !request.range.match(/^[a-z0-9._-]+$/i)
? [request.range, `latest`]
: [`unknown`, request.range]
: [`unknown`, `latest`];
Expand Down
2 changes: 2 additions & 0 deletions packages/yarnpkg-core/sources/semverUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function satisfiesWithPrereleases(version: string | null, range: string,
let semverRange = satisfiesWithPrereleasesCache.get(key);
if (typeof semverRange === `undefined`) {
try {
// eslint-disable-next-line no-restricted-properties
semverRange = new semver.Range(range, {includePrerelease: true, loose});
} catch {
return false;
Expand Down Expand Up @@ -74,6 +75,7 @@ export function validRange(potentialRange: string): semver.Range | null {
return range;

try {
// eslint-disable-next-line no-restricted-properties
range = new semver.Range(potentialRange);
} catch {
range = null;
Expand Down

0 comments on commit 0eb915f

Please sign in to comment.