"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1259.0/src/lib/formatters/iac-output/text/issues-list/issue.ts" (30 Nov 2023, 3011 Bytes) of package /linux/misc/snyk-cli-1.1259.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as capitalize from 'lodash.capitalize';
    2 import chalk from 'chalk';
    3 import { EOL } from 'os';
    4 import * as wrapAnsi from 'wrap-ansi';
    5 import { iacRemediationTypes } from '../../../../iac/constants';
    6 import { printPath } from '../../../remediation-based-format-issues';
    7 import { colors, contentPadding, maxLineWidth } from '../utils';
    8 import { FormattedOutputResult, Issue } from '../types';
    9 import { Options } from './types';
   10 
   11 export function formatIssue(
   12   result: FormattedOutputResult,
   13   options?: Options,
   14 ): string {
   15   const titleOutput = formatTitle(result.issue);
   16 
   17   const propertiesOutput = formatProperties(result, options);
   18   return (
   19     contentPadding +
   20     titleOutput +
   21     EOL +
   22     contentPadding +
   23     propertiesOutput.join(EOL + contentPadding)
   24   );
   25 }
   26 
   27 function formatTitle(issue: Issue): string {
   28   const severity = issue.severity;
   29   const titleOutput = colors.severities[severity](
   30     `[${capitalize([issue.severity])}] ${chalk.bold(issue.title)}`,
   31   );
   32 
   33   return titleOutput;
   34 }
   35 
   36 function formatInfo(issue: Issue): string | undefined {
   37   const issueDesc = issue.issue;
   38   const issueImpact = issue.impact;
   39 
   40   if (!issueDesc) {
   41     return issueImpact;
   42   }
   43 
   44   if (!issueImpact) {
   45     return issueDesc;
   46   }
   47 
   48   return `${issueDesc}${!issueDesc.endsWith('.') ? '.' : ''} ${issueImpact}`;
   49 }
   50 
   51 function formatProperties(
   52   result: FormattedOutputResult,
   53   options?: Options,
   54 ): string[] {
   55   const properties = [
   56     ['Info', formatInfo(result.issue)],
   57     [
   58       'Rule',
   59       result.issue.isGeneratedByCustomRule
   60         ? `custom rule ${result.issue.id}`
   61         : chalk.underline(result.issue.documentation || ''),
   62     ],
   63     ['Path', printPath(result.issue.cloudConfigPath, 0)],
   64     [
   65       'File',
   66       `${result.targetFile}${
   67         options?.shouldShowLineNumbers &&
   68         isValidLineNumber(result.issue.lineNumber)
   69           ? `:${result.issue.lineNumber}`
   70           : ''
   71       }`,
   72     ],
   73     ['Resolve', getRemediationText(result)],
   74   ];
   75 
   76   const propKeyColWidth = Math.max(...properties.map(([key]) => key!.length));
   77   const propValColWidth =
   78     maxLineWidth - contentPadding.length - propKeyColWidth - 2;
   79   const indentLength = propKeyColWidth + 2;
   80 
   81   return properties
   82     .filter(([, val]) => !!val)
   83     .map(([key, value]) => [
   84       key,
   85       wrapAnsi(value, propValColWidth, {
   86         hard: true,
   87       }).replace(/\r?\n|\r/g, EOL + contentPadding + ' '.repeat(indentLength)),
   88     ])
   89     .map(
   90       ([key, value]) =>
   91         `${key}: ${' '.repeat(propKeyColWidth - key.length)}${value}`,
   92     );
   93 }
   94 
   95 function isValidLineNumber(lineNumber: number | undefined): boolean {
   96   return (
   97     typeof lineNumber === 'number' && lineNumber! > 0 && lineNumber! % 1 === 0
   98   );
   99 }
  100 
  101 function getRemediationText(result: FormattedOutputResult): string | undefined {
  102   const remediationKey = iacRemediationTypes?.[result.projectType];
  103 
  104   if (result.issue.remediation) {
  105     return result.issue.remediation[remediationKey] ?? result.issue.remediation;
  106   }
  107 
  108   return result.issue.resolve;
  109 }