All files lintAssemblyInput.ts

82.92% Statements 34/41
75.75% Branches 25/33
100% Functions 3/3
82.92% Lines 34/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91            62x   62x 51x               62x 5x 5x 5x 5x               62x           46x 46x   46x 16x 16x 16x 16x       30x 30x     46x     46x 46x 46x 36x   10x 10x               46x 46x 1x     45x 45x 45x           11x   34x           45x    
import type { AssemblyInstructionsInput, StepsInput } from './alphalib/types/template.ts'
import type { ResponseTemplateContent, TemplateContent } from './apiTypes.ts'
 
import { getIndentation } from './alphalib/stepParsing.ts'
import { mergeTemplateContent } from './alphalib/templateMerge.ts'
 
const DEFAULT_INDENT = '  '
 
const isRecord = (value: unknown): value is Record<string, unknown> =>
  typeof value === 'object' && value !== null && !Array.isArray(value)
 
export interface BuildLintInputResult {
  lintContent: string
  wasStepsOnly: boolean
  indent: string
}
 
export const unwrapStepsOnly = (content: string, indent: string): string => {
  try {
    const parsed = JSON.parse(content)
    Eif (isRecord(parsed) && 'steps' in parsed) {
      return JSON.stringify((parsed as { steps?: unknown }).steps ?? {}, null, indent)
    }
  } catch (_err) {
    return content
  }
  return content
}
 
export const buildLintInput = (
  assemblyInstructions?: AssemblyInstructionsInput | StepsInput | string,
  template?: TemplateContent | ResponseTemplateContent,
): BuildLintInputResult => {
  let inputString: string | undefined
  let parsedInput: unknown | undefined
  let parseFailed = false
  let indent = DEFAULT_INDENT
 
  if (typeof assemblyInstructions === 'string') {
    inputString = assemblyInstructions
    indent = getIndentation(assemblyInstructions)
    try {
      parsedInput = JSON.parse(assemblyInstructions)
    } catch (_err) {
      parseFailed = true
    }
  } else Eif (assemblyInstructions != null) {
    parsedInput = assemblyInstructions
  }
 
  let wasStepsOnly = false
  let instructions: AssemblyInstructionsInput | undefined
 
  Eif (parsedInput !== undefined) {
    if (isRecord(parsedInput)) {
      if ('steps' in parsedInput) {
        instructions = parsedInput as AssemblyInstructionsInput
      } else {
        instructions = { steps: parsedInput as StepsInput }
        wasStepsOnly = true
      }
    } else E{
      instructions = { steps: parsedInput as StepsInput }
      wasStepsOnly = true
    }
  }
 
  const shouldMergeTemplate = template != null && !parseFailed
  if (shouldMergeTemplate) {
    instructions = mergeTemplateContent(template, instructions)
  }
 
  let lintContent = ''
  if (instructions != null) {
    if (
      typeof assemblyInstructions === 'string' &&
      !wasStepsOnly &&
      !parseFailed &&
      !shouldMergeTemplate
    ) {
      lintContent = assemblyInstructions
    } else {
      lintContent = JSON.stringify(instructions, null, indent)
    }
  } else Eif (inputString != null) {
    lintContent = inputString
  }
 
  return { lintContent, wasStepsOnly, indent }
}