-
Notifications
You must be signed in to change notification settings - Fork 0
feat(devoid): types flag #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e350962
chore(devoid): lint and update node module imports to specify 'node:'.
ElijahKotyluk cec33f9
feat(types): add scanner, graph builder, and analyzer.
ElijahKotyluk 25a4a50
chore(types): wire up to cli, add formatter, add basic testing.
ElijahKotyluk 3c5458e
chore(devoid): update remaining node module imports.
ElijahKotyluk 7f32f7c
chore(types): lint fixes for test file.
ElijahKotyluk f7de18e
chore(core): minor fixes.
ElijahKotyluk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // src/cli/typesFormat.ts | ||
|
|
||
| import { log } from "../utils"; | ||
| import { colors } from "./colors"; | ||
| import { heading } from "./format"; | ||
|
|
||
| export function logUnusedExportedTypes(unused: { file: string; name: string }[]): void { | ||
| log(heading("Unused Exported Types")); | ||
|
|
||
| if (unused.length === 0) { | ||
| log(`${colors.dim}No unused exported types found!${colors.reset}\n`); | ||
| return; | ||
| } | ||
|
|
||
| const byFile: Record<string, string[]> = {}; | ||
|
|
||
| for (const { file, name } of unused) { | ||
| if (!byFile[file]) byFile[file] = []; | ||
|
|
||
| byFile[file].push(name); | ||
| } | ||
|
|
||
| for (const file of Object.keys(byFile)) { | ||
| log(`${colors.bold}${file}${colors.reset}`); | ||
|
|
||
| for (const name of byFile[file]) log(` • ${name}`); | ||
|
|
||
| log(""); | ||
| } | ||
| } | ||
|
|
||
| export function logUnusedLocalTypes(unused: { file: string; name: string }[]): void { | ||
| log(heading("Unused Local Types")); | ||
|
|
||
| if (unused.length === 0) { | ||
| log(`${colors.dim}No unused local types found!${colors.reset}\n`); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const byFile: Record<string, string[]> = {}; | ||
|
|
||
| for (const { file, name } of unused) { | ||
| if (!byFile[file]) byFile[file] = []; | ||
|
|
||
| byFile[file].push(name); | ||
| } | ||
|
|
||
| for (const file of Object.keys(byFile)) { | ||
| log(`${colors.bold}${file}${colors.reset}`); | ||
|
|
||
| for (const name of byFile[file]) log(` • ${name}`); | ||
|
|
||
| log(""); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import ts from "typescript"; | ||
| import { intern } from "../../utils"; | ||
|
|
||
| /** | ||
| * Type-usage results for a single file. | ||
| * Narrow, syntax-based, no type-checker required. | ||
| */ | ||
| export interface TypeUsageResult { | ||
| declaredTypes: Set<string>; | ||
| exportedTypes: Set<string>; | ||
| referencedTypes: Set<string>; | ||
| qualifiedTypeRefs: Map<string, Set<string>>; | ||
| } | ||
|
|
||
| function hasExportModifier(node: ts.Node): boolean { | ||
| const modifiers = (node as any).modifiers as ts.NodeArray<ts.Modifier> | undefined; | ||
|
|
||
| return !!modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword); | ||
| } | ||
|
|
||
| /** | ||
| * Collect type declarations and references in type positions. | ||
| * This does NOT do cross-file resolution - per-file only. | ||
| */ | ||
| export function analyzeTypeUsage(filePath: string, sourceText: string): TypeUsageResult { | ||
| const ast = ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.ESNext, true); | ||
|
|
||
| const declaredTypes = new Set<string>(); | ||
| const exportedTypes = new Set<string>(); | ||
| const referencedTypes = new Set<string>(); | ||
| const qualifiedTypeRefs = new Map<string, Set<string>>(); | ||
|
|
||
| function addDeclared(name: string, exported: boolean) { | ||
| const type = intern(name); | ||
|
|
||
| declaredTypes.add(type); | ||
|
|
||
| if (exported) exportedTypes.add(type); | ||
| } | ||
|
|
||
| // Collecting declarations for types and interfaces | ||
| function collectDeclarations(node: ts.Node): void { | ||
| if (ts.isInterfaceDeclaration(node)) { | ||
| addDeclared(node.name.text, hasExportModifier(node)); | ||
| } else if (ts.isTypeAliasDeclaration(node)) { | ||
| addDeclared(node.name.text, hasExportModifier(node)); | ||
| } | ||
|
|
||
| ts.forEachChild(node, collectDeclarations); | ||
| } | ||
|
|
||
| // Collect type references | ||
| function collectTypeRefsFromTypeNode(typeNode: ts.TypeNode): void { | ||
| if (ts.isTypeReferenceNode(typeNode)) { | ||
| collectEntityName(typeNode.typeName); | ||
|
|
||
| for (const arg of typeNode.typeArguments ?? []) collectTypeRefsFromTypeNode(arg); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| ts.forEachChild(typeNode, (child) => { | ||
| if (ts.isTypeNode(child)) collectTypeRefsFromTypeNode(child); | ||
| else | ||
| ts.forEachChild(child, (g) => { | ||
| if (ts.isTypeNode(g)) collectTypeRefsFromTypeNode(g); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function collectEntityName(name: ts.EntityName) { | ||
| if (ts.isIdentifier(name)) { | ||
| referencedTypes.add(intern(name.text)); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // QualifiedName: left.right (e.g. T.Foo) | ||
| if (ts.isIdentifier(name.left)) { | ||
| const ns = intern(name.left.text); | ||
| const member = intern(name.right.text); | ||
|
|
||
| let set = qualifiedTypeRefs.get(ns); | ||
|
|
||
| if (!set) qualifiedTypeRefs.set(ns, (set = new Set())); | ||
|
|
||
| set.add(member); | ||
| } else { | ||
| collectEntityName(name.left); | ||
|
|
||
| referencedTypes.add(intern(name.right.text)); | ||
| } | ||
| } | ||
|
|
||
| function visitForTypePositions(node: ts.Node): void { | ||
| // Variable declarations: let x: Foo | ||
| if (ts.isVariableDeclaration(node) && node.type) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
|
|
||
| // Function/method params and return types | ||
| if (ts.isParameter(node) && node.type) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
| if ( | ||
| (ts.isFunctionDeclaration(node) || | ||
| ts.isMethodDeclaration(node) || | ||
| ts.isArrowFunction(node)) && | ||
| node.type | ||
| ) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
|
|
||
| // Type aliases: type X = Foo | ||
| if (ts.isTypeAliasDeclaration(node)) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
|
|
||
| // Property and method signatures in interfaces | ||
| if (ts.isPropertySignature(node) && node.type) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
|
|
||
| if (ts.isMethodSignature(node) && node.type) { | ||
| collectTypeRefsFromTypeNode(node.type); | ||
| } | ||
|
|
||
| // Heritage clauses (extends && implements) | ||
| if ((ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) && node.heritageClauses) { | ||
| for (const heritageClause of node.heritageClauses) { | ||
| for (const t of heritageClause.types) { | ||
| const expression = t.expression; | ||
|
|
||
| if (ts.isIdentifier(expression)) referencedTypes.add(intern(expression.text)); | ||
|
|
||
| if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.name)) { | ||
| referencedTypes.add(intern(expression.name.text)); | ||
| } | ||
|
|
||
| for (const arg of t.typeArguments ?? []) collectTypeRefsFromTypeNode(arg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Import declarations | ||
| if (ts.isImportDeclaration(node)) { | ||
| const clause = node.importClause; | ||
|
|
||
| if (!clause) return; | ||
|
|
||
| if (clause.isTypeOnly) { | ||
| if (clause.name) referencedTypes.add(intern(clause.name.text)); | ||
|
|
||
| const namedBindings = clause.namedBindings; | ||
|
|
||
| if (namedBindings && ts.isNamedImports(namedBindings)) { | ||
| for (const el of namedBindings.elements) { | ||
| referencedTypes.add(intern(el.name.text)); | ||
| } | ||
| } | ||
| } else { | ||
| const namedBindings = clause.namedBindings; | ||
|
|
||
| if (namedBindings && ts.isNamedImports(namedBindings)) { | ||
| for (const element of namedBindings.elements) { | ||
| if (element.isTypeOnly === true) { | ||
| referencedTypes.add(intern(element.name.text)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ts.forEachChild(node, visitForTypePositions); | ||
| } | ||
|
|
||
| collectDeclarations(ast); | ||
| visitForTypePositions(ast); | ||
|
|
||
| return { declaredTypes, exportedTypes, referencedTypes, qualifiedTypeRefs }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.