The best way to control Claude Code, Codex and more from any computer or phone

main
ts 719 lines 24.7 KB
Raw
1 import type { CreateRule, ESTree, Visitor } from "@oxlint/plugins"
2 import * as fs from "node:fs"
3 import * as path from "node:path"
4 import ts from "typescript"
5
6 interface InternalExport {
7 readonly fileName: string
8 readonly declaration: ts.Node
9 readonly name: string
10 readonly range: readonly [number, number]
11 used: boolean
12 }
13
14 interface Diagnostic {
15 readonly fileName: string
16 readonly range: readonly [number, number]
17 readonly message: string
18 }
19
20 interface Analysis {
21 readonly diagnosticsByFile: ReadonlyMap<string, ReadonlyArray<Diagnostic>>
22 }
23
24 interface ImportBinding {
25 readonly fileName: string
26 readonly importedName: string
27 }
28
29 interface FileInfo {
30 readonly fileName: string
31 readonly sourceFile: ts.SourceFile
32 readonly imports: Map<string, ImportBinding>
33 readonly namespaceImports: Map<string, string>
34 readonly internalExports: Map<string, Array<InternalExport>>
35 readonly publicDeclarations: Array<ts.Node>
36 }
37
38 const cache = new Map<string, Analysis>()
39 const sourceExtensions = new Set([".ts"])
40
41 function getCwd(context: { readonly cwd?: string; getCwd?: () => string }): string {
42 return context.cwd ?? context.getCwd?.() ?? process.cwd()
43 }
44
45 function normalizePathName(filename: string): string {
46 return path.resolve(filename).split(path.sep).join("/")
47 }
48
49 function rangeNode(range: readonly [number, number]): ESTree.Node {
50 return { range: [range[0], range[1]] } as ESTree.Node
51 }
52
53 function isSourceFile(fileName: string): boolean {
54 return sourceExtensions.has(path.extname(fileName)) && !fileName.endsWith(".d.ts")
55 }
56
57 function findSourceFiles(cwd: string): Array<string> {
58 const packagesDir = path.join(cwd, "packages")
59 if (!fs.existsSync(packagesDir)) return []
60
61 const files: Array<string> = []
62 const visit = (directory: string) => {
63 for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
64 const entryPath = path.join(directory, entry.name)
65 if (entry.isDirectory()) {
66 if (entry.name !== "dist" && entry.name !== "build" && entry.name !== "node_modules") {
67 visit(entryPath)
68 }
69 } else if (entry.isFile() && isSourceFile(entryPath) && normalizePathName(entryPath).includes("/src/")) {
70 files.push(entryPath)
71 }
72 }
73 }
74
75 visit(packagesDir)
76 return files
77 }
78
79 function findWorkspacePackages(cwd: string): Map<string, string> {
80 const packagesDir = path.join(cwd, "packages")
81 const packages = new Map<string, string>()
82 if (!fs.existsSync(packagesDir)) return packages
83
84 const visit = (directory: string) => {
85 const packageJson = path.join(directory, "package.json")
86 if (fs.existsSync(packageJson)) {
87 try {
88 const json = JSON.parse(fs.readFileSync(packageJson, "utf8")) as { readonly name?: unknown }
89 if (typeof json.name === "string") {
90 packages.set(json.name, directory)
91 }
92 } catch {
93 // Ignore malformed package metadata; TypeScript will report that elsewhere.
94 }
95 return
96 }
97
98 for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
99 if (entry.isDirectory() && entry.name !== "node_modules" && entry.name !== "dist" && entry.name !== "build") {
100 visit(path.join(directory, entry.name))
101 }
102 }
103 }
104
105 visit(packagesDir)
106 return packages
107 }
108
109 function resolveSourceFile(basePath: string): string | undefined {
110 const candidates = path.extname(basePath) === ".ts"
111 ? [basePath]
112 : [basePath, `${basePath}.ts`, path.join(basePath, "index.ts")]
113
114 for (const candidate of candidates) {
115 if (fs.existsSync(candidate) && isSourceFile(candidate)) {
116 return normalizePathName(candidate)
117 }
118 }
119 return undefined
120 }
121
122 function resolveModule(
123 source: string,
124 containingFile: string,
125 workspacePackages: ReadonlyMap<string, string>
126 ): string | undefined {
127 if (source.startsWith("./") || source.startsWith("../")) {
128 return resolveSourceFile(path.resolve(path.dirname(containingFile), source))
129 }
130
131 let packageName: string | undefined
132 for (const name of workspacePackages.keys()) {
133 if (source === name || source.startsWith(`${name}/`)) {
134 if (packageName === undefined || name.length > packageName.length) {
135 packageName = name
136 }
137 }
138 }
139 if (packageName === undefined) return undefined
140
141 const packageRoot = workspacePackages.get(packageName)
142 if (packageRoot === undefined) return undefined
143
144 if (source === packageName) {
145 return resolveSourceFile(path.join(packageRoot, "src", "index.ts"))
146 }
147
148 const subpath = source.slice(packageName.length + 1)
149 return resolveSourceFile(path.join(packageRoot, "src", subpath))
150 }
151
152 function hasModifier(node: ts.Node, kind: ts.SyntaxKind): boolean {
153 return ts.canHaveModifiers(node) && (ts.getModifiers(node) ?? []).some((modifier) => modifier.kind === kind)
154 }
155
156 function hasExportModifier(node: ts.Node): boolean {
157 return hasModifier(node, ts.SyntaxKind.ExportKeyword)
158 }
159
160 function hasPrivateOrProtectedModifier(node: ts.Node): boolean {
161 return hasModifier(node, ts.SyntaxKind.PrivateKeyword) || hasModifier(node, ts.SyntaxKind.ProtectedKeyword)
162 }
163
164 function hasInternalJSDoc(node: ts.Node): boolean {
165 return ts.getJSDocTags(node).some((tag) => tag.tagName.getText() === "internal")
166 }
167
168 function hasInternalApiJSDoc(node: ts.Node): boolean {
169 if (hasInternalJSDoc(node)) return true
170 if (ts.isVariableDeclaration(node)) {
171 return hasInternalJSDoc(node.parent.parent)
172 }
173 if (ts.isBindingElement(node) && ts.isVariableDeclaration(node.parent.parent)) {
174 return hasInternalJSDoc(node.parent.parent.parent)
175 }
176 return false
177 }
178
179 function isInternalPath(fileName: string): boolean {
180 return /\/src\/(?:.*\/)?internal\//.test(normalizePathName(fileName))
181 }
182
183 function collectBindingNames(name: ts.BindingName, out: Array<ts.Identifier>) {
184 if (ts.isIdentifier(name)) {
185 out.push(name)
186 return
187 }
188 for (const element of name.elements) {
189 if (ts.isBindingElement(element)) {
190 collectBindingNames(element.name, out)
191 }
192 }
193 }
194
195 function getTopLevelDeclarationNameNodes(node: ts.Node): Array<ts.Identifier> {
196 if (
197 (ts.isFunctionDeclaration(node) ||
198 ts.isClassDeclaration(node) ||
199 ts.isInterfaceDeclaration(node) ||
200 ts.isTypeAliasDeclaration(node) ||
201 ts.isEnumDeclaration(node) ||
202 ts.isModuleDeclaration(node)) &&
203 node.name !== undefined &&
204 ts.isIdentifier(node.name)
205 ) {
206 return [node.name]
207 }
208
209 if (ts.isVariableStatement(node)) {
210 const names: Array<ts.Identifier> = []
211 for (const declaration of node.declarationList.declarations) {
212 collectBindingNames(declaration.name, names)
213 }
214 return names
215 }
216
217 return []
218 }
219
220 function getDeclarationForName(node: ts.Node, name: ts.Identifier): ts.Node {
221 if (!ts.isVariableStatement(node)) return node
222
223 let result: ts.Node = node
224 const visit = (current: ts.Node) => {
225 if (ts.isVariableDeclaration(current) || ts.isBindingElement(current)) {
226 const names: Array<ts.Identifier> = []
227 collectBindingNames(current.name, names)
228 if (names.includes(name)) {
229 result = current
230 return
231 }
232 }
233 ts.forEachChild(current, visit)
234 }
235 visit(node.declarationList)
236 return result
237 }
238
239 function isImportOrExportBinding(identifier: ts.Identifier): boolean {
240 const parent = identifier.parent
241 return (ts.isImportSpecifier(parent) && (parent.name === identifier || parent.propertyName === identifier)) ||
242 (ts.isImportClause(parent) && parent.name === identifier) ||
243 (ts.isNamespaceImport(parent) && parent.name === identifier) ||
244 (ts.isExportSpecifier(parent) && (parent.name === identifier || parent.propertyName === identifier))
245 }
246
247 function isDeclarationName(identifier: ts.Identifier): boolean {
248 const parent = identifier.parent
249 return (ts.isVariableDeclaration(parent) && parent.name === identifier) ||
250 (ts.isBindingElement(parent) && parent.name === identifier) ||
251 (ts.isParameter(parent) && parent.name === identifier) ||
252 (ts.isFunctionDeclaration(parent) && parent.name === identifier) ||
253 (ts.isClassDeclaration(parent) && parent.name === identifier) ||
254 (ts.isInterfaceDeclaration(parent) && parent.name === identifier) ||
255 (ts.isTypeAliasDeclaration(parent) && parent.name === identifier) ||
256 (ts.isEnumDeclaration(parent) && parent.name === identifier) ||
257 (ts.isEnumMember(parent) && parent.name === identifier) ||
258 (ts.isModuleDeclaration(parent) && parent.name === identifier)
259 }
260
261 function isNonReferenceName(identifier: ts.Identifier): boolean {
262 const parent = identifier.parent
263 return isImportOrExportBinding(identifier) ||
264 isDeclarationName(identifier) ||
265 (ts.isPropertyAccessExpression(parent) && parent.name === identifier) ||
266 (ts.isPropertyAssignment(parent) && parent.name === identifier) ||
267 (ts.isPropertyDeclaration(parent) && parent.name === identifier) ||
268 (ts.isPropertySignature(parent) && parent.name === identifier) ||
269 (ts.isMethodDeclaration(parent) && parent.name === identifier) ||
270 (ts.isMethodSignature(parent) && parent.name === identifier) ||
271 (ts.isGetAccessorDeclaration(parent) && parent.name === identifier) ||
272 (ts.isSetAccessorDeclaration(parent) && parent.name === identifier)
273 }
274
275 function isInside(node: ts.Node, container: ts.Node): boolean {
276 if (node.getSourceFile() !== container.getSourceFile()) return false
277 return node.getStart() >= container.getStart() && node.getEnd() <= container.getEnd()
278 }
279
280 function getExports(
281 fileInfos: ReadonlyMap<string, FileInfo>,
282 fileName: string | undefined,
283 exportName: string
284 ): ReadonlyArray<InternalExport> {
285 if (fileName === undefined) return []
286 return fileInfos.get(fileName)?.internalExports.get(exportName) ?? []
287 }
288
289 function getReExportedInternals(
290 fileInfos: ReadonlyMap<string, FileInfo>,
291 fileInfo: FileInfo,
292 workspacePackages: ReadonlyMap<string, string>,
293 specifier: ts.ExportSpecifier
294 ): ReadonlyArray<InternalExport> {
295 const exportDeclaration = specifier.parent.parent
296 const importedName = (specifier.propertyName ?? specifier.name).text
297 if (
298 exportDeclaration.moduleSpecifier !== undefined &&
299 ts.isStringLiteral(exportDeclaration.moduleSpecifier)
300 ) {
301 const importedFile = resolveModule(
302 exportDeclaration.moduleSpecifier.text,
303 fileInfo.sourceFile.fileName,
304 workspacePackages
305 )
306 return getExports(fileInfos, importedFile, importedName)
307 }
308
309 const imported = fileInfo.imports.get(importedName)
310 return [
311 ...(fileInfo.internalExports.get(importedName) ?? []),
312 ...getExports(fileInfos, imported?.fileName, imported?.importedName ?? "")
313 ]
314 }
315
316 function markUsed(exports: ReadonlyArray<InternalExport>, node: ts.Node) {
317 for (const internal of exports) {
318 if (!isInside(node, internal.declaration)) {
319 internal.used = true
320 }
321 }
322 }
323
324 function addFileDiagnostic(
325 diagnosticsByFile: Map<string, Array<Diagnostic>>,
326 diagnostic: Diagnostic
327 ) {
328 const normalized = normalizePathName(diagnostic.fileName)
329 const diagnostics = diagnosticsByFile.get(normalized) ?? []
330 diagnostics.push(diagnostic)
331 diagnosticsByFile.set(normalized, diagnostics)
332 }
333
334 function addInternalExport(fileInfo: FileInfo, internal: InternalExport) {
335 const exports = fileInfo.internalExports.get(internal.name) ?? []
336 exports.push(internal)
337 fileInfo.internalExports.set(internal.name, exports)
338 }
339
340 function collectFileInfo(
341 sourceFile: ts.SourceFile,
342 workspacePackages: ReadonlyMap<string, string>
343 ): FileInfo {
344 const fileInfo: FileInfo = {
345 fileName: normalizePathName(sourceFile.fileName),
346 sourceFile,
347 imports: new Map(),
348 namespaceImports: new Map(),
349 internalExports: new Map(),
350 publicDeclarations: []
351 }
352 const publicDeclarationSet = new Set<ts.Node>()
353 const isInternalFile = isInternalPath(sourceFile.fileName)
354
355 for (const statement of sourceFile.statements) {
356 if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
357 const importedFile = resolveModule(statement.moduleSpecifier.text, sourceFile.fileName, workspacePackages)
358 const importClause = statement.importClause
359 if (importedFile === undefined || importClause === undefined) continue
360
361 if (importClause.name !== undefined) {
362 fileInfo.imports.set(importClause.name.text, { fileName: importedFile, importedName: "default" })
363 }
364
365 const namedBindings = importClause.namedBindings
366 if (namedBindings === undefined) continue
367 if (ts.isNamespaceImport(namedBindings)) {
368 fileInfo.namespaceImports.set(namedBindings.name.text, importedFile)
369 } else {
370 for (const specifier of namedBindings.elements) {
371 const importedName = (specifier.propertyName ?? specifier.name).text
372 fileInfo.imports.set(specifier.name.text, { fileName: importedFile, importedName })
373 }
374 }
375 continue
376 }
377
378 if (
379 !isInternalFile &&
380 ts.isExportDeclaration(statement) &&
381 !hasInternalApiJSDoc(statement) &&
382 statement.exportClause !== undefined &&
383 ts.isNamedExports(statement.exportClause)
384 ) {
385 fileInfo.publicDeclarations.push(statement)
386 continue
387 }
388
389 if (!hasExportModifier(statement)) continue
390
391 const names = getTopLevelDeclarationNameNodes(statement)
392 if (names.length === 0) continue
393
394 const statementIsInternal = hasInternalApiJSDoc(statement)
395 for (const name of names) {
396 const declaration = getDeclarationForName(statement, name)
397 const declarationIsInternal = statementIsInternal || hasInternalApiJSDoc(declaration)
398 if (declarationIsInternal) {
399 addInternalExport(fileInfo, {
400 fileName: sourceFile.fileName,
401 declaration,
402 name: name.text,
403 range: [name.getStart(sourceFile), name.getEnd()],
404 used: false
405 })
406 } else if (!isInternalFile && !publicDeclarationSet.has(statement)) {
407 fileInfo.publicDeclarations.push(statement)
408 publicDeclarationSet.add(statement)
409 }
410 }
411 }
412
413 return fileInfo
414 }
415
416 function markNamespaceReference(
417 fileInfos: ReadonlyMap<string, FileInfo>,
418 fileInfo: FileInfo,
419 namespace: ts.Identifier,
420 name: ts.Identifier
421 ) {
422 const importedFile = fileInfo.namespaceImports.get(namespace.text)
423 markUsed(getExports(fileInfos, importedFile, name.text), name)
424 }
425
426 function markDynamicImport(
427 fileInfos: ReadonlyMap<string, FileInfo>,
428 fileInfo: FileInfo,
429 workspacePackages: ReadonlyMap<string, string>,
430 importCall: ts.CallExpression
431 ) {
432 const specifier = importCall.arguments[0]
433 if (specifier === undefined || !ts.isStringLiteralLike(specifier)) return
434 const importedFile = resolveModule(specifier.text, fileInfo.sourceFile.fileName, workspacePackages)
435 if (importedFile === undefined) return
436 for (const exports of fileInfos.get(importedFile)?.internalExports.values() ?? []) {
437 markUsed(exports, importCall)
438 }
439 }
440
441 function scanUsage(
442 fileInfos: ReadonlyMap<string, FileInfo>,
443 fileInfo: FileInfo,
444 workspacePackages: ReadonlyMap<string, string>
445 ) {
446 const visit = (node: ts.Node) => {
447 if (ts.isExportSpecifier(node)) {
448 markUsed(getReExportedInternals(fileInfos, fileInfo, workspacePackages, node), node)
449 } else if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword) {
450 markDynamicImport(fileInfos, fileInfo, workspacePackages, node)
451 } else if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && ts.isIdentifier(node.name)) {
452 markNamespaceReference(fileInfos, fileInfo, node.expression, node.name)
453 } else if (ts.isQualifiedName(node) && ts.isIdentifier(node.left)) {
454 markNamespaceReference(fileInfos, fileInfo, node.left, node.right)
455 } else if (ts.isIdentifier(node) && !isNonReferenceName(node)) {
456 markUsed(fileInfo.internalExports.get(node.text) ?? [], node)
457 const imported = fileInfo.imports.get(node.text)
458 markUsed(getExports(fileInfos, imported?.fileName, imported?.importedName ?? ""), node)
459 }
460 ts.forEachChild(node, visit)
461 }
462 visit(fileInfo.sourceFile)
463 }
464
465 function findInternalReferences(
466 fileInfos: ReadonlyMap<string, FileInfo>,
467 fileInfo: FileInfo,
468 diagnostics: Array<Diagnostic>,
469 node: ts.Node
470 ) {
471 const visit = (current: ts.Node) => {
472 if (ts.isPropertyAccessExpression(current) && ts.isIdentifier(current.expression)) {
473 const importedFile = fileInfo.namespaceImports.get(current.expression.text)
474 for (const internal of getExports(fileInfos, importedFile, current.name.text)) {
475 diagnostics.push({
476 fileName: fileInfo.fileName,
477 range: [current.name.getStart(), current.name.getEnd()],
478 message: `Do not reference @internal export "${internal.name}" in a public exported type signature`
479 })
480 }
481 } else if (ts.isQualifiedName(current) && ts.isIdentifier(current.left)) {
482 const importedFile = fileInfo.namespaceImports.get(current.left.text)
483 for (const internal of getExports(fileInfos, importedFile, current.right.text)) {
484 diagnostics.push({
485 fileName: fileInfo.fileName,
486 range: [current.right.getStart(), current.right.getEnd()],
487 message: `Do not reference @internal export "${internal.name}" in a public exported type signature`
488 })
489 }
490 } else if (ts.isIdentifier(current) && !isNonReferenceName(current)) {
491 const localExports = fileInfo.internalExports.get(current.text) ?? []
492 const imported = fileInfo.imports.get(current.text)
493 const importedExports = getExports(fileInfos, imported?.fileName, imported?.importedName ?? "")
494
495 for (const internal of [...localExports, ...importedExports]) {
496 diagnostics.push({
497 fileName: fileInfo.fileName,
498 range: [current.getStart(), current.getEnd()],
499 message: `Do not reference @internal export "${internal.name}" in a public exported type signature`
500 })
501 }
502 }
503 ts.forEachChild(current, visit)
504 }
505 visit(node)
506 }
507
508 function scanTypeParameters(
509 fileInfos: ReadonlyMap<string, FileInfo>,
510 fileInfo: FileInfo,
511 diagnostics: Array<Diagnostic>,
512 typeParameters: ts.NodeArray<ts.TypeParameterDeclaration> | undefined
513 ) {
514 if (typeParameters === undefined) return
515 for (const typeParameter of typeParameters) {
516 if (typeParameter.constraint !== undefined) {
517 findInternalReferences(fileInfos, fileInfo, diagnostics, typeParameter.constraint)
518 }
519 if (typeParameter.default !== undefined) {
520 findInternalReferences(fileInfos, fileInfo, diagnostics, typeParameter.default)
521 }
522 }
523 }
524
525 function scanParameterTypes(
526 fileInfos: ReadonlyMap<string, FileInfo>,
527 fileInfo: FileInfo,
528 diagnostics: Array<Diagnostic>,
529 parameters: ts.NodeArray<ts.ParameterDeclaration>
530 ) {
531 for (const parameter of parameters) {
532 if (parameter.type !== undefined) {
533 findInternalReferences(fileInfos, fileInfo, diagnostics, parameter.type)
534 }
535 }
536 }
537
538 function scanFunctionLikeSignature(
539 fileInfos: ReadonlyMap<string, FileInfo>,
540 fileInfo: FileInfo,
541 diagnostics: Array<Diagnostic>,
542 node: ts.SignatureDeclarationBase
543 ) {
544 scanTypeParameters(fileInfos, fileInfo, diagnostics, node.typeParameters)
545 scanParameterTypes(fileInfos, fileInfo, diagnostics, node.parameters)
546 if (node.type !== undefined) {
547 findInternalReferences(fileInfos, fileInfo, diagnostics, node.type)
548 }
549 }
550
551 function scanClassMemberSignature(
552 fileInfos: ReadonlyMap<string, FileInfo>,
553 fileInfo: FileInfo,
554 diagnostics: Array<Diagnostic>,
555 member: ts.ClassElement
556 ) {
557 if (hasInternalJSDoc(member)) return
558 if (hasPrivateOrProtectedModifier(member)) return
559 if (
560 ts.isMethodDeclaration(member) ||
561 ts.isConstructorDeclaration(member) ||
562 ts.isGetAccessorDeclaration(member) ||
563 ts.isSetAccessorDeclaration(member)
564 ) {
565 scanFunctionLikeSignature(fileInfos, fileInfo, diagnostics, member)
566 } else if (ts.isPropertyDeclaration(member) && member.type !== undefined) {
567 findInternalReferences(fileInfos, fileInfo, diagnostics, member.type)
568 }
569 }
570
571 function scanInterfaceMemberSignature(
572 fileInfos: ReadonlyMap<string, FileInfo>,
573 fileInfo: FileInfo,
574 diagnostics: Array<Diagnostic>,
575 member: ts.TypeElement
576 ) {
577 if (hasInternalJSDoc(member)) return
578 if (
579 ts.isMethodSignature(member) || ts.isCallSignatureDeclaration(member) || ts.isConstructSignatureDeclaration(member)
580 ) {
581 scanFunctionLikeSignature(fileInfos, fileInfo, diagnostics, member)
582 } else if (ts.isPropertySignature(member) && member.type !== undefined) {
583 findInternalReferences(fileInfos, fileInfo, diagnostics, member.type)
584 } else if (ts.isIndexSignatureDeclaration(member)) {
585 scanFunctionLikeSignature(fileInfos, fileInfo, diagnostics, member)
586 }
587 }
588
589 function scanPublicSignature(
590 fileInfos: ReadonlyMap<string, FileInfo>,
591 fileInfo: FileInfo,
592 diagnostics: Array<Diagnostic>,
593 node: ts.Node,
594 workspacePackages: ReadonlyMap<string, string>
595 ) {
596 if (ts.isFunctionDeclaration(node)) {
597 scanFunctionLikeSignature(fileInfos, fileInfo, diagnostics, node)
598 } else if (ts.isVariableStatement(node)) {
599 for (const declaration of node.declarationList.declarations) {
600 if (declaration.type !== undefined) {
601 findInternalReferences(fileInfos, fileInfo, diagnostics, declaration.type)
602 }
603 }
604 } else if (ts.isTypeAliasDeclaration(node)) {
605 scanTypeParameters(fileInfos, fileInfo, diagnostics, node.typeParameters)
606 findInternalReferences(fileInfos, fileInfo, diagnostics, node.type)
607 } else if (ts.isInterfaceDeclaration(node)) {
608 scanTypeParameters(fileInfos, fileInfo, diagnostics, node.typeParameters)
609 for (const heritage of node.heritageClauses ?? []) {
610 for (const type of heritage.types) {
611 findInternalReferences(fileInfos, fileInfo, diagnostics, type)
612 }
613 }
614 for (const member of node.members) {
615 scanInterfaceMemberSignature(fileInfos, fileInfo, diagnostics, member)
616 }
617 } else if (ts.isClassDeclaration(node)) {
618 scanTypeParameters(fileInfos, fileInfo, diagnostics, node.typeParameters)
619 for (const heritage of node.heritageClauses ?? []) {
620 for (const type of heritage.types) {
621 findInternalReferences(fileInfos, fileInfo, diagnostics, type)
622 }
623 }
624 for (const member of node.members) {
625 scanClassMemberSignature(fileInfos, fileInfo, diagnostics, member)
626 }
627 } else if (
628 ts.isExportDeclaration(node) &&
629 node.exportClause !== undefined &&
630 ts.isNamedExports(node.exportClause)
631 ) {
632 for (const specifier of node.exportClause.elements) {
633 for (const internal of getReExportedInternals(fileInfos, fileInfo, workspacePackages, specifier)) {
634 const name = specifier.propertyName ?? specifier.name
635 diagnostics.push({
636 fileName: fileInfo.fileName,
637 range: [name.getStart(), name.getEnd()],
638 message: `Do not re-export @internal export "${internal.name}" from a public module`
639 })
640 }
641 }
642 }
643 }
644
645 function analyze(cwd: string): Analysis {
646 const normalizedCwd = normalizePathName(cwd)
647 const cached = cache.get(normalizedCwd)
648 if (cached !== undefined) return cached
649
650 const workspacePackages = findWorkspacePackages(cwd)
651 const fileInfos = new Map<string, FileInfo>()
652 for (const fileName of findSourceFiles(cwd)) {
653 const sourceFile = ts.createSourceFile(fileName, fs.readFileSync(fileName, "utf8"), ts.ScriptTarget.Latest, true)
654 const fileInfo = collectFileInfo(sourceFile, workspacePackages)
655 fileInfos.set(fileInfo.fileName, fileInfo)
656 }
657
658 for (const fileInfo of fileInfos.values()) {
659 scanUsage(fileInfos, fileInfo, workspacePackages)
660 }
661
662 const diagnosticsByFile = new Map<string, Array<Diagnostic>>()
663
664 for (const fileInfo of fileInfos.values()) {
665 const diagnostics: Array<Diagnostic> = []
666 for (const declaration of fileInfo.publicDeclarations) {
667 scanPublicSignature(fileInfos, fileInfo, diagnostics, declaration, workspacePackages)
668 }
669 for (const diagnostic of diagnostics) {
670 addFileDiagnostic(diagnosticsByFile, diagnostic)
671 }
672
673 for (const exportsForName of fileInfo.internalExports.values()) {
674 for (const internal of exportsForName) {
675 if (!internal.used) {
676 addFileDiagnostic(diagnosticsByFile, {
677 fileName: internal.fileName,
678 range: internal.range,
679 message: `@internal export "${internal.name}" is not used by production sources`
680 })
681 }
682 }
683 }
684 }
685
686 for (const diagnostics of diagnosticsByFile.values()) {
687 diagnostics.sort((self, that) => self.range[0] - that.range[0])
688 }
689
690 const analysis = { diagnosticsByFile }
691 cache.set(normalizedCwd, analysis)
692 return analysis
693 }
694
695 const rule: CreateRule = {
696 meta: {
697 type: "problem",
698 docs: {
699 description: "Disallow unused @internal exports, public re-exports, and references from public type signatures"
700 }
701 },
702 create(context) {
703 const analysis = analyze(getCwd(context))
704 const diagnostics = analysis.diagnosticsByFile.get(normalizePathName(context.filename)) ?? []
705
706 return {
707 Program() {
708 for (const diagnostic of diagnostics) {
709 context.report({
710 node: rangeNode(diagnostic.range),
711 message: diagnostic.message
712 })
713 }
714 }
715 } as Visitor
716 }
717 }
718
719 export default rule