Skip to content

Commit

Permalink
add extends
Browse files Browse the repository at this point in the history
  • Loading branch information
grrowl committed Jul 5, 2024
1 parent d652988 commit 8725d65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports[`cli module call with arguments: ['--list', '--private'] 1`] = `
- property static foo: string
- property bar: string[]
- method constructor(baz: number[]): DefaultClass
- class NamedClass
- class NamedClass extends DefaultClass
- private property static version: number
- method get version(): number
- method constructor(): NamedClass
Expand Down Expand Up @@ -73,7 +73,7 @@ exports[`cli module call with no arguments 1`] = `
│ │ ├── property static foo: string
│ │ ├── property bar: string[]
│ │ └── method constructor(baz: number[]): DefaultClass
│ ├── class NamedClass
│ ├── class NamedClass extends DefaultClass
│ │ ├── method get version(): number
│ │ ├── method constructor(): NamedClass
│ │ ├── method double(): void
Expand Down
17 changes: 2 additions & 15 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
"visibility": "private",
},
{
"children": undefined,
"name": "printHelp",
"signature": "(): void",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "printVersion",
"signature": "(): void",
"type": "function",
Expand All @@ -43,21 +41,18 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
"visibility": "public",
},
{
"children": undefined,
"name": "formatNodeName",
"signature": "(node: TreeNode): string",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "formatAsTree",
"signature": "(node: TreeNode, prefix?: string, isLast?: boolean, isRoot?: boolean): string",
"type": "function",
"visibility": "public",
},
{
"children": undefined,
"name": "formatAsList",
"signature": "(node: TreeNode, depth?: number): string",
"type": "function",
Expand All @@ -77,42 +72,36 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
"visibility": "private",
},
{
"children": undefined,
"name": "TreeNode",
"signature": undefined,
"type": "interface",
"visibility": "public",
},
{
"children": undefined,
"name": "readTsConfig",
"signature": "(rootDir: string): ParsedCommandLine",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "createProgram",
"signature": "(rootDir: string): Program",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "analyzeFile",
"signature": "(sourceFile: SourceFile, typeChecker: TypeChecker, visibilityLevel: VisibilityLevel): TreeNode[]",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "traverseDirectory",
"signature": "(dir: string, program: Program, pathFilter: (path: string) => boolean, visibilityLevel: VisibilityLevel): TreeNode",
"type": "function",
"visibility": "private",
},
{
"children": undefined,
"name": "tree",
"signature": "(rootDir?: string, pathFilter?: (path: string) => boolean, visibilityLevel?: VisibilityLevel): TreeNode",
"type": "function",
Expand Down Expand Up @@ -155,7 +144,7 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
},
],
"name": "DefaultClass",
"signature": undefined,
"superclass": undefined,
"type": "class",
"visibility": "public",
},
Expand Down Expand Up @@ -205,7 +194,7 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
},
],
"name": "NamedClass",
"signature": undefined,
"superclass": "DefaultClass",
"type": "class",
"visibility": "public",
},
Expand Down Expand Up @@ -251,7 +240,6 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
"visibility": "public",
},
{
"children": undefined,
"name": "PublicInterface",
"signature": undefined,
"type": "interface",
Expand Down Expand Up @@ -291,7 +279,6 @@ exports[`tree module calls tree() with private visibility and matches snapshot 1
{
"children": [
{
"children": undefined,
"name": "<anonymous>",
"signature": "(a: number, b: number): string",
"type": "function",
Expand Down
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function formatNodeName(node: TreeNode): string {
return `${node.name}`;
} else if (node.type === "method" || node.type === "function") {
return `${visibilityPrefix}${node.type} ${node.name}${node.signature ? node.signature : ""}`;
} else if (node.type === "class" || node.type === "interface") {
} else if (node.type === "class") {
return `${visibilityPrefix}${node.type} ${node.name}${node.superclass ? ` extends ${node.superclass}` : ""}`;
} else if (node.type === "interface") {
return `${visibilityPrefix}${node.type} ${node.name}`;
} else {
return `${visibilityPrefix}${node.type} ${node.name}${node.signature ? `: ${node.signature}` : ""}`;
Expand Down
57 changes: 31 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface TreeNode {
signature?: string;
children?: TreeNode[];
isDefault?: boolean;
superclass?: string;
}

function readTsConfig(rootDir: string): ts.ParsedCommandLine {
Expand Down Expand Up @@ -71,22 +72,6 @@ function analyzeFile(
return typeChecker.typeToString(typeChecker.getTypeAtLocation(node));
}

// Helper function to check if a node is an exported declaration
function isExportedDeclaration(node: ts.Node): boolean {
return (
(ts.isVariableStatement(node) ||
ts.isFunctionDeclaration(node) ||
ts.isClassDeclaration(node) ||
ts.isInterfaceDeclaration(node) ||
ts.isTypeAliasDeclaration(node) ||
ts.isEnumDeclaration(node)) &&
node.modifiers !== undefined &&
node.modifiers.some(
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
)
);
}

function isVisibleEnough(visibility: VisibilityLevel): boolean {
switch (visibilityLevel) {
case VisibilityLevel.Public:
Expand Down Expand Up @@ -225,28 +210,48 @@ function analyzeFile(
}
}
}
} else if (ts.isClassDeclaration(node)) {
const name = node.name ? node.name.text : "<anonymous>";
const visibility = getVisibility(node);

if (isVisibleEnough(visibility)) {
let superclass: string | undefined;
if (node.heritageClauses) {
for (const clause of node.heritageClauses) {
if (
clause.token === ts.SyntaxKind.ExtendsKeyword &&
clause.types.length > 0
) {
const superclassType = typeChecker.getTypeAtLocation(
clause.types[0],
);
superclass = typeChecker.typeToString(superclassType);
break;
}
}
}
exportNode = {
name,
type: "class",
visibility,
children: handleClassMembers(node),
superclass,
};
nodes.push(exportNode);
}
} else if (
ts.isInterfaceDeclaration(node) ||
ts.isClassDeclaration(node) ||
ts.isFunctionDeclaration(node)
) {
const name = node.name ? node.name.text : "<anonymous>";
const visibility = getVisibility(node);
if (isVisibleEnough(visibility)) {
const type = ts.isInterfaceDeclaration(node)
? "interface"
: ts.isClassDeclaration(node)
? "class"
: "function";
const type = ts.isInterfaceDeclaration(node) ? "interface" : "function";
exportNode = {
name,
type,
visibility,
signature: type === "function" ? getMethodSignature(node) : undefined,
children:
type === "class"
? handleClassMembers(node as ts.ClassDeclaration)
: undefined,
};
nodes.push(exportNode);
}
Expand Down

0 comments on commit 8725d65

Please sign in to comment.