Skip to content

Commit e6410b5

Browse files
authored
chore: code cleanup CN-276 (#693)
* fix: .test is faster than .match regex * chore: we don't need to keep looping * chore: unit tests * chore: improve readability / consistency
1 parent 6ec2083 commit e6410b5

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

lib/analyzer/image-inspector.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ async function pullWithDockerBinary(
4848
password: string | undefined,
4949
platform: string | undefined,
5050
): Promise<boolean> {
51-
let pullAndSaveSuccessful = false;
5251
try {
5352
if (username || password) {
5453
debug(
@@ -57,13 +56,12 @@ async function pullWithDockerBinary(
5756
}
5857
await docker.pullCli(targetImage, { platform });
5958
await docker.save(targetImage, saveLocation);
60-
return (pullAndSaveSuccessful = true);
59+
return true;
6160
} catch (err) {
6261
debug(`couldn't pull ${targetImage} using docker binary: ${err.message}`);
63-
6462
handleDockerPullError(err.stderr, platform);
6563

66-
return pullAndSaveSuccessful;
64+
return false;
6765
}
6866
}
6967

@@ -81,10 +79,9 @@ function handleDockerPullError(err: string, platform?: string) {
8179
"manifest unknown",
8280
];
8381
if (unknownManifestConditions.some((value) => err.includes(value))) {
84-
if (platform) {
85-
throw new Error(`The image does not exist for ${platform}`);
86-
}
87-
throw new Error(`The image does not exist for the current platform`);
82+
throw new Error(
83+
`The image does not exist for ${platform ?? "the current platform"}`,
84+
);
8885
}
8986

9087
if (err.includes("invalid reference format")) {

lib/inputs/file-pattern/static.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ import { ExtractAction, ExtractedLayers } from "../../extractor/types";
55
import { streamToString } from "../../stream-utils";
66
import { ManifestFile } from "../../types";
77

8+
/**
9+
* Return false if any exclusion pattern matches,
10+
* return true if any inclusion pattern matches
11+
*/
812
function generatePathMatcher(
913
globsInclude: string[],
1014
globsExclude: string[],
1115
): (filePath: string) => boolean {
1216
return (filePath: string): boolean => {
13-
let exclude = false;
14-
for (const g of globsExclude) {
15-
if (!exclude && minimatch(filePath, g)) {
16-
exclude = true;
17-
}
18-
}
19-
if (!exclude) {
20-
for (const g of globsInclude) {
21-
if (minimatch(filePath, g)) {
22-
return true;
23-
}
24-
}
17+
if (globsExclude.some((glob) => minimatch(filePath, glob))) {
18+
return false;
2519
}
26-
return false;
20+
21+
return globsInclude.some((glob) => minimatch(filePath, glob));
2722
};
2823
}
2924

lib/python-parser/requirements-parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ export function getRequirements(fileContent: string): PythonRequirement[] {
1515

1616
function parseLine(line: string): PythonRequirement | null {
1717
line = line.trim();
18-
if (line.length === 0) {
18+
// there's no point in calling the regex if the line is a comment
19+
if (line.length === 0 || line.startsWith("#")) {
1920
return null;
2021
}
2122
const parsedLine = VERSION_PARSE_REGEX.exec(line);
2223
if (!parsedLine?.groups) {
2324
return null;
2425
}
2526
const { name, extras, specifier, version } = parsedLine.groups;
26-
const correctedSpecifier = specifierValidRange(specifier, version);
2727
return {
2828
name: name.toLowerCase(),
29-
specifier: correctedSpecifier,
29+
specifier: specifierValidRange(specifier, version),
3030
version,
3131
extras: parseExtraNames(extras),
3232
} as PythonRequirement;

0 commit comments

Comments
 (0)