Skip to content

Commit bf1775d

Browse files
committed
feat(plugin-doc-coverage): add mvp version of a plugin doc coverage based on compodoc
1 parent 2a05a16 commit bf1775d

19 files changed

+858
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# @code-pushup/doc-coverage-plugin
2+
3+
[![npm](https://img.shields.io/npm/v/%40code-pushup%2Fdoc-coverage-plugin.svg)](https://www.npmjs.com/package/@code-pushup/doc-coverage-plugin)
4+
[![downloads](https://img.shields.io/npm/dm/%40code-pushup%2Fdoc-coverage-plugin)](https://npmtrends.com/@code-pushup/doc-coverage-plugin)
5+
[![dependencies](https://img.shields.io/librariesio/release/npm/%40code-pushup%2Fdoc-coverage-plugin)](https://www.npmjs.com/package/@code-pushup/doc-coverage-plugin?activeTab=dependencies)
6+
7+
📚 **Code PushUp plugin for tracking documentation coverage.** 📝
8+
9+
This plugin allows you to measure and track documentation coverage in your TypeScript/JavaScript project.
10+
It analyzes your codebase and checks for documentation on different code elements like classes, functions, interfaces, types, and variables.
11+
12+
Measured documentation types are mapped to Code PushUp audits in the following way:
13+
14+
- The value is in range 0-100 and represents the documentation coverage for all passed results (_documented / total_)
15+
- The score is value converted to 0-1 range
16+
- Missing documentation is mapped to issues in the audit details (undocumented classes, functions, interfaces, etc.)
17+
18+
## Getting started
19+
20+
1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.
21+
22+
2. Install as a dev dependency with your package manager:
23+
24+
```sh
25+
npm install --save-dev @code-pushup/doc-coverage-plugin
26+
```
27+
28+
```sh
29+
yarn add --dev @code-pushup/doc-coverage-plugin
30+
```
31+
32+
```sh
33+
pnpm add --save-dev @code-pushup/doc-coverage-plugin
34+
```
35+
36+
3. Add Compodoc to your project. You can follow the instructions [here](https://compodoc.app/guides/installation.html).
37+
38+
4. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.js`).
39+
40+
Pass the target files to analyze and optionally specify which types of documentation you want to track.
41+
All documentation types are measured by default. If you wish to focus on a subset of offered types, define them in `docTypes`.
42+
43+
The configuration will look similarly to the following:
44+
45+
```js
46+
import docCoveragePlugin from '@code-pushup/doc-coverage-plugin';
47+
48+
export default {
49+
// ...
50+
plugins: [
51+
// ...
52+
await docCoveragePlugin({
53+
coverageToolCommand: {
54+
command: 'npx',
55+
args: ['compodoc', '-p', 'tsconfig.doc.json', '-e', 'json'],
56+
},
57+
}),
58+
],
59+
};
60+
```
61+
62+
5. (Optional) Reference individual audits or the provided plugin group which you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups).
63+
64+
💡 Assign weights based on what influence each documentation type should have on the overall category score (assign weight 0 to only include as extra info, without influencing category score).
65+
66+
```js
67+
export default {
68+
// ...
69+
categories: [
70+
{
71+
slug: 'documentation',
72+
title: 'Documentation',
73+
refs: [
74+
{
75+
type: 'group',
76+
plugin: 'doc-coverage',
77+
slug: 'doc-coverage',
78+
weight: 1,
79+
},
80+
// ...
81+
],
82+
},
83+
// ...
84+
],
85+
};
86+
```
87+
88+
6. Run the CLI with `npx code-pushup collect` and view or upload report (refer to [CLI docs](../cli/README.md)).
89+
90+
## About documentation coverage
91+
92+
Documentation coverage is a metric that indicates what percentage of your code elements have proper documentation. It helps ensure your codebase is well-documented and maintainable.
93+
94+
The plugin provides a single audit that measures the overall percentage of documentation coverage across your codebase:
95+
96+
- **Percentage coverage**: Measures how many percent of the codebase have documentation.
97+
98+
## Plugin architecture
99+
100+
### Plugin configuration specification
101+
102+
The plugin accepts the following parameters:
103+
104+
- (optional) `coverageToolCommand`: If you wish to run your documentation coverage tool (compodoc) to generate the results first, you may define it here.
105+
- `command`: Command to run coverage tool (e.g. `npx`).
106+
- `args`: Arguments to be passed to the coverage tool (e.g. `['compodoc', '-p', 'tsconfig.doc.json', '-e', 'json']`).
107+
- `outputPath`: Path to the documentation.json file. Defaults to `'documentation/documentation.json'`.
108+
109+
### Audits and group
110+
111+
This plugin provides a group for convenient declaration in your config. When defined this way, all measured documentation type audits have the same weight.
112+
113+
```ts
114+
// ...
115+
categories: [
116+
{
117+
slug: 'documentation',
118+
title: 'Documentation',
119+
refs: [
120+
{
121+
type: 'group',
122+
plugin: 'doc-coverage',
123+
slug: 'doc-coverage',
124+
weight: 1,
125+
},
126+
// ...
127+
],
128+
},
129+
// ...
130+
],
131+
```
132+
133+
Each documentation type still has its own audit. So when you want to include a subset of documentation types or assign different weights to them, you can do so in the following way:
134+
135+
```ts
136+
// ...
137+
categories: [
138+
{
139+
slug: 'documentation',
140+
title: 'Documentation',
141+
refs: [
142+
{
143+
type: 'audit',
144+
plugin: 'doc-coverage',
145+
slug: 'class-doc-coverage',
146+
weight: 2,
147+
},
148+
{
149+
type: 'audit',
150+
plugin: 'doc-coverage',
151+
slug: 'function-doc-coverage',
152+
weight: 1,
153+
},
154+
// ...
155+
],
156+
},
157+
// ...
158+
],
159+
```
160+
161+
### Audit output
162+
163+
The plugin outputs a single audit that measures the overall documentation coverage percentage of your codebase.
164+
165+
For instance, this is an example of the plugin output:
166+
167+
```json
168+
{
169+
"packageName": "@code-pushup/doc-coverage-plugin",
170+
"version": "0.57.0",
171+
"title": "Documentation coverage",
172+
"slug": "doc-coverage",
173+
"icon": "folder-src",
174+
"duration": 920,
175+
"date": "2024-12-17T16:45:28.581Z",
176+
"audits": [
177+
{
178+
"slug": "percentage-coverage",
179+
"displayValue": "16 %",
180+
"value": 16,
181+
"score": 0.16,
182+
"details": {
183+
"issues": []
184+
},
185+
"title": "Percentage of codebase with documentation",
186+
"description": "Measures how many % of the codebase have documentation."
187+
}
188+
],
189+
"description": "Official Code PushUp documentation coverage plugin.",
190+
"docsUrl": "https://www.npmjs.com/package/@code-pushup/doc-coverage-plugin/",
191+
"groups": [
192+
{
193+
"slug": "doc-coverage",
194+
"refs": [
195+
{
196+
"slug": "percentage-coverage",
197+
"weight": 1
198+
}
199+
],
200+
"title": "Documentation coverage metrics",
201+
"description": "Group containing all defined documentation coverage types as audits."
202+
}
203+
]
204+
}
205+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import tseslint from 'typescript-eslint';
2+
import baseConfig from '../../eslint.config.js';
3+
4+
export default tseslint.config(
5+
...baseConfig,
6+
{
7+
files: ['**/*.ts'],
8+
languageOptions: {
9+
parserOptions: {
10+
projectService: true,
11+
tsconfigRootDir: import.meta.dirname,
12+
},
13+
},
14+
},
15+
{
16+
files: ['**/*.json'],
17+
rules: {
18+
'@nx/dependency-checks': 'error',
19+
},
20+
},
21+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"coverage": {
3+
"count": 85,
4+
"files": {
5+
"src/app/services/my.service.ts": {
6+
"documented": 17,
7+
"total": 20
8+
}
9+
}
10+
}
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@code-pushup/doc-coverage-plugin",
3+
"version": "0.57.0",
4+
"description": "Code PushUp plugin for tracking documentation coverage 📚",
5+
"license": "MIT",
6+
"homepage": "https://github.com/code-pushup/cli/tree/main/packages/plugin-doc-coverage#readme",
7+
"bugs": {
8+
"url": "https://github.com/code-pushup/cli/issues?q=is%3Aissue%20state%3Aopen%20type%3ABug%20label%3A\"🧩%20doc-coverage-plugin\""
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/code-pushup/cli.git",
13+
"directory": "packages/plugin-doc-coverage"
14+
},
15+
"keywords": [
16+
"CLI",
17+
"Code PushUp",
18+
"plugin",
19+
"automation",
20+
"developer tools",
21+
"conformance",
22+
"documentation coverage",
23+
"documentation",
24+
"docs",
25+
"KPI tracking",
26+
"automated feedback",
27+
"regression guard",
28+
"actionable feedback",
29+
"audit",
30+
"score monitoring"
31+
],
32+
"publishConfig": {
33+
"access": "public"
34+
},
35+
"type": "module",
36+
"dependencies": {
37+
"@code-pushup/models": "0.57.0",
38+
"@code-pushup/utils": "0.57.0",
39+
"ansis": "^3.3.0",
40+
"zod": "^3.22.4"
41+
},
42+
"peerDependenciesMeta": {
43+
"@nx/devkit": {
44+
"optional": true
45+
}
46+
}
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "plugin-doc-coverage",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "packages/plugin-doc-coverage/src",
5+
"projectType": "library",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/js:tsc",
9+
"outputs": ["{options.outputPath}"],
10+
"options": {
11+
"outputPath": "dist/packages/plugin-doc-coverage",
12+
"main": "packages/plugin-doc-coverage/src/index.ts",
13+
"tsConfig": "packages/plugin-doc-coverage/tsconfig.lib.json",
14+
"additionalEntryPoints": ["packages/plugin-doc-coverage/src/bin.ts"],
15+
"assets": ["packages/plugin-doc-coverage/*.md"]
16+
}
17+
},
18+
"lint": {
19+
"executor": "@nx/linter:eslint",
20+
"outputs": ["{options.outputFile}"],
21+
"options": {
22+
"lintFilePatterns": [
23+
"packages/plugin-doc-coverage/**/*.ts",
24+
"packages/plugin-doc-coverage/package.json"
25+
]
26+
}
27+
},
28+
"unit-test": {
29+
"executor": "@nx/vite:test",
30+
"options": {
31+
"configFile": "packages/plugin-doc-coverage/vite.config.unit.ts"
32+
}
33+
},
34+
"integration-test": {
35+
"executor": "@nx/vite:test",
36+
"options": {
37+
"configFile": "packages/plugin-doc-coverage/vite.config.integration.ts"
38+
}
39+
}
40+
},
41+
"tags": ["scope:plugin", "type:feature", "publishable"]
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { executeRunner } from './lib/runner/index.js';
2+
3+
await executeRunner();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { docCoveragePlugin } from './lib/doc-coverage-plugin.js';
2+
3+
export default docCoveragePlugin;
4+
export type { DocCoveragePluginConfig } from './lib/config.js';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { z } from 'zod';
2+
3+
export type DocType = 'percentage-coverage';
4+
5+
export const docCoveragePluginConfigSchema = z.object({
6+
coverageToolCommand: z
7+
.object({
8+
command: z
9+
.string({ description: 'Command to run coverage tool (compodoc).' })
10+
.min(1),
11+
args: z
12+
.array(z.string(), {
13+
description: 'Arguments to be passed to the coverage tool.',
14+
})
15+
.optional(),
16+
})
17+
.optional(),
18+
outputPath: z
19+
.string({ description: 'Path to the documentation.json file.' })
20+
.default('documentation/documentation.json'),
21+
});
22+
23+
export type DocCoveragePluginConfig = z.infer<
24+
typeof docCoveragePluginConfigSchema
25+
>;

0 commit comments

Comments
 (0)