2021-09-29 07:34:40 -05:00
# Guidelines for code comments in grafana-\* packages
2020-02-25 06:59:11 -06:00
This document aims to give you some recommendation on how to add code comments to the exported code in the grafana packages.
## Table of Contents
1. [Add package description ](#add-package-description )
1. [Set stability of an API ](#set-stability-of-an-api )
1. [Deprecate an API ](#deprecate-an-api )
1. [Specify parameters ](#specify-parameters )
1. [Set return values ](#set-return-values )
2021-09-29 07:34:40 -05:00
---
2020-02-25 06:59:11 -06:00
2021-09-29 07:34:40 -05:00
## Add package description
Each package has an overview explaining the overall responsibility and usage of the package.
2020-02-25 06:59:11 -06:00
You can document this description with [`@packageDocumentation` ](https://api-extractor.com/pages/tsdoc/tag_packagedocumentation/ ) tag.
Add this tag to the `<packageRoot>/src/index.ts` entry file to have one place for the package description.
## Set stability of an API
All `exported` apis from the package should have a release tag to indicate its stability.
- [`@alpha` ](https://api-extractor.com/pages/tsdoc/tag_alpha/ ) - early draft of api and will probably change.
- [`@beta` ](https://api-extractor.com/pages/tsdoc/tag_beta/ ) - close to being stable but might change.
2020-06-30 16:42:50 -05:00
- [`@public` ](https://api-extractor.com/pages/tsdoc/tag_public/ ) - ready for usage in production.
2020-02-25 06:59:11 -06:00
- [`@internal` ](https://api-extractor.com/pages/tsdoc/tag_internal/ ) - for internal use only.
### Main stability of APIs
Add a tag to mark the stability of the whole exported `class/interface/function/type` etc.
2021-09-29 07:34:40 -05:00
Please place the `release tag` at the bottom of the comment to make it consistent among files and easier to read.
2020-02-25 06:59:11 -06:00
**Do:**
2021-09-29 07:34:40 -05:00
````typescript
2020-02-25 06:59:11 -06:00
/**
2021-09-29 07:34:40 -05:00
* Will help to create DataFrame objects and handle
2020-02-25 06:59:11 -06:00
* the heavy lifting of creating a complex object.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @example
* ```typescript
* const dataFrame = factory.create();
* ```
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @public
** /
export class DataFrameFactory {
2021-09-29 07:34:40 -05:00
create(): DataFrame {}
2020-02-25 06:59:11 -06:00
}
2021-09-29 07:34:40 -05:00
````
2020-02-25 06:59:11 -06:00
**Don't**
2021-09-29 07:34:40 -05:00
````typescript
2020-02-25 06:59:11 -06:00
/**
2021-09-29 07:34:40 -05:00
* Will help to create DataFrame objects and handle
2020-02-25 06:59:11 -06:00
* the heavy lifting of creating a complex object.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @public
* @example
* ```typescript
* const dataFrame = factory.create();
* ```
** /
export class DataFrameFactory {
2021-09-29 07:34:40 -05:00
create(): DataFrame {}
2020-02-25 06:59:11 -06:00
}
2021-09-29 07:34:40 -05:00
````
2020-02-25 06:59:11 -06:00
### Partial stability of APIs
Add the main stability of the API at the top according to [Main stability of API ](#main-stability-of-api ).
Then override the non-stable parts of the API with the proper [release tag ](#release-tags ). This should also be place at the bottom of the comment block.
**Do:**
2021-09-29 07:34:40 -05:00
````typescript
2020-02-25 06:59:11 -06:00
/**
2021-09-29 07:34:40 -05:00
* Will help to create DataFrame objects and handle
2020-02-25 06:59:11 -06:00
* the heavy lifting of creating a complex object.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @example
* ```typescript
* const dataFrame = factory.create();
* ```
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @public
** /
export class DataFrameFactory {
2021-09-29 07:34:40 -05:00
create(): DataFrame {}
2020-02-25 06:59:11 -06:00
2021-09-29 07:34:40 -05:00
/**
* @beta
** /
createMany(): DataFrames[] {}
2020-02-25 06:59:11 -06:00
}
2021-09-29 07:34:40 -05:00
````
2020-02-25 06:59:11 -06:00
**Don't**
2021-09-29 07:34:40 -05:00
````typescript
2020-02-25 06:59:11 -06:00
/**
2021-09-29 07:34:40 -05:00
* Will help to create DataFrame objects and handle
2020-02-25 06:59:11 -06:00
* the heavy lifting of creating a complex object.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @example
* ```typescript
* const dataFrame = factory.create();
* ```
** /
export class DataFrameFactory {
2021-09-29 07:34:40 -05:00
/**
* @public
** /
create(): DataFrame {}
/**
* @beta
** /
createMany(): DataFrame[] {}
2020-02-25 06:59:11 -06:00
}
2021-09-29 07:34:40 -05:00
````
2020-02-25 06:59:11 -06:00
## Deprecate an API
2021-09-29 07:34:40 -05:00
2020-02-25 06:59:11 -06:00
If you want to mark an API as deprecated to signal that this API will be removed in the future, then add the [`@deprecated` ](https://api-extractor.com/pages/tsdoc/tag_deprecated/ ) tag.
If applicable add a reason why the API is deprecated directly after the `@deprecated tag` .
## Specify parameters
2021-09-29 07:34:40 -05:00
2020-02-25 06:59:11 -06:00
If you want to specify the possible parameters that can be passed to an API, then add the [`@param` ](https://api-extractor.com/pages/tsdoc/tag_param/ ) tag.
This attribute can be skipped if the type provided by `typescript` and the function comment or the function name is enough to explain what the parameters are.
**Do:**
```typescript
/**
2020-06-30 16:42:50 -05:00
* Will help to create a resource resolver depending
2020-02-25 06:59:11 -06:00
* on the current execution context.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @param context - The current execution context.
* @returns FileResolver if executed on the server otherwise a HttpResolver.
* @public
** /
export const factory = (context: Context): IResolver => {
2021-09-29 07:34:40 -05:00
if (context.isServer) {
return new FileResolver();
}
return new HttpResolver();
};
2020-02-25 06:59:11 -06:00
```
**Don't**
```typescript
/**
* Will compare two numbers to see if they are equal to each others.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @param x - The first number
* @param y - The second number
* @public
** /
export const isEqual = (x: number, y: number): boolean => {
2021-09-29 07:34:40 -05:00
return x === y;
};
2020-02-25 06:59:11 -06:00
```
## Set return values
2021-09-29 07:34:40 -05:00
2020-02-25 06:59:11 -06:00
If you want to specify the return value from a function you can use the [`@returns` ](https://api-extractor.com/pages/tsdoc/tag_returns/ ) tag.
This attribute can be skipped if the type provided by `typescript` and the function comment or the function name is enough to explain what the function returns.
**Do:**
```typescript
/**
2020-06-30 16:42:50 -05:00
* Will help to create a resource resolver depending
2020-02-25 06:59:11 -06:00
* on the current execution context.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @param context - The current execution context.
* @returns FileResolver if executed on the server otherwise a HttpResolver.
* @public
** /
export const factory = (context: Context): IResolver => {
2021-09-29 07:34:40 -05:00
if (context.isServer) {
return new FileResolver();
}
return new HttpResolver();
};
2020-02-25 06:59:11 -06:00
```
**Don't**
```typescript
/**
* Will compare two numbers to see if they are equal to each others.
2021-09-29 07:34:40 -05:00
*
2020-02-25 06:59:11 -06:00
* @returns true if values are equal
* @public
** /
export const isEqual = (x: number, y: number): boolean => {
2021-09-29 07:34:40 -05:00
return x === y;
};
2020-06-30 16:42:50 -05:00
```