| #!/usr/bin/env node |
|
|
| import { readFile } from 'node:fs/promises' |
| import path from 'node:path' |
| import process from 'node:process' |
| import { applyCodeFixes, compile, NodeHost } from '@typespec/compiler' |
|
|
| const CODEFIX_ID = 'format-doc-comment' |
| const RULE_NAME = 'doc-format' |
|
|
| const cwd = process.cwd() |
| const pkg = JSON.parse( |
| await readFile(path.resolve(cwd, 'package.json'), 'utf8'), |
| ) |
|
|
| |
| |
| const RULE_ID = `${pkg.name}/${RULE_NAME}` |
| const RULESET = `${pkg.name}/all` |
|
|
| |
| |
| const entryRel = pkg.exports?.['.']?.typespec |
| if (!entryRel) { |
| console.error( |
| "apply-doc-fixes: package.json must declare exports['.'].typespec", |
| ) |
| process.exit(1) |
| } |
| const entry = path.resolve(cwd, entryRel) |
|
|
| |
| |
| const program = await compile(NodeHost, entry, { |
| noEmit: true, |
| linterRuleSet: { extends: [RULESET] }, |
| }) |
|
|
| |
| |
| const matching = program.diagnostics.filter((d) => d.code === RULE_ID) |
| const fixes = matching.flatMap( |
| (d) => d.codefixes?.filter((c) => c.id === CODEFIX_ID) ?? [], |
| ) |
|
|
| if (fixes.length === 0) { |
| console.error('apply-doc-fixes: no doc-format codefixes to apply') |
| process.exit(0) |
| } |
|
|
| await applyCodeFixes(NodeHost, fixes) |
|
|
| console.error( |
| `apply-doc-fixes: applied ${fixes.length} fix(es) across ${matching.length} diagnostic(s)`, |
| ) |
|
|