Skip to content

Commit

Permalink
refactor(datasource/crate): Enable strict null checks (#13915)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 7, 2022
1 parent c966eb1 commit c1555ef
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/datasource/crate/index.spec.ts
Expand Up @@ -341,6 +341,8 @@ describe('datasource/crate/index', () => {
describe('fetchCrateRecordsPayload', () => {
it('rejects if it has neither clonePath nor crates.io flavor', async () => {
const info: RegistryInfo = {
rawUrl: 'https://example.com',
url: new URL('https://example.com'),
flavor: RegistryFlavor.Cloudsmith,
};
const crateDatasource = new CrateDatasource();
Expand Down
16 changes: 11 additions & 5 deletions lib/datasource/crate/index.ts
Expand Up @@ -8,6 +8,7 @@ import { cache } from '../../util/cache/package/decorator';
import { privateCacheDir, readFile } from '../../util/fs';
import { simpleGitConfig } from '../../util/git/config';
import { newlineRegex, regEx } from '../../util/regex';
import { parseUrl } from '../../util/url';
import * as cargoVersioning from '../../versioning/cargo';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
Expand Down Expand Up @@ -163,10 +164,13 @@ export class CrateDatasource extends Datasource {
lookupName,
registryUrl,
}: GetReleasesConfig): Promise<RegistryInfo | null> {
let url: URL;
try {
url = new URL(registryUrl);
} catch (err) {
// istanbul ignore if
if (!registryUrl) {
return null;
}

const url = parseUrl(registryUrl);
if (!url) {
logger.debug({ registryUrl }, 'could not parse registry URL');
return null;
}
Expand Down Expand Up @@ -256,7 +260,9 @@ export class CrateDatasource extends Datasource {
return registry;
}

private static areReleasesCacheable(registryUrl: string): boolean {
private static areReleasesCacheable(
registryUrl: string | undefined
): boolean {
// We only cache public releases, we don't want to cache private
// cloned data between runs.
return registryUrl === 'https://crates.io';
Expand Down
4 changes: 2 additions & 2 deletions lib/datasource/crate/types.ts
Expand Up @@ -14,10 +14,10 @@ export interface RegistryInfo {
flavor: RegistryFlavor;

/** raw URL of the registry, as specified in cargo config */
rawUrl?: string;
rawUrl: string;

/** parsed URL of the registry */
url?: URL;
url: URL;

/** path where the registry is cloned */
clonePath?: string;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/url.spec.ts
Expand Up @@ -72,8 +72,8 @@ describe('util/url', () => {
});

it('parses URL', () => {
expect(parseUrl(null as never)).toBeNull();
expect(parseUrl(undefined as never)).toBeNull();
expect(parseUrl(null)).toBeNull();
expect(parseUrl(undefined)).toBeNull();

const url = parseUrl('https://github.com/renovatebot/renovate');
expect(url?.protocol).toBe('https:');
Expand Down
6 changes: 5 additions & 1 deletion lib/util/url.ts
Expand Up @@ -67,7 +67,11 @@ export function validateUrl(url?: string, httpOnly = true): boolean {
}
}

export function parseUrl(url: string): URL | null {
export function parseUrl(url: string | undefined | null): URL | null {
if (!url) {
return null;
}

try {
return new URL(url);
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.strict.json
Expand Up @@ -81,7 +81,6 @@
"lib/datasource/artifactory/index.ts",
"lib/datasource/bitbucket-tags/index.ts",
"lib/datasource/cdnjs/index.ts",
"lib/datasource/crate/index.ts",
"lib/datasource/datasource.ts",
"lib/datasource/docker/common.ts",
"lib/datasource/docker/index.ts",
Expand Down

0 comments on commit c1555ef

Please sign in to comment.