A test script that happened to contain the same word used to hide an unused export. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.8 KiB
JavaScript
58 lines
2.8 KiB
JavaScript
// Rejects exports in src/ that no other file uses: dead code, or an export
|
|
// keyword that only widens a module's interface.
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import ts from 'typescript'
|
|
|
|
const root = path.resolve(import.meta.dirname, '..')
|
|
|
|
function findUnusedExports() {
|
|
const { config } = ts.readConfigFile(path.join(root, 'tsconfig.json'), ts.sys.readFile)
|
|
const { fileNames, options } = ts.parseJsonConfigFileContent(config, ts.sys, root)
|
|
const service = ts.createLanguageService({
|
|
...ts.sys,
|
|
getCompilationSettings: () => options,
|
|
getScriptFileNames: () => fileNames,
|
|
getScriptVersion: () => '1',
|
|
getScriptSnapshot: (file) => (fs.existsSync(file) ? ts.ScriptSnapshot.fromString(fs.readFileSync(file, 'utf8')) : undefined),
|
|
getCurrentDirectory: () => root,
|
|
getDefaultLibFileName: ts.getDefaultLibFilePath,
|
|
})
|
|
// The test and contract scripts import src modules or read them as text. A
|
|
// name counts as used only in a script that also names its module.
|
|
const scripts = fs.readdirSync(import.meta.dirname)
|
|
.filter((name) => name !== path.basename(import.meta.filename))
|
|
.map((name) => fs.readFileSync(path.join(import.meta.dirname, name), 'utf8'))
|
|
|
|
const unused = []
|
|
for (const file of fileNames) {
|
|
// bridgeContract.ts also documents the message types for plugin authors.
|
|
if (file.endsWith('.gen.ts') || file.endsWith('/bridgeContract.ts')) continue
|
|
const source = service.getProgram().getSourceFile(file)
|
|
const module = path.relative(path.join(root, 'src'), file).split(path.sep).join('/').replace(/(\/index)?\.tsx?$/, '')
|
|
const users = scripts.filter((text) => text.includes(`'./${module}'`) || text.includes(`'${path.basename(file)}'`))
|
|
for (const statement of source.statements) {
|
|
if (!ts.canHaveModifiers(statement) || !ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue
|
|
const names = ts.isVariableStatement(statement)
|
|
? statement.declarationList.declarations.map((d) => d.name).filter(ts.isIdentifier)
|
|
: [statement.name].filter(Boolean)
|
|
for (const name of names) {
|
|
if (users.some((text) => new RegExp(`\\b${name.text}\\b`).test(text))) continue
|
|
const references = (service.findReferences(file, name.getStart(source)) ?? []).flatMap((r) => r.references)
|
|
if (references.every((r) => r.fileName === file)) {
|
|
unused.push(`${path.relative(root, file)}: ${name.text}`)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { unused, files: fileNames.length }
|
|
}
|
|
|
|
test('every export in src/ has a user outside its own file', () => {
|
|
const { unused, files } = findUnusedExports()
|
|
assert.ok(files > 50, `expected to scan src/, found ${files} files`)
|
|
assert.deepEqual(unused, [])
|
|
})
|