Skip to content

Commit

Permalink
refactor: Strict null checks (#13342)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jan 3, 2022
1 parent f43a40f commit cc76e41
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 34 deletions.
4 changes: 0 additions & 4 deletions lib/datasource/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { PypiDatasource } from './pypi';
import type { ReleaseResult } from './types';

describe('datasource/metadata', () => {
it('Should do nothing if dep is not specified', () => {
expect(addMetaData()).toBeUndefined();
});

it('Should handle manualChangelogUrls', () => {
const dep: ReleaseResult = {
releases: [
Expand Down
44 changes: 25 additions & 19 deletions lib/datasource/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ReleaseResult } from './types';

// Use this object to define changelog URLs for packages
// Only necessary when the changelog data cannot be found in the package's source repository
const manualChangelogUrls = {
const manualChangelogUrls: Record<string, Record<string, string>> = {
npm: {
'babel-preset-react-app':
'https://github.com/facebook/create-react-app/releases',
Expand Down Expand Up @@ -66,7 +66,7 @@ const manualChangelogUrls = {

// Use this object to define manual source URLs for packages
// Only necessary if the datasource is unable to locate the source URL itself
const manualSourceUrls = {
const manualSourceUrls: Record<string, Record<string, string>> = {
orb: {
'cypress-io/cypress': 'https://github.com/cypress-io/circleci-orb',
'hutson/library-release-workflows':
Expand Down Expand Up @@ -189,22 +189,22 @@ function massageTimestamps(dep: ReleaseResult): void {
}

export function addMetaData(
dep?: ReleaseResult,
datasource?: string,
lookupName?: string
dep: ReleaseResult,
datasource: string,
lookupName: string
): void {
if (!dep) {
return;
}

massageTimestamps(dep);

const lookupNameLowercase = lookupName ? lookupName.toLowerCase() : null;
if (manualChangelogUrls[datasource]?.[lookupNameLowercase]) {
dep.changelogUrl = manualChangelogUrls[datasource][lookupNameLowercase];
const lookupNameLowercase = lookupName.toLowerCase();
const manualChangelogUrl =
manualChangelogUrls[datasource]?.[lookupNameLowercase];
if (manualChangelogUrl) {
dep.changelogUrl = manualChangelogUrl;
}
if (manualSourceUrls[datasource]?.[lookupNameLowercase]) {
dep.sourceUrl = manualSourceUrls[datasource][lookupNameLowercase];

const manualSourceUrl = manualSourceUrls[datasource]?.[lookupNameLowercase];
if (manualSourceUrl) {
dep.sourceUrl = manualSourceUrl;
}

if (
Expand Down Expand Up @@ -246,12 +246,18 @@ export function addMetaData(
}

// Clean up any empty urls
const urls = ['homepage', 'sourceUrl', 'changelogUrl', 'dependencyUrl'];
for (const url of urls) {
if (is.string(dep[url]) && validateUrl(dep[url].trim())) {
dep[url] = dep[url].trim();
const urlKeys: (keyof ReleaseResult)[] = [
'homepage',
'sourceUrl',
'changelogUrl',
'dependencyUrl',
];
for (const urlKey of urlKeys) {
const urlVal = dep[urlKey];
if (is.string(urlVal) && validateUrl(urlVal.trim())) {
dep[urlKey] = urlVal.trim() as never;
} else {
delete dep[url];
delete dep[urlKey];
}
}
}
4 changes: 2 additions & 2 deletions lib/manager/gradle/shallow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const versionLikeRegex = regEx('^(?<version>[-.\\[\\](),a-zA-Z0-9+]+)');
// from the beginning of input
export function versionLikeSubstring(input: string): string | null {
const match = input ? versionLikeRegex.exec(input) : null;
return match ? match.groups.version : null;
return match?.groups?.version ?? null;
}

export function isDependencyString(input: string): boolean {
Expand All @@ -40,7 +40,7 @@ export function isDependencyString(input: string): boolean {
tempArtifactId,
tempVersionPart,
];
return (
return !!(
groupId &&
artifactId &&
versionPart &&
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/helm-values/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function matchesHelmValuesDockerHeuristic(
parentKey: string,
data: unknown
): data is HelmDockerImageDependency {
return (
return !!(
parentKeyRe.test(parentKey) &&
data &&
typeof data === 'object' &&
Expand All @@ -36,5 +36,5 @@ export function matchesHelmValuesInlineImage(
parentKey: string,
data: unknown
): data is string {
return parentKeyRe.test(parentKey) && data && typeof data === 'string';
return !!(parentKeyRe.test(parentKey) && data && typeof data === 'string');
}
5 changes: 4 additions & 1 deletion lib/manager/npm/post-update/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export function processHostRules(): HostRulesResult {
for (const hostRule of npmHostRules) {
if (hostRule.resolvedHost) {
let uri = hostRule.matchHost;
uri = validateUrl(uri) ? uri.replace(regEx(/^https?:/), '') : `//${uri}/`;
uri =
is.string(uri) && validateUrl(uri)
? uri.replace(regEx(/^https?:/), '')
: `//${uri}/`;
if (hostRule.token) {
const key = hostRule.authType === 'Basic' ? '_auth' : '_authToken';
additionalNpmrcContent.push(`${uri}:${key}=${hostRule.token}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/pre-commit/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PreCommitConfig, PreCommitDependency } from './types';
export function matchesPrecommitConfigHeuristic(
data: unknown
): data is PreCommitConfig {
return data && typeof data === 'object' && hasKey('repos', data);
return !!(data && typeof data === 'object' && hasKey('repos', data));
}

/**
Expand All @@ -25,7 +25,7 @@ export function matchesPrecommitConfigHeuristic(
export function matchesPrecommitDependencyHeuristic(
data: unknown
): data is PreCommitDependency {
return (
return !!(
data &&
typeof data === 'object' &&
hasKey('repo', data) &&
Expand Down
8 changes: 4 additions & 4 deletions lib/versioning/ruby/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ const increment = (from: string, to: string): string => {

const isStable = (x: string): boolean => regEx(/^[0-9.-/]+$/).test(x);
if (major(from) !== major(adapted)) {
nextVersion = [incrementMajor(maj, min, ptch, pre || []), 0, 0].join('.');
nextVersion = [incrementMajor(maj, min, ptch, pre ?? []), 0, 0].join('.');
} else if (minor(from) !== minor(adapted)) {
nextVersion = [maj, incrementMinor(min, ptch, pre || []), 0].join('.');
nextVersion = [maj, incrementMinor(min, ptch, pre ?? []), 0].join('.');
} else if (patch(from) !== patch(adapted)) {
nextVersion = [maj, min, incrementPatch(ptch, pre || [])].join('.');
nextVersion = [maj, min, incrementPatch(ptch, pre ?? [])].join('.');
} else if (isStable(from) && isStable(adapted)) {
nextVersion = [maj, min, incrementPatch(ptch, pre || [])].join('.');
nextVersion = [maj, min, incrementPatch(ptch, pre ?? [])].join('.');
} else {
nextVersion = [maj, min, ptch].join('.');
}
Expand Down
14 changes: 14 additions & 0 deletions tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
"lib/datasource/**/common.ts",
"lib/datasource/**/types.ts",
"lib/datasource/gitlab-tags/util.ts",
"lib/datasource/metadata.ts",
"lib/datasource/sbt-plugin/util.ts",
"lib/globals.d.ts",
"lib/logger/**/*.ts",
"lib/manager/**/common.ts",
"lib/manager/**/types.ts",
"lib/manager/ansible-galaxy/util.ts",
"lib/manager/argocd/util.ts",
"lib/manager/gitlabci/utils.ts",
"lib/manager/gradle/shallow/utils.ts",
"lib/manager/helm-values/util.ts",
"lib/manager/homebrew/util.ts",
"lib/manager/npm/post-update/rules.ts",
"lib/manager/terragrunt/util.ts",
"lib/platform/**/types.ts",
"lib/platform/github/graphql.ts",
"lib/platform/utils/pr-body.ts",
Expand All @@ -37,17 +47,21 @@
"lib/util/http/legacy.ts",
"lib/util/http/types.ts",
"lib/util/index.ts",
"lib/util/json-writer/code-format.ts",
"lib/util/json-writer/indentation-type.ts",
"lib/util/markdown.ts",
"lib/util/mask.spec.ts",
"lib/util/mask.ts",
"lib/util/modules.ts",
"lib/util/object.ts",
"lib/util/regex.spec.ts",
"lib/util/regex.ts",
"lib/util/sanitize.ts",
"lib/util/split.ts",
"lib/util/url.ts",
"lib/versioning/maven/**/*.ts",
"lib/versioning/ruby/operator.ts",
"lib/versioning/ruby/version.ts",
"lib/versioning/semver-coerced/**/*.ts",
"lib/versioning/semver/**/*.ts",
"lib/versioning/swift/**/*.ts",
Expand Down

0 comments on commit cc76e41

Please sign in to comment.