|
| 1 | +# @code-pushup/doc-coverage-plugin |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@code-pushup/doc-coverage-plugin) |
| 4 | +[](https://npmtrends.com/@code-pushup/doc-coverage-plugin) |
| 5 | +[](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 | +``` |
0 commit comments