eslint-plugin-ts-ban-snippets
Version:
Ban snippets of TypeScript from your project, using a custom eslint rule 'ts-ban-snippets'.
197 lines (151 loc) • 6.09 kB
Markdown
from your project, using a custom eslint rule 'ts-ban-snippets'.
examples: "return void reject", "it.only", "debugger".
This is an eslint port of [tslint-ban-snippets](https://github.com/mrseanryan/tslint-ban-snippets)

[](https://packagephobia.now.sh/result?p=eslint-plugin-ts-ban-snippets)
[](https://david-dm.org/mrseanryan/eslint-plugin-ts-ban-snippets)
[](https://www.npmjs.org/package/eslint-plugin-ts-ban-snippets)
[](https://npmjs.org/package/eslint-plugin-ts-ban-snippets)
[](https://github.com/prettier/prettier)
[](https://github.com/semantic-release/semantic-release)
[](https://opensource.org/licenses/MIT)
[](https://ko-fi.com/K3K73ALBJ)
- a custom eslint rule that can detect code snippets that are not desired
- configurable for multiple code snippets
- can include/exclude file paths
- the error message can also be configured
The rule is quite flexible and could potentially avoid having to create multiple custom eslint rules.
This plugin requires your project to use TypeScript (>=4.1.3).
```sh
yarn add eslint-plugin-ts-ban-snippets --dev
```
## Example Configurations
The plugin relies on TypeScript compiler services to resolve types.
The following examples show how to configure banned snippets in the `.eslintrc` file(s) of your project.
note: in the `.eslintrc` file, you need to set your `tsconfig.json` file in your eslint configuration via `parserOptions`:
```json
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
}
}
```
This example detects use of `return void reject` or `return void resolve` and raises an error.
```json
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"ts-ban-snippets/ts-ban-snippets": [
"error",
{
"banned": [
{
"snippets": ["return void reject", "return void resolve"],
"message": "Please do not return void - instead place the return statement on the following line."
}
]
}
]
}
}
```
This example also detects use of `return void reject` or `return void resolve` and raises an error - but using a regular expression.
```json
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"ts-ban-snippets/ts-ban-snippets": [
"error",
{
"banned": [
{
"regexSnippets": ["return void [reject|resolve]"],
"message": "Please do not return void - instead place the return statement on the following line."
}
]
}
]
}
}
```
This example has a list of banned snippets:
- ban `return void reject` or `return void resolve`
- ban only enabling some tests via `it.only` or `describe.only`
- ban disabling tests via `it.skip` or test suites via `describe.skip`
```json
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"ts-ban-snippets/ts-ban-snippets": [
"error",
{
"banned": [
{
"snippets": ["return void reject", "return void resolve"],
"message": "Please do not return void - instead place the return statement on the following line."
},
{
"snippets": ["it.only", "describe.only"],
"message": "Do not enable only some tests."
},
{
"snippets": ["it.skip", "describe.skip"],
"message": "Do not skip tests."
}
]
}
]
}
}
```
This example has one banned snippet (`return void reject` or `return void resolve`).
Only files with a path that contains `my-component` and does not contain `excluded` are scanned.
```json
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"ts-ban-snippets/ts-ban-snippets": [
"error",
{
"banned": [
{
"snippets": ["return void reject", "return void resolve"],
"message": "Please do not return void - instead place the return statement on the following line.",
"includePaths": ["my-component"],
"excludePaths": ["excluded"]
}
]
}
]
}
}
```
For working tests, please see the [unit tests](https://github.com/mrseanryan/eslint-plugin-ts-ban-snippets/blob/master/tests).
For a real code example, see the [test harness](https://github.com/mrseanryan/eslint-plugin-ts-ban-snippets/tree/main/itests/simple-harness). In particular, the [.eslintrc](https://github.com/mrseanryan/eslint-plugin-ts-ban-snippets/tree/main/itests/simple-harness/.eslintrc) file.
Original work by Sean Ryan - mr.sean.ryan(at gmail.com)
This project is licensed under the MIT License - see the [LICENSE](https://github.com/mrseanryan/eslint-plugin-ts-ban-snippets/blob/master/LICENSE) file for details
For available options and for examples, see the Rule doc:
- [ts-ban-snippets](./docs/ts-ban-snippets.md)
Ban snippets of TypeScript