|
| 1 | +# Writing API Docs |
| 2 | + |
| 3 | +API Documentation is generated from [yuidoc](https://yui.github.io/yuidoc/) comments in the source code. |
| 4 | + |
| 5 | +YUIDoc syntax is very similar to [JSDoc](https://jsdoc.app/) but there are occassional nuances where |
| 6 | +it becomes best to know the underlying parser is YUIDoc. |
| 7 | + |
| 8 | +While API Documentation lives with the source-code, the code itself plays no-part in the documentation |
| 9 | +that is generated: everything is compiled from comments alone. |
| 10 | + |
| 11 | +<br> |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +<br> |
| 16 | + |
| 17 | +## Documentation Syntax |
| 18 | + |
| 19 | +<br> |
| 20 | + |
| 21 | +### What are Doc Comments |
| 22 | + |
| 23 | +Only `**` comments are compiled as potential documentation, e.g. |
| 24 | + |
| 25 | +```ts |
| 26 | +/** |
| 27 | + * This is a potential documentation block |
| 28 | + */ |
| 29 | +``` |
| 30 | + |
| 31 | +Where as single star comment blocks are not considered documentation |
| 32 | + |
| 33 | +```ts |
| 34 | +/* |
| 35 | + * This is not a potential documentation block |
| 36 | + */ |
| 37 | +``` |
| 38 | + |
| 39 | +<br> |
| 40 | + |
| 41 | +### Ignored Doc Comments |
| 42 | + |
| 43 | +When compiling the API documentation, several categories of comments will be ignored: |
| 44 | + |
| 45 | +- `@internal` - signifies internal documentation for contributors for a non-public API |
| 46 | +- `@feature` - signifies documentation for an unreleased feature gated by a canary flag |
| 47 | +- `@typedoc` - signifies typescript-only (in-editor) documentation that should not be compiled into the API docs |
| 48 | + |
| 49 | +Additionally, use of the following tags will cause a doc comment to be ignored due to intended use primarily being docs |
| 50 | +written for in-editor experience similar to `@typedoc` |
| 51 | + |
| 52 | +- `@see` |
| 53 | +- `@link` |
| 54 | +- `@inheritdoc` |
| 55 | + |
| 56 | +For example, the below doc comment would be ignored |
| 57 | + |
| 58 | +```ts |
| 59 | +/** |
| 60 | + * This is a private utility for updating the state |
| 61 | + * of a relationship. |
| 62 | + * |
| 63 | + * @internal |
| 64 | + */ |
| 65 | +``` |
| 66 | + |
| 67 | +<br> |
| 68 | + |
| 69 | +### Always Start with `@module` |
| 70 | + |
| 71 | +The YUIDocs parser will attribute all documentation it discovers to the most recent |
| 72 | +`module` (package) declaration it has seen. For this reason, any file that has documentation |
| 73 | +comments should declare the package it applies to at the top of the file. |
| 74 | + |
| 75 | +For instance, if we were writing documentation for a feature within the `@ember-data/store` |
| 76 | +package, we would declare the following at the top of the file: |
| 77 | + |
| 78 | +```ts |
| 79 | +/** |
| 80 | + * @module @ember-data/store |
| 81 | + */ |
| 82 | +``` |
| 83 | + |
| 84 | +### Doc Comments can be Markdown |
| 85 | + |
| 86 | +Doc comments can contain most any valid markdown syntax, most markdown-valid html, |
| 87 | +and can utilize code-highlighting via language prefix on a code block comment. |
| 88 | + |
| 89 | +For instance |
| 90 | + |
| 91 | +```ts |
| 92 | +/** |
| 93 | + * ## Overview |
| 94 | + * |
| 95 | + * Some details |
| 96 | + * |
| 97 | + * ### An Example |
| 98 | + * |
| 99 | + * ```ts |
| 100 | + * new Store(); |
| 101 | + * ``` |
| 102 | + * |
| 103 | + * @class Store |
| 104 | + * @public |
| 105 | + */ |
| 106 | +``` |
| 107 | + |
| 108 | +<br> |
| 109 | + |
| 110 | +### Doc Comments should start every line with a `*` |
| 111 | + |
| 112 | +While technically doc comments only need to start with `/**`, providing a `*` for |
| 113 | +every line with matching indentation ensures correct parsing of all tags and documentation. |
| 114 | + |
| 115 | +Without this, some decorators in code examples may be incorrectly parsed as documentation tags, |
| 116 | +and some documentation may be unexpectedly truncated. |
| 117 | + |
| 118 | +**Good** |
| 119 | + |
| 120 | +```ts |
| 121 | +/** |
| 122 | + * ## Overview |
| 123 | + * |
| 124 | + * Some details |
| 125 | + * |
| 126 | + * ### An Example |
| 127 | + * |
| 128 | + * ```ts |
| 129 | + * class User extends Model { |
| 130 | + * @attr name; |
| 131 | + * } |
| 132 | + * ``` |
| 133 | + * |
| 134 | + * @class Store |
| 135 | + * @public |
| 136 | + */ |
| 137 | +``` |
| 138 | + |
| 139 | +**Bad** |
| 140 | + |
| 141 | +```ts |
| 142 | +/** |
| 143 | + ## Overview |
| 144 | + |
| 145 | + Some details |
| 146 | + |
| 147 | + ### An Example |
| 148 | + |
| 149 | + \```ts |
| 150 | + class User extends Model { |
| 151 | + @attr name; |
| 152 | + } |
| 153 | + \``` |
| 154 | + |
| 155 | + @class Store |
| 156 | + @public |
| 157 | +*/ |
| 158 | +``` |
| 159 | + |
| 160 | +### Documenting Modules |
| 161 | + |
| 162 | +Yuidoc syntax refers to packages as "modules". To declare that some code |
| 163 | +is part of a module, we use `@module <name>`, so the package `@warp-drive/core-types` |
| 164 | +is `@module @warp-drive/core-types`. |
| 165 | + |
| 166 | +Modules are documented using a special `@main` tag. |
| 167 | + |
| 168 | +For instance, to write documentation giving an overview of `@warp-drive/core-types` |
| 169 | +we would do the following. |
| 170 | + |
| 171 | +```ts |
| 172 | +/** |
| 173 | + * This package provides essential types and symbols used |
| 174 | + * by all the other WarpDrive/EmberData packages. |
| 175 | + * |
| 176 | + * @module @warp-drive/core-types |
| 177 | + * @main @warp-drive/core-types |
| 178 | + */ |
| 179 | +``` |
| 180 | + |
| 181 | +Sometimes we may want to reuse the documentation for a primary default export |
| 182 | +class as the module documentation as well. In this case `@module` will be |
| 183 | +declared standalone while `@main` will be affixed to the exported class doc comment. |
| 184 | + |
| 185 | +```ts |
| 186 | +/** |
| 187 | + * @module @ember-data/serializer/json-api |
| 188 | + */ |
| 189 | +/** |
| 190 | + * << module (and class) overview goes here >> |
| 191 | + * |
| 192 | + * @class JSONAPISerializer |
| 193 | + * @main @ember-data/serializer/json-api |
| 194 | + * @public |
| 195 | + */ |
| 196 | +``` |
| 197 | + |
| 198 | +<br> |
| 199 | + |
| 200 | +### Documenting Classes |
| 201 | + |
| 202 | +Classes are documented using `@class`. |
| 203 | + |
| 204 | +```ts |
| 205 | +/** |
| 206 | + * @since 1.13.0 |
| 207 | + * @class JSONAPIAdapter |
| 208 | + * @main @ember-data/adapter/json-api |
| 209 | + * @public |
| 210 | + * @constructor |
| 211 | + * @extends RESTAdapter |
| 212 | +*/ |
| 213 | +``` |
| 214 | + |
| 215 | +Methods are documented with `@method` and attatch to the most recent class the parser has |
| 216 | +seen. |
| 217 | + |
| 218 | +```ts |
| 219 | +/** |
| 220 | + * Some documentation |
| 221 | + * |
| 222 | + * @method myMethod |
| 223 | + * @public |
| 224 | + * @param {AType} myParam explanation of the param |
| 225 | + * @return {AnotherType} explanation of the return value |
| 226 | + */ |
| 227 | +``` |
| 228 | + |
| 229 | +Properties are documented with `@property` and attach to the most recent class the parser has seen. |
| 230 | + |
| 231 | +```ts |
| 232 | +/** |
| 233 | + * An explanation of the property |
| 234 | + * |
| 235 | + * @property {SomeType} propertyName |
| 236 | + * @public |
| 237 | + */ |
| 238 | +``` |
| 239 | + |
| 240 | +Static methods and properties can be documented by adding `@static` to the definition. |
| 241 | + |
| 242 | +<br> |
| 243 | + |
| 244 | +### Documenting Functions |
| 245 | + |
| 246 | +Functions are documented as "static" methods on modules. For instance the method `recordIdentifierFor` |
| 247 | +imported from `@ember-data/store` would be done like the below |
| 248 | + |
| 249 | +```ts |
| 250 | +/** |
| 251 | + * Description of the function |
| 252 | + * |
| 253 | + * @public |
| 254 | + * @static |
| 255 | + * @for @ember-data/store |
| 256 | + * @param {Object} record a record instance previously obstained from the store. |
| 257 | + * @return {StableRecordIdentifier} |
| 258 | + */ |
| 259 | +``` |
| 260 | + |
| 261 | + |
| 262 | +<br> |
| 263 | + |
| 264 | +### Documenting Interfaces and Types |
| 265 | + |
| 266 | +Yuidoc and the ember API docs do not have a mechanism for documenting types. However, because |
| 267 | +documentation is entirely doc-comment driven, we can document interfaces and types as classes, |
| 268 | +and mark them as such by giving them impossible names. Generally we follow the convention of |
| 269 | +`<Interface> AnInterface` and `<Type> AType` for this. |
| 270 | + |
| 271 | +For example, the interface for creating a request handler is documented as a class below |
| 272 | +whose name is `<Interface> Handler`. |
| 273 | + |
| 274 | +```ts |
| 275 | +/** |
| 276 | + * << Handler Documentation >> |
| 277 | + * |
| 278 | + * @class <Interface> Handler |
| 279 | + * @public |
| 280 | + */ |
| 281 | +``` |
| 282 | + |
| 283 | +<br> |
| 284 | + |
| 285 | +--- |
| 286 | + |
| 287 | +<br> |
| 288 | + |
| 289 | +## Documentation Hygeine |
| 290 | + |
| 291 | +<br> |
| 292 | + |
| 293 | +### Documentation Tests |
| 294 | + |
| 295 | +Run `pnpm test:docs` |
| 296 | + |
| 297 | +This will lint discovered doc comments for common sources of error, as well as validate that no published documentation |
| 298 | +has been added or removed unexpectedly. |
| 299 | + |
| 300 | +If documentation has been added, an entry for it should be added to `tests/docs/fixtures/expected.js`. |
| 301 | +If documentation has been removed, similarly any entry for it should be removed from `tests/docs/fixtures/expected.js`. |
| 302 | + |
| 303 | +If documentation you've added is not being discovered by the test, it is likely that either |
| 304 | + |
| 305 | +- it may have been excluded due to using an [ignored doc comment](#ignored-doc-comments) |
| 306 | +- it may have been excluded due to not using the right [comment syntax](#what-are-doc-comments) |
| 307 | +- it may have been included in the list of paths to search for source code documentation in [yuidoc.json](../docs-generator/yuidoc.json) |
| 308 | + |
| 309 | +<br> |
| 310 | + |
| 311 | +### Previewing Documentation |
| 312 | + |
| 313 | +Run `bun preview-api-docs` from the project root or the `docs-viewer` directory. |
| 314 | + |
| 315 | +This will build and run the (external) api-docs app with the current state of the api docs in the repo. |
| 316 | + |
| 317 | +Changes need to be manually rebuilt with `bun rebuild-api-docs`. |
| 318 | + |
| 319 | +See the [Docs Viewer README](../docs-viewer/README.md) for more info. |
0 commit comments