UNPKG

8.09 kBMarkdownView Raw
1# :no_entry_sign: tslint-ban-snippets readme
2
3A custom tslint rule to ban configurable lists of code snippets.
4
5examples: "return void reject", "it.only", "debugger".
6
7## status - stable
8
9**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.
10
11**tslint-ban-snippets** WILL be maintained, as long as tslint is around. BUT recommend switching to the `eslint` equivalent, [eslint-plugin-ts-ban-snippets](https://github.com/mrseanryan/eslint-plugin-ts-ban-snippets).
12
13[![Build Status](https://travis-ci.com/mrseanryan/tslint-ban-snippets.svg?branch=master)](https://travis-ci.com/mrseanryan/tslint-ban-snippets)
14[![Coveralls](https://img.shields.io/coveralls/mrseanryan/tslint-ban-snippets.svg)](https://coveralls.io/github/mrseanryan/tslint-ban-snippets)
15[![Size](https://packagephobia.now.sh/badge?p=tslint-ban-snippets)](https://packagephobia.now.sh/result?p=tslint-ban-snippets)
16
17[![Dependencies](https://david-dm.org/mrseanryan/tslint-ban-snippets.svg)](https://david-dm.org/mrseanryan/tslint-ban-snippets)
18
19[![npm Package](https://img.shields.io/npm/v/tslint-ban-snippets.svg?style=flat-square)](https://www.npmjs.org/package/tslint-ban-snippets)
20[![NPM Downloads](https://img.shields.io/npm/dm/tslint-ban-snippets.svg)](https://npmjs.org/package/tslint-ban-snippets)
21
22[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
23[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
24
25[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
26
27[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/K3K73ALBJ)
28
29## dependencies
30
31No special dependencies - just `TypeScript` and of course `tslint`.
32
33## features
34
35- a custom tslint rule that can detect code snippets that are not desired
36- configurable for multiple code snippets
37- can include/exclude file paths
38- the error message can also be configured
39
40The rule is quite flexible and could potentially avoid having to create multiple custom tslint rules.
41
42### custom tslint rule
43
44The custom rule `tsl-ban-snippets` can be configured with small snippets of code that should NOT be used by developers.
45
46If tslint finds the snippets of code, it will raise an error for that line of code.
47
48In this way, a code base can be kept clean of unwanted coding practices.
49
50### note: the rule name
51
52The 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.
53
54### note: comparison to the standard `ban` rule
55
56There 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.
57
58## usage
59
60### 1 Install via npm (or yarn) into your TypeScript project
61
62```
63npm install tslint-ban-snippets
64```
65
66### 2 Configure tslint to pick up the custom rule
67
68Edit your `tslint.json` to have an entry `"rulesDirectory"` that points to **tslint-ban-snippets**.
69
70Normally this would be like:
71
72```json
73{
74 "rulesDirectory": "node_modules/tslint-ban-snippets/dist/lib",
75 "rules": {
76 "tsl-ban-snippets": [
77 // custom banned snippets here...
78 ]
79 }
80}
81```
82
83Alternatively, you can store the custom rules in a separate file:
84
85`tslint.json:`
86
87```json
88{
89 "extends": ["./tslint.tslint-ban-snippets.json"],
90 // ...
91}
92
93`tslint.tslint-ban-snippets.json:`
94
95```json
96{
97 "rulesDirectory": "node_modules/tslint-ban-snippets/dist/lib",
98 "rules": {
99 "tsl-ban-snippets": [
100 // custom banned snippets here...
101 ]
102 }
103}
104```
105
106### 3 Configure the custom rule `tsl-ban-snippets`
107
108Now you can configure the custom rule, to ban whatever code snippets you do NOT want developers to use.
109
110#### examples
111
112Example of how to ban the use of "return void":
113
114```json
115 "rules": {
116 // other rules here...
117 "tsl-ban-snippets": [
118 true,
119 {
120 "banned": [
121 {
122 "snippets": ["return void"]
123 }
124 ]
125 }
126 ]
127 }
128```
129
130Example to ban the disabling of tests:
131
132```json
133 "rules": {
134 "tsl-ban-snippets": [
135 true,
136 {
137 "banned": [
138 {
139 "snippets": ["it.only", "describe.only"],
140 "message": "Do not enable only some tests."
141 },
142 {
143 "snippets": ["it.skip", "describe.skip"],
144 "message": "Do not skip tests."
145 }
146 ]
147 }
148 ]
149 }
150```
151
152Example that uses a regular expression, to ban both `return void reject` and `return void resolve`:
153
154```json
155 "rules": {
156 // other rules here...
157 "tsl-ban-snippets": [
158 true,
159 {
160 "banned": [
161 {
162 "regexSnippets": ["return void [reject|resolve]"],
163 }
164 ]
165 }
166 ]
167 }
168```
169
170Here is another example, with more options.
171
172This example bans `return void` aswell as `return void reject` and `return void resolve`.
173
174```json
175 "rules": {
176 // other rules here...
177 "tsl-ban-snippets": [
178 true,
179 {
180 "banned": [
181 {
182 "snippets": ["return void"],
183 "message": "Please do not return void - instead place the return statement on the following line.",
184 "includePaths": [".ts", ".tsx"],
185 "excludePaths": ["itest"]
186 },
187 {
188 "regexSnippets": ["return void [reject|resolve]"],
189 "message": "Please do not return void - instead place the return statement on the following line.",
190 "includePaths": [".ts", ".tsx"],
191 "excludePaths": []
192 }
193 ]
194 }
195 ]
196 }
197```
198
199For more examples of how to configure, please see [tslint.json](https://github.com/mrseanryan/tslint-ban-snippets/blob/master/tslint.tslint-ban-snippets.json).
200
201For working tests, please see the [unit tests](https://github.com/mrseanryan/tslint-ban-snippets/blob/master/test/rules).
202
203For a real code example, see the [test harness](https://github.com/mrseanryan/tslint-ban-snippets/tree/master/testHarness/simple-typescript-project) and in particular the [config file](https://github.com/mrseanryan/tslint-ban-snippets/blob/master/testHarness/simple-typescript-project/tslint.tslint-ban-snippets.json).
204
205## sites
206
207| site | URL |
208| -------------------- | ------------------------------------------------- |
209| source code (github) | https://github.com/mrseanryan/tslint-ban-snippets |
210| github page | https://mrseanryan.github.io/tslint-ban-snippets/ |
211| npm | https://www.npmjs.com/package/tslint-ban-snippets |
212
213## developing code in _this_ repository
214
215see the [contributing readme](CONTRIBUTING.md).
216
217## origin
218
219This project is based on the excellent seeder project [typescript-library-starter](https://github.com/alexjoverm/typescript-library-starter).
220
221The project was started to avoid having to repeatedly fix similar coding issues in large TypeScript code bases.
222
223### ORIGINAL readme (from the seeder project)
224
225[see here](https://github.com/mrseanryan/tslint-ban-snippets/blob/master/readme.original.md)
226
227## authors
228
229Original work by Sean Ryan - mr.sean.ryan(at gmail.com)
230
231## licence = MIT
232
233This project is licensed under the MIT License - see the [LICENSE](https://github.com/mrseanryan/tslint-ban-snippets/blob/master/LICENSE) file for details