Skip to content

Commit

Permalink
Fix lint errors from unicorn/no-array-reduce rule
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Sep 23, 2024
1 parent f92ada6 commit 6770844
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
19 changes: 11 additions & 8 deletions node-src/lib/compareBaseline.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Context } from '../types';
import { getDependencies } from './getDependencies';

// Retrieve a set of values which is in either set, but not both.
const xor = <T>(left: Set<T>, right: Set<T>) =>
[...right.values()].reduce((acc, value) => {
if (acc.has(value)) acc.delete(value);
else acc.add(value);
return acc;
}, new Set(left));

interface BaselineConfig {
ref: string;
rootPath: string;
Expand All @@ -31,3 +23,14 @@ export const compareBaseline = async (
}
return changedDependencyNames;
};

// Retrieve a set of values which is in either set, but not both.
function xor<T>(left: Set<T>, right: Set<T>) {
const result = new Set(left);

for (const value of right.values()) {
result.has(value) ? result.delete(value) : result.add(value);
}

return result;
}
18 changes: 9 additions & 9 deletions node-src/lib/getDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import { buildDepTreeFromFiles, PkgTree } from 'snyk-nodejs-lockfile-parser';

import { Context } from '../types';

const flattenDependencyTree = (
tree: PkgTree['dependencies'],
results = new Set<string>()
): Set<string> =>
Object.values(tree).reduce((acc, dep) => {
acc.add(`${dep.name}@@${dep.version}`);
return flattenDependencyTree(dep.dependencies || {}, acc);
}, results);

export const getDependencies = async (
ctx: Context,
{
Expand Down Expand Up @@ -41,3 +32,12 @@ export const getDependencies = async (
throw err;
}
};

function flattenDependencyTree(tree: PkgTree['dependencies'], results = new Set<string>()) {
for (const dep of Object.values(tree)) {
results.add(`${dep.name}@@${dep.version}`);
flattenDependencyTree(dep.dependencies || {}, results);
}

return results;
}
12 changes: 7 additions & 5 deletions node-src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ export async function uploadBuild(
const targets: (TargetInfo & FileDesc)[] = [];
let zipTarget: TargetInfo | undefined;

const batches = files.reduce<(typeof files)[]>((acc, file, fileIndex) => {
const batches: FileDesc[][] = [];
for (const [fileIndex, file] of files.entries()) {
const batchIndex = Math.floor(fileIndex / MAX_FILES_PER_REQUEST);
if (!acc[batchIndex]) acc[batchIndex] = [];
acc[batchIndex].push(file);
return acc;
}, []);
if (!batches[batchIndex]) {
batches[batchIndex] = [];
}
batches[batchIndex].push(file);
}

// The uploadBuild mutation has to run in batches to avoid hitting request/response payload limits
// or running out of memory. These run sequentially to avoid too many concurrent requests.
Expand Down
13 changes: 7 additions & 6 deletions node-src/tasks/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ interface AnnounceBuildMutationResult {
}

export const setEnvironment = async (ctx: Context) => {
if (!ctx.environment) {
ctx.environment = {};
}

// We send up all environment variables provided by these complicated systems.
// We don't want to send up *all* environment vars as they could include sensitive information
// about the user's build environment
ctx.environment = Object.entries(process.env).reduce((acc, [key, value]) => {
if (ctx.env.ENVIRONMENT_WHITELIST.some((regex) => key.match(regex))) {
acc[key] = value;
}
return acc;
}, {});
for (const [key, value] of Object.entries(process.env)) {
ctx.environment[key] = value;
}

ctx.log.debug(`Got environment:\n${JSON.stringify(ctx.environment, null, 2)}`);
};
Expand Down
32 changes: 22 additions & 10 deletions node-src/ui/messages/info/tracedAffectedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,41 @@ export default (

const printModules = (moduleName, indent = '') => {
if (!expanded) return '';

const { modules } = modulesByName[moduleName] || {};
return modules
? modules.reduce((acc, mod) => chalk`${acc}\n${indent} ⎸ {dim ${normalize(mod.name)}}`, '')
: '';
if (!modules) return '';

let result = '';
for (const module of modules) {
result += chalk`\n${indent} ⎸ {dim ${normalize(module.name)}}`;
}
return result;
};

const seen = new Set();
const traces = [...ctx.turboSnap.tracedPaths].map((p) => {
const parts = p.split('\n');
const traceParts = parts.reduce((acc, part, index) => {
if (index === 0) return chalk`— ${printPath(part)} {cyan [changed]}${printModules(part)}`;
const indent = ' '.repeat(index);

let results = '';
for (const [index, part] of parts.entries()) {
if (index === 0) {
results += chalk`— ${printPath(part)} {cyan [changed]}${printModules(part)}`;
}

let note = '';
if (index === parts.length - 1) {
if (seen.has(part)) note = chalk` {yellow [duplicate]}`;
else seen.add(part);
}
return chalk`${

const indent = ' '.repeat(index);
results += chalk`${
expanded ? `File Path: ${part}\n\nBase Directory: ${basedir}\n\n` : ''
}${acc}\n${indent}${printPath(part)}${note}${printModules(part, indent)}`;
}, '');
}\n${indent}${printPath(part)}${note}${printModules(part, indent)}`;
}

return traceParts + chalk`\n${' '.repeat(parts.length)}∟ {cyan [story index]}`;
results += chalk`\n${' '.repeat(parts.length)}∟ {cyan [story index]}`;
return results;
});

const note = chalk`\n\nSet {bold ${flag}} to {bold 'expanded'} to reveal underlying modules.`;
Expand Down

0 comments on commit 6770844

Please sign in to comment.