A custom tslint rule to ban configurable lists of code snippets.
examples: "return void reject", "it.only", "debugger".
tslint-ban-snippets is stable and in use every day in CI builds and on dev boxes (Linux, Mac, Windows) for at least one major product.
No special dependencies - just TypeScript
and of course tslint
.
The rule is quite flexible and could potentially avoid having to create multiple custom tslint rules.
The custom rule tsl-ban-snippets
can be configured with small snippets of code that should NOT be used by developers.
If tslint finds the snippets of code, it will raise an error for that line of code.
In this way, a code base can be kept clean of unwanted coding practices.
The rule name is tsl-ban-snippets
to avoid using the prefix tslint-
which was found to be problematic when other tslint
libraries are in use.
ban
ruleThere is a standard tslint rule named ban
. However its scope is quite limited - the tsl-ban-snippets
rule applies to any statement in a TypeScript file, and so can be configured to detect most unwanted code snippets.
npm install tslint-ban-snippets
Edit your tslint.json
to have an entry "rulesDirectory"
that points to tslint-ban-snippets.
Normally this would be like:
{
"rulesDirectory": "node_modules/tslint-ban-snippets/dist/lib",
"rules": {
// tslint rules here...
}
}
tsl-ban-snippets
Now you can configure the custom rule, to ban whatever code snippets you do NOT want developers to use.
Example of how to ban the use of "return void":
"rules": {
// other rules here...
"tsl-ban-snippets": [
true,
{
"banned": [
{
"snippets": ["return void"]
}
]
}
]
}
Example that uses a regular expression:
"rules": {
// other rules here...
"tsl-ban-snippets": [
true,
{
"banned": [
{
"regexSnippets": ["return void [reject|resolve]"],
}
]
}
]
}
Here is another example, with more options:
"rules": {
// other rules here...
"tsl-ban-snippets": [
true,
{
"banned": [
{
"snippets": ["return void"],
"message": "Please do not return void - instead place the return statement on the following line.",
"includePaths": [".ts", ".tsx"],
"excludePaths": ["itest"]
},
{
"regexSnippets": ["return void [reject|resolve]"],
"message": "Please do not return void - instead place the return statement on the following line.",
"includePaths": [".ts", ".tsx"],
"excludePaths": []
}
]
}
]
}
For more examples of how to configure, please see tslint.json.
For working examples, please see the unit tests.
site | URL |
---|---|
source code (github) | https://github.com/mrseanryan/tslint-ban-snippets |
github page | https://mrseanryan.github.io/tslint-ban-snippets/ |
npm | https://www.npmjs.com/package/tslint-ban-snippets |
see the contributing readme.
This project is based on the excellent seeder project typescript-library-starter.
The project was started to avoid having to repeatedly fix similar coding issues in large TypeScript code bases.
Original work by Sean Ryan - mr.sean.ryan(at gmail.com)
This project is licensed under the MIT License - see the LICENSE file for details
Generated using TypeDoc