Skip to content

Commit

Permalink
fix: rename reachable paths field
Browse files Browse the repository at this point in the history
  • Loading branch information
muscar committed Jul 1, 2020
1 parent fef488b commit c2f2443
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
12 changes: 12 additions & 0 deletions src/cli/commands/constants.ts
@@ -0,0 +1,12 @@
// Separator used while displaying various paths (e.g. package paths, call
// paths) to the user
export const PATH_SEPARATOR = ' > ';

// String used to signify hidden path elements e.g. for abbreviated paths
export const PATH_HIDDEN_ELEMENTS = '...';

// Number of function names to show in the beginning of an abbreviated call path
export const CALL_PATH_LEADING_ELEMENTS = 2;

// Number of function names to show in the end of an abbreviated call path
export const CALL_PATH_TRAILING_ELEMENTS = 2;
6 changes: 4 additions & 2 deletions src/cli/commands/protect/prompts.ts
@@ -1,3 +1,5 @@
import { PATH_SEPARATOR } from '../constants';

export {
getUpdatePrompts,
getPatchPrompts,
Expand Down Expand Up @@ -648,8 +650,8 @@ function generatePrompt(
);
messageIntro += '\n Description: ' + vuln.title;
fromText =
from !== vuln.from.slice(1).join(' > ')
? ' From: ' + vuln.from.slice(1).join(' > ')
from !== vuln.from.slice(1).join(PATH_SEPARATOR)
? ' From: ' + vuln.from.slice(1).join(PATH_SEPARATOR)
: '';
}

Expand Down
20 changes: 10 additions & 10 deletions src/cli/commands/test/formatters/format-reachability.ts
Expand Up @@ -7,12 +7,12 @@ import {
REACHABILITY,
} from '../../../../lib/snyk-test/legacy';
import { SampleReachablePaths } from './types';

// Number of function names to show in the beginning of an abbreviated code path
const LEADING_PATH_ELEMENTS = 2;

// Number of function names to show in the end of an abbreviated code path
const TRAILING_PATH_ELEMENTS = 2;
import {
CALL_PATH_LEADING_ELEMENTS,
PATH_SEPARATOR,
CALL_PATH_TRAILING_ELEMENTS,
PATH_HIDDEN_ELEMENTS,
} from '../../constants';

const reachabilityLevels: {
[key in REACHABILITY]: { color: Function; text: string };
Expand Down Expand Up @@ -99,9 +99,9 @@ export function formatReachablePaths(
}

export function formatReachablePath(path: CallPath): string {
const head = path.slice(0, LEADING_PATH_ELEMENTS).join('>');
const head = path.slice(0, CALL_PATH_LEADING_ELEMENTS).join(PATH_SEPARATOR);
const tail = path
.slice(path.length - TRAILING_PATH_ELEMENTS, path.length)
.join('>');
return `${head} > ... > ${tail}`;
.slice(path.length - CALL_PATH_TRAILING_ELEMENTS, path.length)
.join(PATH_SEPARATOR);
return `${head}${PATH_SEPARATOR}${PATH_HIDDEN_ELEMENTS}${PATH_SEPARATOR}${tail}`;
}
9 changes: 6 additions & 3 deletions src/cli/commands/test/formatters/legacy-format-issue.ts
Expand Up @@ -16,6 +16,7 @@ import {
} from '../../../../lib/snyk-test/legacy';
import { formatLegalInstructions } from './legal-license-instructions';
import { getReachabilityText } from './format-reachability';
import { PATH_SEPARATOR } from '../../constants';

export function formatIssues(
vuln: GroupedVuln,
Expand Down Expand Up @@ -136,7 +137,7 @@ function createTruncatedVulnsPathsText(
const fromWithoutBaseProject = i.slice(1);
// If more than one From path
if (fromWithoutBaseProject.length) {
return i.slice(1).join(' > ');
return i.slice(1).join(PATH_SEPARATOR);
}
// Else issue is in the core package
return i;
Expand Down Expand Up @@ -221,7 +222,7 @@ function createRemediationText(
// Example command: snyk test express@3
const selfUpgradeInfo =
v.upgradePath.length > 0
? ` (triggers upgrades to ${v.upgradePath.join(' > ')})`
? ` (triggers upgrades to ${v.upgradePath.join(PATH_SEPARATOR)})`
: '';
const testedPackageName = snykModule(v.upgradePath[0] as string);
return (
Expand All @@ -230,7 +231,9 @@ function createRemediationText(
);
}
if (shouldUpgradeDirectDep) {
const formattedUpgradePath = v.upgradePath.slice(1).join(' > ');
const formattedUpgradePath = v.upgradePath
.slice(1)
.join(PATH_SEPARATOR);
const upgradeTextInfo = v.upgradePath.length
? ` (triggers upgrades to ${formattedUpgradePath})`
: '';
Expand Down
Expand Up @@ -25,6 +25,7 @@ import {
SampleReachablePaths,
UpgradesByAffectedPackage,
} from './types';
import { PATH_SEPARATOR } from '../../constants';

// How many reachable paths to show in the output
const MAX_REACHABLE_PATHS = 2;
Expand All @@ -45,7 +46,7 @@ export function formatIssuesWithRemediation(
for (const vuln of vulns) {
const allReachablePaths: SampleReachablePaths = { pathCount: 0, paths: [] };
for (const issue of vuln.list) {
const issueReachablePaths = issue.reachablePaths?.reachablePaths || [];
const issueReachablePaths = issue.reachablePaths?.paths || [];
for (const functionReachablePaths of issueReachablePaths) {
allReachablePaths.paths = allReachablePaths.paths.concat(
functionReachablePaths.callPaths,
Expand Down Expand Up @@ -415,7 +416,7 @@ function constructUnfixableText(
}

export function printPath(path: string[]) {
return path.slice(1).join(' > ');
return path.slice(1).join(PATH_SEPARATOR);
}

export function formatIssue(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snyk-test/legacy.ts
Expand Up @@ -95,7 +95,7 @@ export interface ReachableFunctionPaths {

export interface ReachablePaths {
pathCount: number;
reachablePaths: ReachableFunctionPaths[];
paths: ReachableFunctionPaths[];
}

interface AnnotatedIssue extends IssueData {
Expand Down

0 comments on commit c2f2443

Please sign in to comment.