All files lintAssemblyInput.ts

80.28% Statements 57/71
60% Branches 12/20
100% Functions 3/3
80.28% Lines 57/71

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 901x 1x       1x   1x 9x               1x 1x 1x 1x 1x 1x 1x           1x 8x 8x 8x 8x 8x 8x 8x   8x 1x 1x 1x 1x 1x     8x 7x 7x   8x 8x   8x 8x 8x 2x 8x 6x 6x 6x 8x       8x   8x 8x 1x 1x   7x 7x 7x 7x 1x     7x   7x 7x 7x 8x       7x 7x  
import { getIndentation } from './alphalib/stepParsing.ts'
import { mergeTemplateContent } from './alphalib/templateMerge.ts'
import type { AssemblyInstructionsInput, StepsInput } from './alphalib/types/template.ts'
import type { ResponseTemplateContent, TemplateContent } from './apiTypes.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)
    if (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 if (assemblyInstructions != null) {
    parsedInput = assemblyInstructions
  }
 
  let wasStepsOnly = false
  let instructions: AssemblyInstructionsInput | undefined
 
  if (parsedInput !== undefined) {
    if (isRecord(parsedInput)) {
      if ('steps' in parsedInput) {
        instructions = parsedInput as AssemblyInstructionsInput
      } else {
        instructions = { steps: parsedInput as StepsInput }
        wasStepsOnly = true
      }
    } else {
      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 if (inputString != null) {
    lintContent = inputString
  }
 
  return { lintContent, wasStepsOnly, indent }
}