This script will extract deprecations using the TypeScript API.
It's not 100% reliable, but it's pretty good. There seems to be an issue where overloaded functions that have a single deprecated clause will aways report.
#!/usr/bin/env node | |
import { createRequire } from "module"; | |
import path from "path"; | |
const require = createRequire(path.join(process.cwd(), "package.json")); | |
const ts = require("typescript"); | |
const configPath = ts.findConfigFile("./", ts.sys.fileExists, "tsconfig.json"); | |
if (!configPath) throw new Error("Could not find a valid 'tsconfig.json'."); | |
const configFile = ts.readConfigFile(configPath, ts.sys.readFile); | |
const parsedCommandLine = ts.parseJsonConfigFileContent( | |
configFile.config, | |
ts.sys, | |
path.dirname(configPath), | |
); | |
const program = ts.createProgram({ | |
rootNames: parsedCommandLine.fileNames, | |
options: parsedCommandLine.options, | |
}); | |
const checker = program.getTypeChecker(); | |
for (const sourceFile of program.getSourceFiles()) { | |
if (sourceFile.isDeclarationFile) continue; | |
ts.forEachChild(sourceFile, function visit(node) { | |
const symbol = node.name && checker.getSymbolAtLocation(node.name); | |
if (symbol) { | |
const docs = symbol.getJsDocTags(); | |
const deprecated = docs.find((tag) => tag.name === "deprecated"); | |
if (deprecated) { | |
const name = symbol.getName(); | |
const { line, character } = sourceFile.getLineAndCharacterOfPosition( | |
node.getStart(), | |
); | |
const filename = path.relative(process.cwd(), sourceFile.fileName); | |
console.log(`${name} at ${filename}:${line + 1}:${character + 1}`); | |
} | |
} | |
ts.forEachChild(node, visit); | |
}); | |
} |