Files
gsfdc/demo-dify/dify/packages/tooling/plugins/tsslint/test-utils.ts
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

47 lines
1.5 KiB
TypeScript

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
}