Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.5 KB
Raw Blame
import type { Reporter, Rule } from '@tsslint/config'
import ts from 'typescript'

const fileName = '/virtual/test.ts'

/** Runs a tsslint rule on a single strict-mode file and returns each report with its fix applied. */
export function lintWithRule(rule: Rule, code: string) {
  const options: ts.CompilerOptions = { strict: true, noEmit: true }
  const host = ts.createCompilerHost(options)
  const getSourceFile = host.getSourceFile
  host.getSourceFile = (name, languageVersion, ...rest) =>
    name === fileName
      ? ts.createSourceFile(name, code, languageVersion, true)
      : getSourceFile(name, languageVersion, ...rest)
  const program = ts.createProgram([fileName], options, host)
  const file = program.getSourceFile(fileName)!

  const results: { message: string; fixed: string }[] = []
  rule({
    typescript: ts,
    program,
    file,
    report(message) {
      const reporter: Reporter = {
        at: () => reporter,
        asWarning: () => reporter,
        asError: () => reporter,
        asSuggestion: () => reporter,
        withDeprecated: () => reporter,
        withUnnecessary: () => reporter,
        withRefactor: () => reporter,
        withoutCache: () => reporter,
        withFix(_title, getChanges) {
          const { span, newText } = getChanges()[0]!.textChanges[0]!
          results.push({
            message,
            fixed: code.slice(0, span.start) + newText + code.slice(span.start + span.length),
          })
          return reporter
        },
      }
      return reporter
    },
  })
  return results
}