From 3a2c041ce18099db0dc1c5c7e66e00fe810f888a Mon Sep 17 00:00:00 2001
From: IKEDA Sho <suicaicoca@gmail.com>
Date: Sun, 18 Aug 2019 04:21:14 +0900
Subject: [PATCH] chore(maven): add more type annotations (#4301)

---
 lib/datasource/maven/index.ts   | 14 +++++++-------
 lib/datasource/maven/util.ts    |  2 +-
 lib/manager/maven/extract.ts    | 19 ++++++++++++-------
 lib/manager/maven/update.ts     |  7 +++++--
 lib/versioning/maven/compare.ts |  7 +++++--
 lib/versioning/maven/index.ts   | 12 ++++++------
 6 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/lib/datasource/maven/index.ts b/lib/datasource/maven/index.ts
index 455546f922..7b57e4870c 100644
--- a/lib/datasource/maven/index.ts
+++ b/lib/datasource/maven/index.ts
@@ -11,7 +11,7 @@ import { PkgReleaseConfig, ReleaseResult } from '../common';
 export async function getPkgReleases({
   lookupName,
   registryUrls,
-}: PkgReleaseConfig): Promise<ReleaseResult> {
+}: PkgReleaseConfig): Promise<ReleaseResult | null> {
   const versions: string[] = [];
   const dependency = getDependencyParts(lookupName);
   if (!is.nonEmptyArray(registryUrls)) {
@@ -68,7 +68,7 @@ export async function getPkgReleases({
   };
 }
 
-function getDependencyParts(lookupName) {
+function getDependencyParts(lookupName: string): MavenDependency {
   const [group, name] = lookupName.split(':');
   const dependencyUrl = `${group.replace(/\./g, '/')}/${name}`;
   return {
@@ -90,7 +90,7 @@ async function downloadMavenXml(
   dependency: MavenDependency,
   repoUrl: string,
   dependencyFilePath: string
-) {
+): Promise<XmlDocument | null> {
   const pkgUrl = new url.URL(
     `${dependency.dependencyUrl}/${dependencyFilePath}`,
     repoUrl
@@ -128,14 +128,14 @@ async function downloadMavenXml(
   }
 }
 
-function extractVersions(metadata: XmlDocument) {
+function extractVersions(metadata: XmlDocument): string[] {
   const versions = metadata.descendantWithPath('versioning.versions');
   const elements = versions && versions.childrenNamed('version');
   if (!elements) return [];
   return elements.map(el => el.val);
 }
 
-async function downloadFileProtocol(pkgUrl: url.URL) {
+async function downloadFileProtocol(pkgUrl: url.URL): Promise<string | null> {
   const pkgPath = pkgUrl.toString().replace('file://', '');
   if (!(await fs.exists(pkgPath))) {
     return null;
@@ -143,7 +143,7 @@ async function downloadFileProtocol(pkgUrl: url.URL) {
   return fs.readFile(pkgPath, 'utf8');
 }
 
-function getLatestVersion(versions: string[]) {
+function getLatestVersion(versions: string[]): string | null {
   if (versions.length === 0) return null;
   return versions.reduce((latestVersion, version) =>
     compare(version, latestVersion) === 1 ? version : latestVersion
@@ -154,7 +154,7 @@ async function getDependencyInfo(
   dependency: MavenDependency,
   repoUrl: string,
   version: string
-) {
+): Promise<Partial<ReleaseResult>> {
   const result: Partial<ReleaseResult> = {};
   const path = `${version}/${dependency.name}-${version}.pom`;
 
diff --git a/lib/datasource/maven/util.ts b/lib/datasource/maven/util.ts
index 9b1b1b8e76..fd3befc442 100644
--- a/lib/datasource/maven/util.ts
+++ b/lib/datasource/maven/util.ts
@@ -35,7 +35,7 @@ function isConnectionError(err: { code: string }) {
 export async function downloadHttpProtocol(
   pkgUrl: url.URL | string,
   hostType = 'maven'
-) {
+): Promise<string | null> {
   let raw: { body: string };
   try {
     raw = await got(pkgUrl, { hostType });
diff --git a/lib/manager/maven/extract.ts b/lib/manager/maven/extract.ts
index bfd5057152..9466289c38 100644
--- a/lib/manager/maven/extract.ts
+++ b/lib/manager/maven/extract.ts
@@ -6,7 +6,7 @@ import { ExtractConfig, PackageFile, PackageDependency } from '../common';
 
 export const DEFAULT_MAVEN_REPO = 'https://repo.maven.apache.org/maven2';
 
-export function parsePom(raw: string) {
+export function parsePom(raw: string): XmlDocument | null {
   let project: XmlDocument;
   try {
     project = new XmlDocument(raw);
@@ -29,7 +29,7 @@ interface MavenProp {
   packageFile: string;
 }
 
-function depFromNode(node: XmlElement): PackageDependency {
+function depFromNode(node: XmlElement): PackageDependency | null {
   if (!node.valueWithPath) return null;
   const groupId = node.valueWithPath('groupId');
   const artifactId = node.valueWithPath('artifactId');
@@ -53,7 +53,7 @@ function depFromNode(node: XmlElement): PackageDependency {
 
 function deepExtract(
   node: XmlElement,
-  result = [],
+  result: PackageDependency[] = [],
   isRoot = true
 ): PackageDependency[] {
   const dep = depFromNode(node as XmlElement);
@@ -71,7 +71,7 @@ function deepExtract(
 function applyProps(
   dep: PackageDependency<Record<string, any>>,
   props: MavenProp
-) {
+): PackageDependency<Record<string, any>> {
   const replaceAll = (str: string) =>
     str.replace(/\${.*?}/g, substr => {
       const propKey = substr.slice(2, -1).trim();
@@ -123,7 +123,7 @@ function applyProps(
   return result;
 }
 
-function resolveParentFile(packageFile: string, parentPath: string) {
+function resolveParentFile(packageFile: string, parentPath: string): string {
   let parentFile = 'pom.xml';
   let parentDir = parentPath;
   const parentBasename = basename(parentPath);
@@ -135,7 +135,10 @@ function resolveParentFile(packageFile: string, parentPath: string) {
   return normalize(join(dir, parentDir, parentFile));
 }
 
-export function extractPackage(rawContent: string, packageFile: string = null) {
+export function extractPackage(
+  rawContent: string,
+  packageFile: string | null = null
+): PackageFile<Record<string, any>> | null {
   if (!rawContent) return null;
 
   const project = parsePom(rawContent);
@@ -237,7 +240,9 @@ export function resolveProps(packages: PackageFile[]): PackageFile[] {
   }));
 }
 
-function cleanResult(packageFiles) {
+function cleanResult(
+  packageFiles: PackageFile<Record<string, any>>[]
+): PackageFile<Record<string, any>>[] {
   packageFiles.forEach(packageFile => {
     delete packageFile.mavenProps; // eslint-disable-line no-param-reassign
     packageFile.deps.forEach(dep => {
diff --git a/lib/manager/maven/update.ts b/lib/manager/maven/update.ts
index 1bfc0fa088..c25dab1eb6 100644
--- a/lib/manager/maven/update.ts
+++ b/lib/manager/maven/update.ts
@@ -5,7 +5,7 @@ export function updateAtPosition(
   fileContent: string,
   upgrade: Upgrade,
   endingAnchor = '"'
-) {
+): string | null {
   const { depName, currentValue, newValue, fileReplacePosition } = upgrade;
   const leftPart = fileContent.slice(0, fileReplacePosition);
   const rightPart = fileContent.slice(fileReplacePosition);
@@ -24,7 +24,10 @@ export function updateAtPosition(
   return null;
 }
 
-export function updateDependency(fileContent: string, upgrade: Upgrade) {
+export function updateDependency(
+  fileContent: string,
+  upgrade: Upgrade
+): string | null {
   const offset = fileContent.indexOf('<');
   const spaces = fileContent.slice(0, offset);
   const restContent = fileContent.slice(offset);
diff --git a/lib/versioning/maven/compare.ts b/lib/versioning/maven/compare.ts
index 621803733c..4d343dd627 100644
--- a/lib/versioning/maven/compare.ts
+++ b/lib/versioning/maven/compare.ts
@@ -370,7 +370,7 @@ function parseRange(rangeStr: string) {
   );
 }
 
-function rangeToStr(fullRange: Range[]) {
+function rangeToStr(fullRange: Range[]): string | null {
   if (fullRange === null) return null;
 
   const valToStr = (val: string) => (val === null ? '' : val);
@@ -398,7 +398,10 @@ function rangeToStr(fullRange: Range[]) {
   return intervals.join(',');
 }
 
-function autoExtendMavenRange(currentRepresentation: string, newValue: string) {
+function autoExtendMavenRange(
+  currentRepresentation: string,
+  newValue: string
+): string | null {
   const range = parseRange(currentRepresentation);
   if (!range) return currentRepresentation;
   const isPoint = (vals: Range[]) => {
diff --git a/lib/versioning/maven/index.ts b/lib/versioning/maven/index.ts
index 600336074c..968b1f01a4 100644
--- a/lib/versioning/maven/index.ts
+++ b/lib/versioning/maven/index.ts
@@ -45,7 +45,7 @@ function matches(a: string, b: string) {
   }, false);
 }
 
-const getMajor = (version: string) => {
+const getMajor = (version: string): number | null => {
   if (isVersion(version)) {
     const tokens = tokenize(version);
     const majorToken = tokens[0];
@@ -54,7 +54,7 @@ const getMajor = (version: string) => {
   return null;
 };
 
-const getMinor = (version: string) => {
+const getMinor = (version: string): number | null => {
   if (isVersion(version)) {
     const tokens = tokenize(version);
     const minorToken = tokens[1];
@@ -66,7 +66,7 @@ const getMinor = (version: string) => {
   return null;
 };
 
-const getPatch = (version: string) => {
+const getPatch = (version: string): number | null => {
   if (isVersion(version)) {
     const tokens = tokenize(version);
     const minorToken = tokens[1];
@@ -85,7 +85,7 @@ const getPatch = (version: string) => {
 
 const isGreaterThan = (a: string, b: string) => compare(a, b) === 1;
 
-const isStable = (version: string) => {
+const isStable = (version: string): boolean | null => {
   if (isVersion(version)) {
     const tokens = tokenize(version);
     const qualToken = tokens.find(token => token.type === TYPE_QUALIFIER);
@@ -103,7 +103,7 @@ const isStable = (version: string) => {
   return null;
 };
 
-const maxSatisfyingVersion = (versions: string[], range: string) => {
+const maxSatisfyingVersion = (versions: string[], range: string): string => {
   // istanbul ignore next
   return versions.reduce((result, version) => {
     if (matches(version, range)) {
@@ -119,7 +119,7 @@ function getNewValue(
   rangeStrategy: RangeStrategy,
   _fromVersion: string,
   toVersion: string
-) {
+): string | null {
   if (isVersion(currentValue) || rangeStrategy === 'pin') {
     return toVersion;
   }
-- 
GitLab