An open API service indexing awesome lists of open source software.

https://github.com/re-quant/eslint-plugin-z-brain

Custom ESLint rules that are being used in Z-Brain projects. Some of them are project-specific, some are helpful anywhere.
https://github.com/re-quant/eslint-plugin-z-brain

Last synced: over 1 year ago
JSON representation

Custom ESLint rules that are being used in Z-Brain projects. Some of them are project-specific, some are helpful anywhere.

Awesome Lists containing this project

README

          

# Z-Brain ESLint Rules



Build status


NPM version


Code Coverage


License: GPL v3

Custom ESLint (TypeScript) rules that are being used in Z-Brain projects. Some of them are project-specific, some are helpful anywhere.

*Notice: If you have any propositions feel free to make an issue or create a pull request.*

## How to use

### Installing

`yarn add @z-brain/eslint-plugin-z-brain`
or
`npm i -s @z-brain/eslint-plugin-z-brain`

### Adding to `.eslintrc`

The plugin import and adding the recommended rule set.

```ts
module.exports = {
env: {
es6: true,
},
extends: ["plugin:@z-brain/z-brain/recommended"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["./tsconfig.json"],
sourceType: "module",
ecmaVersion: "es2019",
},
plugins: ["@z-brain/z-brain"],
};
```

**Configuring specific rules:**

```js
module.exports = {
rules: {
'@z-brain/z-brain/empty-array-check-with-absent-length': 'warn',
},
};
```

## Rules

### `empty-array-check-with-absent-length`

#### Short description:

Protects against forgotten '.length' when checking an array for emptiness

#### Detailed description:

The condition result being never changed according to current typings.
Add `.length` or if you're writing a check for a falsy-value please add an appropriate type to the array definition.

#### Examples

See all cases & examples in the [unit tests](./src/rules/empty-array-check-with-absent-length.rule.spec.ts).

```ts
function foo(ids: number[]) {
if (ids.length) return; // GOOD
if (ids) return; // BAD
}
```

```ts
// GOOD
function foo(ids: number[] | null | undefined) {
if (ids) return;
}
function foo(ids: number[] | boolean) {
if (ids) return;
}
function foo(ids?: number[]) {
if (ids) return;
}

// BAD
function foo(ids: number[]) {
if (ids) return; // in accordance with typings 'ids' is always trusty-value
}
```

## Helpful links

- [AST explorer](https://astexplorer.net/)

#### Articles
- [Simplest rule: Create a custom eslint rule with typescript](https://dev.to/bwca/create-a-custom-eslint-rule-with-typescript-4j3d)
- [More details: How I learned to love the AST](https://dev.to/alexgomesdev/writing-custom-typescript-eslint-rules-how-i-learned-to-love-the-ast-15pn)
- [The most complex article: How to write an ESLint plugin in TypeScript](https://dev.to/darraghor/how-to-write-an-eslint-plugin-in-typescript-3k5a)

#### Documentation & repos
- [Awesome official guide @typescript-eslint/experimental-utils · GitHub](https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/development/CUSTOM_RULES.md#writing-rules-in-typescript)
- [Lots of good & simple examples: eslint-plugin-nestjs-typed package - GitHub](https://github.com/darraghoriordan/eslint-plugin-nestjs-typed)
- [Official ESLint custom rules guide](https://eslint.org/docs/developer-guide/working-with-rules)
- [Description of all packages in @typescript-eslint · GitHub](https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/development/architecture/PACKAGES.md)
- [Using the Compiler API · microsoft/TypeScript Wiki · GitHub](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#using-the-type-checker)
- [More about type checking via TS Compiler API](https://learning-notes.mistermicheels.com/javascript/typescript/compiler-api/#getting-type-information)

## Development notes

### Quick Start

```bash
cd /Users/volumes/code/z-brain
git clone git@github.com:z-brain/eslint-plugin-z-brain.git
cd eslint-plugin-z-brain
yarn install
```

### How to use NodeJS version from the `.nvmrc`

1. Install NVM
2. Use `.nvmrc` file one of the next ways:

* Execute `nvm use` in the project root directory
* Install [NVM Loader](https://github.com/korniychuk/ankor-shell) and your .nvmrc will be loaded automatically when you open the terminal.
![NVM Loader demo](./resources/readme.nvm-loader.png)

### How to make a build

`yarn run build`

### How to run lint

Notice: _linter isn't configured yet_

* Just show problems `yarn run lint`
* Fix problems if it is possible `yarn run lint:fix`

### How to run tests

* All tests

`yarn run test`
`yarn run test:watch`
* Specific tests

`yarn run test src/my.spec.ts`
`yarn run test:watch src/my.spec.ts`

### How to build and publish NPM package

*NPM Token:* `npm_UVqN......qTww`

CI configuration details here: [.github/workflows/npmpublish.yml](.github/workflows/npmpublish.yml)

```bash
yarn run ci \
&& npm version patch -m 'Update package version version to %s' \
&& npm publish --access public \
&& git push --no-verify && git push --tags --no-verify
```

### How to build package to local installation

1. `yarn run build`
2. Then you can install a local package build from the root repo dir (not `dist`) path `file:.../eslint-plugin-z-brain`.

## Author

| [
Anton Korniychuk](https://korniychuk.pro) |
| :---: |