ts-analyzer 1.3.2 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,55 +4,146 @@
|
|
|
4
4
|

|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
`ts-analyzer` is a
|
|
7
|
+
`ts-analyzer` is a code analysis tool that checks your TypeScript and JavaScript projects. It looks at how safe your types are, how complex your code is, and finds common mistakes.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Type Safety Checks**: It calculates your real type coverage. It tracks things like `any` usage, generics, and assertions.
|
|
14
|
+
- **Code Complexity**: It checks how hard your code is to read by measuring cyclomatic complexity and nested blocks.
|
|
15
|
+
- **Anti-patterns**: It spots bad practices like Magic Numbers, Callback Hell, huge files, and functions with too many parameters.
|
|
16
|
+
- **Reports**: You can view the results as a nice HTML page, JSON, or just plain text in your terminal.
|
|
17
|
+
- **Tested**: The project has around 88% test coverage using Vitest.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## How It Works
|
|
22
|
+
|
|
23
|
+
### Type Safety
|
|
24
|
+
The tool looks at your code to see if your variables and parameters actually have types, or if they are just left as `any`.
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
┌─────────────────────┐
|
|
28
|
+
│ TypeScript Files │
|
|
29
|
+
└─────────┬───────────┘
|
|
30
|
+
│
|
|
31
|
+
▼
|
|
32
|
+
┌─────────────────────┐ ┌─────────────────────┐
|
|
33
|
+
│ AST Analysis │───>│ Node Classification │
|
|
34
|
+
└─────────┬───────────┘ └─────────┬───────────┘
|
|
35
|
+
│ │
|
|
36
|
+
▼ ▼
|
|
37
|
+
┌─────────────────────┐ ┌─────────────────────┐
|
|
38
|
+
│ Explicitly Typed │ │ Implicitly Typed │
|
|
39
|
+
│ Nodes │ │ Nodes │
|
|
40
|
+
└─────────┬───────────┘ └─────────┬───────────┘
|
|
41
|
+
│ │
|
|
42
|
+
└──────────┬──────────────┘
|
|
43
|
+
│
|
|
44
|
+
▼
|
|
45
|
+
┌─────────────────────┐ ┌─────────────────────┐
|
|
46
|
+
│ Type Coverage │<───│ tsconfig.json │
|
|
47
|
+
│ Calculation │ │ Analysis │
|
|
48
|
+
└─────────┬───────────┘ └─────────────────────┘
|
|
49
|
+
│
|
|
50
|
+
▼
|
|
51
|
+
┌─────────────────────┐
|
|
52
|
+
│ "any" & Assertion │
|
|
53
|
+
│ Penalty Calculation │
|
|
54
|
+
└─────────┬───────────┘
|
|
55
|
+
│
|
|
56
|
+
▼
|
|
57
|
+
┌─────────────────────┐
|
|
58
|
+
│ Final Type Safety │
|
|
59
|
+
│ Score & Rating │
|
|
60
|
+
└─────────────────────┘
|
|
61
|
+
```
|
|
8
62
|
|
|
9
|
-
|
|
63
|
+
* **Score**: It calculates a score based on your type coverage percentage, minus a penalty if you use `any` or type assertions too much.
|
|
64
|
+
* **tsconfig**: You get extra bonus points if your `tsconfig.json` has strict settings enabled.
|
|
10
65
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
66
|
+
### Code Complexity
|
|
67
|
+
It looks at your functions to see how complicated they are:
|
|
68
|
+
1. **Cyclomatic Complexity**: How many different paths your code can take.
|
|
69
|
+
2. **Nesting Depth**: How many blocks are inside each other (to avoid Callback Hell).
|
|
70
|
+
3. **Size**: How many lines and parameters your functions have.
|
|
16
71
|
|
|
17
|
-
|
|
72
|
+
Based on this, it gives a rating: <30 is simple, 30-60 is moderate, and >60 means it's too complex.
|
|
18
73
|
|
|
19
|
-
|
|
74
|
+
### Why switch to `ts-analyzer`?
|
|
75
|
+
This tool is an upgrade from `react-loc-analyzer`. Instead of just counting lines, it actually understands your TypeScript code and gives you useful feedback to improve it.
|
|
20
76
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Quick Start
|
|
24
80
|
|
|
25
|
-
|
|
81
|
+
You can run it directly using npx:
|
|
26
82
|
|
|
27
|
-
Analyze a specific directory:
|
|
28
83
|
```bash
|
|
29
|
-
ts-analyzer /path/to/project
|
|
84
|
+
npx ts-analyzer /path/to/project
|
|
30
85
|
```
|
|
31
86
|
|
|
32
|
-
|
|
33
|
-
- `-f, --format <type>`:
|
|
34
|
-
- `-e, --exclude <patterns>`:
|
|
35
|
-
- `--no-safety`: Skip
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
87
|
+
Some options you can use:
|
|
88
|
+
- `-f, --format <type>`: Choose between `text`, `json`, or `html`
|
|
89
|
+
- `-e, --exclude <patterns>`: Folders to ignore, separated by commas
|
|
90
|
+
- `--no-safety` / `--no-complexity`: Skip specific checks to run faster
|
|
91
|
+
|
|
92
|
+
## Example Reports
|
|
93
|
+
|
|
94
|
+
### JSON Output
|
|
95
|
+
When you run `npx ts-analyzer /path -f json`, it generates structured data you can use in your CI/CD pipelines:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"files": 156,
|
|
100
|
+
"totalLines": 15234,
|
|
101
|
+
"codeLines": 12845,
|
|
102
|
+
"typescriptSafety": {
|
|
103
|
+
"tsPercentage": "84.6",
|
|
104
|
+
"avgTypeCoverage": "92.3",
|
|
105
|
+
"totalAnyCount": 12,
|
|
106
|
+
"avgTypeSafetyScore": 85,
|
|
107
|
+
"overallComplexity": "Low"
|
|
108
|
+
},
|
|
109
|
+
"codeComplexity": {
|
|
110
|
+
"avgComplexity": "3.2",
|
|
111
|
+
"maxComplexity": 12,
|
|
112
|
+
"overallComplexity": "Low",
|
|
113
|
+
"codeSmells": {
|
|
114
|
+
"magicNumbers": 0,
|
|
115
|
+
"callbackHell": 0,
|
|
116
|
+
"godFiles": 0
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
45
120
|
```
|
|
46
121
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
122
|
+
### HTML Output
|
|
123
|
+
When you run `npx ts-analyzer /path -f html`, it creates a `ts-analyzer-report.html` file. This acts as a beautiful visual dashboard with:
|
|
124
|
+
- **Project Summary Cards**: Big, clear numbers for your total lines and code lines.
|
|
125
|
+
- **Color-coded Progress Bars**: Visual bars for Type Coverage percentages (turns red if too low).
|
|
126
|
+
- **Red/Green Indicators**: Fast visual checks for code smells like Callback Hell and God Files.
|
|
127
|
+
|
|
128
|
+
## Config File
|
|
129
|
+
You don't have to type arguments every time. Just create a `ts-analyzer.config.json` in your project folder:
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"safety": true,
|
|
133
|
+
"complexity": true,
|
|
134
|
+
"format": "text",
|
|
135
|
+
"exclude": ["node_modules", "dist", ".next"],
|
|
136
|
+
"include": [".js", ".ts", ".tsx"]
|
|
137
|
+
}
|
|
50
138
|
```
|
|
51
139
|
|
|
52
|
-
|
|
140
|
+
## Development
|
|
141
|
+
To work on this project, install the dependencies and run the tests:
|
|
53
142
|
```bash
|
|
143
|
+
npm install
|
|
144
|
+
npm test
|
|
54
145
|
npm run test:coverage
|
|
55
146
|
```
|
|
56
147
|
|
|
57
|
-
##
|
|
148
|
+
## License
|
|
58
149
|
MIT
|
package/package.json
CHANGED