🚫 eslint-plugin-ts-ban-snippets readme
Ban snippets of TypeScript 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
features
- 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.
Installation
This plugin requires your project to use TypeScript (>=4.1.3).
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
:
{
"plugins": ["ts-ban-snippets"],
"parserOptions": {
"project": "./tsconfig.json"
}
}
.eslintrc
Simple example This example detects use of return void reject
or return void resolve
and raises an error.
{
"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."
}
]
}
]
}
}
.eslintrc
Regex example This example also detects use of return void reject
or return void resolve
and raises an error - but using a regular expression.
{
"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."
}
]
}
]
}
}
.eslintrc
with multiple banned snippets
Example This example has a list of banned snippets:
- ban
return void reject
orreturn void resolve
- ban only enabling some tests via
it.only
ordescribe.only
- ban disabling tests via
it.skip
or test suites viadescribe.skip
{
"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."
}
]
}
]
}
}
.eslintrc
with included and excluded paths
Example 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.
{
"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.
For a real code example, see the test harness. In particular, the .eslintrc file.
authors
Original work by Sean Ryan - mr.sean.ryan(at gmail.com)
licence = MIT
This project is licensed under the MIT License - see the LICENSE file for details
Rules
For available options and for examples, see the Rule doc: