Skip to content

Commit b78cfca

Browse files
committed
private function
1 parent 8c239bb commit b78cfca

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ The tool will output a tree-like structure of your project, including files, dir
5555
You can customize ignored patterns by passing them as arguments to the CLI command. For example:
5656

5757
```
58-
npx ts-print-tree --ignore "__snapshots__" --ignore "/\\.(test|spec)\\.ts$/"
58+
npx ts-print-tree -- --ignore "__snapshots__" --ignore "/\\.(test|spec)\\.ts$/"
5959
```
6060

6161
This will ignore files and directories of __snapshots__ or ending with `.test.ts` or `.spec.ts`.
6262

6363
You can also set your project directory with the `--cwd` option:
6464

6565
```
66-
npx ts-print-tree --cwd ./my-project
66+
npx ts-print-tree -- --cwd ./my-project
67+
```
68+
69+
By default, only public members are shown. You can include protected and private members using the `--protected` and `--private` flags, respectively:
70+
71+
```
72+
npx ts-print-tree -- --private
6773
```
6874

6975
## 📚 Contributing

src/__snapshots__/index.test.ts.snap

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,11 @@ exports[`tree module calls tree() and matches console.log snapshot 1`] = `
77
├─ cli.ts
88
├─ index.ts
99
│ └─ const ignoredPatterns
10-
│ └─ function readTsConfig(rootDir: string): ParsedCommandLine
11-
│ └─ function createProgram(rootDir: string): Program
12-
│ └─ function analyzeFile(sourceFile: SourceFile, typeChecker: TypeChecker, visibilityLevel: VisibilityLevel): string
13-
│ └─ function traverseDirectory(dir: string, program: Program, shouldIgnore: (path: string) => boolean, visibilityLevel: VisibilityLevel, prefix: string): string
14-
│ └─ function shouldIgnore(ignoredPatterns: (string | RegExp)[], pathToCheck: string): boolean
1510
│ └─ function tree(rootDir: string, ignored: (string | RegExp)[], visibilityLevel: VisibilityLevel): void
1611
└─ tests/
1712
├─ class.ts
1813
│ └─ export default class DefaultClass
19-
│ ├─ constructor(baz: number[]): void
2014
│ └─ class NamedClass
21-
│ ├─ constructor(): void
22-
│ ├─ double(): void
23-
│ ├─ octuple(): void
2415
└─ consts.ts
2516
└─ const publicArray
2617
└─ interface PublicInterface
@@ -46,23 +37,23 @@ exports[`tree module calls tree() with custom visibility level and matches conso
4637
│ └─ private const args
4738
├─ index.ts
4839
│ └─ const ignoredPatterns
49-
│ └─ function readTsConfig(rootDir: string): ParsedCommandLine
50-
│ └─ function createProgram(rootDir: string): Program
51-
│ └─ function analyzeFile(sourceFile: SourceFile, typeChecker: TypeChecker, visibilityLevel: VisibilityLevel): string
52-
│ └─ function traverseDirectory(dir: string, program: Program, shouldIgnore: (path: string) => boolean, visibilityLevel: VisibilityLevel, prefix: string): string
53-
│ └─ function shouldIgnore(ignoredPatterns: (string | RegExp)[], pathToCheck: string): boolean
40+
│ └─ private function readTsConfig(rootDir: string): ParsedCommandLine
41+
│ └─ private function createProgram(rootDir: string): Program
42+
│ └─ private function analyzeFile(sourceFile: SourceFile, typeChecker: TypeChecker, visibilityLevel: VisibilityLevel): string
43+
│ └─ private function traverseDirectory(dir: string, program: Program, shouldIgnore: (path: string) => boolean, visibilityLevel: VisibilityLevel, prefix: string): string
44+
│ └─ private function shouldIgnore(ignoredPatterns: (string | RegExp)[], pathToCheck: string): boolean
5445
│ └─ function tree(rootDir: string, ignored: (string | RegExp)[], visibilityLevel: VisibilityLevel): void
5546
└─ tests/
5647
├─ class.ts
5748
│ └─ private const tree
5849
│ └─ export default class DefaultClass
59-
│ ├─ constructor(baz: number[]): void
50+
│ ├─ private constructor(baz: number[]): void
6051
│ └─ class NamedClass
61-
│ ├─ constructor(): void
62-
│ ├─ double(): void
52+
│ ├─ private constructor(): void
53+
│ ├─ private double(): void
6354
│ ├─ private triple(): void
6455
│ ├─ protected quadruple(): void
65-
│ ├─ octuple(): void
56+
│ ├─ private octuple(): void
6657
└─ consts.ts
6758
└─ private const privateString
6859
└─ private const privateNumber

src/index.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,25 @@ function analyzeFile(
6666
return `(${parameters}): ${returnType}`;
6767
}
6868

69-
function getVisibility(node: ts.Declaration): string {
69+
function getVisibility(node: ts.Declaration | ts.VariableStatement): string {
70+
if (ts.isVariableStatement(node)) {
71+
return node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)
72+
? "public"
73+
: "private";
74+
}
7075
const modifiers = ts.getCombinedModifierFlags(node);
7176
if (modifiers & ts.ModifierFlags.Private) return "private";
7277
if (modifiers & ts.ModifierFlags.Protected) return "protected";
73-
return "public";
78+
if (modifiers & ts.ModifierFlags.Export) return "public";
79+
return "private"; // Non-exported top-level declarations are considered private
7480
}
7581

76-
function isVisibleEnough(node: ts.Declaration): boolean {
77-
const nodeVisibility = getVisibility(node);
82+
function isVisibleEnough(visibility: string): boolean {
7883
switch (visibilityLevel) {
7984
case VisibilityLevel.Public:
80-
return nodeVisibility === "public";
85+
return visibility === "public";
8186
case VisibilityLevel.Protected:
82-
return nodeVisibility === "public" || nodeVisibility === "protected";
87+
return visibility === "public" || visibility === "protected";
8388
case VisibilityLevel.Private:
8489
return true;
8590
}
@@ -93,10 +98,10 @@ function analyzeFile(
9398
ts.isClassDeclaration(node) ||
9499
ts.isVariableStatement(node)
95100
) {
96-
let exportType = "export";
101+
let exportType = "";
97102
let name = "";
98103
let isDefault = false;
99-
let visibility = "public";
104+
let visibility = "private";
100105

101106
if (
102107
ts.isFunctionDeclaration(node) ||
@@ -118,17 +123,11 @@ function analyzeFile(
118123
if (ts.isIdentifier(declaration.name)) {
119124
exportType = "const";
120125
name = declaration.name.text;
121-
visibility = node.modifiers?.some(
122-
(m) => m.kind === ts.SyntaxKind.ExportKeyword,
123-
)
124-
? "public"
125-
: "private";
126+
visibility = getVisibility(node);
126127
}
127128
}
128129

129-
const shouldInclude = ts.isVariableStatement(node)
130-
? visibilityLevel === VisibilityLevel.Private || visibility === "public"
131-
: isVisibleEnough(node as ts.Declaration);
130+
const shouldInclude = isVisibleEnough(visibility);
132131

133132
if (name && shouldInclude) {
134133
const visibilityPrefix =
@@ -139,17 +138,18 @@ function analyzeFile(
139138
output += "\n";
140139
node.members.forEach((member) => {
141140
if (
142-
(ts.isMethodDeclaration(member) ||
143-
ts.isConstructorDeclaration(member)) &&
144-
isVisibleEnough(member)
141+
ts.isMethodDeclaration(member) ||
142+
ts.isConstructorDeclaration(member)
145143
) {
146144
const methodName = ts.isConstructorDeclaration(member)
147145
? "constructor"
148146
: member.name.getText();
149147
const memberVisibility = getVisibility(member);
150-
const memberVisibilityPrefix =
151-
memberVisibility !== "public" ? `${memberVisibility} ` : "";
152-
output += ` ├─ ${memberVisibilityPrefix}${methodName}${getMethodSignature(member)}\n`;
148+
if (isVisibleEnough(memberVisibility)) {
149+
const memberVisibilityPrefix =
150+
memberVisibility !== "public" ? `${memberVisibility} ` : "";
151+
output += ` ├─ ${memberVisibilityPrefix}${methodName}${getMethodSignature(member)}\n`;
152+
}
153153
}
154154
});
155155
} else if (ts.isFunctionDeclaration(node)) {

0 commit comments

Comments
 (0)