refactor: More strict null checks (#13387)

* refactor: More strict null checks

* Update lib/manager/terragrunt/providers.ts

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

* Fix coverage

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Sergei Zharinov 2022-01-06 11:53:01 +03:00 committed by GitHub
parent c84748e5be
commit 9743f1ef7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 39 deletions

View file

@ -11,14 +11,19 @@ const githubRelease = regEx(
export function findSourceUrl(release: HelmRelease): RepoSource { export function findSourceUrl(release: HelmRelease): RepoSource {
// it's a github release :) // it's a github release :)
let match = githubRelease.exec(release.urls[0]); const releaseMatch = githubRelease.exec(release.urls[0]);
if (match) { if (releaseMatch) {
return { sourceUrl: match[1] }; return { sourceUrl: releaseMatch[1] };
} }
match = githubUrl.exec(release.home); if (release.home) {
if (chartRepo.test(match?.groups.repo)) { const githubUrlMatch = githubUrl.exec(release.home);
return { sourceUrl: match.groups.url, sourceDirectory: match.groups.path }; if (githubUrlMatch?.groups && chartRepo.test(githubUrlMatch?.groups.repo)) {
return {
sourceUrl: githubUrlMatch.groups.url,
sourceDirectory: githubUrlMatch.groups.path,
};
}
} }
if (!release.sources?.length) { if (!release.sources?.length) {
@ -26,11 +31,11 @@ export function findSourceUrl(release: HelmRelease): RepoSource {
} }
for (const url of release.sources) { for (const url of release.sources) {
match = githubUrl.exec(url); const githubUrlMatch = githubUrl.exec(url);
if (chartRepo.test(match?.groups.repo)) { if (githubUrlMatch?.groups && chartRepo.test(githubUrlMatch?.groups.repo)) {
return { return {
sourceUrl: match.groups.url, sourceUrl: githubUrlMatch.groups.url,
sourceDirectory: match.groups.path, sourceDirectory: githubUrlMatch.groups.path,
}; };
} }
} }

View file

@ -314,7 +314,7 @@ function updateGlobalMapVariables(
newValue: string newValue: string
): string | null { ): string | null {
let variable = variables[`${dependency.group}:${dependency.name}`]; let variable = variables[`${dependency.group}:${dependency.name}`];
if (variable) { if (variable && dependency.version) {
while (variable && variable.split('.').length > 0) { while (variable && variable.split('.').length > 0) {
const regex = variableMapDefinitionFormatMatch( const regex = variableMapDefinitionFormatMatch(
variable, variable,

View file

@ -5,11 +5,9 @@ export interface GradleDependency {
} }
export interface UpdateFunction { export interface UpdateFunction {
( (dependency: GradleDependency, buildGradleContent: string, newValue: string):
dependency: GradleDependency, | string
buildGradleContent: string, | null;
newValue: string
): string;
} }
export interface GradleProject { export interface GradleProject {

View file

@ -8,6 +8,7 @@ const escapedChars = {
[TokenType.EscapedChar]: { [TokenType.EscapedChar]: {
match: escapedCharRegex, match: escapedCharRegex,
value: (x: string): string => value: (x: string): string =>
/* istanbul ignore next */
({ ({
"\\'": "'", "\\'": "'",
'\\"': '"', '\\"': '"',
@ -17,7 +18,7 @@ const escapedChars = {
'\\r': '\r', '\\r': '\r',
'\\t': '\t', '\\t': '\t',
'\\\\': '\\', '\\\\': '\\',
}[x]), }[x] ?? x),
}, },
}; };

View file

@ -8,7 +8,7 @@ export const sourceExtractionRegex = regEx(
/^(?:(?<hostname>(?:[a-zA-Z0-9]+\.+)+[a-zA-Z0-9]+)\/)?(?:(?<namespace>[^/]+)\/)?(?<type>[^/]+)/ /^(?:(?<hostname>(?:[a-zA-Z0-9]+\.+)+[a-zA-Z0-9]+)\/)?(?:(?<namespace>[^/]+)\/)?(?<type>[^/]+)/
); );
function extractBracesContent(content): number { function extractBracesContent(content: string): number {
const stack = []; const stack = [];
let i = 0; let i = 0;
for (i; i < content.length; i += 1) { for (i; i < content.length; i += 1) {
@ -32,12 +32,11 @@ export function extractTerragruntProvider(
const lineNumber = startingLine; const lineNumber = startingLine;
let line: string; let line: string;
const deps: PackageDependency[] = []; const deps: PackageDependency[] = [];
const dep: PackageDependency = { const managerData: Record<string, unknown> = {
managerData: {
moduleName, moduleName,
terragruntDependencyType: TerragruntDependencyTypes.terragrunt, terragruntDependencyType: TerragruntDependencyTypes.terragrunt,
},
}; };
const dep: PackageDependency = { managerData };
const teraformContent = lines const teraformContent = lines
.slice(lineNumber) .slice(lineNumber)
.join('\n') .join('\n')
@ -46,10 +45,10 @@ export function extractTerragruntProvider(
for (let lineNo = 0; lineNo < teraformContent.length; lineNo += 1) { for (let lineNo = 0; lineNo < teraformContent.length; lineNo += 1) {
line = teraformContent[lineNo]; line = teraformContent[lineNo];
const kvMatch = keyValueExtractionRegex.exec(line); const kvGroups = keyValueExtractionRegex.exec(line)?.groups;
if (kvMatch) { if (kvGroups) {
dep.managerData.source = kvMatch.groups.value; managerData.source = kvGroups.value;
dep.managerData.sourceLine = lineNumber + lineNo; managerData.sourceLine = lineNumber + lineNo;
} }
} }
deps.push(dep); deps.push(dep);

View file

@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { regEx } from '../../util/regex'; import { regEx } from '../../util/regex';
export enum TokenType { export enum TokenType {
@ -10,7 +11,10 @@ type Token = {
val: string | number; val: string | number;
}; };
function iterateChars(str: string, cb: (p: string, n: string) => void): void { function iterateChars(
str: string,
cb: (p: string | null, n: string | null) => void
): void {
let prev = null; let prev = null;
let next = null; let next = null;
for (let i = 0; i < str.length; i += 1) { for (let i = 0; i < str.length; i += 1) {
@ -41,7 +45,7 @@ function isTransition(prevChar: string, nextChar: string): boolean {
} }
export function tokenize(versionStr: string): Token[] | null { export function tokenize(versionStr: string): Token[] | null {
let result = []; let result: Token[] | null = [];
let currentVal = ''; let currentVal = '';
function yieldToken(): void { function yieldToken(): void {
@ -142,7 +146,7 @@ function stringTokenCmp(left: string, right: string): number {
function tokenCmp(left: Token | null, right: Token | null): number { function tokenCmp(left: Token | null, right: Token | null): number {
if (left === null) { if (left === null) {
if (right.type === TokenType.String) { if (right?.type === TokenType.String) {
return 1; return 1;
} }
return -1; return -1;
@ -174,8 +178,8 @@ function tokenCmp(left: Token | null, right: Token | null): number {
} }
export function compare(left: string, right: string): number { export function compare(left: string, right: string): number {
const leftTokens = tokenize(left); const leftTokens = tokenize(left) ?? [];
const rightTokens = tokenize(right); const rightTokens = tokenize(right) ?? [];
const length = Math.max(leftTokens.length, rightTokens.length); const length = Math.max(leftTokens.length, rightTokens.length);
for (let idx = 0; idx < length; idx += 1) { for (let idx = 0; idx < length; idx += 1) {
const leftToken = leftTokens[idx] || null; const leftToken = leftTokens[idx] || null;
@ -252,23 +256,30 @@ export function parseMavenBasedRange(input: string): MavenBasedRange | null {
return null; return null;
} }
const match = mavenBasedRangeRegex.exec(input); const matchGroups = mavenBasedRangeRegex.exec(input)?.groups;
if (match) { if (matchGroups) {
const { leftBoundStr, separator, rightBoundStr } = match.groups; const { leftBoundStr, separator, rightBoundStr } = matchGroups;
let { leftVal, rightVal } = match.groups; let leftVal: string | null = matchGroups.leftVal;
let rightVal: string | null = matchGroups.rightVal;
if (!leftVal) { if (!leftVal) {
leftVal = null; leftVal = null;
} }
if (!rightVal) { if (!rightVal) {
rightVal = null; rightVal = null;
} }
const isVersionLeft = isVersion(leftVal); const isVersionLeft = is.string(leftVal) && isVersion(leftVal);
const isVersionRight = isVersion(rightVal); const isVersionRight = is.string(rightVal) && isVersion(rightVal);
if ( if (
(leftVal === null || isVersionLeft) && (leftVal === null || isVersionLeft) &&
(rightVal === null || isVersionRight) (rightVal === null || isVersionRight)
) { ) {
if (isVersionLeft && isVersionRight && compare(leftVal, rightVal) === 1) { if (
isVersionLeft &&
isVersionRight &&
leftVal &&
rightVal &&
compare(leftVal, rightVal) === 1
) {
return null; return null;
} }
const leftBound = const leftBound =

View file

@ -15,6 +15,7 @@
"lib/datasource/**/common.ts", "lib/datasource/**/common.ts",
"lib/datasource/**/types.ts", "lib/datasource/**/types.ts",
"lib/datasource/gitlab-tags/util.ts", "lib/datasource/gitlab-tags/util.ts",
"lib/datasource/helm/common.ts",
"lib/datasource/metadata.ts", "lib/datasource/metadata.ts",
"lib/datasource/sbt-plugin/util.ts", "lib/datasource/sbt-plugin/util.ts",
"lib/globals.d.ts", "lib/globals.d.ts",
@ -24,14 +25,20 @@
"lib/manager/ansible-galaxy/util.ts", "lib/manager/ansible-galaxy/util.ts",
"lib/manager/argocd/util.ts", "lib/manager/argocd/util.ts",
"lib/manager/gitlabci/utils.ts", "lib/manager/gitlabci/utils.ts",
"lib/manager/gradle/deep/build-gradle.ts",
"lib/manager/gradle/shallow/tokenizer.ts",
"lib/manager/gradle/shallow/utils.ts", "lib/manager/gradle/shallow/utils.ts",
"lib/manager/helm-values/util.ts", "lib/manager/helm-values/util.ts",
"lib/manager/homebrew/util.ts", "lib/manager/homebrew/util.ts",
"lib/manager/npm/extract/type.ts",
"lib/manager/npm/post-update/rules.ts", "lib/manager/npm/post-update/rules.ts",
"lib/manager/pre-commit/parsing.ts",
"lib/manager/terragrunt/providers.ts",
"lib/manager/terragrunt/util.ts", "lib/manager/terragrunt/util.ts",
"lib/platform/**/types.ts", "lib/platform/**/types.ts",
"lib/platform/github/graphql.ts", "lib/platform/github/graphql.ts",
"lib/platform/utils/pr-body.ts", "lib/platform/utils/pr-body.ts",
"lib/platform/utils/read-only-issue-body.ts",
"lib/proxy.ts", "lib/proxy.ts",
"lib/types/**/*.ts", "lib/types/**/*.ts",
"lib/util/cache/**/*.ts", "lib/util/cache/**/*.ts",
@ -48,7 +55,9 @@
"lib/util/http/types.ts", "lib/util/http/types.ts",
"lib/util/index.ts", "lib/util/index.ts",
"lib/util/json-writer/code-format.ts", "lib/util/json-writer/code-format.ts",
"lib/util/json-writer/editor-config.ts",
"lib/util/json-writer/indentation-type.ts", "lib/util/json-writer/indentation-type.ts",
"lib/util/json-writer/json-writer.ts",
"lib/util/markdown.ts", "lib/util/markdown.ts",
"lib/util/mask.spec.ts", "lib/util/mask.spec.ts",
"lib/util/mask.ts", "lib/util/mask.ts",
@ -59,6 +68,7 @@
"lib/util/sanitize.ts", "lib/util/sanitize.ts",
"lib/util/split.ts", "lib/util/split.ts",
"lib/util/url.ts", "lib/util/url.ts",
"lib/versioning/gradle/compare.ts",
"lib/versioning/maven/**/*.ts", "lib/versioning/maven/**/*.ts",
"lib/versioning/ruby/**/*.ts", "lib/versioning/ruby/**/*.ts",
"lib/versioning/semver-coerced/**/*.ts", "lib/versioning/semver-coerced/**/*.ts",